catcher_modules.database package

Submodules

catcher_modules.database.couchbase module

class catcher_modules.database.couchbase.Couchbase(**kwargs)[source]
Input:
Conf:couchbase configuration. Is an object. Required.
  • bucket: bucket to work with
  • user: database user (optional)
  • host: database host (optional)
  • password: user’s password
Put:put value in the database by the key.
Get:get object by key.
Delete:delete object by key.
Query:query to run.
Examples:

Put value by key

couchbase:
    request:
        conf:
            bucket: test
            host: localhost
        put:
            key: my_key
            value: {foo: bar, baz: [1,2,3,4]}

Get value by key

couchbase:
    request:
        conf:
            bucket: test
            user: test
            password: test
            host: localhost
        get:
            key: my_key

Delete value by key

couchbase:
    request:
        conf:
            bucket: test
            user: test
            password: test
            host: localhost
        delete:
            key: my_key

Query by foo

couchbase:
    request:
        conf:
            bucket: test
            user: test
            password: test
            host: localhost
        query: "select `baz` from test where `foo` = 'bar'"

catcher_modules.database.postgres module

class catcher_modules.database.postgres.Postgres(**kwargs)[source]
Input:
Conf:postgres configuration. Can be a single line string or object. Dialect is not mandatory. Required.
  • dbname: name of the database to connect to
  • user: database user
  • host: database host
  • password: user’s password
  • port: database port
Query:query to run. Required
Examples:

Select all from test, use object configuration

postgres:
  request:
      conf:
          dbname: test
          user: user
          password: password
          host: localhost
          port: 5433
      query: 'select count(*) from test'
  register: {documents: '{{ OUTPUT }}'}

Insert into test, using string configuration

postgres:
  request:
      conf: 'user:[email protected]:5432/test'
      query: 'insert into test(id, num) values(3, 3);'

Insert into test, using string configuration with dialect

postgres:
  request:
      conf: 'postgresql://user:[email protected]:5432/test'
      query: 'insert into test(id, num) values(3, 3);'

catcher_modules.database.mongo module

class catcher_modules.database.mongo.Mongo(**kwargs)[source]
Input:
Conf:mongodb configuration. Can be a single line

string url or kv object. Required.

  • database: name of the database to connect to
  • username: database user. Must be RFC 2396 encoded when in URI.
  • host: database host
  • password: user’s password. Must be RFC 2396 encoded when in URI.
  • port: database port
  • authSource: The database to authenticate on. Default is database.

See pymongo for more options. :collection: collection to use. Required

