-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathndfc-template.py
140 lines (129 loc) · 4.46 KB
/
ndfc-template.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import requests
import os
from urllib3.exceptions import InsecureRequestWarning
import click
from prettytable import PrettyTable
import json
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
class ndfcTemplate(object):
def __init__(self):
self.base_url = ""
self.username = ""
self.password = ""
self.domain = ""
self.ssl_verify = False
self.headers = {"Content-Type": "application/json"}
def login(self, user, pw, url, domain='local'):
self.base_url = url
self.username = user
self.password = pw
self.domain = domain
payload = f'{{ "userName": "{self.username}", "userPasswd": "{self.password}", "domain": "{self.domain}" }}'
response = requests.request(
'POST',
f'{self.base_url}/login',
headers=self.headers,
data=payload,
verify=self.ssl_verify
)
if response.status_code == 200:
self.headers["Cookie"] = f'AuthCookie={response.json()["jwttoken"]}'
else:
print(f"{response.text}")
exit(1)
def list(self, name:str="") -> list:
template_list = []
response = requests.request(
"GET",
f'{self.base_url}/appcenter/cisco/ndfc/api/v1/configtemplate/rest/config/templates/{name}',
headers=self.headers,
verify=self.ssl_verify
)
if response.status_code == 200:
if name == '':
for item in response.json():
template_list.append(item)
else:
template_list.append(response.json())
return template_list
def get(self, template_name: str, path: str) -> None:
response = requests.request(
"GET",
f'{self.base_url}/appcenter/cisco/ndfc/api/v1/configtemplate/rest/config/templates/{template_name}',
headers=self.headers,
verify=self.ssl_verify
)
content = response.json()["content"]
with open(f"{path}/{template_name}.template", "w") as f:
f.write(content)
@click.group()
@click.pass_context
def ndfc_template(ctx):
'''
Command Line Interface for managing Cisco NDFC Template
'''
if "NDFC_USERNAME" not in os.environ or "NDFC_PASSWORD" not in os.environ or "ND_URL" not in os.environ:
click.secho(
"ERROR: You must specify NDFC_USERNAME and NDFC_PASSWORD and ND_URL as environment variables.",
fg="red"
)
exit(1)
obj = ndfcTemplate()
obj.login(
user=os.environ['NDFC_USERNAME'],
pw=os.environ['NDFC_PASSWORD'],
url=os.environ['ND_URL'],
domain=os.environ['ND_DOMAIN']
)
ctx.obj = obj
@click.command()
@click.pass_obj
@click.option("--name", type=str, required=False, default='', help='template name')
@click.option("--all", 'type', flag_value='all', default=True)
@click.option("--template-cli", 'type', flag_value='TEMPLATE_CLI', default=True)
@click.option("--python", 'type', flag_value='PYTHON', default=True)
def list(obj, name, type):
'''Display a list of template from NDFC'''
table = PrettyTable()
table.field_names = [
"name",
"templateType",
"templateSubType",
"contentType",
"supportedPlatforms"
]
table.align = "l"
for item in obj.list(name=name):
row = [
item["name"],
item["templateType"],
item["templateSubType"],
item["contentType"],
item["supportedPlatforms"]
]
if type != 'all':
if item["contentType"] == type:
table.add_row(row)
else:
table.add_row(row)
click.echo(table)
@click.command()
@click.pass_obj
@click.option("--name", type=str, required=False, help='name of template to download')
@click.option("--all", 'all', flag_value=True, default=False, help='download all templates')
@click.option("--path", type=str, required=True, default=".", help='path for downloading files')
def get(obj, name, all, path):
'''Download NDFC template(s)'''
if not name and not all:
print("Error: Command 'get' requires arguments: --all or --name")
exit(1)
if not all:
obj.get(name, path)
elif all:
templates = []
for template in obj.list():
obj.get(template['name'], path)
ndfc_template.add_command(list)
ndfc_template.add_command(get)
if __name__ == "__main__":
ndfc_template()