Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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
  • Loading branch information
joaopalet authored Apr 17, 2024
commit 3de5783d1bf217d8ba146eb6617b3aa8d475b731
1 change: 1 addition & 0 deletions docs/stackit_argus_grafana.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ stackit argus grafana [flags]

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

35 changes: 35 additions & 0 deletions docs/stackit_argus_grafana_public-read-access.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## stackit argus grafana public-read-access

Enable or disable public read access for Grafana in Argus instances

### Synopsis

Enable or disable public read access for Grafana in Argus instances.
When enabled, anyone can access the Grafana dashboards of the instance without logging in. Otherwise, a login is required.

```
stackit argus grafana public-read-access [flags]
```

### Options

```
-h, --help Help for "stackit argus grafana public-read-access"
```

### Options inherited from parent commands

```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty"]
-p, --project-id string Project ID
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```

### SEE ALSO

* [stackit argus grafana](./stackit_argus_grafana.md) - Provides functionality for the Grafana configuration of Argus instances
* [stackit argus grafana public-read-access disable](./stackit_argus_grafana_public-read-access_disable.md) - Disables public read access for Grafana on Argus instances
* [stackit argus grafana public-read-access enable](./stackit_argus_grafana_public-read-access_enable.md) - Enables public read access for Grafana on Argus instances

40 changes: 40 additions & 0 deletions docs/stackit_argus_grafana_public-read-access_disable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## stackit argus grafana public-read-access disable

Disables public read access for Grafana on Argus instances

### Synopsis

Disables public read access for Grafana on Argus instances.
When disabled, a login is required to access the Grafana dashboards of the instance. Otherwise, anyone can access the dashboards.

```
stackit argus grafana public-read-access disable INSTANCE_ID [flags]
```

### Examples

```
Disable public read access for Grafana on an Argus instance with ID "xxx"
$ stackit argus grafana public-read-access disable xxx
```

### Options

```
-h, --help Help for "stackit argus grafana public-read-access disable"
```

### Options inherited from parent commands

```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty"]
-p, --project-id string Project ID
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```

### SEE ALSO

