|
| 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