-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(COR-1127): Add cluster lock/unlock
- Loading branch information
Showing
9 changed files
with
180 additions
and
132 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,63 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/qovery/qovery-cli/utils" | ||
"github.com/qovery/qovery-client-go" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"io" | ||
"os" | ||
) | ||
|
||
var clusterLockCmd = &cobra.Command{ | ||
Use: "lock", | ||
Short: "Lock a cluster", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
lockCluster() | ||
}, | ||
} | ||
|
||
func init() { | ||
clusterLockCmd.Flags().StringVarP(&clusterId, "cluster-id", "c", "", "Cluster ID") | ||
clusterLockCmd.Flags().StringVarP(&lockReason, "reason", "r", "", "Reason") | ||
clusterLockCmd.Flags().Int32VarP(&lockTtlInDays, "ttl-in-days", "d", -1, "TTL in days") | ||
|
||
clusterLockCmd.MarkFlagRequired("cluster-id") | ||
clusterLockCmd.MarkFlagRequired("reason") | ||
|
||
clusterCmd.AddCommand(clusterLockCmd) | ||
} | ||
|
||
func lockCluster() { | ||
var ttlInDays *int32 = nil | ||
if lockTtlInDays != -1 { | ||
ttlInDays = &lockTtlInDays | ||
} | ||
|
||
if utils.Validate("lock") { | ||
tokenType, token, err := utils.GetAccessToken() | ||
if err != nil { | ||
utils.PrintlnError(err) | ||
os.Exit(1) | ||
} | ||
|
||
client := utils.GetQoveryClient(tokenType, token) | ||
|
||
lockClusterRequest := qovery.ClusterLockRequest{ | ||
Reason: lockReason, | ||
TtlInDays: ttlInDays, | ||
} | ||
|
||
_, http, err := client.ClustersAPI.LockCluster(context.Background(), clusterId).ClusterLockRequest(lockClusterRequest).Execute() | ||
if err != nil { | ||
utils.PrintlnError(err) | ||
result, _ := io.ReadAll(http.Body) | ||
log.Error("%s", string(result)) | ||
Check failure on line 57 in cmd/cluster_lock.go GitHub Actions / test
|
||
os.Exit(1) | ||
} else { | ||
fmt.Println("Cluster locked.") | ||
} | ||
} | ||
} |
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,63 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/qovery/qovery-cli/utils" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"io" | ||
"net/http" | ||
"os" | ||
"strconv" | ||
"text/tabwriter" | ||
"time" | ||
) | ||
|
||
var clusterLockedCmd = &cobra.Command{ | ||
Use: "locked", | ||
Short: "List locked clusters", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
clusterLocked() | ||
}, | ||
} | ||
|
||
func init() { | ||
clusterLockedCmd.Flags().StringVarP(&organizationId, "organization-id", "o", "", "Organization ID") | ||
clusterLockCmd.MarkFlagRequired("organization-id") | ||
|
||
clusterCmd.AddCommand(clusterLockedCmd) | ||
} | ||
|
||
func clusterLocked() { | ||
tokenType, token, err := utils.GetAccessToken() | ||
if err != nil { | ||
utils.PrintlnError(err) | ||
os.Exit(1) | ||
} | ||
|
||
client := utils.GetQoveryClient(tokenType, token) | ||
lockedClusters, res, err := client.OrganizationClusterLockAPI.ListClusterLock(context.Background(), organizationId).Execute() | ||
if res != nil && res.StatusCode != http.StatusOK { | ||
result, _ := io.ReadAll(res.Body) | ||
log.Errorf("Could not list locked clusters : %s. %s", res.Status, string(result)) | ||
return | ||
} | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0) | ||
format := "%s\t | %s\t | %s\t | %s\t | %s\t | %s\n" | ||
fmt.Fprintf(w, format, "", "cluster_id", "locked_at", "ttl_in_days", "locked_by", "reason") | ||
for idx, lock := range lockedClusters.Results { | ||
ttlInDays := "infinite" | ||
if lock.TtlInDays != nil { | ||
ttlInDays = strconv.Itoa(int(*lock.TtlInDays)) | ||
} | ||
|
||
fmt.Fprintf(w, format, fmt.Sprintf("%d", idx+1), lock.ClusterId, lock.LockedAt.Format(time.RFC1123), ttlInDays, lock.OwnerName, lock.Reason) | ||
} | ||
w.Flush() | ||
} |
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,44 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/qovery/qovery-cli/utils" | ||
"github.com/spf13/cobra" | ||
"os" | ||
) | ||
|
||
var clusterUnlockCmd = &cobra.Command{ | ||
Use: "unlock", | ||
Short: "Unlock a cluster", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
unlockCluster() | ||
}, | ||
} | ||
|
||
func init() { | ||
clusterUnlockCmd.Flags().StringVarP(&clusterId, "cluster-id", "c", "", "Cluster ID") | ||
clusterLockCmd.MarkFlagRequired("cluster-id") | ||
|
||
clusterCmd.AddCommand(clusterUnlockCmd) | ||
} | ||
|
||
func unlockCluster() { | ||
if utils.Validate("unlock") { | ||
tokenType, token, err := utils.GetAccessToken() | ||
if err != nil { | ||
utils.PrintlnError(err) | ||
os.Exit(1) | ||
} | ||
|
||
client := utils.GetQoveryClient(tokenType, token) | ||
|
||
_, err = client.ClustersAPI.UnlockCluster(context.Background(), clusterId).Execute() | ||
if err != nil { | ||
utils.PrintlnError(err) | ||
os.Exit(1) | ||
} else { | ||
fmt.Println("Cluster unlocked.") | ||
} | ||
} | ||
} |
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
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
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