|
| 1 | +package describe |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/goccy/go-yaml" |
| 9 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 10 | + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
| 11 | + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/flags" |
| 13 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 14 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 15 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/sqlserverflex/client" |
| 16 | + "github.com/stackitcloud/stackit-cli/internal/pkg/tables" |
| 17 | + "github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex" |
| 18 | + |
| 19 | + "github.com/spf13/cobra" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + databaseNameArg = "DATABASE_NAME" |
| 24 | + |
| 25 | + instanceIdFlag = "instance-id" |
| 26 | +) |
| 27 | + |
| 28 | +type inputModel struct { |
| 29 | + *globalflags.GlobalFlagModel |
| 30 | + DatabaseName string |
| 31 | + InstanceId string |
| 32 | +} |
| 33 | + |
| 34 | +func NewCmd(p *print.Printer) *cobra.Command { |
| 35 | + cmd := &cobra.Command{ |
| 36 | + Use: fmt.Sprintf("describe %s", databaseNameArg), |
| 37 | + Short: "Shows details of an SQLServer Flex database", |
| 38 | + Long: "Shows details of an SQLServer Flex database.", |
| 39 | + Args: args.SingleArg(databaseNameArg, nil), |
| 40 | + Example: examples.Build( |
| 41 | + examples.NewExample( |
| 42 | + `Get details of an SQLServer Flex database with name "my-database" of instance with ID "xxx"`, |
| 43 | + "$ stackit beta sqlserverflex database describe my-database --instance-id xxx"), |
| 44 | + examples.NewExample( |
| 45 | + `Get details of an SQLServer Flex database with name "my-database" of instance with ID "xxx" in JSON format`, |
| 46 | + "$ stackit beta sqlserverflex database describe my-database --instance-id xxx --output-format json"), |
| 47 | + ), |
| 48 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 49 | + ctx := context.Background() |
| 50 | + model, err := parseInput(p, cmd, args) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + // Configure API client |
| 55 | + apiClient, err := client.ConfigureClient(p) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + |
| 60 | + // Call API |
| 61 | + req := buildRequest(ctx, model, apiClient) |
| 62 | + resp, err := req.Execute() |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("read SQLServer Flex database: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + return outputResult(p, model.OutputFormat, resp) |
| 68 | + }, |
| 69 | + } |
| 70 | + configureFlags(cmd) |
| 71 | + return cmd |
| 72 | +} |
| 73 | + |
| 74 | +func configureFlags(cmd *cobra.Command) { |
| 75 | + cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "SQLServer Flex instance ID") |
| 76 | + |
| 77 | + err := flags.MarkFlagsRequired(cmd, instanceIdFlag) |
| 78 | + cobra.CheckErr(err) |
| 79 | +} |
| 80 | + |
| 81 | +func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) { |
| 82 | + databaseName := inputArgs[0] |
| 83 | + |
| 84 | + globalFlags := globalflags.Parse(p, cmd) |
| 85 | + if globalFlags.ProjectId == "" { |
| 86 | + return nil, &errors.ProjectIdError{} |
| 87 | + } |
| 88 | + |
| 89 | + model := inputModel{ |
| 90 | + GlobalFlagModel: globalFlags, |
| 91 | + DatabaseName: databaseName, |
| 92 | + InstanceId: flags.FlagToStringValue(p, cmd, instanceIdFlag), |
| 93 | + } |
| 94 | + |
| 95 | + if p.IsVerbosityDebug() { |
| 96 | + modelStr, err := print.BuildDebugStrFromInputModel(model) |
| 97 | + if err != nil { |
| 98 | + p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err) |
| 99 | + } else { |
| 100 | + p.Debug(print.DebugLevel, "parsed input values: %s", modelStr) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return &model, nil |
| 105 | +} |
| 106 | + |
| 107 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *sqlserverflex.APIClient) sqlserverflex.ApiGetDatabaseRequest { |
| 108 | + req := apiClient.GetDatabase(ctx, model.ProjectId, model.InstanceId, model.DatabaseName) |
| 109 | + return req |
| 110 | +} |
| 111 | + |
| 112 | +func outputResult(p *print.Printer, outputFormat string, database *sqlserverflex.GetDatabaseResponse) error { |
| 113 | + switch outputFormat { |
| 114 | + case print.JSONOutputFormat: |
| 115 | + details, err := json.MarshalIndent(database, "", " ") |
| 116 | + if err != nil { |
| 117 | + return fmt.Errorf("marshal SQLServer Flex database: %w", err) |
| 118 | + } |
| 119 | + p.Outputln(string(details)) |
| 120 | + |
| 121 | + return nil |
| 122 | + case print.YAMLOutputFormat: |
| 123 | + details, err := yaml.MarshalWithOptions(database, yaml.IndentSequence(true)) |
| 124 | + if err != nil { |
| 125 | + return fmt.Errorf("marshal SQLServer Flex database: %w", err) |
| 126 | + } |
| 127 | + p.Outputln(string(details)) |
| 128 | + |
| 129 | + return nil |
| 130 | + default: |
| 131 | + database := database.Database |
| 132 | + table := tables.NewTable() |
| 133 | + table.AddRow("ID", *database.Id) |
| 134 | + table.AddSeparator() |
| 135 | + table.AddRow("NAME", *database.Name) |
| 136 | + table.AddSeparator() |
| 137 | + if database.CreateDate != nil { |
| 138 | + table.AddRow("CREATE DATE", *database.CreateDate) |
| 139 | + table.AddSeparator() |
| 140 | + } |
| 141 | + if database.Collation != nil { |
| 142 | + table.AddRow("COLLATION", *database.Collation) |
| 143 | + table.AddSeparator() |
| 144 | + } |
| 145 | + if database.Options != nil { |
| 146 | + if database.Options.CompatibilityLevel != nil { |
| 147 | + table.AddRow("COMPATIBILITY LEVEL", *database.Options.CompatibilityLevel) |
| 148 | + table.AddSeparator() |
| 149 | + } |
| 150 | + if database.Options.IsEncrypted != nil { |
| 151 | + table.AddRow("IS ENCRYPTED", *database.Options.IsEncrypted) |
| 152 | + table.AddSeparator() |
| 153 | + } |
| 154 | + if database.Options.Owner != nil { |
| 155 | + table.AddRow("OWNER", *database.Options.Owner) |
| 156 | + table.AddSeparator() |
| 157 | + } |
| 158 | + if database.Options.UserAccess != nil { |
| 159 | + table.AddRow("USER ACCESS", *database.Options.UserAccess) |
| 160 | + } |
| 161 | + } |
| 162 | + err := table.Display(p) |
| 163 | + if err != nil { |
| 164 | + return fmt.Errorf("render table: %w", err) |
| 165 | + } |
| 166 | + |
| 167 | + return nil |
| 168 | + } |
| 169 | +} |
0 commit comments