Skip to content

Commit

Permalink
first run at managing sns-based rules alongside onetime
Browse files Browse the repository at this point in the history
- remove autogenerated properties from static rule files
- remove proeprties requiring ARNs (account IDs) from static rule files
- require patch for CNM rules to prevent breaking Cumulus-managed fields (subscription id)
- populate ARN fields during bulk update
  • Loading branch information
aliziel committed Dec 2, 2024
1 parent b412881 commit ec483ed
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 3 deletions.
27 changes: 27 additions & 0 deletions _templates/cnm-rule/new/cnm-rule.ejs.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
to: "app/stacks/cumulus/resources/rules/<%= collectionName %>/v<%= collectionVersion %>/<%= collectionName %>___<%= collectionVersion %>_CNM.json"
message: >
hygen cnm-rule new
--provider <%= provider %>
--collection-name <%= collectionName %>
--collection-version <%= collectionVersion %>
---
{
"name": "<%= collectionName %>___<%= collectionVersion %>_CNM",
"state": "ENABLED",
"workflow": "CNMIngestAndPublishGranule",
"provider": "<%= provider %>",
"collection": {
"name": "<%= collectionName %>",
"version": "<%= collectionVersion %>"
},
"rule": {
"type": "sns"
},
"meta": {
"cnmResponseMethod": "sns"
},
"tags": [
"cnm"
]
}
20 changes: 20 additions & 0 deletions _templates/cnm-rule/new/prompt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// see types of prompts:
// https://github.com/enquirer/enquirer/tree/master/examples
//
module.exports = [
{
type: 'input',
name: 'provider',
message: "Provider ID (example: maxar):"
},
{
type: 'input',
name: 'collectionName',
message: "Collection name (example: WV03_MSI_L1B):"
},
{
type: 'input',
name: 'collectionVersion',
message: "Collection version (example: 1):"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "WV04_MSI_L1B___1_CNM",
"state": "ENABLED",
"workflow": "CNMIngestAndPublishGranule",
"provider": "cumulus",
"collection": {
"name": "WV04_MSI_L1B",
"version": "1"
},
"rule": {
"type": "sns"
},
"meta": {
"cnmResponseMethod": "sns"
},
"tags": [
"cnm"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "WV04_Pan_L1B___1_CNM",
"state": "ENABLED",
"workflow": "CNMIngestAndPublishGranule",
"provider": "cumulus",
"collection": {
"name": "WV04_Pan_L1B",
"version": "1"
},
"rule": {
"type": "sns"
},
"meta": {
"cnmResponseMethod": "sns"
},
"tags": [
"cnm"
]
}
63 changes: 60 additions & 3 deletions bin/create-data-management-items.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,37 @@ function find_provider_bucket() {
sed -E 's/.+ = "(.+)"/\1/'
}

function fetch_cnm_params() {
aws ssm get-parameter --with-decryption \
--name "${1}" \
--query "Parameter.Value"
}

function find_throttle_queue_url() {
# Piping to sed shouldn't be necessary, but additional output may be
# captured if console lock requests take >400ms.
# See: https://github.com/hashicorp/terraform/issues/19176
#
# The final sed command should send the first line containing 'sqs' to stdout

echo "aws_sqs_queue.background_job_queue.id" |
terraspace console cumulus 2>/dev/null |
sed '/sqs/!d;q'
}

function sync_items() {
local _path=${1}
local _type=${2}
local _file

find "${_path}/${_type}" -name '*.json' -print0 | while IFS= read -r -d '' _file; do
echo -n "Upserting ${_type} $(basename "${_file}")..."
cumulus "${_type}" upsert --data "${_file}" >/dev/null
if [[ ${_file} =~ 'CNM' ]]; then
echo -n "Patching ${_type} $(basename "${_file}")..."
# cumulus "${_type}" patch --data "${_file}" >/dev/null
else
echo -n "Upserting ${_type} $(basename "${_file}")..."
cumulus "${_type}" upsert --data "${_file}" >/dev/null
fi
echo "done"
done
}
Expand Down Expand Up @@ -49,12 +72,46 @@ function sync_providers() {
fi
}

function sync_rules() {
local _resources_path=${1}
local _subscribe_notification_topic
local _publish_response_topic
local _queue_url
local _tmpdir

echo "Retrieving ARNs for CNM rules..."
_subscribe_notification_topic=$(fetch_cnm_params "/shared/cumulus/cnm-sns-submission-topic")
_publish_response_topic=$(fetch_cnm_params "/shared/cumulus/cnm-sns-response-topic")
_queue_url=$(find_throttle_queue_url)

if [[ ${#_subscribe_notification_topic} -le 5 || ${#_publish_response_topic} -le 5 || -z $_queue_url ]]; then
echo "Missing one or more ARNs for CNM rules"
echo "Continuing without updating current values"

sync_items "${_resources_path}" "rules"
else
_tmpdir=$(mktemp --directory)
# shellcheck disable=SC2064
trap "rm -rf \"${_tmpdir}\"" EXIT

# Copy all rules to temp directory
cp -r --parents "${_resources_path}/rules" "${_tmpdir}"

# Populate CNM rules with ARN properties and retrieved values
# shellcheck disable=SC2016
find "${_resources_path}/rules" -type f -name '*CNM*.json' -print0 |
xargs -0 -I{} sh -c 'jq ".rule.value=${1} | .meta.cnmResponseStream=${2} | .queueUrl=${3}" "${4}" >"${5}"' -- "${_subscribe_notification_topic}" "${_publish_response_topic}" "${_queue_url}" {} "${_tmpdir}/{}"

sync_items "${_tmpdir}/${_resources_path}" "rules"
fi
}

function main() {
local _resources_path=app/stacks/cumulus/resources

sync_providers "${_resources_path}"
sync_items "${_resources_path}" "collections"
sync_items "${_resources_path}" "rules"
sync_rules "${_resources_path}"
}

main

0 comments on commit ec483ed

Please sign in to comment.