|
| 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/flags" |
| 11 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 13 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/client" |
| 14 | + argusUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/utils" |
| 15 | + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" |
| 16 | + |
| 17 | + "github.com/spf13/cobra" |
| 18 | + "github.com/stackitcloud/stackit-sdk-go/services/argus" |
| 19 | +) |
| 20 | + |
| 21 | +const ( |
| 22 | + instanceIdFlag = "instance-id" |
| 23 | +) |
| 24 | + |
| 25 | +type inputModel struct { |
| 26 | + *globalflags.GlobalFlagModel |
| 27 | + InstanceId string |
| 28 | +} |
| 29 | + |
| 30 | +func NewCmd(p *print.Printer) *cobra.Command { |
| 31 | + cmd := &cobra.Command{ |
| 32 | + Use: "disable", |
| 33 | + Short: "Disables single sign-on for Grafana on Argus instances", |
| 34 | + Long: fmt.Sprintf("%s\n%s", |
| 35 | + "Disables single sign-on for Grafana on Argus instances.", |
| 36 | + "When disabled for an instance, the generic OAuth2 authentication is used for that instance.", |
| 37 | + ), |
| 38 | + Args: args.NoArgs, |
| 39 | + Example: examples.Build( |
| 40 | + examples.NewExample( |
| 41 | + `Disable single sign-on for Grafana on an Argus instance with ID "xxx"`, |
| 42 | + "$ stackit argus grafana single-sign-on disable --instance-id xxx"), |
| 43 | + ), |
| 44 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 45 | + ctx := context.Background() |
| 46 | + model, err := parseInput(cmd) |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + |
| 51 | + // Configure API client |
| 52 | + apiClient, err := client.ConfigureClient(p) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + instanceLabel, err := argusUtils.GetInstanceName(ctx, apiClient, model.InstanceId, model.ProjectId) |
| 58 | + if err != nil || instanceLabel == "" { |
| 59 | + instanceLabel = model.InstanceId |
| 60 | + } |
| 61 | + |
| 62 | + if !model.AssumeYes { |
| 63 | + prompt := fmt.Sprintf("Are you sure you want to disable single sign-on for instance %q?", instanceLabel) |
| 64 | + err = p.PromptForConfirmation(prompt) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // Call API |
| 71 | + req, err := buildRequest(ctx, model, apiClient) |
| 72 | + if err != nil { |
| 73 | + return fmt.Errorf("build request: %w", err) |
| 74 | + } |
| 75 | + _, err = req.Execute() |
| 76 | + if err != nil { |
| 77 | + return fmt.Errorf("disable single sign-on: %w", err) |
| 78 | + } |
| 79 | + |
| 80 | + p.Info("Disabled single sign-on for instance %q\n", instanceLabel) |
| 81 | + return nil |
| 82 | + }, |
| 83 | + } |
| 84 | + configureFlags(cmd) |
| 85 | + return cmd |
| 86 | +} |
| 87 | + |
| 88 | +func configureFlags(cmd *cobra.Command) { |
| 89 | + cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "Instance ID") |
| 90 | + |
| 91 | + err := flags.MarkFlagsRequired(cmd, instanceIdFlag) |
| 92 | + cobra.CheckErr(err) |
| 93 | +} |
| 94 | + |
| 95 | +func parseInput(cmd *cobra.Command) (*inputModel, error) { |
| 96 | + globalFlags := globalflags.Parse(cmd) |
| 97 | + if globalFlags.ProjectId == "" { |
| 98 | + return nil, &errors.ProjectIdError{} |
| 99 | + } |
| 100 | + |
| 101 | + return &inputModel{ |
| 102 | + GlobalFlagModel: globalFlags, |
| 103 | + InstanceId: flags.FlagToStringValue(cmd, instanceIdFlag), |
| 104 | + }, nil |
| 105 | +} |
| 106 | + |
| 107 | +func buildRequest(ctx context.Context, model *inputModel, apiClient argusUtils.ArgusClient) (argus.ApiUpdateGrafanaConfigsRequest, error) { |
| 108 | + req := apiClient.UpdateGrafanaConfigs(ctx, model.InstanceId, model.ProjectId) |
| 109 | + payload, err := argusUtils.GetPartialUpdateGrafanaConfigsPayload(ctx, apiClient, model.InstanceId, model.ProjectId, utils.Ptr(false), nil) |
| 110 | + if err != nil { |
| 111 | + return req, fmt.Errorf("build request payload: %w", err) |
| 112 | + } |
| 113 | + req = req.UpdateGrafanaConfigsPayload(*payload) |
| 114 | + return req, nil |
| 115 | +} |
0 commit comments