Skip to content

Commit

Permalink
Move files, update cloudbuild
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbryer committed Jan 16, 2025
1 parent 42f87f6 commit ac84bcc
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 6 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
steps:
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/$PROJECT_ID/zip', '.', '-f', 'schema-upload/Dockerfile-zip']
args: ['build', '-t', 'gcr.io/$PROJECT_ID/zip', '.', '-f', 'schema-publish/Dockerfile-zip']

- name: 'gcr.io/$PROJECT_ID/zip'
args: ['-r', '/workspace/function.zip', '.']
dir: 'schema-upload/src'
dir: 'schema-publish/src'

- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
entrypoint: 'bash'
Expand All @@ -29,7 +29,8 @@ steps:
- 'europe-west2'
- '--entry-point'
- '${_ENTRY_POINT}'
- '--trigger-http'
- '--trigger-topic'
- '${_SCHEMA_PUBLISH_PUBSUB_TOPIC}'
- '--source'
- 'gs://$PROJECT_ID-europe-west2-cloudfunctions/${_FUNCTION_NAME}/${_FUNCTION_NAME}-source.zip'

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
39 changes: 37 additions & 2 deletions schema-upload/src/main.py → publish-schema/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
logger = logging.getLogger(__name__)

@functions_framework.cloud_event
def subscribe(cloud_event: CloudEvent) -> None:
def publish_schema(cloud_event: CloudEvent) -> None:
"""
Method to retrieve, verify, and publish a schema to SDS.
Expand Down Expand Up @@ -139,4 +139,39 @@ def verify_version(filepath, schema) -> bool:
return True
else:
logger.error(f"Schema version for {filepath} does not match. Expected {filename}, got {schema['properties']['schema_version']['const']}")
return False
return False

def check_duplicate_versions(schema, survey_id) -> bool:
"""
Method to call the schema_metadata endpoint and check that the schema_version for the new schema is not already present in SDS.
Parameters:
schema (dict): the schema to be posted.
survey_id (str): the survey ID.
Returns:
bool: True if there are no duplicate versions, False otherwise.
"""
logger.info(f"Checking for duplicate schema versions for survey {survey_id}")

session = setup_session()
headers = generate_headers()
response = session.get(
f"{Config.API_URL}/v1/schema_metadata?survey_id={survey_id}",
headers=headers,
)
if response.status_code != 200:
logger.error(f"Failed to fetch schema metadata for survey {survey_id}. Cannot verify schema version. Exiting.")
logger.error(response.text)
return False

metadata = response.json()
new_schema_version = schema["properties"]["schema_version"]["const"]

for version in metadata:
if new_schema_version == version["schema_version"]:
logger.error(f"Schema version {new_schema_version} already exists for survey {survey_id}")
return False
logger.info(f"Verified schema_version {new_schema_version} for survey {survey_id} is unique.")
return True

File renamed without changes.
1 change: 0 additions & 1 deletion schema-upload/requirements.txt

This file was deleted.

0 comments on commit ac84bcc

Please sign in to comment.