-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathtemplate.py
More file actions
executable file
·288 lines (244 loc) · 10.7 KB
/
Copy pathtemplate.py
File metadata and controls
executable file
·288 lines (244 loc) · 10.7 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/env python
from __future__ import print_function
import sys
import json
import logging
from argparse import ArgumentParser
import csv
from util import DeploymentError
from util import get_url, deploy_and_wait, put_and_wait, post_and_wait, put
def show_templates():
print("Available Templates:")
result = get_url("dna/intent/api/v1/template-programmer/template")
print ('\n'.join(sorted([ ' {0}/{1}'.format(project['projectName'], project['name']) for project in result])))
#for project in result:
# print( '{0}/{1}'.format(project['projectName'], project['name']))
def get_template_id(fqtn):
'''
takes a fully qualified template name (project/template) and returns the id and number of the latest version
:param fqtn:
:return: uuid of the latest version as well as the version number
'''
parts = fqtn.split("/")
projectName = parts[0]
templateName = parts[1]
print ('Looking for: {0}/{1}'.format(projectName, templateName))
result = get_url("dna/intent/api/v1/template-programmer/template")
max = 0
id = 0
for project in result:
if project['projectName'] == projectName and project['name'] == templateName:
# look for latest version
for v in project['versionsInfo']:
#if v['version'] > max:
if int(v['version']) > max:
max = int(v['version'])
id = v['id']
return id,max
def check_implicit(reqparams, device, params):
# TODO: This just looks for __device and __interface and adds the managed deviceIP to the resourceParms attribute to resolve implict vars
# Need to extend this to handle the other use cases
# "type" : "SITE_UUID", __sitetag
# "type": "MANAGED_AP_LOCATIONS",
# "type": "SECONDARY_MANAGED_AP_LOCATIONS",
# "type": "POLICY_PROFILES" __policyprofile
# need to get these from the values passed in as params and translate them. They are a comma sepperated list
#
return_resource = False
try:
req_dict = json.loads(reqparams)
except json.decoder.JSONDecodeError as e:
sys.exit(f"Invalid params {e.message}")
for params in req_dict.keys():
print(params)
if "__device" in params or "__interface" in params:
return_resource = True
if return_resource == True:
resourceParams = [
{
"type": "MANAGED_DEVICE_IP",
"value": device
}
]
return resourceParams
return None
def execute(id, reqparams, bindings, device, params, doForce):
#parts = deviceParams.split(';')
#device = parts[0]
#params = json.loads(parts[1])
print ("\nExecuting template on:{0}, with Params:{1}".format(device,params))
# need to check device params to make sure all present
payload = {
"templateId": id,
"forcePushTemplate" : doForce,
"targetInfo": [
{
"id": device,
"type": "MANAGED_DEVICE_IP",
"params": json.loads(params)
}
]
}
# this is for implicit variables, needs to be generalised
resource_params = check_implicit(reqparams, device, params)
if resource_params is not None:
payload['targetInfo'][0]['resourceParams']= resource_params
print ("payload", json.dumps(payload))
return deploy_and_wait("dna/intent/api/v1/template-programmer/template/deploy", payload)
def preview_template(id, params):
print("Previewing template")
payload = {
"templateId": id,
"params": json.loads(params)
}
#print(json.dumps(payload))
response = put("dna/intent/api/v1/template-programmer/template/preview", payload)
if "cliPreview" in response:
print(response['cliPreview'])
else:
print(json.dumps(response,indent=2))
def bulk(id, reqparams, bindings, bulkfile, doForce):
targets = []
with open(bulkfile, "rt") as f:
reader = csv.DictReader(f)
for row in reader:
device_ip = row.pop('device_ip')
params = dict(row)
targets.append ({"id": device_ip, "type": "MANAGED_DEVICE_IP","params": params})
print("\nExecuting template on:{0}, with Params:{1}".format(device_ip, params))
# need to check device params to make sure all present
payload = {
"templateId": id,
"forcePushTemplate" : doForce,
"targetInfo": targets
}
print ("payload", json.dumps(payload))
return(deploy_and_wait("dna/intent/api/v1/template-programmer/template/deploy", payload))
def paramsfiletojson (paramsfile):
params = {}
headers = []
with open(paramsfile, "rt") as f:
reader = csv.reader(f)
headers = [header.strip() for header in next(f).split(",")]
for line in f:
values = [value.strip() for value in line.split(",")]
for (header,value) in zip(headers,values):
if (header not in params):
params[header] = [value]
else:
params[header].append(value)
return (json.dumps(params))
def print_template(template):
print("Showing Template Body:")
print(template)
# wolverine hack
if 'response' in template:
template=template['response']
print(template['templateContent'])
params = ['"{0}":""'.format(p['parameterName']) for p in template['templateParams']
if p['binding'] == '']
params = ",".join(params)
params = '{' + params + '}'
print("\nRequired Parameters for template body:", params)
bindings = ['"{0}":"{1}.{2}"'.format(p['parameterName'],
json.loads(p['binding'])['source'],
json.loads(p['binding'])['entity']) for p in template['templateParams']
if p['binding'] != '']
print ("\nBindings", bindings)
return params, bindings
def update_template(template, template_name, new_body):
'''
takes an existing template and updates it with a new body. Does a save and commit
:param template:
:param template_name:
:param new_body:
:return:
'''
# need to be careful here with params and how they are handled....
print("Updating template:{} to {}".format(template_name, new_body))
print(json.dumps(template))
template['templateContent'] = new_body
# need to change the template ID to the parent
template_id = template['parentTemplateId']
template['id'] = template_id
# need to find a way to get the variables from the new template... addition/subtraction
response = put_and_wait("template-programmer/template", template)
print("Saving:{}".format(response['progress']))
body= {"comments":"","templateId":template_id}
response = post_and_wait("template-programmer/template/version", body)
print("Commit:{}".format(response['progress']))
def uuid2ip(uuid):
response = get_url('network-device/{}'.format(uuid))
return (response['response']['managementIpAddress'])
def parse_response(response):
print(json.dumps(response, indent=2))
# prime cache as response is broken.
cache={}
for device in response['devices']:
cache[device['deviceId']] = uuid2ip(device['ipAddress'])
print('Response:')
print('Deployment of template:{}/{}(v{}) (id:{})'.format(response['projectName'],
response['templateName'],
response['templateVersion'],
response['deploymentId']))
for device in response['devices']:
print('{}:{}:{}'.format(cache[device['ipAddress']],
device['status'],
device['detailedStatusMessage']))
if __name__ == "__main__":
parser = ArgumentParser(description='Select options.')
parser.add_argument('--template', type=str, required=False,
help="tempate name (project/template")
parser.add_argument('--update', type=str, required=False,
help="update body of template. Be careful of unix variable substution, use '<template body>'")
parser.add_argument('--device', type=str, required=False,
help="deviceIp e.g 1.1.1.1")
parser.add_argument('--preview', action="store_true", default=False,
help="preview aa template")
parser.add_argument('--force', action="store_true", default = False,
help="force template to be appliec")
parser.add_argument('--params', type=str, required=False,
help="'{\"param1\" :\"v1\"}'")
parser.add_argument('--paramsfile', type=str, required=False,
help="json file containing params")
parser.add_argument('--bulkfile', type=str, required=False,
help="csv file containing devices + params")
parser.add_argument('-v', action='store_true',
help="verbose")
args = parser.parse_args()
if args.v:
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
# no args, print all projects/templates
if args.template:
id, v = get_template_id(args.template)
if id == 0:
raise ValueError('Cannot find template:{0}'.format(args.template))
print ("TemplateId:", id, "Version:", v, "\n")
template = get_url('dna/intent/api/v1/template-programmer/template/{0}'.format(id))
params, bindings = print_template(template)
if args.update:
update_template(template, args.template, args.update)
if args.device:
if args.paramsfile:
if args.paramsfile.endswith('.json'):
with open(args.paramsfile, "r") as f:
print(args.paramsfile)
inputParams = json.dumps(json.load(f))
elif args.paramsfile.endswith('.csv'):
inputParams = paramsfiletojson(args.paramsfile)
else:
print("Error reading paramsfile - expected <.csv|.json> in argument")
sys.exit()
if args.preview:
preview_template(id, inputParams)
sys.exit(0)
else:
inputParams = args.params
response = execute(id, params, bindings, args.device, inputParams, args.force)
print ("\nResponse:")
print (json.dumps(response, indent=2))
elif args.bulkfile:
response = bulk(id, params, bindings, args.bulkfile, args.force)
parse_response(response)
else:
show_templates()