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

Minor improvements to DBaaS commands #295

Merged
merged 3 commits into from
Jan 16, 2023
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
5 changes: 2 additions & 3 deletions cmd/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@ func init() {

dbCredentialCmd.Flags().BoolVarP(&connectionString, "connection-string", "c", false, "show the connection string for the database")

dbCreateCmd.Flags().IntVarP(&nodes, "nodes", "", 0, "the number of nodes for the database")
dbCreateCmd.Flags().IntVarP(&nodes, "nodes", "", 1, "the number of nodes for the database")
dbCreateCmd.Flags().StringVarP(&firewallID, "firewall", "", "", "the firewall to use for the database")
dbCreateCmd.Flags().StringVarP(&networkID, "network", "n", "", "the network to use for the database")
dbCreateCmd.Flags().StringVarP(&rulesFirewall, "firewall-rules", "u", "", "the firewall rules to use for the database")
dbCreateCmd.Flags().StringVarP(&size, "size", "s", "", "the size of the database")
dbCreateCmd.MarkFlagRequired("size")
dbCreateCmd.Flags().StringVarP(&size, "size", "s", "g3.db.small", "the size of the database. You can list available DB sizes by `civo size list -s database`")

dbUpdateCmd.Flags().IntVarP(&nodes, "nodes", "", 0, "the number of nodes for the database")
dbUpdateCmd.Flags().StringVarP(&firewallID, "firewall", "", "", "the firewall to use for the database")
Expand Down
10 changes: 1 addition & 9 deletions cmd/database/database_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ var dbCreateCmd = &cobra.Command{
}
}

if nodes == 0 {
nodes = 1
} else if nodes < 0 {
utility.Error("Nodes can't be negative")
os.Exit(1)
}

if firewallID != "" {
_, err = client.FindFirewall(firewallID)
if err != nil {
Expand All @@ -69,8 +62,7 @@ var dbCreateCmd = &cobra.Command{
sizeIsValid := false
if size != "" {
for _, s := range sizes {
if s.Name == size {
size = s.Name
if s.Name == size && utility.SizeType(s.Name) == "Database" {
sizeIsValid = true
break
}
Expand Down
6 changes: 4 additions & 2 deletions cmd/database/database_list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package database

import (
"fmt"
"os"
"strconv"

Expand Down Expand Up @@ -37,13 +38,13 @@ var dbListCmd = &cobra.Command{
ow := utility.NewOutputWriter()
for _, db := range databases.Items {
ow.StartLine()
ow.AppendDataWithLabel("id", db.ID, "ID")
ow.AppendDataWithLabel("id", utility.TrimID(db.ID), "ID")
ow.AppendDataWithLabel("name", db.Name, "Name")
ow.AppendDataWithLabel("size", db.Size, "Size")
ow.AppendDataWithLabel("nodes", strconv.Itoa(db.Nodes), "Nodes")
ow.AppendDataWithLabel("software", db.Software, "Software")
ow.AppendDataWithLabel("software_version", db.SoftwareVersion, "Software Version")
ow.AppendDataWithLabel("public_ip", db.PublicIPv4, "Public IPv4")
ow.AppendDataWithLabel("host", fmt.Sprintf("%s:%d", db.PublicIPv4, db.Port), "Host")
ow.AppendDataWithLabel("status", db.Status, "Status")

if common.OutputFormat == "json" || common.OutputFormat == "custom" {
Expand All @@ -61,6 +62,7 @@ var dbListCmd = &cobra.Command{
ow.WriteCustomOutput(common.OutputFields)
default:
ow.WriteTable()
fmt.Println("To get the credentials for a database, use `civo db credential <name/ID>`")
}
},
}
3 changes: 2 additions & 1 deletion cmd/database/database_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var dbShowCmd = &cobra.Command{
ow.AppendDataWithLabel("nodes", strconv.Itoa(db.Nodes), "Nodes")
ow.AppendDataWithLabel("software", db.Software, "Software")
ow.AppendDataWithLabel("software_version", db.SoftwareVersion, "Software Version")
ow.AppendDataWithLabel("public_ip", db.PublicIPv4, "Public IPv4")
ow.AppendDataWithLabel("host", fmt.Sprintf("%s:%d", db.PublicIPv4, db.Port), "Host")

if common.OutputFormat == "json" || common.OutputFormat == "custom" {
ow.AppendDataWithLabel("firewall_id", db.FirewallID, "Firewall ID")
Expand All @@ -64,6 +64,7 @@ var dbShowCmd = &cobra.Command{
ow.WriteCustomOutput(common.OutputFields)
default:
ow.WriteKeyValues()
fmt.Println("To get the credentials, run : civo db credential", db.Name)
}
},
}
2 changes: 1 addition & 1 deletion cmd/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func init() {
kubernetesConfigCmd.Flags().BoolVarP(&overwriteConfig, "overwrite", "w", false, "overwrite the kubeconfig file")
kubernetesConfigCmd.Flags().StringVarP(&localPathConfig, "local-path", "p", fmt.Sprintf("%s/.kube/config", home), "local path to save the kubeconfig file")

kubernetesCreateCmd.Flags().StringVarP(&targetNodesSize, "size", "s", "g4s.kube.medium", "the size of nodes to create.")
kubernetesCreateCmd.Flags().StringVarP(&targetNodesSize, "size", "s", "g4s.kube.medium", "the size of nodes to create. You can list available kubernetes sizes by `civo size list -s kubernetes`")
kubernetesCreateCmd.Flags().StringVarP(&networkID, "network", "t", "default", "the name of the network to use in the creation")
kubernetesCreateCmd.Flags().IntVarP(&numTargetNodes, "nodes", "n", 3, "the number of nodes to create (the master also acts as a node).")
kubernetesCreateCmd.Flags().StringVarP(&kubernetesVersion, "version", "v", "latest", "the k3s version to use on the cluster. Defaults to the latest. Example - 'civo k3s create --version 1.21.2+k3s1'")
Expand Down
2 changes: 1 addition & 1 deletion cmd/size/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ var SizeCmd = &cobra.Command{

func init() {
SizeCmd.AddCommand(sizeListCmd)
sizeListCmd.Flags().StringVarP(&filterSize, "filter", "s", "", "filter the result by the type (kubernest, database, inatance)")
sizeListCmd.Flags().StringVarP(&filterSize, "filter", "s", "", "filter the result by the type (kubernetes, database, instance)")
}
10 changes: 6 additions & 4 deletions cmd/size/size_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Example: civo size ls -o custom -f "Code: name (type)"`,
}

filter := []civogo.InstanceSize{}
// TODO: Rename ListInstanceSizes to ListSizes
// TODO: Add a filter in civogo to filter by type
sizes, err := client.ListInstanceSizes()
if err != nil {
utility.Error("%s", err)
Expand All @@ -52,11 +54,11 @@ Example: civo size ls -o custom -f "Code: name (type)"`,
search := ""

switch {
case filterSize == "database" || filterSize == "Database":
case strings.ToLower(filterSize) == "database" || strings.ToLower(filterSize) == "db":
search = ".db."
case filterSize == "kubernetes" || filterSize == "Kubernetes":
case strings.ToLower(filterSize) == "kubernetes" || strings.ToLower(filterSize) == "k8s" || strings.ToLower(filterSize) == "k3s" || strings.ToLower(filterSize) == "kube":
search = ".kube."
case filterSize == "instance" || filterSize == "Instance":
case strings.ToLower(filterSize) == "instance" || strings.ToLower(filterSize) == "iaas" || strings.ToLower(filterSize) == "vm":
search = "iaas"
}

Expand Down Expand Up @@ -84,7 +86,7 @@ Example: civo size ls -o custom -f "Code: name (type)"`,
ow.StartLine()
ow.AppendDataWithLabel("name", size.Name, "Name")
ow.AppendDataWithLabel("description", size.Description, "Description")
ow.AppendDataWithLabel("type", utility.CheckSize(size.Name), "Type")
ow.AppendDataWithLabel("type", utility.SizeType(size.Name), "Type")
ow.AppendDataWithLabel("cpu_cores", strconv.Itoa(size.CPUCores), "CPU")
ow.AppendDataWithLabel("ram_mb", strconv.Itoa(size.RAMMegabytes), "RAM")
ow.AppendDataWithLabel("disk_gb", strconv.Itoa(size.DiskGigabytes), "SSD")
Expand Down
13 changes: 11 additions & 2 deletions utility/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,17 @@ func UpdateNodePool(s []civogo.KubernetesClusterPoolConfig, id string, count int
return s
}

// CheckSize is a utility function to check thesize an return the size in the right format
func CheckSize(size string) string {
// TrimID is a utility function to trim the ID to 6 characters
func TrimID(id string) string {
if len(id) > 6 {
return id[:6]
}
return id
}

// SizeType is a utility function to to return the type of the size given it's name.
// Possible types(case-sensitive) : Instance, Database, Kubernetes
func SizeType(size string) string {
switch {
case strings.Contains(size, ".db."):
return "Database"
Expand Down