blob: 1d733bca8f121fcebb0525a184d66e144b7397d5 [file] [log] [blame]
tiernoc94c3df2018-02-09 15:38:54 +01001"""
2Contains html text in variables to make and html response
3"""
4
5import yaml
6from http import HTTPStatus
7
8__author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
9
10html_start = """
11 <!DOCTYPE html>
12<html>
13<head>
14 <link href="/osm/static/style.css" rel="stylesheet">
15<title>Welcome to OSM</title>
16</head>
17<body>
18 <div id="osm_topmenu">
19 <div>
20 <a href="https://osm.etsi.org"> <img src="/osm/static/OSM-logo.png" height="42" width="100" style="vertical-align:middle"> </a>
21 <a>( {} )</a>
tiernof27c79b2018-03-12 17:08:42 +010022 <a href="/osm/vnfpkgm/v1/vnf_packages_content">VNFDs </a>
23 <a href="/osm/nsd/v1/ns_descriptors_content">NSDs </a>
24 <a href="/osm/nslcm/v1/ns_instances_content">NSs </a>
25 <a href="/osm/admin/v1/users">USERs </a>
26 <a href="/osm/admin/v1/projects">PROJECTs </a>
27 <a href="/osm/admin/v1/tokens">TOKENs </a>
tierno65acb4d2018-04-06 16:42:40 +020028 <a href="/osm/admin/v1/vims">VIMs </a>
tiernof27c79b2018-03-12 17:08:42 +010029 <a href="/osm/admin/v1/tokens?METHOD=DELETE">logout </a>
tiernoc94c3df2018-02-09 15:38:54 +010030 </div>
31 </div>
32"""
33
34html_body = """
35<h1>{item}</h1>
36"""
37
38html_end = """
39</body>
40</html>
41"""
42
43html_body_error = "<h2> Error <pre>{}</pre> </h2>"
44
45
46
47html_auth2 = """
48<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
49<html>
50<head><META http-equiv="Content-Type" content="text/html; charset=UTF-8">
51 <link href="/osm/static/style.css" rel="stylesheet">
52 <title>OSM Login</title>
53</head>
54<body>
55 <div id="osm_header">
56 <div>
57 <a href="https://osm.etsi.org"> <h1><img src="/osm/static/OSM-logo.png" style="vertical-align:middle"></h1> </a>
58 </div>
59 </div>
60 <div id="osm_error_message">
61 <h1>{error}</h1>
62 </div>
63 <div class="gerritBody" id="osm_body">
64 <h1>Sign in to OSM</h1>
tiernof27c79b2018-03-12 17:08:42 +010065 <form action="/osm/admin/v1/tokens" id="login_form" method="POST">
tiernoc94c3df2018-02-09 15:38:54 +010066 <table style="border: 0;">
67 <tr><th>Username</th><td><input id="f_user" name="username" size="25" tabindex="1" type="text"></td></tr>
68 <tr><th>Password</th><td><input id="f_pass" name="password" size="25" tabindex="2" type="password"></td></tr>
69 <tr><td><input tabindex="3" type="submit" value="Sign In"></td></tr>
70 </table>
71 </form>
72 <div style="clear: both; margin-top: 15px; padding-top: 2px; margin-bottom: 15px;">
73 <div id="osm_footer">
74 <div></div>
75 </div>
76 </div>
77 </div>
78 <script src="/osm/static/login.js"> </script>
79</body>
80</html>
81"""
82
83html_upload_body = """
84<form action="/osm{}" method="post" enctype="multipart/form-data">
85 <h3> <table style="border: 0;"> <tr>
86 <td> Upload {} descriptor (tar.gz) file: <input type="file" name="descriptor_file"/> </td>
87 <td> <input type="submit" value="Upload"/> </td>
88 </tr> </table> </h3>
89</form>
90"""
91
tierno65acb4d2018-04-06 16:42:40 +020092html_nslcmop_body = """
93<a href="/osm/nslcm/v1/ns_lcm_op_occs?nsInstanceId={id}">nslcm operations </a>
94<form action="/osm/nslcm/v1/ns_instances/{id}/terminate" method="post" enctype="multipart/form-data">
95 <h3> <table style="border: 0;"> <tr>
96 <td> <input type="submit" value="Terminate"/> </td>
97 </tr> </table> </h3>
98</form>
99"""
100
tiernoc94c3df2018-02-09 15:38:54 +0100101
102def format(data, request, response, session):
103 """
104 Format a nice html response, depending on the data
105 :param data:
106 :param request: cherrypy request
107 :param response: cherrypy response
108 :return: string with teh html response
109 """
110 response.headers["Content-Type"] = 'text/html'
111 if response.status == HTTPStatus.UNAUTHORIZED.value:
112 if response.headers.get("WWW-Authenticate") and request.config.get("auth.allow_basic_authentication"):
113 response.headers["WWW-Authenticate"] = "Basic" + response.headers["WWW-Authenticate"][6:]
114 return
115 else:
116 return html_auth2.format(error=data)
117 body = html_body.format(item=request.path_info)
118 if response.status and response.status > 202:
119 body += html_body_error.format(yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False))
120 elif isinstance(data, (list, tuple)):
tiernof27c79b2018-03-12 17:08:42 +0100121 if request.path_info == "/vnfpkgm/v1/vnf_packages_content":
tierno0f98af52018-03-19 10:28:22 +0100122 body += html_upload_body.format(request.path_info, "VNFD")
tiernof27c79b2018-03-12 17:08:42 +0100123 elif request.path_info == "/nsd/v1/ns_descriptors_content":
tierno0f98af52018-03-19 10:28:22 +0100124 body += html_upload_body.format(request.path_info, "NSD")
tiernoc94c3df2018-02-09 15:38:54 +0100125 for k in data:
tiernof27c79b2018-03-12 17:08:42 +0100126 if isinstance(k, dict):
127 data_id = k.pop("_id", None)
128 elif isinstance(k, str):
129 data_id = k
tiernoc94c3df2018-02-09 15:38:54 +0100130 body += '<p> <a href="/osm/{url}/{id}">{id}</a>: {t} </p>'.format(url=request.path_info, id=data_id, t=k)
131 elif isinstance(data, dict):
132 if "Location" in response.headers:
133 body += '<a href="{}"> show </a>'.format(response.headers["Location"])
134 else:
135 body += '<a href="/osm/{}?METHOD=DELETE"> <img src="/osm/static/delete.png" height="25" width="25"> </a>'.format(request.path_info)
tierno65acb4d2018-04-06 16:42:40 +0200136 if request.path_info.startswith("/nslcm/v1/ns_instances_content/") or \
137 request.path_info.startswith("/nslcm/v1/ns_instances/"):
138 _id = request.path_info[request.path_info.rfind("/")+1:]
139 body += html_nslcmop_body.format(id=_id)
tiernoc94c3df2018-02-09 15:38:54 +0100140 body += "<pre>" + yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False) + "</pre>"
tierno0f98af52018-03-19 10:28:22 +0100141 elif data is None:
142 if request.method == "DELETE" or "METHOD=DELETE" in request.query_string:
143 body += "<pre> deleted </pre>"
tiernoc94c3df2018-02-09 15:38:54 +0100144 else:
145 body = str(data)
146 user_text = " "
147 if session:
148 if session.get("username"):
149 user_text += "user: {}".format(session.get("username"))
150 if session.get("project_id"):
151 user_text += ", project: {}".format(session.get("project_id"))
152 return html_start.format(user_text) + body + html_end
153 #yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False)
154 # tags=False,
155 # encoding='utf-8', allow_unicode=True)
156
157