Skip to content

Commit

Permalink
support pause and resume clusters (#9)
Browse files Browse the repository at this point in the history
* cluster creation final

* validate map input

* support update cluster

* support pausing cluster

* support resume cluster
  • Loading branch information
posriniv authored Sep 23, 2022
1 parent 978d7cc commit 25bb81b
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 2 deletions.
7 changes: 5 additions & 2 deletions cmd/delete_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ var deleteClusterCmd = &cobra.Command{
accountID, _, _ := getAccountID(context.Background(), apiClient)
projectID, _, _ := getProjectID(context.Background(), apiClient, accountID)
clusterName, _ := cmd.Flags().GetString("cluster-name")
clusterID, _, _ := getClusterID(context.Background(), apiClient, accountID, projectID, clusterName)

clusterID, clusterIDOK, errMsg := getClusterID(context.Background(), apiClient, accountID, projectID, clusterName)
if !clusterIDOK {
fmt.Fprintf(os.Stderr, "Error when fetching cluster ID: %v\n", errMsg)
return
}
r, err := apiClient.ClusterApi.DeleteCluster(context.Background(), accountID, projectID, clusterID).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `ClusterApi.ListClusters``: %v\n", err)
Expand Down
32 changes: 32 additions & 0 deletions cmd/pause.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"github.com/spf13/cobra"
)

// pauseCmd represents the list command
var pauseCmd = &cobra.Command{
Use: "pause",
Short: "Pause resources in YB Managed",
Long: "Pause resources in YB Managed",
Run: func(cmd *cobra.Command, args []string) {
//fmt.Println("pause called")
},
}

func init() {
rootCmd.AddCommand(pauseCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// pauseCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// pauseCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
54 changes: 54 additions & 0 deletions cmd/pause_cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"context"
"fmt"
"os"

"github.com/spf13/cobra"
)

// pauseClusterCmd represents the cluster command
var pauseClusterCmd = &cobra.Command{
Use: "cluster",
Short: "Pause clusters in YugabyteDB Managed",
Long: "Pause clusters in YugabyteDB Managed",
Run: func(cmd *cobra.Command, args []string) {
apiClient, _ := getApiClient(context.Background())
accountID, _, _ := getAccountID(context.Background(), apiClient)
projectID, _, _ := getProjectID(context.Background(), apiClient, accountID)
clusterName, _ := cmd.Flags().GetString("cluster-name")
clusterID, clusterIDOK, errMsg := getClusterID(context.Background(), apiClient, accountID, projectID, clusterName)
if !clusterIDOK {
fmt.Fprintf(os.Stderr, "Error when fetching cluster ID: %v\n", errMsg)
return
}

_, _, err := apiClient.ClusterApi.PauseCluster(context.Background(), accountID, projectID, clusterID).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error while pausing the cluster %v: %v\n", clusterName, err.Error())
return
}

fmt.Fprintf(os.Stdout, "The cluster %v is being paused\n", clusterName)
},
}

func init() {
pauseCmd.AddCommand(pauseClusterCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// pauseClusterCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// pauseClusterCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
pauseClusterCmd.Flags().String("cluster-name", "", "The name of the cluster to be paused")
pauseClusterCmd.MarkFlagRequired("cluster-name")
}
32 changes: 32 additions & 0 deletions cmd/resume.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"github.com/spf13/cobra"
)

// resumeCmd represents the list command
var resumeCmd = &cobra.Command{
Use: "resume",
Short: "Resume resources in YB Managed",
Long: "Resume resources in YB Managed",
Run: func(cmd *cobra.Command, args []string) {
//fmt.Println("pause called")s
},
}

func init() {
rootCmd.AddCommand(resumeCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// resumeCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// resumeCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
54 changes: 54 additions & 0 deletions cmd/resume_cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"context"
"fmt"
"os"

"github.com/spf13/cobra"
)

// resumeClusterCmd represents the cluster command
var resumeClusterCmd = &cobra.Command{
Use: "cluster",
Short: "Pause clusters in YugabyteDB Managed",
Long: "Pause clusters in YugabyteDB Managed",
Run: func(cmd *cobra.Command, args []string) {
apiClient, _ := getApiClient(context.Background())
accountID, _, _ := getAccountID(context.Background(), apiClient)
projectID, _, _ := getProjectID(context.Background(), apiClient, accountID)
clusterName, _ := cmd.Flags().GetString("cluster-name")
clusterID, clusterIDOK, errMsg := getClusterID(context.Background(), apiClient, accountID, projectID, clusterName)
if !clusterIDOK {
fmt.Fprintf(os.Stderr, "Error when fetching cluster ID: %v\n", errMsg)
return
}

_, _, err := apiClient.ClusterApi.ResumeCluster(context.Background(), accountID, projectID, clusterID).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error while resuming the cluster %v\n", clusterName)
return
}

fmt.Fprintf(os.Stdout, "The cluster %v is being resumed\n", clusterName)
},
}

func init() {
resumeCmd.AddCommand(resumeClusterCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// resumeClusterCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// resumeClusterCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
resumeClusterCmd.Flags().String("cluster-name", "", "The name of the cluster to be resumed")
resumeClusterCmd.MarkFlagRequired("cluster-name")
}

0 comments on commit 25bb81b

Please sign in to comment.