2 Contains html text in variables to make and html response
6 from http
import HTTPStatus
7 from html
import escape
as html_escape
9 __author__
= "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
15 <link href="/osm/static/style.css" rel="stylesheet">
16 <title>Welcome to OSM</title>
19 <div id="osm_topmenu">
21 <a href="https://osm.etsi.org"> <img src="/osm/static/OSM-logo.png" height="42" width="100"
22 style="vertical-align:middle"> </a>
24 <a href="/osm/vnfpkgm/v1/vnf_packages">VNFDs </a>
25 <a href="/osm/nsd/v1/ns_descriptors">NSDs </a>
26 <a href="/osm/nslcm/v1/ns_instances">NSs </a>
27 <a href="/osm/admin/v1/users">USERs </a>
28 <a href="/osm/admin/v1/projects">PROJECTs </a>
29 <a href="/osm/admin/v1/tokens">TOKENs </a>
30 <a href="/osm/admin/v1/vim_accounts">VIMs </a>
31 <a href="/osm/admin/v1/sdns">SDNs </a>
32 <a href="/osm/admin/v1/tokens?METHOD=DELETE">logout </a>
46 html_body_error
= "<h2> Error <pre>{}</pre> </h2>"
50 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
52 <head><META http-equiv="Content-Type" content="text/html; charset=UTF-8">
53 <link href="/osm/static/style.css" rel="stylesheet">
54 <title>OSM Login</title>
59 <a href="https://osm.etsi.org"> <h1><img src="/osm/static/OSM-logo.png" style="vertical-align:middle"></h1> </a>
62 <div id="osm_error_message">
65 <div class="gerritBody" id="osm_body">
66 <h1>Sign in to OSM</h1>
67 <form action="/osm/admin/v1/tokens" id="login_form" method="POST">
68 <table style="border: 0;">
69 <tr><th>Username</th><td><input id="f_user" name="username" size="25" tabindex="1" type="text"></td></tr>
70 <tr><th>Password</th><td><input id="f_pass" name="password" size="25" tabindex="2" type="password"></td></tr>
71 <tr><td><input tabindex="3" type="submit" value="Sign In"></td></tr>
74 <div style="clear: both; margin-top: 15px; padding-top: 2px; margin-bottom: 15px;">
80 <script src="/osm/static/login.js"> </script>
85 html_upload_body
= """
86 <form action="/osm{}" method="post" enctype="multipart/form-data">
87 <h3> <table style="border: 0;"> <tr>
88 <td> Upload {} descriptor (tar.gz) file: <input type="file" name="descriptor_file"/> </td>
89 <td> <input type="submit" value="Upload"/> </td>
94 html_nslcmop_body
= """
95 <a href="/osm/nslcm/v1/ns_lcm_op_occs?nsInstanceId={id}">nslcm operations </a>
96 <a href="/osm/nslcm/v1/vnf_instances?nsr-id-ref={id}">VNFRS </a>
97 <form action="/osm/nslcm/v1/ns_instances/{id}/terminate" method="post" enctype="multipart/form-data">
98 <h3> <table style="border: 0;"> <tr>
99 <td> <input type="submit" value="Terminate"/> </td>
105 def format(data
, request
, response
, session
):
107 Format a nice html response, depending on the data
109 :param request: cherrypy request
110 :param response: cherrypy response
111 :return: string with teh html response
113 response
.headers
["Content-Type"] = 'text/html'
114 if response
.status
== HTTPStatus
.UNAUTHORIZED
.value
:
115 if response
.headers
.get("WWW-Authenticate") and request
.config
.get("auth.allow_basic_authentication"):
116 response
.headers
["WWW-Authenticate"] = "Basic" + response
.headers
["WWW-Authenticate"][6:]
119 return html_auth2
.format(error
=data
)
120 body
= html_body
.format(item
=request
.path_info
)
121 if response
.status
and response
.status
> 202:
122 body
+= html_body_error
.format(yaml
.safe_dump(data
, explicit_start
=True, indent
=4, default_flow_style
=False))
123 elif isinstance(data
, (list, tuple)):
124 if request
.path_info
== "/vnfpkgm/v1/vnf_packages":
125 body
+= html_upload_body
.format(request
.path_info
, "VNFD")
126 elif request
.path_info
== "/nsd/v1/ns_descriptors":
127 body
+= html_upload_body
.format(request
.path_info
+ "_content", "NSD")
129 if isinstance(k
, dict):
130 data_id
= k
.pop("_id", None)
131 elif isinstance(k
, str):
133 body
+= '<p> <a href="/osm/{url}/{id}">{id}</a>: {t} </p>'.format(url
=request
.path_info
, id=data_id
,
134 t
=html_escape(str(k
)))
135 elif isinstance(data
, dict):
136 if "Location" in response
.headers
:
137 body
+= '<a href="{}"> show </a>'.format(response
.headers
["Location"])
139 body
+= '<a href="/osm/{}?METHOD=DELETE"> <img src="/osm/static/delete.png" height="25" width="25"> </a>'\
140 .format(request
.path_info
)
141 if request
.path_info
.startswith("/nslcm/v1/ns_instances_content/") or \
142 request
.path_info
.startswith("/nslcm/v1/ns_instances/"):
143 _id
= request
.path_info
[request
.path_info
.rfind("/")+1:]
144 body
+= html_nslcmop_body
.format(id=_id
)
145 body
+= "<pre>" + html_escape(yaml
.safe_dump(data
, explicit_start
=True, indent
=4, default_flow_style
=False)) + \
148 if request
.method
== "DELETE" or "METHOD=DELETE" in request
.query_string
:
149 body
+= "<pre> deleted </pre>"
151 body
= html_escape(str(data
))
154 if session
.get("username"):
155 user_text
+= "user: {}".format(session
.get("username"))
156 if session
.get("project_id"):
157 user_text
+= ", project: {}".format(session
.get("project_id"))
158 return html_start
.format(user_text
) + body
+ html_end
159 # yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False)
161 # encoding='utf-8', allow_unicode=True)