Skip to content

Commit

Permalink
chore: Changes skip project logic for cleanup action (#1995)
Browse files Browse the repository at this point in the history
* don't use project to be deleted

* change skip deletion

* comment out delete project command

* remove network org
  • Loading branch information
lantoli authored Mar 7, 2024
1 parent 341931f commit c62b211
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 37 deletions.
20 changes: 0 additions & 20 deletions .github/workflows/cleanup-test-env.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,7 @@ jobs:
MONGODB_ATLAS_PRIVATE_KEY: ${{ secrets.MONGODB_ATLAS_PRIVATE_KEY_CLOUD_DEV }}
MONGODB_ATLAS_ORG_ID: ${{ vars.MONGODB_ATLAS_ORG_ID_CLOUD_DEV }}
MONGODB_ATLAS_OPS_MANAGER_URL: ${{vars.MONGODB_ATLAS_BASE_URL}}
PROJECT_TO_NOT_DELETE: ${{vars.MONGODB_ATLAS_ACCTEST_TRIGGER_PROJECT_ID_CLOUD_DEV}}
run: ./scripts/cleanup-test-env.sh
cleanup-test-env-network:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
with:
sparse-checkout: |
scripts
- uses: mongodb/atlas-github-action@07d212bf80c068dfcfbf6e15b22c61ae6e66d96e
- name: Cleanup test env network
shell: bash
env:
MONGODB_ATLAS_PUBLIC_KEY: ${{ secrets.MONGODB_ATLAS_PUBLIC_KEY_CLOUD_DEV_NETWORK }}
MONGODB_ATLAS_PRIVATE_KEY: ${{ secrets.MONGODB_ATLAS_PRIVATE_KEY_CLOUD_DEV_NETWORK }}
MONGODB_ATLAS_ORG_ID: ${{ vars.MONGODB_ATLAS_ORG_ID_CLOUD_DEV_NETWORK }}
MONGODB_ATLAS_OPS_MANAGER_URL: ${{vars.MONGODB_ATLAS_BASE_URL}}
PROJECT_TO_NOT_DELETE: ${{vars.MONGODB_ATLAS_PROJECT_ID_CLOUD_DEV_NETWORK}}
run: ./scripts/cleanup-test-env.sh
cleanup-test-env-qa:
runs-on: ubuntu-latest
steps:
Expand All @@ -58,5 +39,4 @@ jobs:
MONGODB_ATLAS_PRIVATE_KEY: ${{ secrets.MONGODB_ATLAS_PRIVATE_KEY_CLOUD_QA }}
MONGODB_ATLAS_ORG_ID: ${{ vars.MONGODB_ATLAS_ORG_ID_CLOUD_QA }}
MONGODB_ATLAS_OPS_MANAGER_URL: ${{ vars.MONGODB_ATLAS_BASE_URL_QA }}
PROJECT_TO_NOT_DELETE: ${{ vars.MONGODB_ATLAS_PROJECT_ID_CLOUD_QA }}
run: ./scripts/cleanup-test-env.sh
32 changes: 15 additions & 17 deletions scripts/cleanup-test-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,18 @@ set -Eeou pipefail

delete_endpoint() {
provider=$1
count=$(atlas privateEndpoints "${provider}" list --projectId "${clean_project_id}" -o=go-template="{{len .}}")
count=$(atlas privateEndpoints "${provider}" list --projectId "${project_id}" -o=go-template="{{len .}}")
retVal=$?
if [ $retVal -ne 0 ]; then
count=0
fi
if [ "${count}" != "0" ]; then
echo "Project ${clean_project_id} contains ${provider} endpoints, will start deleting"
id=$(atlas privateEndpoints "${provider}" list --projectId "${clean_project_id}" -o=go-template="{{(index . 0).Id}}")
atlas privateEndpoints "${provider}" delete "${id}" --force --projectId "${clean_project_id}"
echo "Project ${project_display} contains ${provider} endpoints, will start deleting"
id=$(atlas privateEndpoints "${provider}" list --projectId "${project_id}" -o=go-template="{{(index . 0).Id}}")
atlas privateEndpoints "${provider}" delete "${id}" --force --projectId "${project_id}"
fi
}

projectToSkip="${PROJECT_TO_NOT_DELETE:-NONE}"

export MCLI_OPS_MANAGER_URL="${MONGODB_ATLAS_OPS_MANAGER_URL}"
export MCLI_PRIVATE_API_KEY="${MONGODB_ATLAS_PRIVATE_KEY}"
export MCLI_PUBLIC_API_KEY="${MONGODB_ATLAS_PUBLIC_KEY}"
Expand All @@ -41,30 +39,30 @@ org_id="${MONGODB_ATLAS_ORG_ID}"
# Get all project Ids inside the organization
projects=$(atlas project ls --limit 500 --orgId "${org_id}" -o json)

echo "${projects}" | jq -c '.results[].id' | while read -r id; do
# Trim the quotes from the id
clean_project_id=$(echo "$id" | tr -d '"')
if [ "${clean_project_id}" = "${projectToSkip}" ]; then
echo "Skipping project with ID ${projectToSkip}"
echo "${projects}" | jq -r '.results[] | "\(.id) \(.name)"' | while read -r project_id project_name; do
project_display="${project_name} - ${project_id}"

if [[ "${project_name}" == test-acc-tf-p-keep-* ]]; then
echo "Skipping project ${project_display}"
continue
fi

clusters=$(atlas cluster ls --projectId "${clean_project_id}" -o=go-template="{{.TotalCount}}")
set +e
clusters=$(atlas cluster ls --projectId "${project_id}" -o=go-template="{{.TotalCount}}")
if [ "${clusters}" != "0" ]; then
echo "Project ${clean_project_id} contains clusters. Skipping..."
echo "Project ${project_display} contains clusters. Skipping..."
continue
fi

set +e
delete_endpoint "aws"
delete_endpoint "gcp"
delete_endpoint "azure"
set -e

echo "Deleting projectId ${clean_project_id}"
echo "Deleting project ${project_display}"
# This command can fail if project has a cluster, a private endpoint, or general failure. The echo command always succeeds so the subshell will succeed and continue
(
atlas project delete "${clean_project_id}" --force || \
echo "Failed to delete project with ID ${clean_project_id}"
atlas project delete "${project_id}" --force || \
echo "Failed to delete project ${project_display}"
)
done

0 comments on commit c62b211

Please sign in to comment.