99
1010from mdps_ds_lib .lib .aws .aws_message_transformers import AwsMessageTransformers
1111from mdps_ds_lib .lib .utils .json_validator import JsonValidator
12+ from mdps_ds_lib .stac_fast_api_client .sfa_client_factory import SFAClientFactory
1213
1314from cumulus_lambda_functions .lib .uds_db .granules_db_index import GranulesDbIndex
1415from mdps_ds_lib .lib .aws .aws_sns import AwsSns
@@ -147,25 +148,46 @@ def send_to_daac(self, event: dict):
147148 self .send_to_daac_internal (uds_cnm_json )
148149 return
149150
150- def receive_from_daac (self , event : dict ):
151- LOGGER .debug (f'receive_from_daac#event: { event } ' )
152- sns_msg = AwsMessageTransformers ().sqs_sns (event )
153- LOGGER .debug (f'sns_msg: { sns_msg } ' )
154- cnm_notification_msg = sns_msg
151+ def update_stac (self , cnm_notification_msg ):
152+ update_type = os .getenv ('ARCHIVAL_STATUS_MECHANISM' , '' )
153+ if not any ([k for k in ['UDS' , 'FAST_STAC' ] if k == update_type ]):
154+ raise ValueError (f"missing ARCHIVAL_STATUS_MECHANISM environment variable or value is not { ['UDS' , 'FAST_STAC' ]} " )
155+ if update_type == 'UDS' :
156+ return self .update_stac_uds (cnm_notification_msg )
157+ return self .update_stac_fast_api (cnm_notification_msg )
155158
156- cnm_msg_schema = requests .get ('https://raw.githubusercontent.com/podaac/cloud-notification-message-schema/v1.6.1/cumulus_sns_schema.json' )
157- cnm_msg_schema .raise_for_status ()
158- cnm_msg_schema = json .loads (cnm_msg_schema .text )
159- result = JsonValidator (cnm_msg_schema ).validate (cnm_notification_msg )
160- if result is not None :
161- raise ValueError (f'input cnm event has cnm_msg_schema validation errors: { result } ' )
162- if 'response' not in cnm_notification_msg :
163- raise ValueError (f'missing response in { cnm_notification_msg } ' )
159+ def update_stac_fast_api (self , cnm_notification_msg ):
160+ sfa_client = SFAClientFactory ().get_instance_from_env ()
161+ collection_id , granule_id = ':' .join (cnm_notification_msg ['identifier' ].split (':' )[:- 1 ]), cnm_notification_msg ['identifier' ]
162+ # TODO assuming granule ID is URN:NASA:VENUE:TENANT:VENUE:COLLECTION_ID:COLLECTION_ID
163+ existing_item = sfa_client .get_item (collection_id , granule_id )
164+ # TODO handle error when no existing_item. Currently, it is requests.HTTPError with 404
165+ if cnm_notification_msg ['response' ]['status' ] == 'SUCCESS' :
166+ latest_daac_status = {
167+ 'archive_status' : 'cnm_r_success' ,
168+ 'archive_error_message' : '' ,
169+ 'archive_error_code' : '' ,
170+ }
171+ else :
172+ latest_daac_status = {
173+ 'archive_status' : 'cnm_r_failed' ,
174+ 'archive_error_message' : cnm_notification_msg ['response' ]['errorMessage' ] if 'errorMessage' in cnm_notification_msg ['response' ] else 'unknown' ,
175+ 'archive_error_code' : cnm_notification_msg ['response' ]['errorCode' ] if 'errorCode' in cnm_notification_msg ['response' ] else 'unknown' ,
176+ }
177+ latest_daac_status ['event_time' ] = TimeUtils .get_current_time ()
178+ existing_item ['properties' ]['archival_statuses' ] = existing_item ['properties' ]['archival_statuses' ] + [latest_daac_status ] if 'archival_statuses' in existing_item ['properties' ] else [latest_daac_status ]
179+ updated_item = sfa_client .update_item (collection_id , granule_id , existing_item , update_whole = True ) # TODO partial update via patch is not working at this moment.
180+ return
181+
182+ def update_stac_uds (self , cnm_notification_msg ):
164183 granule_identifier = UdsCollections .decode_identifier (cnm_notification_msg ['identifier' ]) # This is normally meant to be for collection. Since our granule ID also has collection id prefix. we can use this.
165184 try :
166- existing_granule_object = self .__granules_index .get_entry (granule_identifier .tenant , granule_identifier .venue , cnm_notification_msg ['identifier' ])
185+ existing_granule_object = self .__granules_index .get_entry (granule_identifier .tenant ,
186+ granule_identifier .venue ,
187+ cnm_notification_msg ['identifier' ])
167188 except Exception as e :
168- LOGGER .exception (f"error while attempting to retrieve existing record: { cnm_notification_msg ['identifier' ]} , not continuing" )
189+ LOGGER .exception (
190+ f"error while attempting to retrieve existing record: { cnm_notification_msg ['identifier' ]} , not continuing" )
169191 return
170192 LOGGER .debug (f'existing_granule_object: { existing_granule_object } ' )
171193 if cnm_notification_msg ['response' ]['status' ] == 'SUCCESS' :
@@ -177,7 +199,27 @@ def receive_from_daac(self, event: dict):
177199 return
178200 self .__granules_index .update_entry (granule_identifier .tenant , granule_identifier .venue , {
179201 'archive_status' : 'cnm_r_failed' ,
180- 'archive_error_message' : cnm_notification_msg ['response' ]['errorMessage' ] if 'errorMessage' in cnm_notification_msg ['response' ] else 'unknown' ,
181- 'archive_error_code' : cnm_notification_msg ['response' ]['errorCode' ] if 'errorCode' in cnm_notification_msg ['response' ] else 'unknown' ,
202+ 'archive_error_message' : cnm_notification_msg ['response' ]['errorMessage' ] if 'errorMessage' in
203+ cnm_notification_msg [
204+ 'response' ] else 'unknown' ,
205+ 'archive_error_code' : cnm_notification_msg ['response' ]['errorCode' ] if 'errorCode' in cnm_notification_msg [
206+ 'response' ] else 'unknown' ,
182207 }, cnm_notification_msg ['identifier' ])
183208 return
209+
210+ def receive_from_daac (self , event : dict ):
211+ LOGGER .debug (f'receive_from_daac#event: { event } ' )
212+ sns_msg = AwsMessageTransformers ().sqs_sns (event )
213+ LOGGER .debug (f'sns_msg: { sns_msg } ' )
214+ cnm_notification_msg = sns_msg
215+
216+ cnm_msg_schema = requests .get ('https://raw.githubusercontent.com/podaac/cloud-notification-message-schema/v1.6.1/cumulus_sns_schema.json' )
217+ cnm_msg_schema .raise_for_status ()
218+ cnm_msg_schema = json .loads (cnm_msg_schema .text )
219+ result = JsonValidator (cnm_msg_schema ).validate (cnm_notification_msg )
220+ if result is not None :
221+ raise ValueError (f'input cnm event has cnm_msg_schema validation errors: { result } ' )
222+ if 'response' not in cnm_notification_msg :
223+ raise ValueError (f'missing response in { cnm_notification_msg } ' )
224+ self .update_stac (cnm_notification_msg )
225+ return
0 commit comments