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