Skip to content

Commit

Permalink
Fixing grafana apikey
Browse files Browse the repository at this point in the history
  • Loading branch information
jtyr committed Jul 1, 2021
1 parent 0a128ff commit f06df78
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 58 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ gcapi-cli grafana restart mystackslug
# Create Grafana API key
gcapi-cli grafana apikey create mystackslug myname viewer
# List Grafana API keys
gcapi-cli grafana apikey list myorgslug mystackslug
gcapi-cli grafana apikey list mystackslug
# List a specific Grafana API key
gcapi-cli grafana apikey list myorgslug mystackslug myname
gcapi-cli grafana apikey list mystackslug myname
# Delete a Grafana API key
gcapi-cli grafana apikey delete myorgslug mystackslug myname
gcapi-cli grafana apikey delete mystackslug myname

## Grafana API key (using only Grafana API)
# Create Grafana API key
Expand Down
18 changes: 7 additions & 11 deletions cmd/grafana/apikey/apikeyDelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// NewCmdDelete returns a new cobra command.
func NewCmdDelete() *cobra.Command {
cmd := &cobra.Command{
Use: "delete (ORG_SLUG STACK_SLUG|--grafana-api-url STRING) NAME",
Use: "delete (STACK_SLUG|--grafana-api-url STRING) NAME",
Aliases: []string{"add"},
Short: "Delete API key",
Long: "Delete Grafana API keys in a specific Stack of the Grafana Cloud and print them out.",
Expand Down Expand Up @@ -57,20 +57,16 @@ func checkDeleteArgs(cmd *cobra.Command, args []string) error {
if err := ak.SetBaseURL(gauFlag); err != nil {
return err
}
} else if argsLen < 3 {
return errors.New("requires ORG_SLUG, STACK_SLUG and NAME argument")
} else if argsLen > 3 {
return errors.New("requires only ORG_SLUG, STACK_SLUG and NAME argument")
} else if argsLen < 2 {
return errors.New("requires STACK_SLUG and NAME argument")
} else if argsLen > 2 {
return errors.New("requires only STACK_SLUG and NAME argument")
} else {
if err := ak.SetOrgSlug(args[0]); err != nil {
if err := ak.SetStackSlug(args[0]); err != nil {
return err
}

if err := ak.SetStackSlug(args[1]); err != nil {
return err
}

if err := ak.SetName(args[2]); err != nil {
if err := ak.SetName(args[1]); err != nil {
return err
}

Expand Down
23 changes: 9 additions & 14 deletions cmd/grafana/apikey/apikeyList.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// NewCmdList returns a new cobra command.
func NewCmdList() *cobra.Command {
cmd := &cobra.Command{
Use: "list (ORG_SLUG STACK_SLUG|--grafana-api-url STRING) [NAME]",
Use: "list (STACK_SLUG|--grafana-api-url STRING) [NAME]",
Aliases: []string{"add"},
Short: "List API keys",
Long: "List Grafana API keys in a specific Stack of the Grafana Cloud and print them out.",
Expand Down Expand Up @@ -60,21 +60,15 @@ func checkListArgs(cmd *cobra.Command, args []string) error {
} else if argsLen == 0 {
cmd.Usage()
os.Exit(0)
} else if argsLen < 2 {
return errors.New("requires ORG_SLUG and STACK_SLUG argument")
} else if argsLen > 3 {
return errors.New("requires only ORG_SLUG, STACK_SLUG and optionally NAME argument")
} else if argsLen > 2 {
return errors.New("requires only STACK_SLUG and optionally NAME argument")
} else {
if err := ak.SetOrgSlug(args[0]); err != nil {
if err := ak.SetStackSlug(args[0]); err != nil {
return err
}

if err := ak.SetStackSlug(args[1]); err != nil {
return err
}

if argsLen == 3 {
if err := ak.SetName(args[2]); err != nil {
if argsLen == 2 {
if err := ak.SetName(args[1]); err != nil {
return err
}
}
Expand All @@ -97,9 +91,10 @@ func checkListArgs(cmd *cobra.Command, args []string) error {

// runList runs the command's action.
func runList(cmd *cobra.Command, args []string) {
list, raw, err := ak.List()
list, raw, ec, err := ak.List()
if err != nil {
log.Fatalf("failed to get API keys: %s", err)
log.Errorf("failed to get API keys: %s", err)
log.Exit(ec)
}

oraFlag, err := cmd.Flags().GetBool("only-role-admin")
Expand Down
17 changes: 1 addition & 16 deletions pkg/grafana/apikey/apikeyCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,7 @@ type createResp struct {
// Create creates a new Grafana API key and returns the value of the newly
// created API key and the raw API response.
func (a *APIKey) Create() (string, string, error) {
// Use Grafana API token
grafanaClientConfig := a.ClientConfig
grafanaClientConfig.Token = a.GrafanaToken

if a.BaseURL == "" {
// Get Grafana API URL
var err error
grafanaClientConfig.BaseURL, err = a.GetGrafanaAPIURL()
if err != nil {
return "", "", fmt.Errorf("failed to get Grafana API URL: %s", err)
}
} else {
grafanaClientConfig.BaseURL = a.BaseURL
}

client, err := _client.New(grafanaClientConfig)
client, err := _client.New(a.ClientConfig)
if err != nil {
return "", "", fmt.Errorf("failed to get client: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/grafana/apikey/apikeyDelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (a *APIKey) Delete() (string, int, error) {
return "", consts.ExitError, fmt.Errorf("failed to get client: %s", err)
}

list, _, err := a.List()
list, _, _, err := a.List()
if err != nil {
return "", consts.ExitError, fmt.Errorf("failed to get API key ID: %s", err)
}
Expand All @@ -52,7 +52,7 @@ func (a *APIKey) Delete() (string, int, error) {
}

// This is here only to be able to mock the Delete() response
if deleteClient.Transport != nil {
if deleteClient != nil && deleteClient.Transport != nil {
_client.Client = deleteClient
client, _ = _client.New(grafanaClientConfig)
}
Expand Down
15 changes: 8 additions & 7 deletions pkg/grafana/apikey/apikeyList.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

_client "github.com/jtyr/gcapi/pkg/client"
"github.com/jtyr/gcapi/pkg/consts"
)

// ListItem described properties of individual List item returned by the API.
Expand All @@ -18,7 +19,7 @@ type ListItem struct {
type ListResp []ListItem

// List lists Grafana API keys and returns the list and the raw API response.
func (a *APIKey) List() (*ListResp, string, error) {
func (a *APIKey) List() (*ListResp, string, int, error) {
// Use Grafana API token
grafanaClientConfig := a.ClientConfig
grafanaClientConfig.Token = a.GrafanaToken
Expand All @@ -28,32 +29,32 @@ func (a *APIKey) List() (*ListResp, string, error) {
var err error
grafanaClientConfig.BaseURL, err = a.GetGrafanaAPIURL()
if err != nil {
return nil, "", fmt.Errorf("failed to get Grafana API URL: %s", err)
return nil, "", consts.ExitError, fmt.Errorf("failed to get Grafana API URL: %s", err)
}
} else {
grafanaClientConfig.BaseURL = a.BaseURL
}

client, err := _client.New(grafanaClientConfig)
if err != nil {
return nil, "", fmt.Errorf("failed to get client: %s", err)
return nil, "", consts.ExitError, fmt.Errorf("failed to get client: %s", err)
}

client.Endpoint = a.GrafanaEndpoint

body, statusCode, err := client.Get()
if err != nil {
if statusCode == 404 {
return nil, "", fmt.Errorf("Grafana instance not found: %s", err)
return nil, "", consts.ExitNotFound, fmt.Errorf("Grafana instance not found: %s", err)
}

return nil, "", err
return nil, "", consts.ExitError, err
}

var jsonData ListResp
if err := json.Unmarshal(body, &jsonData); err != nil {
return nil, "", fmt.Errorf("cannot parse API response as JSON: %s", err)
return nil, "", consts.ExitError, fmt.Errorf("cannot parse API response as JSON: %s", err)
}

return &jsonData, string(body), nil
return &jsonData, string(body), consts.ExitOk, nil
}
4 changes: 2 additions & 2 deletions pkg/grafana/apikey/apikeyList_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ func TestAPIKeyList(t *testing.T) {
a := New()
a.SetBaseURL(test.baseURL)

list, body, err := a.List()
list, body, ec, err := a.List()
if !test.expectingError && err != nil {
t.Errorf("Test [%d]: failed to call List: %s", i, err)
t.Errorf("Test [%d]: failed to call List (exit code=%d): %s", i, ec, err)
}

if list != nil && len(*list) == 0 {
Expand Down
3 changes: 0 additions & 3 deletions pkg/grafana/grafana_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ func TestGrafana(t *testing.T) {
expectingGrafanaAPIURLError: true,
expectingGrafanaTokenError: true,
expectingNameError: true,
expectingOrgSlugError: true,
expectingStackSlugError: true,
expectingTokenError: true,
orgSlug: "testOrgSlug",
Expand All @@ -123,8 +122,6 @@ func TestGrafana(t *testing.T) {
expectingGrafanaAPIURLError: true,
expectingGrafanaTokenError: true,
expectingNameError: true,
expectingOrgSlugError: true,
expectingStackSlugError: true,
expectingTokenError: true,
orgSlug: "testOrgSlug",
stackSlug: "testStackSlug",
Expand Down

0 comments on commit f06df78

Please sign in to comment.