get_current_dlm_policies 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #! /bin/bash
  2. # Gets the current dlm policies, if any.
  3. #
  4. # WARNING: THIS IS RUN DURRING A 'PLAN' STEP. Do not make changes. Read-only in this script.
  5. set -euo pipefail
  6. PARTITION=$1
  7. REGION=$2
  8. ACCOUNT=$3
  9. ACCOUNT_NAME=$4
  10. # Fix for some accounts having -gov already appended and some not.
  11. # Accounts in gov will get it appended.
  12. ACCOUNT_NAME=${ACCOUNT_NAME%%-gov}
  13. if [[ ${REGION} == "us-gov-east-1" ]]; then
  14. PROFILE=${ACCOUNT_NAME}-gov
  15. elif [[ ${REGION} == "us-gov-west-1" ]]; then
  16. PROFILE=${ACCOUNT_NAME}-gov
  17. elif [[ ${REGION} == "us-east-1" ]]; then
  18. PROFILE=${ACCOUNT_NAME}
  19. elif [[ ${REGION} == "us-west-1" ]]; then
  20. PROFILE=${ACCOUNT_NAME}
  21. else
  22. >&2 echo ERROR: Could not determine target region from source region \"${REGION}\"
  23. exit -1
  24. fi
  25. # Fix the accounts that we foolish prepended 'afs-' to.
  26. PROFILE=${PROFILE##afs-}
  27. POLICIES=$(aws --profile ${PROFILE} --region ${REGION} dlm get-lifecycle-policies)
  28. # Extracts the policy IDs of IMAGE_MANAGEMENT policye
  29. POLICY_IDS=$(echo $POLICIES | jq -r '[.Policies[] | select(.PolicyType=="IMAGE_MANAGEMENT") | select(.Tags.SnapshotPolicy=="Daily")] | first | .PolicyId')
  30. # jq ensures a safe json output
  31. # Will return 'null' if no matching policy
  32. jq -n --arg policy $POLICY_IDS '{ "PolicyId": $policy }'