Skip to content

Commit e3379d7

Browse files
authored
Add argus grafana describe command (#221)
* Add `argus grafana config describe` command * Extend command to also show dashboard URL * Generate docs * Improve command descriptions * Adjust function names * Show initial Grafana credentials in output * Add example for showing password * Change to hide-password flag
1 parent 69041f2 commit e3379d7

File tree

8 files changed

+523
-14
lines changed

8 files changed

+523
-14
lines changed

docs/stackit.md

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,22 @@ stackit [flags]
2626

2727
### SEE ALSO
2828

29-
* [stackit argus](./stackit_argus.md) - Provides functionality for Argus
3029
* [stackit argus](./stackit_argus.md) - Provides functionality for Argus
3130
* [stackit auth](./stackit_auth.md) - Provides authentication functionality
3231
* [stackit config](./stackit_config.md) - Provides functionality for CLI configuration options
3332
* [stackit curl](./stackit_curl.md) - Executes an authenticated HTTP request to an endpoint
3433
* [stackit dns](./stackit_dns.md) - Provides functionality for DNS
3534
* [stackit logme](./stackit_logme.md) - Provides functionality for LogMe
36-
* [stackit logme](./stackit_logme.md) - Provides functionality for LogMe
3735
* [stackit mariadb](./stackit_mariadb.md) - Provides functionality for MariaDB
38-
* [stackit mariadb](./stackit_mariadb.md) - Provides functionality for MariaDB
39-
* [stackit mongodbflex](./stackit_mongodbflex.md) - Provides functionality for MongoDB Flex
4036
* [stackit mongodbflex](./stackit_mongodbflex.md) - Provides functionality for MongoDB Flex
4137
* [stackit object-storage](./stackit_object-storage.md) - Provides functionality regarding Object Storage
42-
* [stackit object-storage](./stackit_object-storage.md) - Provides functionality regarding Object Storage
43-
* [stackit opensearch](./stackit_opensearch.md) - Provides functionality for OpenSearch
4438
* [stackit opensearch](./stackit_opensearch.md) - Provides functionality for OpenSearch
4539
* [stackit organization](./stackit_organization.md) - Provides functionality regarding organizations
46-
* [stackit organization](./stackit_organization.md) - Provides functionality regarding organizations
47-
* [stackit postgresflex](./stackit_postgresflex.md) - Provides functionality for PostgreSQL Flex
4840
* [stackit postgresflex](./stackit_postgresflex.md) - Provides functionality for PostgreSQL Flex
4941
* [stackit project](./stackit_project.md) - Provides functionality regarding projects
5042
* [stackit rabbitmq](./stackit_rabbitmq.md) - Provides functionality for RabbitMQ
51-
* [stackit rabbitmq](./stackit_rabbitmq.md) - Provides functionality for RabbitMQ
52-
* [stackit redis](./stackit_redis.md) - Provides functionality for Redis
5343
* [stackit redis](./stackit_redis.md) - Provides functionality for Redis
5444
* [stackit secrets-manager](./stackit_secrets-manager.md) - Provides functionality for Secrets Manager
55-
* [stackit secrets-manager](./stackit_secrets-manager.md) - Provides functionality for Secrets Manager
56-
* [stackit service-account](./stackit_service-account.md) - Provides functionality for service accounts
5745
* [stackit service-account](./stackit_service-account.md) - Provides functionality for service accounts
5846
* [stackit ske](./stackit_ske.md) - Provides functionality for SKE
59-
* [stackit ske](./stackit_ske.md) - Provides functionality for SKE
6047

docs/stackit_argus.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ stackit argus [flags]
2929
### SEE ALSO
3030

3131
* [stackit](./stackit.md) - Manage STACKIT resources using the command line
32+
* [stackit argus grafana](./stackit_argus_grafana.md) - Provides functionality for the Grafana configuration of Argus instances
3233
* [stackit argus instance](./stackit_argus_instance.md) - Provides functionality for Argus instances
3334
* [stackit argus plans](./stackit_argus_plans.md) - Lists all Argus service plans
3435

docs/stackit_argus_grafana.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## stackit argus grafana
2+
3+
Provides functionality for the Grafana configuration of Argus instances
4+
5+
### Synopsis
6+
7+
Provides functionality for the Grafana configuration of Argus instances.
8+
9+
```
10+
stackit argus grafana [flags]
11+
```
12+
13+
### Options
14+
15+
```
16+
-h, --help Help for "stackit argus grafana"
17+
```
18+
19+
### Options inherited from parent commands
20+
21+
```
22+
-y, --assume-yes If set, skips all confirmation prompts
23+
--async If set, runs the command asynchronously
24+
-o, --output-format string Output format, one of ["json" "pretty"]
25+
-p, --project-id string Project ID
26+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
27+
```
28+
29+
### SEE ALSO
30+
31+
* [stackit argus](./stackit_argus.md) - Provides functionality for Argus
32+
* [stackit argus grafana describe](./stackit_argus_grafana_describe.md) - Shows details of the Grafana configuration of an Argus instance
33+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
## stackit argus grafana describe
2+
3+
Shows details of the Grafana configuration of an Argus instance
4+
5+
### Synopsis
6+
7+
Shows details of the Grafana configuration of an Argus instance.
8+
The Grafana dashboard URL and initial credentials (admin user and password) will be shown in the "pretty" output format. These credentials are only valid for first login. Please change the password after first login. After changing, the initial password is no longer valid.
9+
The initial password is shown by default, if you want to hide it use the "--hide-password" flag.
10+
11+
```
12+
stackit argus grafana describe [flags]
13+
```
14+
15+
### Examples
16+
17+
```
18+
Get details of the Grafana configuration of an Argus instance with ID "xxx"
19+
$ stackit argus credentials describe --instance-id xxx
20+
21+
Get details of the Grafana configuration of an Argus instance with ID "xxx" in a table format
22+
$ stackit argus credentials describe --instance-id xxx --output-format pretty
23+
24+
Get details of the Grafana configuration of an Argus instance with ID "xxx" and hide the initial admin password
25+
$ stackit argus credentials describe --instance-id xxx --output-format pretty --hide-password
26+
```
27+
28+
### Options
29+
30+
```
31+
-h, --help Help for "stackit argus grafana describe"
32+
--hide-password Show the initial admin password in the "pretty" output format
33+
--instance-id string Instance ID
34+
```
35+
36+
### Options inherited from parent commands
37+
38+
```
39+
-y, --assume-yes If set, skips all confirmation prompts
40+
--async If set, runs the command asynchronously
41+
-o, --output-format string Output format, one of ["json" "pretty"]
42+
-p, --project-id string Project ID
43+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
44+
```
45+
46+
### SEE ALSO
47+
48+
* [stackit argus grafana](./stackit_argus_grafana.md) - Provides functionality for the Grafana configuration of Argus instances
49+

internal/cmd/argus/argus.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package argus
22

33
import (
4+
"github.com/stackitcloud/stackit-cli/internal/cmd/argus/grafana"
45
"github.com/stackitcloud/stackit-cli/internal/cmd/argus/instance"
56
"github.com/stackitcloud/stackit-cli/internal/cmd/argus/plans"
67
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
@@ -23,6 +24,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
2324
}
2425

2526
func addSubcommands(cmd *cobra.Command, p *print.Printer) {
26-
cmd.AddCommand(plans.NewCmd(p))
27+
cmd.AddCommand(grafana.NewCmd(p))
2728
cmd.AddCommand(instance.NewCmd(p))
29+
cmd.AddCommand(plans.NewCmd(p))
2830
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package describe
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
8+
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
9+
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
11+
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
12+
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
13+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
14+
"github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/client"
15+
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
16+
17+
"github.com/spf13/cobra"
18+
"github.com/stackitcloud/stackit-sdk-go/services/argus"
19+
)
20+
21+
const (
22+
instanceIdFlag = "instance-id"
23+
hidePasswordFlag = "hide-password"
24+
)
25+
26+
type inputModel struct {
27+
*globalflags.GlobalFlagModel
28+
InstanceId string
29+
HidePassword bool
30+
}
31+
32+
func NewCmd(p *print.Printer) *cobra.Command {
33+
cmd := &cobra.Command{
34+
Use: "describe",
35+
Short: "Shows details of the Grafana configuration of an Argus instance",
36+
Long: fmt.Sprintf("%s\n%s\n%s",
37+
"Shows details of the Grafana configuration of an Argus instance.",
38+
`The Grafana dashboard URL and initial credentials (admin user and password) will be shown in the "pretty" output format. These credentials are only valid for first login. Please change the password after first login. After changing, the initial password is no longer valid.`,
39+
`The initial password is shown by default, if you want to hide it use the "--hide-password" flag.`,
40+
),
41+
Args: args.NoArgs,
42+
Example: examples.Build(
43+
examples.NewExample(
44+
`Get details of the Grafana configuration of an Argus instance with ID "xxx"`,
45+
"$ stackit argus credentials describe --instance-id xxx"),
46+
examples.NewExample(
47+
`Get details of the Grafana configuration of an Argus instance with ID "xxx" in a table format`,
48+
"$ stackit argus credentials describe --instance-id xxx --output-format pretty"),
49+
examples.NewExample(
50+
`Get details of the Grafana configuration of an Argus instance with ID "xxx" and hide the initial admin password`,
51+
"$ stackit argus credentials describe --instance-id xxx --output-format pretty --hide-password"),
52+
),
53+
RunE: func(cmd *cobra.Command, args []string) error {
54+
ctx := context.Background()
55+
model, err := parseInput(cmd)
56+
if err != nil {
57+
return err
58+
}
59+
// Configure API client
60+
apiClient, err := client.ConfigureClient(p)
61+
if err != nil {
62+
return err
63+
}
64+
65+
// Call API
66+
grafanaConfigsReq := buildGetGrafanaConfigRequest(ctx, model, apiClient)
67+
grafanaConfigsResp, err := grafanaConfigsReq.Execute()
68+
if err != nil {
69+
return fmt.Errorf("get Grafana configs: %w", err)
70+
}
71+
instanceReq := buildGetInstanceRequest(ctx, model, apiClient)
72+
instanceResp, err := instanceReq.Execute()
73+
if err != nil {
74+
return fmt.Errorf("get instance: %w", err)
75+
}
76+
77+
return outputResult(p, model, grafanaConfigsResp, instanceResp)
78+
},
79+
}
80+
configureFlags(cmd)
81+
return cmd
82+
}
83+
84+
func configureFlags(cmd *cobra.Command) {
85+
cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "Instance ID")
86+
cmd.Flags().Bool(hidePasswordFlag, false, `Show the initial admin password in the "pretty" output format`)
87+
88+
err := flags.MarkFlagsRequired(cmd, instanceIdFlag)
89+
cobra.CheckErr(err)
90+
}
91+
92+
func parseInput(cmd *cobra.Command) (*inputModel, error) {
93+
globalFlags := globalflags.Parse(cmd)
94+
if globalFlags.ProjectId == "" {
95+
return nil, &errors.ProjectIdError{}
96+
}
97+
98+
return &inputModel{
99+
GlobalFlagModel: globalFlags,
100+
InstanceId: flags.FlagToStringValue(cmd, instanceIdFlag),
101+
HidePassword: flags.FlagToBoolValue(cmd, hidePasswordFlag),
102+
}, nil
103+
}
104+
105+
func buildGetGrafanaConfigRequest(ctx context.Context, model *inputModel, apiClient *argus.APIClient) argus.ApiGetGrafanaConfigsRequest {
106+
req := apiClient.GetGrafanaConfigs(ctx, model.InstanceId, model.ProjectId)
107+
return req
108+
}
109+
110+
func buildGetInstanceRequest(ctx context.Context, model *inputModel, apiClient *argus.APIClient) argus.ApiGetInstanceRequest {
111+
req := apiClient.GetInstance(ctx, model.InstanceId, model.ProjectId)
112+
return req
113+
}
114+
115+
func outputResult(p *print.Printer, inputModel *inputModel, grafanaConfigs *argus.GrafanaConfigs, instance *argus.GetInstanceResponse) error {
116+
switch inputModel.OutputFormat {
117+
case globalflags.PrettyOutputFormat:
118+
initialAdminPassword := *instance.Instance.GrafanaAdminPassword
119+
if inputModel.HidePassword {
120+
initialAdminPassword = "<hidden>"
121+
}
122+
123+
table := tables.NewTable()
124+
table.AddRow("GRAFANA DASHBOARD", *instance.Instance.GrafanaUrl)
125+
table.AddSeparator()
126+
table.AddRow("PUBLIC READ ACCESS", *grafanaConfigs.PublicReadAccess)
127+
table.AddSeparator()
128+
table.AddRow("SINGLE SIGN-ON", *grafanaConfigs.UseStackitSso)
129+
table.AddSeparator()
130+
table.AddRow("INITIAL ADMIN USER (DEFAULT)", *instance.Instance.GrafanaAdminUser)
131+
table.AddSeparator()
132+
table.AddRow("INITIAL ADMIN PASSWORD (DEFAULT)", initialAdminPassword)
133+
err := table.Display(p)
134+
if err != nil {
135+
return fmt.Errorf("render table: %w", err)
136+
}
137+
138+
return nil
139+
default:
140+
details, err := json.MarshalIndent(grafanaConfigs, "", " ")
141+
if err != nil {
142+
return fmt.Errorf("marshal Grafana configs: %w", err)
143+
}
144+
p.Outputln(string(details))
145+
146+
return nil
147+
}
148+
}

0 commit comments

Comments
 (0)