Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: [Terraform] Create github action to delete projects in the test env #1378

Merged
merged 6 commits into from
Aug 9, 2023
Merged
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
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