Skip to content

Commit

Permalink
astra-client-go devops api (#30)
Browse files Browse the repository at this point in the history
* removed deprecated go devops api
* new framework using swagger API  https://github.com/datastax/astra-client-go/
  • Loading branch information
Ryan SVIHLA authored Jan 10, 2022
1 parent 402fa7a commit 601bcac
Show file tree
Hide file tree
Showing 20 changed files with 805 additions and 163 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ bin/
.DS_Store

dist/

.vagrant

*.swp
Vagrantfile
18 changes: 8 additions & 10 deletions cmd/db/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"fmt"
"os"

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

Expand Down Expand Up @@ -64,22 +64,20 @@ func executeCreate(makeClient func() (pkg.Client, error)) error {
if err != nil {
return fmt.Errorf("unable to login with error %v", err)
}
capacity := int32(createDbCapacityUnit)
createDb := astraops.CreateDb{
createDb := astraops.DatabaseInfoCreate{
Name: createDbName,
Keyspace: createDbKeyspace,
CapacityUnits: capacity,
CapacityUnits: createDbCapacityUnit,
Region: createDbRegion,
User: createDbUser,
Password: createDbPassword,
Tier: createDbTier,
CloudProvider: createDbCloudProvider,
User: &createDbUser,
Password: &createDbPassword,
Tier: astraops.Tier(createDbTier),
CloudProvider: astraops.CloudProvider(createDbCloudProvider),
}
db, err := client.CreateDb(createDb)
if err != nil {
return fmt.Errorf("unable to create '%v' with error %v", createDb, err)
}
id := db.ID
fmt.Printf("database %v created\n", id)
fmt.Printf("database %v created\n", db.Id)
return nil
}
39 changes: 21 additions & 18 deletions cmd/db/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (
"fmt"
"testing"

astraops "github.com/datastax/astra-client-go/v2/astra"
"github.com/rsds143/astra-cli/pkg"
tests "github.com/rsds143/astra-cli/pkg/tests"
"github.com/rsds143/astra-devops-sdk-go/astraops"
)

func TestCreateGetsId(t *testing.T) {
Expand All @@ -30,7 +30,7 @@ func TestCreateGetsId(t *testing.T) {
mockClient := &tests.MockClient{
Databases: []astraops.Database{
{
ID: expectedID,
Id: expectedID,
},
},
}
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestCreateSetsName(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error '%v'", err)
}
arg0 := mockClient.Call(0).(astraops.CreateDb)
arg0 := mockClient.Call(0).(astraops.DatabaseInfoCreate)
if arg0.Name != createDbName {
t.Errorf("expected '%v' but was '%v'", arg0.Name, createDbName)
}
Expand All @@ -105,7 +105,7 @@ func TestCreateSetsKeyspace(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error '%v'", err)
}
arg0 := mockClient.Call(0).(astraops.CreateDb)
arg0 := mockClient.Call(0).(astraops.DatabaseInfoCreate)
if arg0.Keyspace != createDbKeyspace {
t.Errorf("expected '%v' but was '%v'", arg0.Keyspace, createDbKeyspace)
}
Expand All @@ -120,8 +120,8 @@ func TestCreateSetsCapacityUnit(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error '%v'", err)
}
arg0 := mockClient.Call(0).(astraops.CreateDb)
if arg0.CapacityUnits != int32(createDbCapacityUnit) {
arg0 := mockClient.Call(0).(astraops.DatabaseInfoCreate)
if arg0.CapacityUnits != createDbCapacityUnit {
t.Errorf("expected '%v' but was '%v'", arg0.CapacityUnits, createDbCapacityUnit)
}
}
Expand All @@ -135,39 +135,42 @@ func TestCreateSetsRegion(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error '%v'", err)
}
arg0 := mockClient.Call(0).(astraops.CreateDb)
arg0 := mockClient.Call(0).(astraops.DatabaseInfoCreate)
if arg0.Region != createDbRegion {
t.Errorf("expected '%v' but was '%v'", arg0.Region, createDbRegion)
}
}

func TestCreateSetsUser(t *testing.T) {
mockClient := &tests.MockClient{}
// sets createDbUser on the package variable in cmd/db/create.go
createDbUser = "[email protected]"
err := executeCreate(func() (pkg.Client, error) {
return mockClient, nil
})
if err != nil {
t.Fatalf("unexpected error '%v'", err)
}
arg0 := mockClient.Call(0).(astraops.CreateDb)
if arg0.User != createDbUser {
t.Errorf("expected '%v' but was '%v'", arg0.User, createDbUser)
arg0 := mockClient.Call(0).(astraops.DatabaseInfoCreate)
if arg0.User != &createDbUser {
user := *arg0.User
t.Errorf("expected '%v' but was '%v'", user, createDbUser)
}
}

func TestCreateSetsPass(t *testing.T) {
mockClient := &tests.MockClient{}
createDbUser = "afdfdf"
// sets createDbPassword on the package variable in cmd/db/create.go
createDbPassword = "afdfdf"
err := executeCreate(func() (pkg.Client, error) {
return mockClient, nil
})
if err != nil {
t.Fatalf("unexpected error '%v'", err)
}
arg0 := mockClient.Call(0).(astraops.CreateDb)
if arg0.Password != createDbPassword {
t.Errorf("expected '%v' but was '%v'", arg0.Password, createDbPassword)
arg0 := mockClient.Call(0).(astraops.DatabaseInfoCreate)
if arg0.Password != &createDbPassword {
t.Errorf("expected '%v' but was '%v'", *arg0.Password, createDbPassword)
}
}

Expand All @@ -180,8 +183,8 @@ func TestCreateSetsTier(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error '%v'", err)
}
arg0 := mockClient.Call(0).(astraops.CreateDb)
if arg0.Tier != createDbTier {
arg0 := mockClient.Call(0).(astraops.DatabaseInfoCreate)
if arg0.Tier != astraops.Tier(createDbTier) {
t.Errorf("expected '%v' but was '%v'", arg0.Tier, createDbTier)
}
}
Expand All @@ -195,8 +198,8 @@ func TestCreateSetsProvider(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error '%v'", err)
}
arg0 := mockClient.Call(0).(astraops.CreateDb)
if arg0.CloudProvider != createDbCloudProvider {
arg0 := mockClient.Call(0).(astraops.DatabaseInfoCreate)
if arg0.CloudProvider != astraops.CloudProvider(createDbCloudProvider) {
t.Errorf("expected '%v' but was '%v'", arg0.CloudProvider, createDbCloudProvider)
}
}
5 changes: 3 additions & 2 deletions cmd/db/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import (
"fmt"
"os"

astraops "github.com/datastax/astra-client-go/v2/astra"
"github.com/rsds143/astra-cli/pkg"
"github.com/rsds143/astra-devops-sdk-go/astraops"

"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -63,7 +64,7 @@ func executeGet(args []string, login func() (pkg.Client, error)) (string, error)
case pkg.TextFormat:
var rows [][]string
rows = append(rows, []string{"name", "id", "status"})
rows = append(rows, []string{db.Info.Name, db.ID, string(db.Status)})
rows = append(rows, []string{*db.Info.Name, db.Id, string(db.Status)})
var buf bytes.Buffer
err = pkg.WriteRows(&buf, rows)
if err != nil {
Expand Down
22 changes: 11 additions & 11 deletions cmd/db/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ import (
"strings"
"testing"

astraops "github.com/datastax/astra-client-go/v2/astra"
"github.com/rsds143/astra-cli/pkg"
tests "github.com/rsds143/astra-cli/pkg/tests"
"github.com/rsds143/astra-devops-sdk-go/astraops"
)

func TestGet(t *testing.T) {
getFmt = pkg.JSONFormat
dbs := []astraops.Database{
{ID: "1"},
{ID: "2"},
{Id: "1"},
{Id: "2"},
}
jsonTxt, err := executeGet([]string{"1"}, func() (pkg.Client, error) {
return &tests.MockClient{
Expand All @@ -45,8 +45,8 @@ func TestGet(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error with json %v with text %v", err, jsonTxt)
}
if fromServer.ID != dbs[0].ID {
t.Errorf("expected '%v' but was '%v'", dbs[0].ID, fromServer.ID)
if fromServer.Id != dbs[0].Id {
t.Errorf("expected '%v' but was '%v'", dbs[0].Id, fromServer.Id)
}
}

Expand Down Expand Up @@ -96,18 +96,18 @@ func TestGetText(t *testing.T) {
getFmt = pkg.TextFormat
dbs := []astraops.Database{
{
ID: "1",
Id: "1",
Info: astraops.DatabaseInfo{
Name: "A",
Name: astraops.StringPtr("A"),
},
Status: astraops.ACTIVE,
Status: astraops.StatusEnumACTIVE,
},
{
ID: "2",
Id: "2",
Info: astraops.DatabaseInfo{
Name: "B",
Name: astraops.StringPtr("B"),
},
Status: astraops.TERMINATING,
Status: astraops.StatusEnumTERMINATING,
},
}
txt, err := executeGet([]string{"1"}, func() (pkg.Client, error) {
Expand Down
5 changes: 3 additions & 2 deletions cmd/db/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import (
"fmt"
"os"

astraops "github.com/datastax/astra-client-go/v2/astra"
"github.com/rsds143/astra-cli/pkg"
"github.com/rsds143/astra-devops-sdk-go/astraops"

"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -71,7 +72,7 @@ func executeList(login func() (pkg.Client, error)) (string, error) {
var rows [][]string
rows = append(rows, []string{"name", "id", "status"})
for _, db := range dbs {
rows = append(rows, []string{db.Info.Name, db.ID, string(db.Status)})
rows = append(rows, []string{*db.Info.Name, db.Id, string(db.Status)})
}
var out bytes.Buffer
err = pkg.WriteRows(&out, rows)
Expand Down
26 changes: 13 additions & 13 deletions cmd/db/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ import (
"strings"
"testing"

astraops "github.com/datastax/astra-client-go/v2/astra"
"github.com/rsds143/astra-cli/pkg"
tests "github.com/rsds143/astra-cli/pkg/tests"
"github.com/rsds143/astra-devops-sdk-go/astraops"
)

func TestList(t *testing.T) {
listFmt = pkg.JSONFormat
dbs := []astraops.Database{
{ID: "1"},
{ID: "2"},
{Id: "1"},
{Id: "2"},
}
jsonTxt, err := executeList(func() (pkg.Client, error) {
return &tests.MockClient{
Expand All @@ -48,30 +48,30 @@ func TestList(t *testing.T) {
if len(fromServer) != len(dbs) {
t.Errorf("expected '%v' but was '%v'", len(dbs), len(fromServer))
}
if fromServer[0].ID != dbs[0].ID {
t.Errorf("expected '%v' but was '%v'", dbs[0].ID, fromServer[0].ID)
if fromServer[0].Id != dbs[0].Id {
t.Errorf("expected '%v' but was '%v'", dbs[0].Id, fromServer[0].Id)
}
if fromServer[1].ID != dbs[1].ID {
t.Errorf("expected '%v' but was '%v'", dbs[1].ID, fromServer[1].ID)
if fromServer[1].Id != dbs[1].Id {
t.Errorf("expected '%v' but was '%v'", dbs[1].Id, fromServer[1].Id)
}
}

func TestListText(t *testing.T) {
listFmt = pkg.TextFormat
dbs := []astraops.Database{
{
ID: "1",
Id: "1",
Info: astraops.DatabaseInfo{
Name: "A",
Name: astraops.StringPtr("A"),
},
Status: astraops.ACTIVE,
Status: astraops.StatusEnumACTIVE,
},
{
ID: "2",
Id: "2",
Info: astraops.DatabaseInfo{
Name: "B",
Name: astraops.StringPtr("B"),
},
Status: astraops.TERMINATING,
Status: astraops.StatusEnumTERMINATING,
},
}
txt, err := executeList(func() (pkg.Client, error) {
Expand Down
13 changes: 7 additions & 6 deletions cmd/db/secBundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import (
"fmt"
"os"

astraops "github.com/datastax/astra-client-go/v2/astra"
"github.com/rsds143/astra-cli/pkg"
"github.com/rsds143/astra-cli/pkg/httputils"
"github.com/rsds143/astra-devops-sdk-go/astraops"

"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -59,7 +60,7 @@ func executeSecBundle(args []string, login func() (pkg.Client, error)) (string,
return "", fmt.Errorf("unable to login with error %v", err)
}
id := args[0]
var secBundle astraops.SecureBundle
var secBundle astraops.CredsURL
if secBundle, err = client.GetSecureBundle(id); err != nil {
return "", fmt.Errorf("unable to get '%s' with error %v", id, err)
}
Expand All @@ -70,11 +71,11 @@ func executeSecBundle(args []string, login func() (pkg.Client, error)) (string,
case "external":
urlToDownload = secBundle.DownloadURL
case "internal":
urlToDownload = secBundle.DownloadURLInternal
urlToDownload = *secBundle.DownloadURLInternal
case "proxy-external":
urlToDownload = secBundle.DownloadURLMigrationProxy
urlToDownload = *secBundle.DownloadURLMigrationProxy
case "proxy-internal":
urlToDownload = secBundle.DownloadURLMigrationProxyInternal
urlToDownload = *secBundle.DownloadURLMigrationProxyInternal
default:
return "", fmt.Errorf("invalid download type %s passed. valid options are 'external', 'internal', 'proxy-external', 'proxy-internal'", secBundleDownloadType)
}
Expand All @@ -95,7 +96,7 @@ func executeSecBundle(args []string, login func() (pkg.Client, error)) (string,
internal bundle: %s
external proxy: %s
internal proxy: %s
`, secBundle.DownloadURL, secBundle.DownloadURLInternal, secBundle.DownloadURLMigrationProxy, secBundle.DownloadURLMigrationProxyInternal), nil
`, secBundle.DownloadURL, *secBundle.DownloadURLInternal, *secBundle.DownloadURLMigrationProxy, *secBundle.DownloadURLMigrationProxyInternal), nil
default:
return "", fmt.Errorf("-o %q is not valid option", secBundleFmt)
}
Expand Down
Loading

0 comments on commit 601bcac

Please sign in to comment.