add Jenkinsfile
[osm/NBI.git] / osm_nbi / html_out.py
1 """
2 Contains html text in variables to make and html response
3 """
4
5 import yaml
6 from http import HTTPStatus
7
8 __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
9
10 html_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>
22 <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>
28 <a href="/osm/admin/v1/tokens?METHOD=DELETE">logout </a>
29 </div>
30 </div>
31 """
32
33 html_body = """
34 <h1>{item}</h1>
35 """
36
37 html_end = """
38 </body>
39 </html>
40 """
41
42 html_body_error = "<h2> Error <pre>{}</pre> </h2>"
43
44
45
46 html_auth2 = """
47 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
48 <html>
49 <head><META http-equiv="Content-Type" content="text/html; charset=UTF-8">
50 <link href="/osm/static/style.css" rel="stylesheet">
51 <title>OSM Login</title>
52 </head>
53 <body>
54 <div id="osm_header">
55 <div>
56 <a href="https://osm.etsi.org"> <h1><img src="/osm/static/OSM-logo.png" style="vertical-align:middle"></h1> </a>
57 </div>
58 </div>
59 <div id="osm_error_message">
60 <h1>{error}</h1>
61 </div>
62 <div class="gerritBody" id="osm_body">
63 <h1>Sign in to OSM</h1>
64 <form action="/osm/admin/v1/tokens" id="login_form" method="POST">
65 <table style="border: 0;">
66 <tr><th>Username</th><td><input id="f_user" name="username" size="25" tabindex="1" type="text"></td></tr>
67 <tr><th>Password</th><td><input id="f_pass" name="password" size="25" tabindex="2" type="password"></td></tr>
68 <tr><td><input tabindex="3" type="submit" value="Sign In"></td></tr>
69 </table>
70 </form>
71 <div style="clear: both; margin-top: 15px; padding-top: 2px; margin-bottom: 15px;">
72 <div id="osm_footer">
73 <div></div>
74 </div>
75 </div>
76 </div>
77 <script src="/osm/static/login.js"> </script>
78 </body>
79 </html>
80 """
81
82 html_upload_body = """
83 <form action="/osm{}" method="post" enctype="multipart/form-data">
84 <h3> <table style="border: 0;"> <tr>
85 <td> Upload {} descriptor (tar.gz) file: <input type="file" name="descriptor_file"/> </td>
86 <td> <input type="submit" value="Upload"/> </td>
87 </tr> </table> </h3>
88 </form>
89 """
90
91
92 def format(data, request, response, session):
93 """
94 Format a nice html response, depending on the data
95 :param data:
96 :param request: cherrypy request
97 :param response: cherrypy response
98 :return: string with teh html response
99 """
100 response.headers["Content-Type"] = 'text/html'
101 if response.status == HTTPStatus.UNAUTHORIZED.value:
102 if response.headers.get("WWW-Authenticate") and request.config.get("auth.allow_basic_authentication"):
103 response.headers["WWW-Authenticate"] = "Basic" + response.headers["WWW-Authenticate"][6:]
104 return
105 else:
106 return html_auth2.format(error=data)
107 body = html_body.format(item=request.path_info)
108 if response.status and response.status > 202:
109 body += html_body_error.format(yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False))
110 elif isinstance(data, (list, tuple)):
111 if request.path_info == "/vnfpkgm/v1/vnf_packages_content":
112 body += html_upload_body.format(request.path_info, "VNFD")
113 elif request.path_info == "/nsd/v1/ns_descriptors_content":
114 body += html_upload_body.format(request.path_info, "NSD")
115 for k in data:
116 if isinstance(k, dict):
117 data_id = k.pop("_id", None)
118 elif isinstance(k, str):
119 data_id = k
120 body += '<p> <a href="/osm/{url}/{id}">{id}</a>: {t} </p>'.format(url=request.path_info, id=data_id, t=k)
121 elif isinstance(data, dict):
122 if "Location" in response.headers:
123 body += '<a href="{}"> show </a>'.format(response.headers["Location"])
124 else:
125 body += '<a href="/osm/{}?METHOD=DELETE"> <img src="/osm/static/delete.png" height="25" width="25"> </a>'.format(request.path_info)
126 body += "<pre>" + yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False) + "</pre>"
127 elif data is None:
128 if request.method == "DELETE" or "METHOD=DELETE" in request.query_string:
129 body += "<pre> deleted </pre>"
130 else:
131 body = str(data)
132 user_text = " "
133 if session:
134 if session.get("username"):
135 user_text += "user: {}".format(session.get("username"))
136 if session.get("project_id"):
137 user_text += ", project: {}".format(session.get("project_id"))
138 return html_start.format(user_text) + body + html_end
139 #yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False)
140 # tags=False,
141 # encoding='utf-8', allow_unicode=True)
142
143