Skip to content

Commit 792b383

Browse files
committed
Added all actions and python files for neccessary actions. Also added pack requirements and config data. Delete all uneccessary files and folders. Need to write unit tests and fill out readme section.
1 parent 5610512 commit 792b383

File tree

16 files changed

+689
-2
lines changed

16 files changed

+689
-2
lines changed

actions/README.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

actions/delete.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from lib.base_action import BaseAction
2+
import sqlalchemy
3+
4+
5+
class SQLDeleteAction(BaseAction):
6+
def __init__(self, config):
7+
"""Creates a new BaseAction given a StackStorm config object (kwargs works too)
8+
:param config: StackStorm configuration object for the pack
9+
:returns: a new BaseAction
10+
"""
11+
super(SQLDeleteAction, self).__init__(config)
12+
13+
def run(self, **kwargs):
14+
"""Main entry point for the StackStorm actions to execute the operation.
15+
:returns: the dns adapters and the number of network adapters to be
16+
on the VM.
17+
"""
18+
kwargs_dict = dict(kwargs)
19+
20+
where_dict = self.get_del_arg('where_data', kwargs_dict, True)
21+
table = self.get_del_arg('table', kwargs_dict, True)
22+
23+
# Get the connection details from either config or from action params
24+
connection_details = self.resolve_connection(kwargs_dict)
25+
26+
# Connect to the Database
27+
self.connect_to_db(connection_details)
28+
29+
# Get the SQL table
30+
sql_table = sqlalchemy.Table(table, self.meta, autoload=True, autoload_with=self.engine)
31+
32+
# Intantiate delete object
33+
delete = sql_table.delete()
34+
35+
# Generate Where Statement
36+
if where_dict:
37+
delete, where_dict = self.generate_where_clause(sql_table, delete, where_dict)
38+
39+
# Execute query
40+
result = self.conn.execute(delete, where_dict)
41+
42+
# Disconnect from the database
43+
self.conn.close()
44+
45+
return {'affected_rows': result.rowcount}

actions/delete.yaml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
name: delete
3+
runner_type: "python-script"
4+
description: "Deletes data from the SQL database"
5+
enabled: true
6+
entry_point: delete.py
7+
parameters:
8+
connection:
9+
type: string
10+
description: "Name of <connection> from this pack's configuration that specifies how to connect to a database server."
11+
required: false
12+
host:
13+
type: string
14+
description: >
15+
Optional override of the database host in <connection> (required if <connection> is not specified). Database server to connect to. If not using a default port add that here. ex. host.domain.tld or host.domain.tld:1234
16+
required: false
17+
username:
18+
type: string
19+
description: "Optional override of the username in <connection> (required if <connection> is not specified). Username for authentication"
20+
required: false
21+
password:
22+
type: string
23+
description: "Optional override of the password in <connection> (required if <connection> is not specified). Password of the specified username"
24+
secret: true
25+
required: false
26+
database:
27+
type: string
28+
description: "Optional override of the database in <connection> (required if <connection> is not specified). Database to connect to, to run querys against."
29+
required: false
30+
database_type:
31+
type: string
32+
description: "Optional override of the database_type in <connection> (required if <connection> is not specified). The type of database that is being connected to."
33+
enum:
34+
- postgresql
35+
- sqlite
36+
- mssql
37+
- mysql
38+
- oracle
39+
- firebird
40+
- sybase
41+
required: false
42+
table:
43+
type: string
44+
description: "Database table to DELETE."
45+
required: true
46+
where_data:
47+
type: object
48+
description: >
49+
Dictionary of data to be used to create a WHERE clause for the DELETE statement
50+
{
51+
'column_1': 'data_to_match_1',
52+
'column_2': 'data_to_match_2',
53+
'column_3': 'data_to_match_3',
54+
'column_4': 'data_to_match_4',
55+
}
56+
required: false
57+
default: {}

actions/insert.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from lib.base_action import BaseAction
2+
import sqlalchemy
3+
4+
5+
class SQLInsertAction(BaseAction):
6+
def __init__(self, config):
7+
"""Creates a new BaseAction given a StackStorm config object (kwargs works too)
8+
:param config: StackStorm configuration object for the pack
9+
:returns: a new BaseAction
10+
"""
11+
super(SQLInsertAction, self).__init__(config)
12+
13+
def run(self, **kwargs):
14+
"""Main entry point for the StackStorm actions to execute the operation.
15+
:returns: the dns adapters and the number of network adapters to be
16+
on the VM.
17+
"""
18+
kwargs_dict = dict(kwargs)
19+
20+
insert_data = self.get_del_arg('data', kwargs_dict, True)
21+
insert_table = self.get_del_arg('table', kwargs_dict, True)
22+
23+
if not isinstance(insert_data, list):
24+
insert_data = [insert_data]
25+
26+
# Get the connection details from either config or from action params
27+
connection_details = self.resolve_connection(kwargs_dict)
28+
29+
# Connect to the Database
30+
self.connect_to_db(connection_details)
31+
32+
# Get the Table to insert data into
33+
sql_table = sqlalchemy.Table(insert_table,
34+
self.meta,
35+
autoload=True,
36+
autoload_with=self.engine)
37+
38+
# Execute the insert query
39+
self.conn.execute(sql_table.insert(), insert_data)
40+
41+
# Disconnect from the database
42+
self.conn.close()
43+
44+
return True

