Skip to content

Commit

Permalink
improved help and secure bundle (#9)
Browse files Browse the repository at this point in the history
* converted all commands to cobra, this only added 2 megs to the deploy on linux and I think is worth it
* added secure bundle
* updating docs to match
  • Loading branch information
rsds143 authored Mar 12, 2021
1 parent 77179cb commit 80fe31e
Show file tree
Hide file tree
Showing 19 changed files with 1,057 additions and 500 deletions.
58 changes: 46 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,35 @@ Apache 2.0 licensed Astra Cloud Management CLI
* login
* execute commands on your database

### login
### login with token

After creating a token with rights to use the devops api

```
./bin/astra-cli login --token "changed"
Login information saved
```
### login service account

After creating a service account on the Astra page

```
./bin/astra-cli login -id "changed" -name "changed" -secret "changed"
./bin/astra-cli login --id "changed" --name "changed" --secret "changed"
Login information saved
```

## login with json
## login service account with json

```
./bin/astra-cli login -json '{"clientId":"changed","clientName":"[email protected]","clientSecret":"changed"}'
./bin/astra-cli login --json '{"clientId":"changed","clientName":"[email protected]","clientSecret":"changed"}'
Login information saved
```

### creating database

```
./bin/astra-cli db create -user dbuser -password test234 -keyspace myks -name mydb
./bin/astra-cli db create -v --keyspace myks --name mydb
2021/02/24 18:23:24 db 2c3bc0d6-5e3e-4d77-81c8-d95a35bdc58b in state PENDING but expected ACTIVE trying again 19 more times
2021/02/24 18:23:29 db 2c3bc0d6-5e3e-4d77-81c8-d95a35bdc58b in state PENDING but expected ACTIVE trying again 18 more times
2021/02/24 18:23:35 db 2c3bc0d6-5e3e-4d77-81c8-d95a35bdc58b in state PENDING but expected ACTIVE trying again 17 more times
Expand All @@ -61,6 +69,25 @@ Login information saved
database 2c3bc0d6-5e3e-4d77-81c8-d95a35bdc58b created
```

### get secure connection bundle

```
./bin/astra-cli db secBundle 3c577e51-4ff5-4551-86a4-41d475c61822
file secureBundle.zip saved 12072 bytes written
```

### get secure connection bundle url

```
./bin/astra-cli db secBundle 3c577e51-4ff5-4551-86a4-41d475c61822 -o json
{
"downloadURL": "changed",
"downloadURLInternal": "changed",
"downloadURLMigrationProxy": "changed",
"downloadURLMigrationProxyInternal": "changed"
}
```

### listing databases

```
Expand All @@ -72,7 +99,7 @@ mydb 2c3bc0d6-5e3e-4d77-81c8-d95a35bdc58b ACTIVE
### listing databases in json

```
./bin/astra-cli db list -format json
./bin/astra-cli db list -o json
[
{
"id": "2c3bc0d6-5e3e-4d77-81c8-d95a35bdc58b",
Expand Down Expand Up @@ -128,7 +155,7 @@ mydb 2c3bc0d6-5e3e-4d77-81c8-d95a35bdc58b ACTIVE
### getting database by id in json

```
./bin/astra-cli db get 2c3bc0d6-5e3e-4d77-81c8-d95a35bdc58b -format json
./bin/astra-cli db get 2c3bc0d6-5e3e-4d77-81c8-d95a35bdc58b -o json
json
{
"id": "2c3bc0d6-5e3e-4d77-81c8-d95a35bdc58b",
Expand Down Expand Up @@ -176,8 +203,10 @@ json

### parking database

Does not work on serverless

```
./bin/astra-cli db park 2c3bc0d6-5e3e-4d77-81c8-d95a35bdc58b
./bin/astra-cli db park -v 2c3bc0d6-5e3e-4d77-81c8-d95a35bdc58b
starting to park database 2c3bc0d6-5e3e-4d77-81c8-d95a35bdc58b
2021/02/24 18:31:26 db 2c3bc0d6-5e3e-4d77-81c8-d95a35bdc58b in state PARKING but expected PARKED trying again 29 more times
2021/02/24 18:31:56 db 2c3bc0d6-5e3e-4d77-81c8-d95a35bdc58b in state PARKING but expected PARKED trying again 28 more times
Expand All @@ -189,8 +218,10 @@ database 2c3bc0d6-5e3e-4d77-81c8-d95a35bdc58b parked

### unparking database

Does not work on serverless

```
./bin/astra-cli db unpark 2c3bc0d6-5e3e-4d77-81c8-d95a35bdc58b
./bin/astra-cli db unpark -v 2c3bc0d6-5e3e-4d77-81c8-d95a35bdc58b
starting to unpark database 2c3bc0d6-5e3e-4d77-81c8-d95a35bdc58b
2021/02/25 08:41:02 db 2c3bc0d6-5e3e-4d77-81c8-d95a35bdc58b in state UNPARKING but expected ACTIVE trying again 59 more times
2021/02/25 08:41:32 db 2c3bc0d6-5e3e-4d77-81c8-d95a35bdc58b in state UNPARKING but expected ACTIVE trying again 58 more times
Expand All @@ -211,10 +242,10 @@ starting to unpark database 2c3bc0d6-5e3e-4d77-81c8-d95a35bdc58b
database 2c3bc0d6-5e3e-4d77-81c8-d95a35bdc58b unparked
```

### deleteting database
### deleting database

```
./bin/astra-cli db delete 2c3bc0d6-5e3e-4d77-81c8-d95a35bdc58b
./bin/astra-cli db delete -v 2c3bc0d6-5e3e-4d77-81c8-d95a35bdc58b
starting to delete database 2c3bc0d6-5e3e-4d77-81c8-d95a35bdc58b
database 2c3bc0d6-5e3e-4d77-81c8-d95a35bdc58b deleted
```
Expand All @@ -224,5 +255,8 @@ database 2c3bc0d6-5e3e-4d77-81c8-d95a35bdc58b deleted
I did not have a paid account to verify this works, but you can see it succesfully starts the process

```
./bin/astra-cli db resize 72c4d35b-1875-495a-b5f1-97329d90b6c5 2
./bin/astra-cli db resize -v 72c4d35b-1875-495a-b5f1-97329d90b6c5 2
unable to unpark '72c4d35b-1875-495a-b5f1-97329d90b6c5' with error expected status code 2xx but had: 400 error was [map[ID:2.000009e+06 message:resizing is not supported for this database tier]]
```


119 changes: 33 additions & 86 deletions cmd/db.go
Original file line number Diff line number Diff line change
@@ -1,99 +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.
*/
// 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"
"os"

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

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")
func init() {
dbCmd.AddCommand(db.CreateCmd)
dbCmd.AddCommand(db.DeleteCmd)
dbCmd.AddCommand(db.ParkCmd)
dbCmd.AddCommand(db.UnparkCmd)
dbCmd.AddCommand(db.ResizeCmd)
dbCmd.AddCommand(db.GetCmd)
dbCmd.AddCommand(db.ListCmd)
dbCmd.AddCommand(db.TiersCmd)
dbCmd.AddCommand(db.SecBundleCmd)
}

// ExecuteDB launches several different subcommands and as of today is the main entry point
// into automation of Astra
func ExecuteDB(args []string, confFile pkg.ConfFiles, verbose bool) error {
hasToken, err := confFile.HasToken()
if err != nil {
return fmt.Errorf("unable to read conf file %v with error %v", confFile.TokenPath, err)
}
var client *astraops.AuthenticatedClient
if hasToken {
token, err := pkg.ReadToken(confFile.TokenPath)
if err != nil {
return fmt.Errorf("found token at %v but unable to read it with error %v", confFile.TokenPath, err)
}
client = astraops.AuthenticateToken(token, verbose)
} else {
hasSa, err := confFile.HasServiceAccount()
if err != nil {
return fmt.Errorf("unable to read conf file %v with error %v", confFile.SaPath, err)
}
if !hasSa {
return fmt.Errorf("unable to access any configuration, run astra-cli login first")
}
clientInfo, err := pkg.ReadLogin(confFile.SaPath)
if err != nil {
return fmt.Errorf("%v", err)
}
client, err = astraops.Authenticate(clientInfo, verbose)
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"),
var dbCmd = &cobra.Command{
Use: "db",
Short: "Shows all the db commands",
Long: `Shows all other db commands. Create, Delete, Get information on your databases`,
Run: func(cobraCmd *cobra.Command, args []string) {
if err := cobraCmd.Usage(); err != nil {
fmt.Fprintf(os.Stderr, "Unable to print usage with error %v\n", err)
}
}
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
},
}
77 changes: 48 additions & 29 deletions cmd/db/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,65 @@
package db

import (
"flag"
"fmt"

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

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", "serverless", "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")
var createDbName string
var createDbKeyspace string
var createDbUser string
var createDbPassword string
var createDbRegion string
var createDbTier string
var createDbCapacityUnit int
var createDbCloudProvider string

func init() {
CreateCmd.Flags().StringVarP(&createDbName, "name", "n", "", "name to give to the Astra Database")
CreateCmd.Flags().StringVarP(&createDbKeyspace, "keyspace", "k", "", "keyspace user to give to the Astra Database")
CreateCmd.Flags().StringVarP(&createDbUser, "user", "u", "", "user password to give to the Astra Database")
CreateCmd.Flags().StringVarP(&createDbPassword, "password", "p", "", "db password to give to the Astra Database")
CreateCmd.Flags().StringVarP(&createDbRegion, "region", "r", "us-east1", "region to give to the Astra Database")
CreateCmd.Flags().StringVarP(&createDbTier, "tier", "t", "serverless", "tier to give to the Astra Database")
CreateCmd.Flags().IntVarP(&createDbCapacityUnit, "capacityUnit", "c", 1, "capacityUnit flag to give to the Astra Database")
CreateCmd.Flags().StringVarP(&createDbCloudProvider, "cloudProvider", "l", "GCP", "cloud provider flag to give to the Astra Database")

// CreateUsage shows the help for the create command
func CreateUsage() string {
return pkg.PrintFlags(createCmd, "create", "creates a database by id")
}

// ExecuteCreate submits a new database to astra
func ExecuteCreate(args []string, client *astraops.AuthenticatedClient) error {
if err := createCmd.Parse(args); err != nil {
return &pkg.ParseError{
Args: args,
Err: err,
//CreateCmd creates a database in Astra
var CreateCmd = &cobra.Command{
Use: "create",
Short: "creates a database by id",
Long: ``,
Run: func(cobraCmd *cobra.Command, args []string) {
client, err := pkg.LoginClient()
if err != nil {
fmt.Fprintf(os.Stderr, "unable to login with error %v\n", err)
os.Exit(1)
}
}
capacity := int32(*createDbCapacityUnitFlag)

err = executeCreate(client)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
},
}

func executeCreate(client *astraops.AuthenticatedClient) error {
capacity := int32(createDbCapacityUnit)
createDb := astraops.CreateDb{
Name: *createDbNameFlag,
Keyspace: *createDbKeyspaceFlag,
Name: createDbName,
Keyspace: createDbKeyspace,
CapacityUnits: capacity,
Region: *createDbRegionFlag,
User: *createDbUserFlag,
Password: *createDbPasswordFlag,
Tier: *createDbTierFlag,
CloudProvider: *createDbCloudProviderFlag,
Region: createDbRegion,
User: createDbUser,
Password: createDbPassword,
Tier: createDbTier,
CloudProvider: createDbCloudProvider,
}
db, err := client.CreateDb(createDb)
if err != nil {
Expand Down
Loading

0 comments on commit 80fe31e

Please sign in to comment.