Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

single-region cluster crud operations #7

Merged
merged 8 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@ release:
GOOS=windows GOARCH=amd64 go build -ldflags="-X 'main.version=v${VERSION}'" -o ./bin/${BINARY}_${VERSION}_windows_amd64

clean:
rm -rf ybm
rm -rf credentials
rm -rf ybm
103 changes: 103 additions & 0 deletions cmd/create_cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"context"
"fmt"
"os"

"github.com/spf13/cobra"
ybmclient "github.com/yugabyte/yugabytedb-managed-go-client-internal"
)

// createClusterCmd represents the cluster command
var createClusterCmd = &cobra.Command{
Use: "cluster",
Short: "Create a cluster in YB Managed",
Long: "Create a cluster in YB 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")
credentials, _ := cmd.Flags().GetStringToString("credentials")

username := credentials["username"]
password := credentials["password"]
regionInfoList := []map[string]string{}
if cmd.Flags().Changed("region-info") {
regionInfo, _ := cmd.Flags().GetStringToString("region-info")
if _, ok := regionInfo["region"]; !ok {
fmt.Fprintf(os.Stderr, "Region not specified in region info\n")
return
}
if _, ok := regionInfo["num_nodes"]; !ok {
fmt.Fprintf(os.Stderr, "Number of nodes not specified in region info\n")
return
}
regionInfoList = append(regionInfoList, regionInfo)
}

if cmd.Flags().Changed("node-config") {
nodeConfig, _ := cmd.Flags().GetStringToInt("node-config")
if _, ok := nodeConfig["num_cores"]; !ok {
fmt.Fprintf(os.Stderr, "Number of cores not specified in node config\n")
return
}
}

clusterSpec, clusterOK, errMsg := createClusterSpec(context.Background(), apiClient, cmd, accountID, regionInfoList)
if !clusterOK {
fmt.Fprintf(os.Stderr, "Error while creating cluster spec: %v\n", errMsg)
return
}

dbCredentials := ybmclient.NewCreateClusterRequestDbCredentials()
dbCredentials.Ycql = ybmclient.NewDBCredentials(username, password)
dbCredentials.Ysql = ybmclient.NewDBCredentials(username, password)

createClusterRequest := ybmclient.NewCreateClusterRequest(*clusterSpec, *dbCredentials)

resp, r, err := apiClient.ClusterApi.CreateCluster(context.Background(), accountID, projectID).CreateClusterRequest(*createClusterRequest).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `ClusterApi.CreateCluster``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
return
}
// response from `CreateCluster`: ClusterResponse
fmt.Fprintf(os.Stdout, "Response from `ClusterApi.CreateCluster`: %v\n", resp)
fmt.Fprintf(os.Stdout, "The cluster %v is being creted\n", clusterName)
},
}

func init() {
createCmd.AddCommand(createClusterCmd)

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

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

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
createClusterCmd.Flags().String("cluster-name", "", "Name of the cluster.")
createClusterCmd.MarkFlagRequired("cluster-name")
createClusterCmd.Flags().StringToString("credentials", nil, `Credentials to login to the cluster. Please provide key value pairs
username=<user-name>,password=<password>.`)
createClusterCmd.MarkFlagRequired("credentials")

createClusterCmd.Flags().String("cloud-type", "", "The cloud provider where database needs to be deployed. AWS or GCP.")
createClusterCmd.Flags().String("cluster-type", "", "Cluster replication type. SYNCHRONOUS or GEO_PARTITIONED.")
createClusterCmd.Flags().StringToInt("node-config", nil, "Configuration of the cluster nodes.")
createClusterCmd.Flags().StringToString("region-info", nil, `Region information for the cluster. Please provide key value pairs
region=<region-name>,num_nodes=<number-of-nodes>,vpc_id=<vpc-id> as the value. region and num_nodes are mandatory, vpc_id is optional.`)
createClusterCmd.Flags().String("cluster-tier", "", "The tier of the cluster. FREE or PAID.")
createClusterCmd.Flags().String("fault-tolerance", "", "The fault tolerance of the cluster. The possible values are NONE, ZONE and REGION.")
createClusterCmd.Flags().String("database-track", "", "The database track of the cluster. Stable or Preview.")

}
51 changes: 51 additions & 0 deletions cmd/delete_cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"context"
"fmt"
"os"

"github.com/spf13/cobra"
)

// deleteClusterCmd represents the cluster command
var deleteClusterCmd = &cobra.Command{
Use: "cluster",
Short: "Delete cluster in YugabyteDB Managed",
Long: "Delete cluster 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, _, _ := getClusterID(context.Background(), apiClient, accountID, projectID, clusterName)

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)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
return
}

fmt.Fprintf(os.Stdout, "The cluster %v is scheduled for deletion\n", clusterName)

},
}

func init() {
deleteCmd.AddCommand(deleteClusterCmd)

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

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

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
deleteClusterCmd.Flags().String("cluster-name", "", "The name of the cluster to be deleted")
deleteClusterCmd.MarkFlagRequired("cluster-name")
}
21 changes: 16 additions & 5 deletions cmd/cluster.go → cmd/get_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,29 @@ import (
"github.com/spf13/cobra"
)

// clustersCmd represents the clusters command
// getClusterCmd represents the cluster command
var getClusterCmd = &cobra.Command{
Use: "cluster",
Short: "Get clusters in YugabyteDB Managed",
Long: `Get clusters in YugabyteDB Managed`,
Long: "Get 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)
resp, r, err := apiClient.ClusterApi.ListClusters(context.Background(), accountID, projectID).Execute()
clusterListRequest := apiClient.ClusterApi.ListClusters(context.Background(), accountID, projectID)

// if user filters by name, add it to the request
clusterName, _ := cmd.Flags().GetString("cluster-name")
if clusterName != "" {
clusterListRequest = clusterListRequest.Name(clusterName)
}

resp, r, err := clusterListRequest.Execute()

if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `ClusterApi.ListClusters``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
return
}
// response from `ListClusters`: ClusterListResponse
prettyPrintJson(resp)
Expand All @@ -37,9 +47,10 @@ func init() {

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

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

import (
"fmt"

"github.com/spf13/cobra"
)

// updateCmd represents the create command
var updateCmd = &cobra.Command{
Use: "update",
Short: "Update a resource in YB Managed",
Long: "Update a resource in YB Managed",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("update called")
},
}

func init() {
rootCmd.AddCommand(updateCmd)

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

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

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// createCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
Loading