diff --git a/labapp/app/app.py b/labapp/app/app.py index 41a77f7..d3dadf3 100644 --- a/labapp/app/app.py +++ b/labapp/app/app.py @@ -43,12 +43,12 @@ def eph_ns() -> str: this_eph_ns = request.cookies.get('eph_ns', None) return this_eph_ns -def cloudapp_fetch(url, timeout, prop, value, headers = {}): +def cloudapp_fetch(session, url, timeout, prop, value, headers = {}): """ Fetch data from URL Validate prop and value in the JSON response """ - response = requests.get(url, timeout=timeout, headers=headers) + response = session.get(url, timeout=timeout) response.raise_for_status() data = response.json() if data.get(prop) != value: @@ -101,6 +101,7 @@ def arch(): @app.route('/setup', methods=['GET', 'POST']) def setup(): """setup page""" + ns = eph_ns() if request.method == 'POST': action = request.form['action'] if action == 'save': @@ -110,7 +111,7 @@ def setup(): return redirect(url_for('setup')) response = make_response(redirect('/setup')) response.set_cookie('eph_ns', this_eph_ns, max_age=60*60*24) - flash("Ephemeral NS successfully set.", "success") + flash('Ephemeral NS successfully set.', "success") return response if action == 'clear': response = make_response(redirect('/setup')) @@ -120,7 +121,8 @@ def setup(): html = render_md("markdown/setup.md") return render_template('setup.html', title="MCN Practical: Setup", - content=html + content=html, + ns=ns ) @app.route('/_ce_status') @@ -201,8 +203,9 @@ def score(): def ex_test(): """Example test""" try: + s = requests.Session() url = f"https://foo.{app.config['base_url']}/" - data = cloudapp_fetch(url, 5, 'info', 'bar') + data = cloudapp_fetch(s, url, 5, 'info', 'bar') return jsonify(status='success', data=data) except (LabException, requests.RequestException, ValueError) as e: return jsonify(status='fail', error=str(e)) @@ -211,8 +214,9 @@ def ex_test(): def ex_test2(): """Example test""" try: + s = requests.Session() url = f"https://bar.{app.config['base_url']}/" - data = cloudapp_fetch(url, 5, 'info', 'foo') + data = cloudapp_fetch(s, url, 5, 'info', 'foo') return jsonify(status='success', data=data) except (LabException, requests.RequestException, ValueError) as e: return jsonify(status='fail', error=str(e)) @@ -222,11 +226,12 @@ def ex_test2(): def lb_aws(): """Azure LB test""" try: + s = requests.Session() ns = eph_ns() if not ns: raise LabException("Ephemeral NS not set") url = f"https://{ns}.{app.config['base_url']}" - data = cloudapp_fetch(url, 5, 'env', 'AWS') + data = cloudapp_fetch(s, url, 5, 'env', 'AWS') return jsonify(status='success', data=data) except (LabException, requests.RequestException, ValueError) as e: return jsonify(status='fail', error=str(e)) @@ -235,11 +240,12 @@ def lb_aws(): def lb_azure(): """Azure LB test""" try: + s = requests.Session() ns = eph_ns() if not ns: raise LabException("Ephemeral NS not set") url = f"https://{ns}.{app.config['base_url']}" - data = cloudapp_fetch(url, 5, 'env', 'Azure') + data = cloudapp_fetch(s, url, 5, 'env', 'Azure') return jsonify(status='success', data=data) except (LabException, requests.RequestException, ValueError) as e: return jsonify(status='fail', error=str(e)) @@ -248,14 +254,15 @@ def lb_azure(): def route1(): """First Route Test""" try: + s = requests.Session() ns = eph_ns() if not ns: raise LabException("Ephemeral NS not set") base_url = app.config['base_url'] aws_url = f"https://{ns}.{base_url}/aws/raw" azure_url = f"https://{ns}.{base_url}/azure/raw" - aws_data = cloudapp_fetch(aws_url, 5, 'env', 'AWS') - azure_data = cloudapp_fetch(azure_url, 5, 'env', 'Azure') + aws_data = cloudapp_fetch(s, aws_url, 5, 'env', 'AWS') + azure_data = cloudapp_fetch(s, azure_url, 5, 'env', 'Azure') data = { "aws": aws_data, "azure": azure_data @@ -268,14 +275,17 @@ def route1(): def route2(): """First Route Test""" try: + s = requests.Session() ns = eph_ns() if not ns: raise LabException("Ephemeral NS not set") base_url = app.config['base_url'] aws_url = f"https://{ns}.{base_url}/" azure_url = f"https://{ns}.{base_url}/" - aws_data = cloudapp_fetch(aws_url, 5, 'env', 'AWS', headers={"X-MCN-lab": "aws"}) - azure_data = cloudapp_fetch(azure_url, 5, 'env', 'Azure', headers={"X-MCN-lab": "azure"}) + s.headers["X-MCN-lab"] = "aws" + aws_data = cloudapp_fetch(s, aws_url, 5, 'env', 'AWS') + s.headers["X-MCN-lab"] = "azure" + azure_data = cloudapp_fetch(s, azure_url, 5, 'env', 'Azure', headers={"X-MCN-lab": "azure"}) data = { "aws": aws_data, "azure": azure_data @@ -288,12 +298,13 @@ def route2(): def manip1(): """First Manip Test""" try: + s = requests.Session() ns = eph_ns() if not ns: raise LabException("Ephemeral NS not set") base_url = app.config['base_url'] url = f"https://{ns}.{base_url}/" - r_data = cloudapp_fetch(url, 5, 'info[path]', '/') + r_data = cloudapp_fetch(s, url, 5, 'info[path]', '/') return jsonify(status='success', data=r_data) except (LabException, requests.RequestException, ValueError) as e: return jsonify(status='fail', error=str(e)) diff --git a/labapp/app/markdown/lb.md b/labapp/app/markdown/lb.md index 01f9d3f..1d0eb3c 100644 --- a/labapp/app/markdown/lb.md +++ b/labapp/app/markdown/lb.md @@ -20,19 +20,19 @@ Build an origin pool and load balancer based on the exercise requirements.