Command:String. Use this if you have to run command without any parameters. Where command’s value is your command to run, like command: find_one. Optional
<command>:Object. Use this when you have command with parameters. Where <command> key is your command name and it’s value is parameter object (list or dict). Optional Either <command> or command should exist.
Next:Run other operation just after your operation. Can be string like next: count or object with params next: {‘sort’: ‘author’}. You can chain multiple next (see example). `Optional
List_params:Pass command params as different arguments. Useful when pymongo command takes several arguments (both *args and **kwargs). *args will be set in case of params in list while **kwargs will be sent in case of dict. See examples for more info.
Examples:

Find one document. Use command key when no params.

mongo:
  request:
      conf:
          database: test
          username: test
          password: test
          host: localhost
          port: 27017
      collection: 'your_collection'
      command: 'find_one'
  register: {document: '{{ OUTPUT }}'}

Insert into test, using string configuration

mongo:
  request:
      conf: 'mongodb://username:[email protected]'
      collection: 'your_collection'
      insert_one:
        'author': 'Mike'
        'text': 'My first blog post!'
        'tags': ['mongodb', 'python', 'pymongo']
        'date': '{{ NOW_DT }}'

Find specific document

mongo:
  request:
      conf:
          database: test
          username: test
          password: test
          host: localhost
          port: 27017
      collection: 'your_collection'
      find_one: {'author': 'Mike'}
  register: {document: '{{ OUTPUT }}'}

To find multiple documents just use find instead of find_one.

Bulk insert

mongo:
  request:
      conf: '{{ mongo_conf }}'
      collection: 'your_collection'
      insert_many:
        - {'foo': 'baz'}
        - {'foo': 'bar'}

Chaining operations: db.collection.find().sort().count()

mongo:
  request:
      conf:
          database: test
          username: test
          password: test
          host: localhost
          port: 27017
      collection: 'your_collection'
      find: {'author': 'Mike'}
      next:
        sort: 'author'
        next: 'count'
  register: {document: '{{ OUTPUT }}'}

Will run every next operation on previous one. You can chain more than one operation.

Run operation with list parameters (**kwargs). Is useful when calling commands with additional arguments.

mongo:
  request:
      conf:
          database: test
          username: test
          password: test
          host: localhost
          port: 27017
      collection: 'your_collection'
      find:
        filter: {'author': 'Mike'}
        projection: {'_id': False}
      list_params: true  # pass list arguments as separate params
  register: {document: '{{ OUTPUT }}'}

Run operation with list parameters (*args). Run map-reduce.

mongo:
  request:
      conf:
          database: test
          username: test
          password: test
          host: localhost
          port: 27017
      collection: 'your_collection'
      map_reduce:
        - 'function () {
                this.tags.forEach(function(z) {
                    emit(z, 1);
                });
           }'
        - 'function (key, values) {
             var total = 0;
              for (var i = 0; i < values.length; i++) {
                total += values[i];
              }
              return total;
            }'
        - 'myresults'
      list_params: true  # pass list arguments as separate params
  register: {document: '{{ OUTPUT }}'}

catcher_modules.database.oracle module

class catcher_modules.database.oracle.Oracle(**kwargs)[source]
Input:
Conf:oracle configuration. Can be a single line string or object. Dialect is not mandatory. Required.
  • dbname: name of the database to connect to
  • user: database user
  • host: database host
  • password: user’s password
  • port: database port
Query:query to run. Required
Examples:

Select all from test, use object configuration

oracle:
  request:
      conf:
          dbname: test
          user: user
          password: password
          host: localhost
          port: 1521
      query: 'select count(*) from test'
  register: {documents: '{{ OUTPUT }}'}

Insert into test, using string configuration

oracle:
  request:
      conf: 'user:[email protected]:1521/test'
      query: 'insert into test(id, num) values(3, 3);'

Insert into test, using string configuration with dialect

oracle:
  request:
      conf: 'oracle+cx_oracle://user:[email protected]:1521/test'
      query: 'insert into test(id, num) values(3, 3);'

catcher_modules.database.sqlite module

class catcher_modules.database.sqlite.SQLite(**kwargs)[source]
Input:
Conf:sqlite path string. Dialect is not mandatory. Required.
Query:query to run. Required
Examples:

Select all from test, use relative path

sqlite:
  request:
      conf: '/foo.db'
      query: 'select count(*) as count from test'
  register: {documents: '{{ OUTPUT }}'}

Note that we alias count. For some reason sqlalchemy for sqlite will return count(*) as a column name instead of count.

Insert into test, using string absolute path (with 2 slashes)

sqlite:
  request:
      conf: '//absolute/path/to/foo.db'
      query: 'insert into test(id, num) values(3, 3);'

catcher_modules.database.mysql module

class catcher_modules.database.mysql.MySql(**kwargs)[source]
Input:
Conf:mysql configuration. Can be a single line string or object. Dialect is not mandatory. Required.
  • dbname: name of the database to connect to
  • user: database user
  • host: database host
  • password: user’s password
  • port: database port
Query:query to run. Required
Examples:

Select all from test, use object configuration

mysql:
  request:
      conf:
          dbname: test
          user: user
          password: password
          host: localhost
          port: 3306
      query: 'select count(*) as count from test'
  register: {documents: '{{ OUTPUT }}'}

Note that we alias count. For some reason sqlalchemy for mysql will return count(*) as a column name instead of count.

Insert into test, using string configuration

mysql:
  request:
      conf: 'user:[email protected]:3306/test'
      query: 'insert into test(id, num) values(3, 3);'

Insert into test, using string configuration with dialect

mysql:
  request:
      conf: 'mysql+pymysql://user:[email protected]:3306/test'
      query: 'insert into test(id, num) values(3, 3);'

catcher_modules.database.mssql module

class catcher_modules.database.mssql.MSSql(**kwargs)[source]
Input:
Conf:mssql configuration. Can be a single line string or object. Dialect is not mandatory. Required.
  • dbname: name of the database to connect to
  • user: database user
  • host: database host
  • password: user’s password
  • port: database port
  • driver: odbc driver name you’ve installed. Optional If not specified, the default driver, which comes with
    catcher-modules Dockerfile will be used.
Query:query to run. Required
Examples:

Select all from test, use object configuration

mssql:
  request:
      conf:
          dbname: test
          user: user
          password: password
          host: localhost
          port: 1433
          driver: ODBC Driver 17 for SQL Server
      query: 'select count(*) as count from test'
  register: {documents: '{{ OUTPUT }}'}

Note that we alias count. For some reason sqlalchemy for mssql will return count(*) as a column name instead of count.

Insert into test, using string configuration

mssql:
  request:
      conf: 'user:[email protected]:5432/test'
      query: 'insert into test(id, num) values(3, 3);'

Insert into test, using string configuration with pymssql (pymssql should be installed)

mssql:
  request:
      conf: 'mssql+pymssql://user:[email protected]:5432/test'
      query: 'insert into test(id, num) values(3, 3);'