|
| 1 | +package describe |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/goccy/go-yaml" |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "github.com/stackitcloud/stackit-cli/internal/cmd/params" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 13 | + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
| 14 | + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 15 | + "github.com/stackitcloud/stackit-cli/internal/pkg/flags" |
| 16 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 17 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 18 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client" |
| 19 | + "github.com/stackitcloud/stackit-cli/internal/pkg/tables" |
| 20 | + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" |
| 21 | + "github.com/stackitcloud/stackit-sdk-go/services/iaasalpha" |
| 22 | +) |
| 23 | + |
| 24 | +const ( |
| 25 | + organizationIdFlag = "organization-id" |
| 26 | + networkAreaIdFlag = "network-area-id" |
| 27 | + routingTableArg = "ROUTING_TABLE_ID_ARG" |
| 28 | +) |
| 29 | + |
| 30 | +type inputModel struct { |
| 31 | + *globalflags.GlobalFlagModel |
| 32 | + OrganizationId *string |
| 33 | + NetworkAreaId *string |
| 34 | + RoutingTableId *string |
| 35 | +} |
| 36 | + |
| 37 | +func NewCmd(params *params.CmdParams) *cobra.Command { |
| 38 | + cmd := &cobra.Command{ |
| 39 | + Use: fmt.Sprintf("describe %s", routingTableArg), |
| 40 | + Short: "Describe a routing-table", |
| 41 | + Long: "Describe a routing-table", |
| 42 | + Args: args.SingleArg(routingTableArg, nil), |
| 43 | + Example: examples.Build( |
| 44 | + examples.NewExample( |
| 45 | + `Describe a routing-table`, |
| 46 | + `$ stackit beta routing-table describe xxxx-xxxx-xxxx-xxxx`, |
| 47 | + ), |
| 48 | + ), |
| 49 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 50 | + ctx := context.Background() |
| 51 | + model, err := parseInput(params.Printer, cmd, args) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + |
| 56 | + // Configure API client |
| 57 | + apiClient, err := client.ConfigureAlphaClient(params.Printer, params.CliVersion) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + |
| 62 | + // Call API |
| 63 | + request := apiClient.GetRoutingTableOfArea( |
| 64 | + ctx, |
| 65 | + *model.OrganizationId, |
| 66 | + *model.NetworkAreaId, |
| 67 | + model.Region, |
| 68 | + *model.RoutingTableId, |
| 69 | + ) |
| 70 | + |
| 71 | + response, err := request.Execute() |
| 72 | + if err != nil { |
| 73 | + return fmt.Errorf("describe routing-tables: %w", err) |
| 74 | + } |
| 75 | + |
| 76 | + return outputResult(params.Printer, model.OutputFormat, response) |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + configureFlags(cmd) |
| 81 | + return cmd |
| 82 | +} |
| 83 | + |
| 84 | +func configureFlags(cmd *cobra.Command) { |
| 85 | + cmd.Flags().Var(flags.UUIDFlag(), organizationIdFlag, "Organization ID") |
| 86 | + cmd.Flags().Var(flags.UUIDFlag(), networkAreaIdFlag, "Network-Area ID") |
| 87 | + |
| 88 | + err := flags.MarkFlagsRequired(cmd, organizationIdFlag, networkAreaIdFlag) |
| 89 | + cobra.CheckErr(err) |
| 90 | +} |
| 91 | + |
| 92 | +func parseInput(p *print.Printer, cmd *cobra.Command, args []string) (*inputModel, error) { |
| 93 | + globalFlags := globalflags.Parse(p, cmd) |
| 94 | + if globalFlags.ProjectId == "" { |
| 95 | + return nil, &errors.ProjectIdError{} |
| 96 | + } |
| 97 | + |
| 98 | + if len(args) <= 0 { |
| 99 | + return nil, fmt.Errorf("at least one argument is required") |
| 100 | + } |
| 101 | + routingTableId := args[0] |
| 102 | + |
| 103 | + model := inputModel{ |
| 104 | + GlobalFlagModel: globalFlags, |
| 105 | + NetworkAreaId: flags.FlagToStringPointer(p, cmd, networkAreaIdFlag), |
| 106 | + OrganizationId: flags.FlagToStringPointer(p, cmd, organizationIdFlag), |
| 107 | + RoutingTableId: &routingTableId, |
| 108 | + } |
| 109 | + |
| 110 | + if p.IsVerbosityDebug() { |
| 111 | + modelStr, err := print.BuildDebugStrFromInputModel(model) |
| 112 | + if err != nil { |
| 113 | + p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err) |
| 114 | + } else { |
| 115 | + p.Debug(print.DebugLevel, "parsed input values: %s", modelStr) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return &model, nil |
| 120 | +} |
| 121 | + |
| 122 | +func outputResult(p *print.Printer, outputFormat string, routingTable *iaasalpha.RoutingTable) error { |
| 123 | + switch outputFormat { |
| 124 | + case print.JSONOutputFormat: |
| 125 | + details, err := json.MarshalIndent(routingTable, "", " ") |
| 126 | + if err != nil { |
| 127 | + return fmt.Errorf("marshal routing-table describe: %w", err) |
| 128 | + } |
| 129 | + p.Outputln(string(details)) |
| 130 | + |
| 131 | + return nil |
| 132 | + case print.YAMLOutputFormat: |
| 133 | + details, err := yaml.MarshalWithOptions(routingTable, yaml.IndentSequence(true), yaml.UseJSONMarshaler()) |
| 134 | + if err != nil { |
| 135 | + return fmt.Errorf("marshal routing-table describe: %w", err) |
| 136 | + } |
| 137 | + p.Outputln(string(details)) |
| 138 | + |
| 139 | + return nil |
| 140 | + default: |
| 141 | + var labels []string |
| 142 | + for key, value := range *routingTable.Labels { |
| 143 | + labels = append(labels, fmt.Sprintf("%s: %s", key, value)) |
| 144 | + } |
| 145 | + |
| 146 | + table := tables.NewTable() |
| 147 | + table.SetHeader("ID", "NAME", "DESCRIPTION", "CREATED_AT", "UPDATED_AT", "DEFAULT", "LABELS", "SYSTEM_ROUTES") |
| 148 | + table.AddRow( |
| 149 | + utils.PtrString(routingTable.Id), |
| 150 | + utils.PtrString(routingTable.Name), |
| 151 | + utils.PtrString(routingTable.Description), |
| 152 | + routingTable.CreatedAt.String(), |
| 153 | + routingTable.UpdatedAt.String(), |
| 154 | + utils.PtrString(routingTable.Default), |
| 155 | + strings.Join(labels, "\n"), |
| 156 | + utils.PtrString(routingTable.SystemRoutes), |
| 157 | + ) |
| 158 | + |
| 159 | + err := table.Display(p) |
| 160 | + if err != nil { |
| 161 | + return fmt.Errorf("render table: %w", err) |
| 162 | + } |
| 163 | + |
| 164 | + return nil |
| 165 | + } |
| 166 | +} |
0 commit comments