Skip to content

Commit 68235a8

Browse files
authored
Allow saving results in st2 key/value store (#6)
* first pass on storing results in keystore * updated keynames * added params to all get actions * added changelog entry and version bumb
1 parent 5f00f68 commit 68235a8

12 files changed

+121
-3
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Change Log
2+
## 0.3.0
3+
- Added ability to save results into st2 key/value store. Useful when the result from Netbox is very large and will be piped to another action.
24
## 0.2.1
35
- Added `role` to dcim_get_devices
46
## 0.2.0

actions/base_get_action.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11

2+
import json
3+
24
from lib.action import NetboxBaseAction
5+
from st2client.client import Client
6+
from st2client.models import KeyValuePair
37

48

59
class NetboxBaseGetAction(NetboxBaseAction):
@@ -9,4 +13,19 @@ def run(self, endpoint_uri, **kwargs):
913
"""Base get action
1014
endpoint_uri is pased from metadata file
1115
"""
16+
17+
if kwargs.get('save_in_key_store') and not kwargs.get('save_in_key_store_key_name'):
18+
return (False, 'save_in_key_store_key_name MUST be used with save_in_key_store!')
19+
20+
result = self.get(endpoint_uri, **kwargs)
21+
22+
if kwargs['save_in_key_store']:
23+
# save the result in the st2 keystore
24+
client = Client(base_url='http://localhost')
25+
key_name = kwargs['save_in_key_store_key_name']
26+
client.keys.update(KeyValuePair(name=key_name, value=json.dumps(result),
27+
ttl=kwargs['save_in_key_store_ttl']))
28+
29+
return (True, "Result stored in st2 key {}".format(key_name))
30+
1231
return self.get(endpoint_uri, **kwargs)

actions/dcim_get_devices.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,14 @@ parameters:
9595
type: integer
9696
default: 0
9797
description: Offset result set by X objects. Used for pagination.
98+
save_in_key_store:
99+
type: boolean
100+
default: false
101+
description: Save the result of the action as a json object in the st2 key store. Used when the expected result from Netbox is very large and the result will be piped to another action. You must also specify a save_in_key_store_keyname and an optional save_in_key_store_ttl.
102+
save_in_key_store_key_name:
103+
type: string
104+
description: Name of the key to store the json result value in the st2 key store. Must be used with save_in_key_store and optionally save_in_key_store_ttl.
105+
save_in_key_store_ttl:
106+
type: integer
107+
default: 60
108+
description: TTL (seconds) of the saved json result in the st2 key store. Defaults to 60 seconds. Must be used with save_in_key_store and save_in_key_store_key_name.

actions/dcim_get_interfaces.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,14 @@ parameters:
3535
type: integer
3636
default: 0
3737
description: Offset result set by X objects. Used for pagination.
38+
save_in_key_store:
39+
type: boolean
40+
default: false
41+
description: Save the result of the action as a json object in the st2 key store. Used when the expected result from Netbox is very large and the result will be piped to another action. You must also specify a save_in_key_store_keyname and an optional save_in_key_store_ttl.
42+
save_in_key_store_key_name:
43+
type: string
44+
description: Name of the key to store the json result value in the st2 key store. Must be used with save_in_key_store and optionally save_in_key_store_ttl.
45+
save_in_key_store_ttl:
46+
type: integer
47+
default: 60
48+
description: TTL (seconds) of the saved json result in the st2 key store. Defaults to 60 seconds. Must be used with save_in_key_store and save_in_key_store_key_name.

actions/dcim_get_sites.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,14 @@ parameters:
4444
type: integer
4545
default: 0
4646
description: Offset result set by X objects. Used for pagination.
47+
save_in_key_store:
48+
type: boolean
49+
default: false
50+
description: Save the result of the action as a json object in the st2 key store. Used when the expected result from Netbox is very large and the result will be piped to another action. You must also specify a save_in_key_store_keyname and an optional save_in_key_store_ttl.
51+
save_in_key_store_key_name:
52+
type: string
53+
description: Name of the key to store the json result value in the st2 key store. Must be used with save_in_key_store and optionally save_in_key_store_ttl.
54+
save_in_key_store_ttl:
55+
type: integer
56+
default: 60
57+
description: TTL (seconds) of the saved json result in the st2 key store. Defaults to 60 seconds. Must be used with save_in_key_store and save_in_key_store_key_name.

actions/ipam_get_available_ips.yaml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,14 @@ parameters:
2020
type: integer
2121
default: 0
2222
description: Offset result set by X objects. Used for pagination.
23-
23+
save_in_key_store:
24+
type: boolean
25+
default: false
26+
description: Save the result of the action as a json object in the st2 key store. Used when the expected result from Netbox is very large and the result will be piped to another action. You must also specify a save_in_key_store_keyname and an optional save_in_key_store_ttl.
27+
save_in_key_store_key_name:
28+
type: string
29+
description: Name of the key to store the json result value in the st2 key store. Must be used with save_in_key_store and optionally save_in_key_store_ttl.
30+
save_in_key_store_ttl:
31+
type: integer
32+
default: 60
33+
description: TTL (seconds) of the saved json result in the st2 key store. Defaults to 60 seconds. Must be used with save_in_key_store and save_in_key_store_key_name.

actions/ipam_get_ip_addresses.yaml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,14 @@ parameters:
5656
type: integer
5757
default: 0
5858
description: Offset result set by X objects. Used for pagination.
59-
59+
save_in_key_store:
60+
type: boolean
61+
default: false
62+
description: Save the result of the action as a json object in the st2 key store. Used when the expected result from Netbox is very large and the result will be piped to another action. You must also specify a save_in_key_store_keyname and an optional save_in_key_store_ttl.
63+
save_in_key_store_key_name:
64+
type: string
65+
description: Name of the key to store the json result value in the st2 key store. Must be used with save_in_key_store and optionally save_in_key_store_ttl.
66+
save_in_key_store_ttl:
67+
type: integer
68+
default: 60
69+
description: TTL (seconds) of the saved json result in the st2 key store. Defaults to 60 seconds. Must be used with save_in_key_store and save_in_key_store_key_name.

actions/ipam_get_prefixes.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,14 @@ parameters:
6565
type: integer
6666
default: 0
6767
description: Offset result set by X objects. Used for pagination.
68+
save_in_key_store:
69+
type: boolean
70+
default: false
71+
description: Save the result of the action as a json object in the st2 key store. Used when the expected result from Netbox is very large and the result will be piped to another action. You must also specify a save_in_key_store_keyname and an optional save_in_key_store_ttl.
72+
save_in_key_store_key_name:
73+
type: string
74+
description: Name of the key to store the json result value in the st2 key store. Must be used with save_in_key_store and optionally save_in_key_store_ttl.
75+
save_in_key_store_ttl:
76+
type: integer
77+
default: 60
78+
description: TTL (seconds) of the saved json result in the st2 key store. Defaults to 60 seconds. Must be used with save_in_key_store and save_in_key_store_key_name.

actions/ipam_get_vlan_groups.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,14 @@ parameters:
2626
type: integer
2727
default: 0
2828
description: Offset result set by X objects. Used for pagination.
29+
save_in_key_store:
30+
type: boolean
31+
default: false
32+
description: Save the result of the action as a json object in the st2 key store. Used when the expected result from Netbox is very large and the result will be piped to another action. You must also specify a save_in_key_store_keyname and an optional save_in_key_store_ttl.
33+
save_in_key_store_key_name:
34+
type: string
35+
description: Name of the key to store the json result value in the st2 key store. Must be used with save_in_key_store and optionally save_in_key_store_ttl.
36+
save_in_key_store_ttl:
37+
type: integer
38+
default: 60
39+
description: TTL (seconds) of the saved json result in the st2 key store. Defaults to 60 seconds. Must be used with save_in_key_store and save_in_key_store_key_name.

actions/ipam_get_vlans.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,14 @@ parameters:
5656
type: integer
5757
default: 0
5858
description: Offset result set by X objects. Used for pagination.
59+
save_in_key_store:
60+
type: boolean
61+
default: false
62+
description: Save the result of the action as a json object in the st2 key store. Used when the expected result from Netbox is very large and the result will be piped to another action. You must also specify a save_in_key_store_keyname and an optional save_in_key_store_ttl.
63+
save_in_key_store_key_name:
64+
type: string
65+
description: Name of the key to store the json result value in the st2 key store. Must be used with save_in_key_store and optionally save_in_key_store_ttl.
66+
save_in_key_store_ttl:
67+
type: integer
68+
default: 60
69+
description: TTL (seconds) of the saved json result in the st2 key store. Defaults to 60 seconds. Must be used with save_in_key_store and save_in_key_store_key_name.

0 commit comments

Comments
 (0)