* [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 changes: 40 additions & 0 deletions docs/stackit_argus_grafana_public-read-access_enable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## stackit argus grafana public-read-access enable

Enables public read access for Grafana on Argus instances

### Synopsis

Enables public read access for Grafana on Argus instances.
When enabled, anyone can access the Grafana dashboards of the instance without logging in. Otherwise, a login is required.

```
stackit argus grafana public-read-access enable INSTANCE_ID [flags]
```

### Examples

```
Enable public read access for Grafana on an Argus instance with ID "xxx"
$ stackit argus grafana public-read-access enable xxx
```

### Options

```
-h, --help Help for "stackit argus grafana public-read-access enable"
```

### Options inherited from parent commands

```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty"]
-p, --project-id string Project ID
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```

### SEE ALSO

* [stackit argus grafana public-read-access](./stackit_argus_grafana_public-read-access.md) - Enable or disable public read access for Grafana in Argus instances

7 changes: 3 additions & 4 deletions docs/stackit_argus_grafana_single-sign-on_disable.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@ Disables single sign-on for Grafana on Argus instances.
When disabled for an instance, the generic OAuth2 authentication is used for that instance.

```
stackit argus grafana single-sign-on disable [flags]
stackit argus grafana single-sign-on disable INSTANCE_ID [flags]
```

### Examples

```
Disable single sign-on for Grafana on an Argus instance with ID "xxx"
$ stackit argus grafana single-sign-on disable --instance-id xxx
$ stackit argus grafana single-sign-on disable xxx
```

### Options

```
-h, --help Help for "stackit argus grafana single-sign-on disable"
--instance-id string Instance ID
-h, --help Help for "stackit argus grafana single-sign-on disable"
```

### Options inherited from parent commands
Expand Down
7 changes: 3 additions & 4 deletions docs/stackit_argus_grafana_single-sign-on_enable.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@ Enables single sign-on for Grafana on Argus instances.
When enabled for an instance, overwrites the generic OAuth2 authentication and configures STACKIT single sign-on for that instance.

```
stackit argus grafana single-sign-on enable [flags]
stackit argus grafana single-sign-on enable INSTANCE_ID [flags]
```

### Examples

```
Enable single sign-on for Grafana on an Argus instance with ID "xxx"
$ stackit argus grafana single-sign-on enable --instance-id xxx
$ stackit argus grafana single-sign-on enable xxx
```

### Options

```
-h, --help Help for "stackit argus grafana single-sign-on enable"
--instance-id string Instance ID
-h, --help Help for "stackit argus grafana single-sign-on enable"
```

### Options inherited from parent commands
Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/argus/grafana/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package grafana

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

func addSubcommands(cmd *cobra.Command, p *print.Printer) {
cmd.AddCommand(describe.NewCmd(p))
cmd.AddCommand(publicreadaccess.NewCmd(p))
cmd.AddCommand(singlesignon.NewCmd(p))
}
108 changes: 108 additions & 0 deletions internal/cmd/argus/grafana/public-read-access/disable/disable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package disable

import (
"context"
"fmt"

"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/client"
argusUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/utils"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-sdk-go/services/argus"
)

const (
instanceIdArg = "INSTANCE_ID"
)

type inputModel struct {
*globalflags.GlobalFlagModel
InstanceId string
}

func NewCmd(p *print.Printer) *cobra.Command {
cmd := &cobra.Command{
Use: fmt.Sprintf("disable %s", instanceIdArg),
Short: "Disables public read access for Grafana on Argus instances",
Long: fmt.Sprintf("%s\n%s",
"Disables public read access for Grafana on Argus instances.",
"When disabled, a login is required to access the Grafana dashboards of the instance. Otherwise, anyone can access the dashboards.",
),
Args: args.SingleArg(instanceIdArg, utils.ValidateUUID),
Example: examples.Build(
examples.NewExample(
`Disable public read access for Grafana on an Argus instance with ID "xxx"`,
"$ stackit argus grafana public-read-access disable xxx"),
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
model, err := parseInput(cmd, args)
if err != nil {
return err
}

// Configure API client
apiClient, err := client.ConfigureClient(p)
if err != nil {
return err
}

instanceLabel, err := argusUtils.GetInstanceName(ctx, apiClient, model.InstanceId, model.ProjectId)
if err != nil || instanceLabel == "" {
instanceLabel = model.InstanceId
}

if !model.AssumeYes {
prompt := fmt.Sprintf("Are you sure you want to disable Grafana public read access for instance %q?", instanceLabel)
err = p.PromptForConfirmation(prompt)
if err != nil {
return err
}
}

// Call API
req, err := buildRequest(ctx, model, apiClient)
if err != nil {
return fmt.Errorf("build request: %w", err)
}
_, err = req.Execute()
if err != nil {
return fmt.Errorf("disable grafana public read access: %w", err)
}

p.Info("Disabled Grafana public read access for instance %q\n", instanceLabel)
return nil
},
}
return cmd
}

func parseInput(cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
instanceId := inputArgs[0]

globalFlags := globalflags.Parse(cmd)
if globalFlags.ProjectId == "" {
return nil, &errors.ProjectIdError{}
}

return &inputModel{
GlobalFlagModel: globalFlags,
InstanceId: instanceId,
}, nil
}

func buildRequest(ctx context.Context, model *inputModel, apiClient argusUtils.ArgusClient) (argus.ApiUpdateGrafanaConfigsRequest, error) {
req := apiClient.UpdateGrafanaConfigs(ctx, model.InstanceId, model.ProjectId)
payload, err := argusUtils.GetPartialUpdateGrafanaConfigsPayload(ctx, apiClient, model.InstanceId, model.ProjectId, nil, utils.Ptr(false))
if err != nil {
return req, fmt.Errorf("build request payload: %w", err)
}
req = req.UpdateGrafanaConfigsPayload(*payload)
return req, nil
}
Loading