From 23a22ab689e2facb3fc3b92dd86e341b79fd3cd0 Mon Sep 17 00:00:00 2001 From: Pierre Gerbelot Date: Mon, 2 Oct 2023 13:22:58 +0200 Subject: [PATCH] feat(COR-715): add admin api to delete old invalid credentials clusters --- cmd/admin.go | 1 + ...delete_old_invalid_credentials_clusters.go | 26 ++++++++++++++ pkg/delete_cluster.go | 36 +++++++++++++++++++ pkg/delete_orga.go | 6 +++- 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 cmd/admin_delete_old_invalid_credentials_clusters.go diff --git a/cmd/admin.go b/cmd/admin.go index 63edcabd..7d69ee72 100644 --- a/cmd/admin.go +++ b/cmd/admin.go @@ -12,6 +12,7 @@ var ( dryRun bool version string versionErr error + ageInDay int adminCmd = &cobra.Command{Use: "admin", Hidden: true} ) diff --git a/cmd/admin_delete_old_invalid_credentials_clusters.go b/cmd/admin_delete_old_invalid_credentials_clusters.go new file mode 100644 index 00000000..85b65ff9 --- /dev/null +++ b/cmd/admin_delete_old_invalid_credentials_clusters.go @@ -0,0 +1,26 @@ +package cmd + +import ( + "github.com/qovery/qovery-cli/pkg" + "github.com/spf13/cobra" +) + +var ( + adminDeleteOldInvalidCredentialsClustersCmd = &cobra.Command{ + Use: "force-delete-old-invalid-credentials-clusters", + Short: "Force delete clusters with invalid credentials with last updated date more thant n days", + Run: func(cmd *cobra.Command, args []string) { + deleteOldClustersWithInvalidCredentials() + }, + } +) + +func init() { + adminDeleteOldInvalidCredentialsClustersCmd.Flags().IntVarP(&ageInDay, "cluster-last-update-in-days", "d", 30, "cluster last update in days") + adminDeleteOldInvalidCredentialsClustersCmd.Flags().BoolVarP(&dryRun, "disable-dry-run", "y", false, "Disable dry run mode") + adminCmd.AddCommand(adminDeleteOldInvalidCredentialsClustersCmd) +} + +func deleteOldClustersWithInvalidCredentials() { + pkg.DeleteOldClustersWithInvalidCredentials(ageInDay, dryRun) +} diff --git a/pkg/delete_cluster.go b/pkg/delete_cluster.go index 157b3874..b79843f6 100644 --- a/pkg/delete_cluster.go +++ b/pkg/delete_cluster.go @@ -1,6 +1,8 @@ package pkg import ( + "bytes" + "encoding/json" "fmt" "io" "net/http" @@ -43,3 +45,37 @@ func DeleteClusterUnDeployedInError() { } } } + +func DeleteOldClustersWithInvalidCredentials(ageInDay int, dryRunDisabled bool) { + utils.CheckAdminUrl() + + if utils.Validate("delete") { + + params := map[string]interface{}{ + "last_update_in_days": ageInDay, + "dry_run": !dryRunDisabled, + } + + requestBody, err := json.Marshal(params) + if err != nil { + log.Errorf("Could not create body for the request") + return + } + + res := deleteWithBody(utils.AdminUrl+"/cluster/deleteOldClustersWithInvalidCredentials", http.MethodPost, true, bytes.NewBuffer(requestBody)) + + if !strings.Contains(res.Status, "200") { + result, _ := io.ReadAll(res.Body) + log.Errorf("Could not delete all clusters with invalid credentials : %s. %s", res.Status, string(result)) + } else { + result, _ := io.ReadAll(res.Body) + if dryRunDisabled { + fmt.Println("Clusters deleted: " + string(result)) + } else { + fmt.Println("Clusters that will be deleted: " + string(result)) + } + } + } +} + + diff --git a/pkg/delete_orga.go b/pkg/delete_orga.go index 3cb30e18..acae7dd7 100644 --- a/pkg/delete_orga.go +++ b/pkg/delete_orga.go @@ -29,6 +29,10 @@ func DeleteOrganizationByClusterId(clusterId string, dryRunDisabled bool) { } func delete(url string, method string, dryRunDisabled bool) *http.Response { + return deleteWithBody(url, method, dryRunDisabled, nil) +} + +func deleteWithBody(url string, method string, dryRunDisabled bool, body io.Reader) *http.Response { tokenType, token, err := utils.GetAccessToken() if err != nil { utils.PrintlnError(err) @@ -39,7 +43,7 @@ func delete(url string, method string, dryRunDisabled bool) *http.Response { return nil } - req, err := http.NewRequest(method, url, nil) + req, err := http.NewRequest(method, url, body) if err != nil { log.Fatal(err) }