Skip to content

Commit 63189f3

Browse files
authored
Merge pull request #652 from mapswipe/dev
Dev - script to update tms api key
2 parents 6ecbc22 + 4864016 commit 63189f3

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,6 @@ update_firebase_functions_and_db_rules:
2525

2626
deploy_latest_workers_version:
2727
git pull; docker compose build postgres django django-schedule-task manager_dashboard community_dashboard nginx api mapswipe_workers_creation mapswipe_workers_stats mapswipe_workers_firebase_to_postgres firebase_deploy; docker compose up -d postgres django django-schedule-task manager_dashboard community_dashboard nginx api mapswipe_workers_creation mapswipe_workers_stats mapswipe_workers_firebase_to_postgres firebase_deploy
28+
29+
update_tms_api_key:
30+
@echo "first update the .env file with the new api key and then run this script with tms_name=your_tms_provider_name to update API key"; docker-compose run --rm mapswipe_workers_creation python python_scripts/change_tms_api_key_for_projects.py $(tms_name)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import sys
2+
3+
from mapswipe_workers.auth import firebaseDB
4+
from mapswipe_workers.config import IMAGE_API_KEYS
5+
from mapswipe_workers.definitions import ProjectType, logger
6+
7+
8+
def get_all_projects_by_tms(tms_name: str):
9+
"""Get the project ids for active and inactive projects in Firebase DB."""
10+
11+
project_id_list = []
12+
fb_db = firebaseDB()
13+
14+
# we neglect private projects here
15+
# since there are no projects set up in production yet
16+
status_list = ["active", "inactive"]
17+
18+
for status in status_list:
19+
logger.info(f"query {status} projects")
20+
projects = (
21+
fb_db.reference("v2/projects/")
22+
.order_by_child("status")
23+
.equal_to(status)
24+
.get()
25+
)
26+
for project_id, data in projects.items():
27+
project_type = data.get("projectType", None)
28+
29+
# build area, footprint and completeness project types
30+
# use a single tile server for satellite imagery
31+
if (
32+
project_type
33+
in [
34+
ProjectType.BUILD_AREA.value,
35+
ProjectType.FOOTPRINT.value,
36+
ProjectType.COMPLETENESS.value,
37+
]
38+
and data["tileServer"]["name"] == tms_name
39+
):
40+
project_id_list.append(project_id)
41+
42+
logger.info(f"got {len(project_id_list)} project from firebase.")
43+
logger.info(f"projects: {project_id_list}")
44+
return project_id_list
45+
46+
47+
def add_api_key_to_projects(project_id_list, api_key):
48+
fb_db = firebaseDB()
49+
if len(project_id_list) < 1:
50+
logger.info("there are no matching projects.")
51+
else:
52+
for i, project_id in enumerate(project_id_list):
53+
fb_db.reference(f"v2/projects/{project_id}/tileServer/apiKey").set(api_key)
54+
logger.info(f"#{i+1} added api key '{api_key}' to project '{project_id}'")
55+
56+
57+
if __name__ == "__main__":
58+
"""change_tms_api_key_for_projects.py maxar_premium"""
59+
logger.info(
60+
"This script currently supports only the following project types:"
61+
"build_area, footprint, completeness. "
62+
"The new api key will be obtained from the .env file. "
63+
"Make sure to update this file first."
64+
)
65+
66+
try:
67+
tms_name = sys.argv[1]
68+
if tms_name in IMAGE_API_KEYS.keys():
69+
new_api_key = IMAGE_API_KEYS[tms_name]
70+
project_id_list = get_all_projects_by_tms(tms_name)
71+
add_api_key_to_projects(project_id_list, new_api_key)
72+
else:
73+
logger.info(
74+
f"'{tms_name}' is not a valid TMS provider name. "
75+
f"Valid options are: {list(IMAGE_API_KEYS.keys())}"
76+
)
77+
except IndexError:
78+
logger.info(
79+
"Provide a valid TMS provider name as an argument to this script. "
80+
f"Valid options are: {list(IMAGE_API_KEYS.keys())}"
81+
)

0 commit comments

Comments
 (0)