-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
444 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
cumulus_lambda_functions/cumulus_collections_dapa/cumulus_create_collection_dapa.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import json | ||
import os | ||
|
||
import pystac | ||
|
||
from cumulus_lambda_functions.cumulus_stac.collection_transformer import CollectionTransformer | ||
from cumulus_lambda_functions.cumulus_wrapper.query_collections import CollectionsQuery | ||
from cumulus_lambda_functions.lib.lambda_logger_generator import LambdaLoggerGenerator | ||
|
||
LOGGER = LambdaLoggerGenerator.get_logger(__name__, LambdaLoggerGenerator.get_level_from_env()) | ||
|
||
|
||
class CumulusCreateCollectionDapa: | ||
def __init__(self, event): | ||
self.__event = event | ||
self.__request_body = None | ||
self.__cumulus_collection_query = CollectionsQuery('', '') | ||
self.__cumulus_lambda_prefix = os.getenv('CUMULUS_LAMBDA_PREFIX') | ||
|
||
def start(self): | ||
if 'body' not in self.__event: | ||
raise ValueError(f'missing body in {self.__event}') | ||
self.__request_body = json.loads(self.__event['body']) | ||
LOGGER.debug(f'request body: {self.__request_body}') | ||
validation_result = pystac.Collection.from_dict(self.__request_body).validate() | ||
if not isinstance(validation_result, list): | ||
LOGGER.error(f'request body is not valid STAC collection: {validation_result}') | ||
return { | ||
'statusCode': 500, | ||
'body': {'message': f'request body is not valid STAC Collection schema. check details', | ||
'details': validation_result} | ||
} | ||
try: | ||
cumulus_collection_doc = CollectionTransformer().from_stac(self.__request_body) | ||
creation_result = self.__cumulus_collection_query.create_collection(cumulus_collection_doc, self.__cumulus_lambda_prefix) | ||
if 'status' not in creation_result: | ||
return { | ||
'statusCode': 500, | ||
'body': { | ||
'message': {creation_result} | ||
} | ||
} | ||
except Exception as e: | ||
LOGGER.exception('error while creating new collection in Cumulus') | ||
return { | ||
'statusCode': 500, | ||
'body': { | ||
'message': f'error while creating new collection in Cumulus. check details', | ||
'details': str(e) | ||
} | ||
} | ||
return { | ||
'statusCode': 200, | ||
'body': { | ||
'message': creation_result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
cumulus_lambda_functions/cumulus_stac/unity_collection_stac.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from datetime import datetime | ||
|
||
from pystac import Link, Collection, Extent, SpatialExtent, TemporalExtent, Summaries | ||
|
||
from cumulus_lambda_functions.cumulus_stac.collection_transformer import CollectionTransformer | ||
from cumulus_lambda_functions.lib.lambda_logger_generator import LambdaLoggerGenerator | ||
|
||
LOGGER = LambdaLoggerGenerator.get_logger(__name__, LambdaLoggerGenerator.get_level_from_env()) | ||
|
||
|
||
class UnityCollectionStac: | ||
def __init__(self): | ||
self.__id = '' | ||
self.__granule_id_extraction_regex = '' | ||
self.__process = '' | ||
self.__collection_title = '' | ||
self.__granule_id_regex = '' | ||
self.__sample_filename = '' | ||
self.__files = [] | ||
self.__collection_transformer = CollectionTransformer() | ||
|
||
def with_title(self, title: str): | ||
self.__collection_title = title | ||
return self | ||
|
||
def with_process(self, process: str): | ||
self.__process = process | ||
return self | ||
|
||
def with_id(self, collection_id: str): | ||
self.__id = collection_id | ||
if '___' not in collection_id: | ||
LOGGER.warning(f'no ID in {collection_id}. using 001') | ||
self.__id = f'{self.__id}___001' | ||
return self | ||
|
||
def with_graule_id_regex(self, granule_id_regex): | ||
self.__granule_id_regex = granule_id_regex | ||
return self | ||
|
||
def with_granule_id_extraction_regex(self, granule_id_extraction_regex): | ||
self.__granule_id_extraction_regex = granule_id_extraction_regex | ||
return self | ||
|
||
def add_file_type(self, title: str, regex: str, bucket: str, media_type: str, rel: str = 'item'): | ||
if rel == 'root': | ||
LOGGER.debug('updating media_type for rel = root') | ||
media_type = 'application/json' | ||
self.__files.append(Link(rel=rel, target=self.__collection_transformer.generate_target_link_url(regex, bucket), media_type=media_type, title=title)) | ||
return self | ||
|
||
def start(self): | ||
# TODO validate | ||
stac_collection = Collection(id=self.__id, | ||
description='TODO', | ||
extent=Extent(SpatialExtent([[0, 0, 0, 0]]), | ||
TemporalExtent([[datetime.utcnow(), datetime.utcnow()]])), | ||
title=self.__collection_title, | ||
summaries=Summaries({ | ||
'granuleId': [self.__granule_id_regex], | ||
'granuleIdExtraction': [self.__granule_id_extraction_regex], | ||
'process': [self.__process] | ||
}), | ||
) | ||
stac_collection.add_links(self.__files) | ||
new_collection = stac_collection.to_dict(include_self_link=False) | ||
if 'links' in new_collection and len(new_collection['links']) > 0 and new_collection['links'][0]['rel'] == 'root': | ||
new_collection['links'][0]['href'] = './collection.json' | ||
return new_collection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.