Skip to content
This repository was archived by the owner on Dec 18, 2025. It is now read-only.

Commit 8c8964f

Browse files
author
Ryan SVIHLA
committed
support for environments
1 parent cd31870 commit 8c8964f

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ import (
1919
"fmt"
2020
"os"
2121

22+
"github.com/rsds143/astra-cli/pkg"
2223
"github.com/rsds143/astra-cli/pkg/env"
2324
"github.com/spf13/cobra"
2425
)
2526

2627
func init() {
2728
RootCmd.PersistentFlags().BoolVarP(&env.Verbose, "verbose", "v", false, "turns on verbose logging")
29+
RootCmd.PersistentFlags().StringVarP(&pkg.Env, "env", "e", "prod", "environment to automate, other options are test and dev")
2830
RootCmd.AddCommand(loginCmd)
2931
RootCmd.AddCommand(dbCmd)
3032
}

pkg/authenticated_client.go

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"time"
1414

1515
"github.com/datastax/astra-client-go/v2/astra"
16+
"github.com/rsds143/astra-cli/pkg/env"
1617
)
1718

1819
// Error when the api has an error this is the structure
@@ -95,7 +96,7 @@ func timeoutContext(timeSeconds int) (context.Context, context.CancelFunc) {
9596
}
9697

9798
func AuthenticateToken(token string, verbose bool) (*AuthenticatedClient, error) {
98-
astraClient, err := astra.NewClientWithResponses(apiURL, func(c *astra.Client) error {
99+
astraClient, err := astra.NewClientWithResponses(apiURL(), func(c *astra.Client) error {
99100
c.RequestEditors = append(c.RequestEditors, func(ctx context.Context, req *http.Request) error {
100101
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
101102
return nil
@@ -123,7 +124,7 @@ func Authenticate(clientInfo ClientInfo, verbose bool) (*AuthenticatedClient, er
123124
ClientName: clientInfo.ClientName,
124125
ClientSecret: clientInfo.ClientSecret,
125126
}
126-
astraClientTmp, err := astra.NewClientWithResponses(apiURL)
127+
astraClientTmp, err := astra.NewClientWithResponses(apiURL())
127128
if err != nil {
128129
return &AuthenticatedClient{}, fmt.Errorf("unexpected error setting up devops api client: %v", err)
129130
}
@@ -137,7 +138,7 @@ func Authenticate(clientInfo ClientInfo, verbose bool) (*AuthenticatedClient, er
137138
return &AuthenticatedClient{}, fmt.Errorf("unexpected error logging into devops api client: %v - %v", response.StatusCode(), response.Status())
138139
}
139140
token := response.JSON200.Token
140-
astraClient, err := astra.NewClientWithResponses(apiURL, func(c *astra.Client) error {
141+
astraClient, err := astra.NewClientWithResponses(apiURL(), func(c *astra.Client) error {
141142
c.RequestEditors = append(c.RequestEditors, func(ctx context.Context, req *http.Request) error {
142143
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *token))
143144
return nil
@@ -162,8 +163,29 @@ func Authenticate(clientInfo ClientInfo, verbose bool) (*AuthenticatedClient, er
162163
return authenticatedClient, nil
163164
}
164165

165-
const apiURL = "https://api.astra.datastax.com"
166-
const dbURL = "https://api.astra.datastax.com/v2/databases"
166+
func apiURL() string {
167+
if env.Verbose {
168+
log.Printf("env is %v", Env)
169+
}
170+
switch Env {
171+
case "dev":
172+
return "https://api.dev.cloud.datastax.com"
173+
case "test":
174+
return "https://api.test.cloud.datastax.com"
175+
default:
176+
return "https://api.astra.datastax.com"
177+
}
178+
}
179+
180+
func dbURL() string {
181+
url := fmt.Sprintf("%v/v2/databases", apiURL())
182+
if env.Verbose {
183+
log.Printf("db url is %v", url)
184+
}
185+
return url
186+
}
187+
188+
var Env = "prod"
167189

168190
func (a *AuthenticatedClient) ctx() (context.Context, context.CancelFunc) {
169191
return timeoutContext(a.timeoutSeconds)
@@ -337,7 +359,7 @@ func (a *AuthenticatedClient) Terminate(id string, preparedStateOnly bool) error
337359
var lastStatusCode int
338360
for i := 0; i < tries; i++ {
339361
time.Sleep(time.Duration(intervalSeconds) * time.Second)
340-
req, err := http.NewRequest("GET", fmt.Sprintf("%s/%s", dbURL, id), http.NoBody)
362+
req, err := http.NewRequest("GET", fmt.Sprintf("%s/%s", dbURL(), id), http.NoBody)
341363
if err != nil {
342364
return fmt.Errorf("failed creating request to find db with id %s with: %w", id, err)
343365
}
@@ -384,7 +406,7 @@ func (a *AuthenticatedClient) Terminate(id string, preparedStateOnly bool) error
384406
// * @param databaseID string representation of the database ID
385407
// @return error
386408
func (a *AuthenticatedClient) ParkAsync(databaseID string) error {
387-
req, err := http.NewRequest("POST", fmt.Sprintf("%s/%s/park", dbURL, databaseID), http.NoBody)
409+
req, err := http.NewRequest("POST", fmt.Sprintf("%s/%s/park", dbURL(), databaseID), http.NoBody)
388410
if err != nil {
389411
return fmt.Errorf("failed creating request to park db with id %s with: %w", databaseID, err)
390412
}
@@ -421,7 +443,7 @@ func (a *AuthenticatedClient) Park(databaseID string) error {
421443
// * @param databaseID String representation of the database ID
422444
// @return error
423445
func (a *AuthenticatedClient) UnparkAsync(databaseID string) error {
424-
req, err := http.NewRequest("POST", fmt.Sprintf("%s/%s/unpark", dbURL, databaseID), http.NoBody)
446+
req, err := http.NewRequest("POST", fmt.Sprintf("%s/%s/unpark", dbURL(), databaseID), http.NoBody)
425447
if err != nil {
426448
return fmt.Errorf("failed creating request to unpark db with id %s with: %w", databaseID, err)
427449
}
@@ -460,7 +482,7 @@ func (a *AuthenticatedClient) Unpark(databaseID string) error {
460482
// @return error
461483
func (a *AuthenticatedClient) Resize(databaseID string, capacityUnits int32) error {
462484
body := fmt.Sprintf("{\"capacityUnits\":%d}", capacityUnits)
463-
req, err := http.NewRequest("POST", fmt.Sprintf("%s/%s/resize", dbURL, databaseID), bytes.NewBufferString(body))
485+
req, err := http.NewRequest("POST", fmt.Sprintf("%s/%s/resize", dbURL(), databaseID), bytes.NewBufferString(body))
464486
if err != nil {
465487
return fmt.Errorf("failed creating request to unpark db with id %s with: %w", databaseID, err)
466488
}

0 commit comments

Comments
 (0)