Skip to content

Commit

Permalink
CLOUDP-192880: Make clean CLI tool (#1149)
Browse files Browse the repository at this point in the history
Signed-off-by: Jose Vazquez <jose.vazquez@mongodb.com>
  • Loading branch information
josvazg committed Oct 5, 2023
1 parent 0cf936b commit 9599525
Show file tree
Hide file tree
Showing 29 changed files with 127 additions and 665 deletions.
42 changes: 0 additions & 42 deletions .github/actions/clean-vpc/go.mod

This file was deleted.

234 changes: 0 additions & 234 deletions .github/actions/clean-vpc/go.sum

This file was deleted.

45 changes: 0 additions & 45 deletions .github/actions/cleanup-pe/go.mod

This file was deleted.

248 changes: 0 additions & 248 deletions .github/actions/cleanup-pe/go.sum

This file was deleted.

13 changes: 0 additions & 13 deletions .github/actions/cleanup/go.mod

This file was deleted.

58 changes: 0 additions & 58 deletions .github/actions/cleanup/go.sum

This file was deleted.

9 changes: 6 additions & 3 deletions .github/workflows/cleanup-all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ jobs:
with:
go-version-file: "${{ github.workspace }}/go.mod"

- name: Build & Test clean tool
run: (cd tools/clean && go build . && go test .)

- name: Run cleanup VPC
if: github.event_name == 'schedule' || github.event.inputs.cleanVPC == 'true'
env:
Expand All @@ -48,7 +51,7 @@ jobs:
AZURE_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }}
AZURE_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
GCP_SA_CRED: ${{ secrets.GCP_SA_CRED }}
run: (cd .github/actions/clean-vpc && go run .)
run: (cd tools/clean && ./clean vpc)

- name: Run cleanup PE
if: github.event_name == 'schedule' || github.event.inputs.cleanPE == 'true'
Expand All @@ -61,7 +64,7 @@ jobs:
AZURE_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }}
AZURE_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
GCP_SA_CRED: ${{ secrets.GCP_SA_CRED }}
run: (cd .github/actions/cleanup-pe && go run .)
run: (cd tools/clean && ./clean pe)

- name: Run atlas project cleaner
if: always()
Expand All @@ -70,4 +73,4 @@ jobs:
MCLI_PRIVATE_API_KEY: ${{ secrets.ATLAS_PRIVATE_KEY }}
CLEAN_ALL: ${{ github.event.inputs.clean }}
MAX_PROJECT_LIFETIME: ${{ github.event.inputs.timelife }}
run: (cd .github/actions/cleanup && go run .)
run: (cd tools/clean && ./clean atlas)
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ tmp/

# ready to work samples
deploy/

# ignore tool binaries
tools/clean/clean
6 changes: 6 additions & 0 deletions go.work
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
go 1.21.1

use (
.
./tools/clean
)
9 changes: 9 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0GqbN2Wy8c=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
11 changes: 6 additions & 5 deletions .github/actions/cleanup/main.go → tools/clean/atlas/atlas.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package main
package atlas

import (
"actions/cleanup/atlasclient"
"actions/cleanup/project"
"tools/clean/atlas/project"
"context"
"log"
"os"
"tools/clean/atlas/client"

"go.mongodb.org/atlas/mongodbatlas"
)