actions/insert.yaml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
name: insert
3+
runner_type: "python-script"
4+
description: "Insert data into a SQL database"
5+
enabled: true
6+
entry_point: insert.py
7+
parameters:
8+
connection:
9+
type: string
10+
description: "Name of <connection> from this pack's configuration that specifies how to connect to a database server."
11+
required: false
12+
host:
13+
type: string
14+
description: >
15+
Optional override of the database host in <connection> (required if <connection> is not specified). Database server to connect to. If not using a default port add that here. ex. host.domain.tld or host.domain.tld:1234
16+
required: false
17+
username:
18+
type: string
19+
description: "Optional override of the username in <connection> (required if <connection> is not specified). Username for authentication"
20+
required: false
21+
password:
22+
type: string
23+
description: "Optional override of the password in <connection> (required if <connection> is not specified). Password of the specified username"
24+
secret: true
25+
required: false
26+
database:
27+
type: string
28+
description: "Optional override of the database in <connection> (required if <connection> is not specified). Database to connect to, to run querys against."
29+
required: false
30+
database_type:
31+
type: string
32+
description: "Optional override of the database_type in <connection> (required if <connection> is not specified). The type of database that is being connected to."
33+
enum:
34+
- postgresql
35+
- sqlite
36+
- mssql
37+
- mysql
38+
- oracle
39+
- firebird
40+
- sybase
41+
required: false
42+
table:
43+
type: string
44+
description: "Database table to insert data into."
45+
required: true
46+
data:
47+
type: object
48+
description: >
49+
Dictionary of data to be inserted where the key corresponds to the column of the table
50+
{
51+
'column_1': 'data_to_insert_1',
52+
'column_2': 'data_to_insert_2',
53+
'column_3': 'data_to_insert_3',
54+
'column_4': 'data_to_insert_4',
55+
}
56+
required: true

actions/insert_bulk.yaml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
name: insert_bulk
3+
runner_type: "python-script"
4+
description: "Insert bulk data into a SQL database"
5+
enabled: true
6+
entry_point: insert.py
7+
parameters:
8+
connection:
9+
type: string
10+
description: "Name of <connection> from this pack's configuration that specifies how to connect to a database server."
11+
required: false
12+
host:
13+
type: string
14+
description: >
15+
Optional override of the database host in <connection> (required if <connection> is not specified). Database server to connect to. If not using a default port add that here. ex. host.domain.tld or host.domain.tld:1234
16+
required: false
17+
username:
18+
type: string
19+
description: "Optional override of the username in <connection> (required if <connection> is not specified). Username for authentication"
20+
required: false
21+
password:
22+
type: string
23+
description: "Optional override of the password in <connection> (required if <connection> is not specified). Password of the specified username"
24+
secret: true
25+
required: false
26+
database:
27+
type: string
28+
description: "Optional override of the database in <connection> (required if <connection> is not specified). Database to connect to, to run querys against."
29+
required: false
30+
database_type:
31+
type: string
32+
description: "Optional override of the database_type in <connection> (required if <connection> is not specified). The type of database that is being connected to."
33+
enum:
34+
- postgresql
35+
- sqlite
36+
- mssql
37+
- mysql
38+
- oracle
39+
- firebird
40+
- sybase
41+
required: false
42+
table:
43+
type: string
44+
description: "Database table to insert data into."
45+
required: true
46+
data:
47+
type: array
48+
description: >
49+
List of Dictionaries of data to be inserted where the key corresponds to the column of the table
50+
[{
51+
'column_1': 'data_to_insert_1',
52+
'column_2': 'data_to_insert_2',
53+
'column_3': 'data_to_insert_3',
54+
'column_4': 'data_to_insert_4',
55+
},{
56+
'column_1': 'data_to_insert_1',
57+
'column_2': 'data_to_insert_2',
58+
'column_3': 'data_to_insert_3',
59+
'column_4': 'data_to_insert_4',
60+
}]
61+
required: true

actions/lib/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)