Skip to content

Commit

Permalink
feat(COR-715): add admin api to delete old invalid credentials clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
pggb25 committed Oct 2, 2023
1 parent 0c3fc78 commit 23a22ab
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var (
dryRun bool
version string
versionErr error
ageInDay int
adminCmd = &cobra.Command{Use: "admin", Hidden: true}
)

Expand Down
26 changes: 26 additions & 0 deletions cmd/admin_delete_old_invalid_credentials_clusters.go
Original file line number Diff line number Diff line change
@@ -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)
}
36 changes: 36 additions & 0 deletions pkg/delete_cluster.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package pkg

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -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))
}
}
}
}


6 changes: 5 additions & 1 deletion pkg/delete_orga.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down

0 comments on commit 23a22ab

Please sign in to comment.