Skip to content
Draft
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions cmr_s3_subscriber/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
lambda/*.zip
subscriptions/
terraform/.terraform/
terraform/.terraform.lock.hcl
terraform/config.yaml*
terraform/terraform.tfstate*
terraform/*.tfvars
terraform/credentials.env
config.yaml
credentials.env
106 changes: 106 additions & 0 deletions cmr_s3_subscriber/delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import argparse
import requests
import earthaccess
import yaml
import os


def parse_args():
parser = argparse.ArgumentParser()

parser.add_argument(
'config',
default='config.yaml',
help='Configuration file'
)

sub_id = parser.add_mutually_exclusive_group(required=True)

sub_id.add_argument(
'--native_id',
help='Native ID to delete'
)

sub_id.add_argument(
'--response-xml',
help='XML file from subscribe.py from which to get native ID to delete'
)

parser.add_argument(
'--dryrun',
action='store_true',
help='Do not make CMR API calls except for auth'
)

return parser.parse_args()


def main(args):
with open(args.config, 'r') as f:
config = yaml.safe_load(f)

os.environ['EARTHDATA_USERNAME'] = config['edl_username']
os.environ['EARTHDATA_PASSWORD'] = config['edl_password']

auth = earthaccess.login(strategy='environment')
bearer_token = auth.token['access_token']

if args.native_id is not None:
nid = args.native_id
else:
from xml.etree import ElementTree as ET

tree = ET.parse(args.response_xml)
root = tree.getroot()
nid = root.find('native-id').text

headers = {
"Authorization": f"Bearer {bearer_token}",
}

if args.dryrun:
print(f"Would issue a DELETE request to https://cmr.earthdata.nasa.gov/ingest/subscriptions/{nid}")
print(f'{headers=}')
exit()

try:
response = requests.delete(
f"https://cmr.earthdata.nasa.gov/ingest/subscriptions/{nid}",
headers=headers,
)

if response.ok:
print("Successfully deleted CMR subscription")
print(f"Response: {response.text}")

if args.response_xml is not None:
try:
os.unlink(args.response_xml)
except:
print('Could not delete XML file')
else:
print(f"Error deleting subscription. Status code: {response.status_code}")
print(f"Response: {response.text}")

except Exception as e:
print(f"Error making subscription request: {str(e)}")


if __name__ == '__main__':
main(parse_args())
Loading