Skip to content

Commit 3de5783

Browse files
authored
Add argus grafana public-read-access commands (#229)
* Copy files from single sign-on * Adaptations for the public-read-access commands * Update docs * Improvements after review * Move instance ID to argument * Adapt examples
1 parent b89639c commit 3de5783

16 files changed

+1083
-54
lines changed

docs/stackit_argus_grafana.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ stackit argus grafana [flags]
3030

3131
* [stackit argus](./stackit_argus.md) - Provides functionality for Argus
3232
* [stackit argus grafana describe](./stackit_argus_grafana_describe.md) - Shows details of the Grafana configuration of an Argus instance
33+
* [stackit argus grafana public-read-access](./stackit_argus_grafana_public-read-access.md) - Enable or disable public read access for Grafana in Argus instances
3334
* [stackit argus grafana single-sign-on](./stackit_argus_grafana_single-sign-on.md) - Enable or disable single sign-on for Grafana in Argus instances
3435

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## stackit argus grafana public-read-access
2+
3+
Enable or disable public read access for Grafana in Argus instances
4+
5+
### Synopsis
6+
7+
Enable or disable public read access for Grafana in Argus instances.
8+
When enabled, anyone can access the Grafana dashboards of the instance without logging in. Otherwise, a login is required.
9+
10+
```
11+
stackit argus grafana public-read-access [flags]
12+
```
13+
14+
### Options
15+
16+
```
17+
-h, --help Help for "stackit argus grafana public-read-access"
18+
```
19+
20+
### Options inherited from parent commands
21+
22+
```
23+
-y, --assume-yes If set, skips all confirmation prompts
24+
--async If set, runs the command asynchronously
25+
-o, --output-format string Output format, one of ["json" "pretty"]
26+
-p, --project-id string Project ID
27+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
28+
```
29+
30+
### SEE ALSO
31+
32+
* [stackit argus grafana](./stackit_argus_grafana.md) - Provides functionality for the Grafana configuration of Argus instances
33+
* [stackit argus grafana public-read-access disable](./stackit_argus_grafana_public-read-access_disable.md) - Disables public read access for Grafana on Argus instances
34+
* [stackit argus grafana public-read-access enable](./stackit_argus_grafana_public-read-access_enable.md) - Enables public read access for Grafana on Argus instances
35+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## stackit argus grafana public-read-access disable
2+
3+
Disables public read access for Grafana on Argus instances
4+
5+
### Synopsis
6+
7+
Disables public read access for Grafana on Argus instances.
8+
When disabled, a login is required to access the Grafana dashboards of the instance. Otherwise, anyone can access the dashboards.
9+
10+
```
11+
stackit argus grafana public-read-access disable INSTANCE_ID [flags]
12+
```
13+
14+
### Examples
15+
16+
```
17+
Disable public read access for Grafana on an Argus instance with ID "xxx"
18+
$ stackit argus grafana public-read-access disable xxx
19+
```
20+
21+
### Options
22+
23+
```
24+
-h, --help Help for "stackit argus grafana public-read-access disable"
25+
```
26+
27+
### Options inherited from parent commands
28+
29+
```
30+
-y, --assume-yes If set, skips all confirmation prompts
31+
--async If set, runs the command asynchronously
32+
-o, --output-format string Output format, one of ["json" "pretty"]
33+
-p, --project-id string Project ID
34+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
35+
```
36+
37+
### SEE ALSO
38+
39+
* [stackit argus grafana public-read-access](./stackit_argus_grafana_public-read-access.md) - Enable or disable public read access for Grafana in Argus instances
40+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## stackit argus grafana public-read-access enable
2+
3+
Enables public read access for Grafana on Argus instances
4+
5+
### Synopsis
6+
7+
Enables public read access for Grafana on Argus instances.
8+
When enabled, anyone can access the Grafana dashboards of the instance without logging in. Otherwise, a login is required.
9+
10+
```
11+
stackit argus grafana public-read-access enable INSTANCE_ID [flags]
12+
```
13+
14+
### Examples
15+
16+
```
17+
Enable public read access for Grafana on an Argus instance with ID "xxx"
18+
$ stackit argus grafana public-read-access enable xxx
19+
```
20+
21+
### Options
22+
23+
```
24+
-h, --help Help for "stackit argus grafana public-read-access enable"
25+
```
26+
27+
### Options inherited from parent commands
28+
29+
```
30+
-y, --assume-yes If set, skips all confirmation prompts
31+
--async If set, runs the command asynchronously
32+
-o, --output-format string Output format, one of ["json" "pretty"]
33+
-p, --project-id string Project ID
34+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
35+
```
36+
37+
### SEE ALSO
38+
39+
* [stackit argus grafana public-read-access](./stackit_argus_grafana_public-read-access.md) - Enable or disable public read access for Grafana in Argus instances
40+

docs/stackit_argus_grafana_single-sign-on_disable.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,20 @@ Disables single sign-on for Grafana on Argus instances.
88
When disabled for an instance, the generic OAuth2 authentication is used for that instance.
99

1010
```
11-
stackit argus grafana single-sign-on disable [flags]
11+
stackit argus grafana single-sign-on disable INSTANCE_ID [flags]
1212
```
1313

1414
### Examples
1515

1616
```
1717
Disable single sign-on for Grafana on an Argus instance with ID "xxx"
18-
$ stackit argus grafana single-sign-on disable --instance-id xxx
18+
$ stackit argus grafana single-sign-on disable xxx
1919
```
2020

2121
### Options
2222

2323
```
24-
-h, --help Help for "stackit argus grafana single-sign-on disable"
25-
--instance-id string Instance ID
24+
-h, --help Help for "stackit argus grafana single-sign-on disable"
2625
```
2726

2827
### Options inherited from parent commands

docs/stackit_argus_grafana_single-sign-on_enable.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,20 @@ Enables single sign-on for Grafana on Argus instances.
88
When enabled for an instance, overwrites the generic OAuth2 authentication and configures STACKIT single sign-on for that instance.
99

1010
```
11-
stackit argus grafana single-sign-on enable [flags]
11+
stackit argus grafana single-sign-on enable INSTANCE_ID [flags]
1212
```
1313

1414
### Examples
1515

1616
```
1717
Enable single sign-on for Grafana on an Argus instance with ID "xxx"
18-
$ stackit argus grafana single-sign-on enable --instance-id xxx
18+
$ stackit argus grafana single-sign-on enable xxx
1919
```
2020

2121
### Options
2222

2323
```
24-
-h, --help Help for "stackit argus grafana single-sign-on enable"
25-
--instance-id string Instance ID
24+
-h, --help Help for "stackit argus grafana single-sign-on enable"
2625
```
2726

2827
### Options inherited from parent commands

internal/cmd/argus/grafana/grafana.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package grafana
22

33
import (
44
"github.com/stackitcloud/stackit-cli/internal/cmd/argus/grafana/describe"
5+
publicreadaccess "github.com/stackitcloud/stackit-cli/internal/cmd/argus/grafana/public-read-access"
56
singlesignon "github.com/stackitcloud/stackit-cli/internal/cmd/argus/grafana/single-sign-on"
67
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
78
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
@@ -24,5 +25,6 @@ func NewCmd(p *print.Printer) *cobra.Command {
2425

2526
func addSubcommands(cmd *cobra.Command, p *print.Printer) {
2627
cmd.AddCommand(describe.NewCmd(p))
28+
cmd.AddCommand(publicreadaccess.NewCmd(p))
2729
cmd.AddCommand(singlesignon.NewCmd(p))
2830
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package disable
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
8+
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
9+
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
11+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
12+
"github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/client"
13+
argusUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/utils"
14+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
15+
16+
"github.com/spf13/cobra"
17+
"github.com/stackitcloud/stackit-sdk-go/services/argus"
18+
)
19+
20+
const (
21+
instanceIdArg = "INSTANCE_ID"
22+
)
23+
24+
type inputModel struct {
25+
*globalflags.GlobalFlagModel
26+
InstanceId string
27+
}
28+
29+
func NewCmd(p *print.Printer) *cobra.Command {
30+
cmd := &cobra.Command{
31+
Use: fmt.Sprintf("disable %s", instanceIdArg),
32+
Short: "Disables public read access for Grafana on Argus instances",
33+
Long: fmt.Sprintf("%s\n%s",
34+
"Disables public read access for Grafana on Argus instances.",
35+
"When disabled, a login is required to access the Grafana dashboards of the instance. Otherwise, anyone can access the dashboards.",
36+
),
37+
Args: args.SingleArg(instanceIdArg, utils.ValidateUUID),
38+
Example: examples.Build(
39+
examples.NewExample(
40+
`Disable public read access for Grafana on an Argus instance with ID "xxx"`,
41+
"$ stackit argus grafana public-read-access disable xxx"),
42+
),
43+
RunE: func(cmd *cobra.Command, args []string) error {
44+
ctx := context.Background()
45+
model, err := parseInput(cmd, args)
46+
if err != nil {
47+
return err
48+
}
49+
50+
// Configure API client
51+
apiClient, err := client.ConfigureClient(p)
52+
if err != nil {
53+
return err
54+
}
55+
56+
instanceLabel, err := argusUtils.GetInstanceName(ctx, apiClient, model.InstanceId, model.ProjectId)
57+
if err != nil || instanceLabel == "" {
58+
instanceLabel = model.InstanceId
59+
}
60+
61+
if !model.AssumeYes {
62+
prompt := fmt.Sprintf("Are you sure you want to disable Grafana public read access for instance %q?", instanceLabel)
63+
err = p.PromptForConfirmation(prompt)
64+
if err != nil {
65+
return err
66+
}
67+
}
68+
69+
// Call API
70+
req, err := buildRequest(ctx, model, apiClient)
71+
if err != nil {
72+
return fmt.Errorf("build request: %w", err)
73+
}
74+
_, err = req.Execute()
75+
if err != nil {
76+
return fmt.Errorf("disable grafana public read access: %w", err)
77+
}
78+
79+
p.Info("Disabled Grafana public read access for instance %q\n", instanceLabel)
80+
return nil
81+
},
82+
}
83+
return cmd
84+
}
85+
86+
func parseInput(cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
87+
instanceId := inputArgs[0]
88+
89+
globalFlags := globalflags.Parse(cmd)
90+
if globalFlags.ProjectId == "" {
91+
return nil, &errors.ProjectIdError{}
92+
}
93+
94+
return &inputModel{
95+
GlobalFlagModel: globalFlags,
96+
InstanceId: instanceId,
97+
}, nil
98+
}
99+
100+
func buildRequest(ctx context.Context, model *inputModel, apiClient argusUtils.ArgusClient) (argus.ApiUpdateGrafanaConfigsRequest, error) {
101+
req := apiClient.UpdateGrafanaConfigs(ctx, model.InstanceId, model.ProjectId)
102+
payload, err := argusUtils.GetPartialUpdateGrafanaConfigsPayload(ctx, apiClient, model.InstanceId, model.ProjectId, nil, utils.Ptr(false))
103+
if err != nil {
104+
return req, fmt.Errorf("build request payload: %w", err)
105+
}
106+
req = req.UpdateGrafanaConfigsPayload(*payload)
107+
return req, nil
108+
}

0 commit comments

Comments
 (0)