Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix active agencies #5

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions health_ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

def update_sc_component(c_id, data):
try:
log.debug(data)
log.debug(f"Data to POST to strapi {data}")
x = requests.put(f"{sc_api_endpoint}/v1/components/{c_id}", headers=sc_api_headers, json = {"data": data}, timeout=10)
if x.status_code == 200:
log.info(f"Successfully updated component id {c_id}: {x.status_code}")
Expand Down Expand Up @@ -107,24 +107,33 @@ def process_env(c_name, e_name, endpoint, endpoint_type, component):

# Try to get active agencies
try:
active_agencies = output['activeAgencies']
# Need to get other env IDs for strapi update
other_env_ids = []
for e in component["attributes"]["environments"]:
if e_name == e["name"]:
e_id = e["id"]
# Existing active_agencies from the SC
e_active_agencies = e["active_agencies"]
else:
# Collect other env vars for strapi update.
other_env_ids.append({"id": e["id"]})
if ('activeAgencies' in output) and (endpoint_type == 'info'):
active_agencies = output['activeAgencies']

# Test if active_agencies has changed, only update SC if so.
if sorted(active_agencies) != sorted(e_active_agencies):
# Need to add all other env IDs to the data payload otherwise strapi will delete them.
env_data = other_env_ids.append({"id": e_id, "active_agencies": active_agencies })
data = {"environments": env_data}
update_sc_component(c_id, data)
env_data = []
update_sc = False
for e in component["attributes"]["environments"]:
# Work on current environment
if e_name == e["name"]:
# Existing active_agencies from the SC
print(f"SC active_agencies: {e['active_agencies']}")
print(f"Existing active_agencies: {active_agencies}")

# if current active_agencies is empty/None set to empty list to enable comparison.
if e["active_agencies"] is None:
e["active_agencies"] = []
# Test if active_agencies has changed, and update SC if so.
if sorted(active_agencies) != sorted(e["active_agencies"]):
env_data.append({"id": e["id"], "active_agencies": active_agencies })
update_sc = True
else:
# Add rest of envs for strapi update.
env_data.append({"id": e["id"]})

if update_sc:
data = {"environments": env_data}
update_sc_component(c_id, data)
except (KeyError, TypeError):
pass
except Exception as e:
Expand Down