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