-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: [Terraform] Create github action to delete projects in the tes…
…t env (#1378)
- Loading branch information
1 parent
b71f54c
commit 312bc3f
Showing
2 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |