Skip to content

Commit

Permalink
big refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Svihla committed Feb 24, 2021
1 parent f34e505 commit 6a22111
Show file tree
Hide file tree
Showing 14 changed files with 824 additions and 323 deletions.
79 changes: 79 additions & 0 deletions cmd/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
Copyright 2021 Ryan Svihla
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"fmt"
"strings"

"github.com/rsds143/astra-cli/cmd/db"
"github.com/rsds143/astra-cli/pkg"
"github.com/rsds143/astra-devops-sdk-go/astraops"
)

func DBUsage() string {
return strings.Join([]string{
"\tastra-cli db <subcommands>",
"\tdb subcommands:",
fmt.Sprintf("\t%v", db.CreateUsage()),
fmt.Sprintf("\t%v", db.DeleteUsage()),
fmt.Sprintf("\t%v", db.GetUsage()),
fmt.Sprintf("\t%v", db.ListUsage()),
fmt.Sprintf("\t%v", db.ParkUsage()),
fmt.Sprintf("\t%v", db.UnparkUsage()),
fmt.Sprintf("\t%v", db.ResizeUsage()),
fmt.Sprintf("\t%v", db.TiersUsage()),
}, "\n")
}

// ExecuteDB launches several different subcommands and as of today is the main entry point
// into automation of Astra
func ExecuteDB(args []string, confFile string) error {
clientInfo, err := pkg.ReadLogin(confFile)
if err != nil {
return fmt.Errorf("%v", err)
}
client, err := astraops.Authenticate(clientInfo.ClientName, clientInfo.ClientID, clientInfo.ClientSecret)
if err != nil {
return fmt.Errorf("authenticate failed with error %v", err)
}
if len(args) == 0 {
return &pkg.ParseError{
Args: args,
Err: fmt.Errorf("there is no standalone db command"),
}
}
switch args[0] {
case "create":
return db.ExecuteCreate(args[1:], client)
case "delete":
return db.ExecuteDelete(args[1:], client)
case "park":
return db.ExecutePark(args[1:], client)
case "unpark":
return db.ExecuteUnpark(args[1:], client)
case "resize":
return db.ExecuteResize(args[1:], client)
case "get":
return db.ExecuteGet(args[1:], client)
case "list":
return db.ExecuteList(args[1:], client)
case "tiers":
return db.ExecuteTiers(args[1:], client)
}
return nil
}
71 changes: 71 additions & 0 deletions cmd/db/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
Copyright 2021 Ryan Svihla
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package db

import (
"flag"
"fmt"
"strings"

"github.com/rsds143/astra-cli/pkg"
"github.com/rsds143/astra-devops-sdk-go/astraops"
)

var createCmd = flag.NewFlagSet("create", flag.ExitOnError)
var createDbNameFlag = createCmd.String("name", "", "name to give to the Astra Database")
var createDbKeyspaceFlag = createCmd.String("keyspace", "", "keyspace user to give to the Astra Database")
var createDbUserFlag = createCmd.String("user", "", "user password to give to the Astra Database")
var createDbPasswordFlag = createCmd.String("password", "", "db password to give to the Astra Database")
var createDbRegionFlag = createCmd.String("region", "us-east1", "region to give to the Astra Database")
var createDbTierFlag = createCmd.String("tier", "free", "tier to give to the Astra Database")
var createDbCapacityUnitFlag = createCmd.Int("capacityUnit", 1, "capacityUnit flag to give to the Astra Database")
var createDbCloudProviderFlag = createCmd.String("cloudProvider", "GCP", "cloud provider flag to give to the Astra Database")

// CreateUsage shows the help for the create command
func CreateUsage() string {
var out strings.Builder
out.WriteString("\tcreate\n")
createCmd.VisitAll(func(f *flag.Flag) {
out.WriteString(fmt.Sprintf("\t\t%v\n", f.Usage))
})
return out.String()
}

// ExecuteCreate submits a new database to astra
func ExecuteCreate(args []string, client *astraops.AuthenticatedClient) error {
if err := createCmd.Parse(args[1:]); err != nil {
return &pkg.ParseError{
Args: args,
Err: err,
}
}
createDb := astraops.CreateDb{
Name: *createDbNameFlag,
Keyspace: *createDbKeyspaceFlag,
CapacityUnits: *createDbCapacityUnitFlag,
Region: *createDbRegionFlag,
User: *createDbUserFlag,
Password: *createDbPasswordFlag,
Tier: *createDbTierFlag,
CloudProvider: *createDbCloudProviderFlag,
}
id, _, err := client.CreateDb(createDb)
if err != nil {
return fmt.Errorf("unable to create '%v' with error %v", createDb, err)
}
fmt.Printf("database %v created\n", id)
return nil
}
46 changes: 46 additions & 0 deletions cmd/db/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
Copyright 2021 Ryan Svihla
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package db

import (
"fmt"

"github.com/rsds143/astra-cli/pkg"
"github.com/rsds143/astra-devops-sdk-go/astraops"
)

