Skip to content

Commit

Permalink
chore: [Terraform] Create github action to delete projects in the tes…
Browse files Browse the repository at this point in the history
…t env (#1378)
  • Loading branch information
andreaangiolillo authored Aug 9, 2023
1 parent b71f54c commit 312bc3f
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
53 changes: 53 additions & 0 deletions .github/workflows/cleanup-test-env.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: 'Cleanup test env'

on:
workflow_dispatch: {} # workflow can be run manually
schedule:
- cron: "0 3 * * *" # workflow runs every day at 03:00 AM

jobs:
clenup-test-env-general:
runs-on: ubuntu-latest
steps:
- name: setup Atlas CLI
uses: andreaangiolillo/[email protected]
with:
public-key: ${{ secrets.MONGODB_ATLAS_PUBLIC_KEY_CLOUD_DEV }}
private-key: ${{ secrets.MONGODB_ATLAS_PRIVATE_KEY_CLOUD_DEV }}
org-id: ${{ vars.MONGODB_ATLAS_ORG_ID_CLOUD_DEV }}
- name: Checkout
uses: actions/checkout@v3
with:
sparse-checkout: |
scripts
- name: cleanup cloud-dev
shell: bash
env:
MONGODB_ATLAS_PUBLIC_API_KEY: ${{ secrets.MONGODB_ATLAS_PUBLIC_KEY_CLOUD_DEV }}
MONGODB_ATLAS_PRIVATE_API_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}}
run: ./scripts/cleanup-test.sh
clenup-test-env-network:
runs-on: ubuntu-latest
steps:
- name: setup Atlas CLI
uses: andreaangiolillo/[email protected]
with:
public-key: ${{ secrets.MONGODB_ATLAS_PUBLIC_KEY_CLOUD_DEV_NETWORK }}
private-key: ${{ secrets.MONGODB_ATLAS_PRIVATE_KEY_CLOUD_DEV_NETWORK }}
org-id: ${{ vars.MONGODB_ATLAS_ORG_ID_CLOUD_DEV_NETWORK }}
- name: Checkout
uses: actions/checkout@v3
with:
sparse-checkout: |
scripts
- name: cleanup test env network
shell: bash
env:
MONGODB_ATLAS_PUBLIC_API_KEY: ${{ secrets.MONGODB_ATLAS_PUBLIC_KEY_CLOUD_DEV_NETWORK }}
MONGODB_ATLAS_PRIVATE_API_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.sh
41 changes: 41 additions & 0 deletions scripts/cleanup-test-env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash

# Copyright 2023 MongoDB Inc
#
# Licensed 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.

set -Eeou pipefail

projectToSkip="${PROJECT_TO_NOT_DELETE:-NONE}"

# Get all project Ids inside the organization
projects=$(atlas project ls -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}"
continue
fi

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

echo "Deleting projectId ${clean_project_id}"
# This command will fail if the project has a cluster inside
atlas project delete "${clean_project_id}" --force
done

0 comments on commit 312bc3f

Please sign in to comment.