func main() {
func CleanAtlas() {
config, err := NewConfig()
if err != nil {
log.Fatalf("error getting config: %s", err)
}

client, err := atlasclient.SetupClient(config.PublicKey, config.PrivateKey, config.ManagerUrl)
client, err := client.SetupClient(config.PublicKey, config.PrivateKey, config.ManagerUrl)
if err != nil {
log.Fatalf("error creating atlas client: %s", err)
}
Expand All @@ -39,4 +39,5 @@ func main() {
log.Printf("Not all project deleted. Please run cleaner again later")
os.Exit(1)
}
log.Printf("Atlas cleanups completed OK")
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package atlasclient
package client

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package atlas

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func DeleteAllAdvancedClusters(ctx context.Context, client mongodbatlas.Advanced
var allErr error
for _, cluster := range advancedClusters {
log.Printf("Deleting advanced cluster %s", cluster.Name)
if _, err = client.Delete(ctx, projectID, cluster.Name); err != nil {
if _, err = client.Delete(ctx, projectID, cluster.Name, &mongodbatlas.DeleteAdvanceClusterOptions{}); err != nil {
allErr = errors.Join(allErr, fmt.Errorf("error deleting advanced cluster: %w", err))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func DeleteAllDeployments(ctx context.Context, client mongodbatlas.ClustersServi
var allErr error
for _, deployment := range deployments {
log.Printf("Deleting deployment %s", deployment.Name)
if _, err = client.Delete(ctx, projectID, deployment.Name); err != nil {
if _, err = client.Delete(ctx, projectID, deployment.Name, &mongodbatlas.DeleteAdvanceClusterOptions{}); err != nil {
allErr = errors.Join(allErr, fmt.Errorf("error deleting deployment: %s", err))
}
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package project

import (
"actions/cleanup/deployment"
"tools/clean/atlas/deployment"
"context"
"fmt"
"log"
Expand Down Expand Up @@ -62,7 +62,7 @@ func DeleteProjects(ctx context.Context, client mongodbatlas.Client, projectList
ok = false
log.Printf("error deleting project: %s", err)
} else {
log.Printf("Project successufully deleted")
log.Printf("Project successfully deleted")
}
}
return ok
Expand Down
Empty file.
3 changes: 3 additions & 0 deletions tools/clean/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module tools/clean

go 1.21.1
42 changes: 42 additions & 0 deletions tools/clean/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"fmt"
"log"
"os"
"strings"
"tools/clean/atlas"
"tools/clean/pe"
"tools/clean/vpc"
)

func main() {
if err := Clean(os.Args); err != nil {
log.Printf("Invocation failed: %s", err)
log.Fatalf("Usage: %s {atlas|pe|vpc}", os.Args[0])
}
}

var cleanAtlas = atlas.CleanAtlas

var cleanPEs = pe.CleanPEs

var cleanVPCs = vpc.CleanVPCs

func Clean(args []string) error {
if len(args) != 2 {
return fmt.Errorf("Wrong number of arguments: expected 1 got %d", len(args)-1)
}
action := strings.ToLower(args[1])
switch action {
case "atlas":
cleanAtlas()
case "pe":
cleanPEs()
case "vpc":
cleanVPCs()
default:
return fmt.Errorf("Unsupported action %q", action)
}
return nil
}
34 changes: 34 additions & 0 deletions tools/clean/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCleanBadInputs(t *testing.T) {
t.Run("Calling without arguments fails", func(t *testing.T) {
assert.ErrorContains(t, Clean([]string{}), "Wrong number of arguments")
})
t.Run("Calling just with the command fails", func(t *testing.T) {
assert.ErrorContains(t, Clean([]string{"clean"}), "expected 1 got 0")
})
t.Run("Calling with too many arguments fails", func(t *testing.T) {
assert.ErrorContains(t, Clean([]string{"clean", "atlas", "duh"}), "expected 1 got 2")
})
}

func TestCleanCalls(t *testing.T) {
t.Run("Calling atlas works", func(t *testing.T) {
cleanAtlas = func() {}
assert.NoError(t, Clean([]string{"clean", "Atlas"}))
})
t.Run("Calling vpc works", func(t *testing.T) {
cleanVPCs = func() {}
assert.NoError(t, Clean([]string{"clean", "VPC"}))
})
t.Run("Calling atlas fails due to missing credentials", func(t *testing.T) {
cleanPEs = func() {}
assert.NoError(t, Clean([]string{"clean", "Pe"}))
})
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package pe

import (
"context"
Expand Down
Loading

0 comments on commit 9599525

Please sign in to comment.