| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1 | """ |
| 2 | Contains html text in variables to make and html response |
| 3 | """ |
| 4 | |
| 5 | import yaml |
| 6 | from http import HTTPStatus |
| tierno | ef4e224 | 2018-10-31 17:09:52 +0100 | [diff] [blame^] | 7 | from html import escape as html_escape |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 8 | |
| 9 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| 10 | |
| 11 | html_start = """ |
| 12 | <!DOCTYPE html> |
| 13 | <html> |
| 14 | <head> |
| 15 | <link href="/osm/static/style.css" rel="stylesheet"> |
| 16 | <title>Welcome to OSM</title> |
| 17 | </head> |
| 18 | <body> |
| 19 | <div id="osm_topmenu"> |
| 20 | <div> |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 21 | <a href="https://osm.etsi.org"> <img src="/osm/static/OSM-logo.png" height="42" width="100" |
| 22 | style="vertical-align:middle"> </a> |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 23 | <a>( {} )</a> |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 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> |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 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> |
| tierno | f759d82 | 2018-06-11 18:54:54 +0200 | [diff] [blame] | 30 | <a href="/osm/admin/v1/vim_accounts">VIMs </a> |
| 31 | <a href="/osm/admin/v1/sdns">SDNs </a> |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 32 | <a href="/osm/admin/v1/tokens?METHOD=DELETE">logout </a> |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 33 | </div> |
| 34 | </div> |
| 35 | """ |
| 36 | |
| 37 | html_body = """ |
| 38 | <h1>{item}</h1> |
| 39 | """ |
| 40 | |
| 41 | html_end = """ |
| 42 | </body> |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 43 | </html> |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 44 | """ |
| 45 | |
| 46 | html_body_error = "<h2> Error <pre>{}</pre> </h2>" |
| 47 | |
| 48 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 49 | html_auth2 = """ |
| 50 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> |
| 51 | <html> |
| 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> |
| 55 | </head> |
| 56 | <body> |
| 57 | <div id="osm_header"> |
| 58 | <div> |
| 59 | <a href="https://osm.etsi.org"> <h1><img src="/osm/static/OSM-logo.png" style="vertical-align:middle"></h1> </a> |
| 60 | </div> |
| 61 | </div> |
| 62 | <div id="osm_error_message"> |
| 63 | <h1>{error}</h1> |
| 64 | </div> |
| 65 | <div class="gerritBody" id="osm_body"> |
| 66 | <h1>Sign in to OSM</h1> |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 67 | <form action="/osm/admin/v1/tokens" id="login_form" method="POST"> |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 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> |
| 72 | </table> |
| 73 | </form> |
| 74 | <div style="clear: both; margin-top: 15px; padding-top: 2px; margin-bottom: 15px;"> |
| 75 | <div id="osm_footer"> |
| 76 | <div></div> |
| 77 | </div> |
| 78 | </div> |
| 79 | </div> |
| 80 | <script src="/osm/static/login.js"> </script> |
| 81 | </body> |
| 82 | </html> |
| 83 | """ |
| 84 | |
| 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> |
| 90 | </tr> </table> </h3> |
| 91 | </form> |
| 92 | """ |
| 93 | |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 94 | html_nslcmop_body = """ |
| 95 | <a href="/osm/nslcm/v1/ns_lcm_op_occs?nsInstanceId={id}">nslcm operations </a> |
| tierno | f759d82 | 2018-06-11 18:54:54 +0200 | [diff] [blame] | 96 | <a href="/osm/nslcm/v1/vnf_instances?nsr-id-ref={id}">VNFRS </a> |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 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> |
| 100 | </tr> </table> </h3> |
| 101 | </form> |
| 102 | """ |
| 103 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 104 | |
| 105 | def format(data, request, response, session): |
| 106 | """ |
| 107 | Format a nice html response, depending on the data |
| 108 | :param data: |
| 109 | :param request: cherrypy request |
| 110 | :param response: cherrypy response |
| 111 | :return: string with teh html response |
| 112 | """ |
| 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:] |
| 117 | return |
| 118 | else: |
| 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)): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 124 | if request.path_info == "/vnfpkgm/v1/vnf_packages": |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 125 | body += html_upload_body.format(request.path_info, "VNFD") |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 126 | elif request.path_info == "/nsd/v1/ns_descriptors": |
| 127 | body += html_upload_body.format(request.path_info + "_content", "NSD") |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 128 | for k in data: |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 129 | if isinstance(k, dict): |
| 130 | data_id = k.pop("_id", None) |
| 131 | elif isinstance(k, str): |
| 132 | data_id = k |
| tierno | ef4e224 | 2018-10-31 17:09:52 +0100 | [diff] [blame^] | 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))) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 135 | elif isinstance(data, dict): |
| 136 | if "Location" in response.headers: |
| 137 | body += '<a href="{}"> show </a>'.format(response.headers["Location"]) |
| 138 | else: |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 139 | body += '<a href="/osm/{}?METHOD=DELETE"> <img src="/osm/static/delete.png" height="25" width="25"> </a>'\ |
| 140 | .format(request.path_info) |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 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) |
| tierno | ef4e224 | 2018-10-31 17:09:52 +0100 | [diff] [blame^] | 145 | body += "<pre>" + html_escape(yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False)) + \ |
| 146 | "</pre>" |
| tierno | 0f98af5 | 2018-03-19 10:28:22 +0100 | [diff] [blame] | 147 | elif data is None: |
| 148 | if request.method == "DELETE" or "METHOD=DELETE" in request.query_string: |
| 149 | body += "<pre> deleted </pre>" |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 150 | else: |
| tierno | ef4e224 | 2018-10-31 17:09:52 +0100 | [diff] [blame^] | 151 | body = html_escape(str(data)) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 152 | user_text = " " |
| 153 | if session: |
| 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 |
| tierno | 2236d20 | 2018-05-16 19:05:16 +0200 | [diff] [blame] | 159 | # yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 160 | # tags=False, |
| 161 | # encoding='utf-8', allow_unicode=True) |