Skip to content

Commit

Permalink
might need to debug
Browse files Browse the repository at this point in the history
  • Loading branch information
kreynoldsf5 committed May 2, 2024
1 parent 58b8d1f commit 01740fd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 22 deletions.
37 changes: 30 additions & 7 deletions labapp/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import requests
import markdown
from ce import get_ce_info, get_ce_state
from fetch import cloudapp_fetch
from fetch import cloudapp_fetch, cloudapp_req_headers, cloudapp_res_headers

app = Flask(__name__)
app.config['ce_info'] = None
Expand Down Expand Up @@ -295,7 +295,7 @@ def route2():
return jsonify(status='success', data=data)
except (LabException, requests.RequestException, ValueError) as e:
return jsonify(status='fail', error=str(e))

@app.route('/_manip1')
def manip1():
"""First Manip Test"""
Expand All @@ -313,8 +313,8 @@ def manip1():
return jsonify(status='fail', error=str(e))

@app.route('/_manip2')
def manip2():
"""First Manip Test"""
def manip1():
"""Second Manip Test"""
try:
s = requests.Session()
s.headers.update({"User-Agent": "MCN-Lab-Runner/1.0"})
Expand All @@ -323,12 +323,35 @@ def manip2():
raise LabException("Ephemeral NS not set")
base_url = app.config['base_url']
url = f"https://{ns}.{base_url}/"


r_data = cloudapp_fetch(s, url, 5, 'info', {"method": "GET", "path": "/raw"})
t_headers = {"x-mcn-namespace": ns,"x-mcn-src-site": app.config["ce_info"]["site_name"]}
r_data = cloudapp_req_headers(s, url, 7, t_headers)
return jsonify(status='success', data=r_data)
except (LabException, requests.RequestException, ValueError) as e:
return jsonify(status='fail', error=str(e))

@app.route('/_manip3')
def manip2():
"""Third Manip Test"""
try:
s = requests.Session()
s.headers.update({"User-Agent": "MCN-Lab-Runner/1.0"})
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"
azure_url = f"https://{ns}.{base_url}/aws"
aws_headers = {"x-mcn-dest-site": "student-awsnet"}
azure_headers = {"x-mcn-dest-site": "student-azurenet"}
aws_data = cloudapp_res_headers(s, aws_url, 7, aws_headers)
azure_data = cloudapp_res_headers(s, azure_url, 7, azure_headers)
data = {
"aws": aws_data,
"azure": azure_data
}
return jsonify(status='success', data=data)
except (LabException, requests.RequestException, ValueError) as e:
return jsonify(status='fail', error=str(e))


if __name__ == '__main__':
Expand Down
34 changes: 20 additions & 14 deletions labapp/app/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,31 @@ def cloudapp_fetch(session, url, timeout, prop, value):
return data
return data

def cloudapp_fetch_new(session, url, timeout, prop, key, value):
def cloudapp_req_headers(session, url, timeout, headers):
"""
Fetch data from URL
Validate if a specific key-value pair is present in the dictionary located at `prop` in the JSON response
"""
response = session.get(url, timeout=timeout)
response.raise_for_status()

print(response.text)
data = response.json()

print(data)
for header in headers:
if header.lower() not in data['request_headers']:
raise ValueError(f"Header {header} not found request headers.")
clean_headers = headers_cleaner(data['request_headers'])
data['request_headers'] = clean_headers
return data

prop_data = data.get(prop, {})
if not isinstance(prop_data, dict) or prop_data.get(key) != value:
raise ValueError(f"Expected {key}: {value} in {prop}, but got {dict}")

if data.get("request_headers"):
clean_headers = headers_cleaner(data['request_headers'])
data['request_headers'] = clean_headers

return data
def cloudapp_res_headers(session, url, timeout, headers):
"""
Fetch data from URL
Check for response header
"""
response = session.get(url, timeout=timeout)
response.raise_for_status()
data = response.headers
for header in headers:
head_value = data.get(header.key(), None)
if not head_value:
raise ValueError(f"Header {header} not found request headers.")
return data
2 changes: 1 addition & 1 deletion labapp/app/markdown/manipulation.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Host: eph-ns.mcn-lab.f5demos.com
...
"request_headers": {
"x-mcn-namespace": "wiggly-yellowtail",
"X-mcn-src-Site": "cluster-xxxxxxxx",
"x-mcn-src-site": "cluster-xxxxxxxx",
},
...
}
Expand Down

0 comments on commit 01740fd

Please sign in to comment.