forked from StackStorm-Exchange/stackstorm-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathst2packgen.py
120 lines (92 loc) · 3.55 KB
/
st2packgen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/python
from botocore.session import Session
import re
import os
import sys
import botocore
import argparse
import jinja2
def striphtml(data):
p = re.compile(r'<.*?>')
return p.sub('', data)
def convert(name):
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
parser = argparse.ArgumentParser(description="Generate aws stackstorm actions")
parser.add_argument('-d', '--outputdir', default="actions", help="base output directory")
parser.add_argument('-s', '--service', default=None, help="service to generate actions for (eg s3)")
args = parser.parse_args()
outputdir = args.outputdir
myservice = args.service
# print "outputdir: %s" % outputdir
# print "myservice: %s" % myservice
try:
os.stat(outputdir)
except: # noqa: E722
os.mkdir(outputdir)
templateLoader = jinja2.FileSystemLoader(searchpath="templates")
templateEnv = jinja2.Environment(loader=templateLoader)
session = Session()
if myservice is None:
myservices = session.get_available_services()
else:
myservices = [myservice]
for myservice in myservices:
print("%s:" % myservice)
try:
mysrv = session.get_service_model(myservice)
except botocore.exceptions.UnknownServiceError as e:
print("\n%s\n" % e)
sys.exit(1)
for op in mysrv.operation_names: # pylint: disable=not-an-iterable
allvars = {}
allvars['paramsreq'] = []
allvars['params'] = []
model = mysrv.operation_model(op)
op = convert(op)
print(" " + op)
allvars['action'] = op
allvars['name'] = myservice + "_" + op
allvars['service'] = myservice
if model.input_shape is None:
continue
members = model.input_shape.members # pylint: disable=no-member
smodel = model.service_model
# print smodel._shape_resolver
smembers = model.input_shape._shape_model['members'] # pylint: disable=no-member
for sname, sdata in smembers.items():
tmp = {}
stype = smodel._shape_resolver._shape_map[sdata['shape']]['type']
# blob defined in boto as bytes or seekable file-like object - not supported here
if stype == "blob":
stype = "string"
if stype == "double":
stype = "number"
if stype == "long":
stype = "integer"
if stype == "structure":
stype = "object"
if stype == "map":
stype = "object"
if stype == "list":
stype = "array"
if stype == "timestamp":
stype = "string"
if stype == "float":
stype = "number"
tmp['name'] = sname
tmp['type'] = stype
if 'documentation' in sdata:
tmp['description'] = striphtml(sdata['documentation'].rstrip().replace('"', "'"))
tmp['description'] = striphtml(tmp['description'].replace("\\", "\\\\"))
else:
tmp['description'] = ''
if sname in model.input_shape.required_members: # pylint: disable=no-member
allvars['paramsreq'].append(tmp)
else:
allvars['params'].append(tmp)
actionyaml = outputdir + "/" + allvars['name'] + ".yaml"
template = templateEnv.get_template('action_template.yaml.jinja')
with open(actionyaml, 'w') as y:
outputText = template.render(allvars).encode('utf8') # pylint: disable=no-member
y.write(outputText)