2 Contains html text in variables to make and html response
6 from http
import HTTPStatus
8 __author__
= "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
14 <link href="/osm/static/style.css" rel="stylesheet">
15 <title>Welcome to OSM</title>
18 <div id="osm_topmenu">
20 <a href="https://osm.etsi.org"> <img src="/osm/static/OSM-logo.png" height="42" width="100"
21 style="vertical-align:middle"> </a>
23 <a href="/osm/vnfpkgm/v1/vnf_packages_content">VNFDs </a>
24 <a href="/osm/nsd/v1/ns_descriptors_content">NSDs </a>
25 <a href="/osm/nslcm/v1/ns_instances_content">NSs </a>
26 <a href="/osm/admin/v1/users">USERs </a>
27 <a href="/osm/admin/v1/projects">PROJECTs </a>
28 <a href="/osm/admin/v1/tokens">TOKENs </a>
29 <a href="/osm/admin/v1/vims">VIMs </a>
30 <a href="/osm/admin/v1/tokens?METHOD=DELETE">logout </a>
44 html_body_error
= "<h2> Error <pre>{}</pre> </h2>"
48 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
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>
57 <a href="https://osm.etsi.org"> <h1><img src="/osm/static/OSM-logo.png" style="vertical-align:middle"></h1> </a>
60 <div id="osm_error_message">
63 <div class="gerritBody" id="osm_body">
64 <h1>Sign in to OSM</h1>
65 <form action="/osm/admin/v1/tokens" id="login_form" method="POST">
66 <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>
72 <div style="clear: both; margin-top: 15px; padding-top: 2px; margin-bottom: 15px;">
78 <script src="/osm/static/login.js"> </script>
83 html_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>
92 html_nslcmop_body
= """
93 <a href="/osm/nslcm/v1/ns_lcm_op_occs?nsInstanceId={id}">nslcm operations </a>
94 <a href="/osm/nslcm/v1/vnfrs?nsr-id-ref={id}">VNFRS </a>
95 <form action="/osm/nslcm/v1/ns_instances/{id}/terminate" method="post" enctype="multipart/form-data">
96 <h3> <table style="border: 0;"> <tr>
97 <td> <input type="submit" value="Terminate"/> </td>
103 def format(data
, request
, response
, session
):
105 Format a nice html response, depending on the data
107 :param request: cherrypy request
108 :param response: cherrypy response
109 :return: string with teh html response
111 response
.headers
["Content-Type"] = 'text/html'
112 if response
.status
== HTTPStatus
.UNAUTHORIZED
.value
:
113 if response
.headers
.get("WWW-Authenticate") and request
.config
.get("auth.allow_basic_authentication"):
114 response
.headers
["WWW-Authenticate"] = "Basic" + response
.headers
["WWW-Authenticate"][6:]
117 return html_auth2
.format(error
=data
)
118 body
= html_body
.format(item
=request
.path_info
)
119 if response
.status
and response
.status
> 202:
120 body
+= html_body_error
.format(yaml
.safe_dump(data
, explicit_start
=True, indent
=4, default_flow_style
=False))
121 elif isinstance(data
, (list, tuple)):
122 if request
.path_info
== "/vnfpkgm/v1/vnf_packages_content":
123 body
+= html_upload_body
.format(request
.path_info
, "VNFD")
124 elif request
.path_info
== "/nsd/v1/ns_descriptors_content":
125 body
+= html_upload_body
.format(request
.path_info
, "NSD")
127 if isinstance(k
, dict):
128 data_id
= k
.pop("_id", None)
129 elif isinstance(k
, str):
131 body
+= '<p> <a href="/osm/{url}/{id}">{id}</a>: {t} </p>'.format(url
=request
.path_info
, id=data_id
, t
=k
)
132 elif isinstance(data
, dict):
133 if "Location" in response
.headers
:
134 body
+= '<a href="{}"> show </a>'.format(response
.headers
["Location"])
136 body
+= '<a href="/osm/{}?METHOD=DELETE"> <img src="/osm/static/delete.png" height="25" width="25"> </a>'\
137 .format(request
.path_info
)
138 if request
.path_info
.startswith("/nslcm/v1/ns_instances_content/") or \
139 request
.path_info
.startswith("/nslcm/v1/ns_instances/"):
140 _id
= request
.path_info
[request
.path_info
.rfind("/")+1:]
141 body
+= html_nslcmop_body
.format(id=_id
)
142 body
+= "<pre>" + yaml
.safe_dump(data
, explicit_start
=True, indent
=4, default_flow_style
=False) + "</pre>"
144 if request
.method
== "DELETE" or "METHOD=DELETE" in request
.query_string
:
145 body
+= "<pre> deleted </pre>"
150 if session
.get("username"):
151 user_text
+= "user: {}".format(session
.get("username"))
152 if session
.get("project_id"):
153 user_text
+= ", project: {}".format(session
.get("project_id"))
154 return html_start
.format(user_text
) + body
+ html_end
155 # yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False)
157 # encoding='utf-8', allow_unicode=True)