-
Notifications
You must be signed in to change notification settings - Fork 2
/
provisioner-api.py
executable file
·95 lines (78 loc) · 3.08 KB
/
provisioner-api.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
#!venv/bin/python3
import flask
from flask import request, jsonify
import lib.config_template as ct
from lib.configurator import t128ConfigHelper
from jinja2 import Environment, FileSystemLoader, meta
from jinja2.exceptions import TemplateNotFound
import os
from ncclient.operations.rpc import RPCError
from lib.ote_utils.netconfutils.netconfconverter import ConfigParseError
FLASK_DIR = 'flask_pages'
def _load_environment():
return Environment(loader=FileSystemLoader(FLASK_DIR))
app = flask.Flask(__name__)
app.config['DEBUG'] = True
@app.route('/api/v1/list_templates', methods=['GET'])
def api_list_templates():
return jsonify(ct.list_config_templates())
@app.route('/api/v1/get_template_variables', methods=['GET'])
def api_get_template_variables():
if 'template' in request.args:
template_name = request.args['template']
else:
return "ERROR: Template name not provided"
vars = list(ct.show_template_variables(template_name))
return jsonify(vars)
@app.route('/', methods=['GET'])
def display_templates():
jj = _load_environment()
templates = ct.list_config_templates()
context = {}
context['templates'] = templates
page_template = jj.get_template('list_templates.j2')
return page_template.render(context)
@app.route('/fill_template', methods=['GET'])
def fill_template():
jj = _load_environment()
if 'template' in request.args:
template_name = request.args['template']
else:
return "ERROR: Template name not specified"
vars = list(ct.show_template_variables(template_name))
context = {}
context['template'] = template_name
context['variables'] = vars
page_template = jj.get_template('enter_variables.j2')
return page_template.render(context)
@app.route('/api/v1/push_template', methods=['POST'])
def push_template():
context = dict(request.form)
print(context)
conductor_netconf_ip = context['conductor_netconf_ip']
try:
text_config = ct.get_text_config(context, context['template_name'])
with open('consolidatedT128Model.xml') as t128_model:
xml_config = ct.get_xml_config(text_config, t128_model)
except ConfigParseError as e:
return "There was an error in the config: {}".format(e)
try:
with t128ConfigHelper(host=conductor_netconf_ip) as ch:
edit_status = ch.edit(xml_config, 'conductor timeout error')
except RPCError as e:
return "There was an error in the config: {}".format(e)
if isinstance(edit_status, str):
return edit_status
if edit_status.ok:
with t128ConfigHelper(host=conductor_netconf_ip) as ch:
try:
commit_status = ch.commit()
except RPCError as e:
return "There was an error committing the configuration, please check the candidate config: {}".format(e)
if commit_status.ok:
return "Configuration committed successfully"
else:
return "There was an error committing the config"
else:
return "There was an error adding the candidate config"
app.run(host='0.0.0.0')