// DeleteUsage shows the help for the delete command
func DeleteUsage() string {
return "\tdelete <id>\n\t\tdeletes a database by id\n"
}

// ExecuteDelete removes the database with the specified ID. If no ID is provided
// the command will error out
func ExecuteDelete(args []string, client *astraops.AuthenticatedClient) error {
if len(args) == 0 {
return &pkg.ParseError{
Args: args,
Err: fmt.Errorf("there is no id provided for deleting the database"),
}
}
id := args[1]
fmt.Printf("starting to delete database %v\n", id)
if err := client.Terminate(id, false); err != nil {
return fmt.Errorf("unable to delete '%s' with error %v", id, err)
}
fmt.Printf("database %v deleted\n", id)
return nil
}
75 changes: 75 additions & 0 deletions cmd/db/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
Copyright 2021 Ryan Svihla
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package db

import (
"encoding/json"
"flag"
"fmt"
"strings"

"github.com/rsds143/astra-cli/pkg"
"github.com/rsds143/astra-devops-sdk-go/astraops"
)

var getCmd = flag.NewFlagSet("get", flag.ExitOnError)
var getFmt = getCmd.String("format", "text", "Output format for report default is json")

// GetUsage shows the help for the get command
func GetUsage() string {
var out strings.Builder
out.WriteString("\tget <id>\n")
getCmd.VisitAll(func(f *flag.Flag) {
out.WriteString(fmt.Sprintf("\t\t%v\n", f.Usage))
})
return out.String()
}

// ExecuteGet get the database with the specified ID. If no ID is provided
// the command will error out
func ExecuteGet(args []string, client *astraops.AuthenticatedClient) error {
if len(args) == 0 {
return &pkg.ParseError{
Args: args,
Err: fmt.Errorf("there is no id provided for parking the database"),
}
}

id := args[1]
var db astraops.DataBase
var err error
if db, err = client.FindDb(id); err != nil {
return fmt.Errorf("unable to get '%s' with error %v\n", id, err)
}
switch *getFmt {
case "text":
var rows [][]string
rows = append(rows, []string{"name", "id", "status"})
rows = append(rows, []string{db.Info.Name, db.ID, db.Status})
for _, row := range pkg.PadColumns(rows) {
fmt.Println(strings.Join(row, " "))
}
case "json":
b, err := json.MarshalIndent(db, "", " ")
if err != nil {
return fmt.Errorf("unexpected error marshaling to json: '%v', Try -format text instead", err)
}
fmt.Println(string(b))
default:
return fmt.Errorf("-format %q is not valid option.", *getFmt)
}
return nil
}
79 changes: 79 additions & 0 deletions cmd/db/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
Copyright 2021 Ryan Svihla
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package db

import (
"encoding/json"
"flag"
"fmt"
"strings"

"github.com/rsds143/astra-cli/pkg"
"github.com/rsds143/astra-devops-sdk-go/astraops"
)

var listCmd = flag.NewFlagSet("list", flag.ExitOnError)
var limitFlag = listCmd.Int("limit", 10, "limit of databases retrieved")
var includeFlag = listCmd.String("include", "", "the type of filter to apply")
var providerFlag = listCmd.String("provider", "", "provider to filter by")
var startingAfterFlag = listCmd.String("startingAfter", "", "timestamp filter, ie only show databases created after this timestamp")
var listFmt = listCmd.String("format", "text", "Output format for report default is json")

// ListUsage shows the help for the List command
func ListUsage() string {
var out strings.Builder
out.WriteString("\tlist\n")
listCmd.VisitAll(func(f *flag.Flag) {
out.WriteString(fmt.Sprintf("\t\t%v\n", f.Usage))
})
return out.String()
}

// ExecuteList lists databases in astra
func ExecuteList(args []string, client *astraops.AuthenticatedClient) error {
if err := listCmd.Parse(args); err != nil {
return &pkg.ParseError{
Args: args,
Err: err,
}
}
var dbs []astraops.DataBase
var err error
if dbs, err = client.ListDb(*includeFlag, *providerFlag, *startingAfterFlag, int32(*limitFlag)); err != nil {
return fmt.Errorf("unable to get list of dbs with error %v", err)
}
switch *listFmt {
case "text":
var rows [][]string
rows = append(rows, []string{"name", "id", "status"})
for _, db := range dbs {
rows = append(rows, []string{db.Info.Name, db.ID, db.Status})
}
for _, row := range pkg.PadColumns(rows) {
fmt.Println(strings.Join(row, " "))
}
case "json":
b, err := json.MarshalIndent(dbs, "", " ")
if err != nil {
return fmt.Errorf("unexpected error marshaling to json: '%v', Try -format text instead", err)
}
fmt.Println(string(b))
default:
return fmt.Errorf("-format %q is not valid option", *getFmt)
}
return nil
}
Loading

0 comments on commit 6a22111

Please sign in to comment.