Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/stackit_beta_server.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ stackit beta server [flags]
* [stackit beta server describe](./stackit_beta_server_describe.md) - Shows details of a server
* [stackit beta server list](./stackit_beta_server_list.md) - Lists all servers of a project
* [stackit beta server log](./stackit_beta_server_log.md) - Gets server console log
* [stackit beta server machine-type](./stackit_beta_server_machine-type.md) - Provides functionality for server machine types available inside a project
* [stackit beta server network-interface](./stackit_beta_server_network-interface.md) - Allows attaching/detaching network interfaces to servers
* [stackit beta server os-update](./stackit_beta_server_os-update.md) - Provides functionality for managed server updates
* [stackit beta server public-ip](./stackit_beta_server_public-ip.md) - Allows attaching/detaching public IPs to servers
Expand Down
35 changes: 35 additions & 0 deletions docs/stackit_beta_server_machine-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## stackit beta server machine-type

Provides functionality for server machine types available inside a project

### Synopsis

Provides functionality for server machine types available inside a project.

```
stackit beta server machine-type [flags]
```

### Options

```
-h, --help Help for "stackit beta server machine-type"
```

### Options inherited from parent commands

```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
-p, --project-id string Project ID
--region string Target region for region-specific requests
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```

### SEE ALSO

* [stackit beta server](./stackit_beta_server.md) - Provides functionality for servers
* [stackit beta server machine-type describe](./stackit_beta_server_machine-type_describe.md) - Shows details of a server machine type
* [stackit beta server machine-type list](./stackit_beta_server_machine-type_list.md) - Get list of all machine types available in a project

43 changes: 43 additions & 0 deletions docs/stackit_beta_server_machine-type_describe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
## stackit beta server machine-type describe

Shows details of a server machine type

### Synopsis

Shows details of a server machine type.

```
stackit beta server machine-type describe MACHINE_TYPE [flags]
```

### Examples

```
Show details of a server machine type with name "xxx"
$ stackit beta server machine-type describe xxx

Show details of a server machine type with name "xxx" in JSON format
$ stackit beta server machine-type describe xxx --output-format json
```

### Options

```
-h, --help Help for "stackit beta server machine-type describe"
```

### Options inherited from parent commands

```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
-p, --project-id string Project ID
--region string Target region for region-specific requests
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```

### SEE ALSO

* [stackit beta server machine-type](./stackit_beta_server_machine-type.md) - Provides functionality for server machine types available inside a project

47 changes: 47 additions & 0 deletions docs/stackit_beta_server_machine-type_list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
## stackit beta server machine-type list

Get list of all machine types available in a project

### Synopsis

Get list of all machine types available in a project.

```
stackit beta server machine-type list [flags]
```

### Examples

```
Get list of all machine types
$ stackit beta server machine-type list

Get list of all machine types in JSON format
$ stackit beta server machine-type list --output-format json

List the first 10 machine types
$ stackit beta server machine-type list --limit=10
```

### Options

```
-h, --help Help for "stackit beta server machine-type list"
--limit int Limit the output to the first n elements
```

### Options inherited from parent commands

```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
-p, --project-id string Project ID
--region string Target region for region-specific requests
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```

### SEE ALSO

* [stackit beta server machine-type](./stackit_beta_server_machine-type.md) - Provides functionality for server machine types available inside a project

140 changes: 140 additions & 0 deletions internal/cmd/beta/server/machine-type/describe/describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package describe

import (
"context"
"encoding/json"
"fmt"

"github.com/goccy/go-yaml"

"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
"github.com/stackitcloud/stackit-sdk-go/services/iaas"

"github.com/spf13/cobra"
)

const (
machineTypeArg = "MACHINE_TYPE"
)

type inputModel struct {
*globalflags.GlobalFlagModel
MachineType string
}

func NewCmd(p *print.Printer) *cobra.Command {
cmd := &cobra.Command{
Use: fmt.Sprintf("describe %s", machineTypeArg),
Short: "Shows details of a server machine type",
Long: "Shows details of a server machine type.",
Args: args.SingleArg(machineTypeArg, nil),
Example: examples.Build(
examples.NewExample(
`Show details of a server machine type with name "xxx"`,
"$ stackit beta server machine-type describe xxx",
),
examples.NewExample(
`Show details of a server machine type with name "xxx" in JSON format`,
"$ stackit beta server machine-type describe xxx --output-format json",
),
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
model, err := parseInput(p, cmd, args)
if err != nil {
return err
}

// Configure API client
apiClient, err := client.ConfigureClient(p)
if err != nil {
return err
}

// Call API
req := buildRequest(ctx, model, apiClient)
resp, err := req.Execute()
if err != nil {
return fmt.Errorf("read server machine type: %w", err)
}

return outputResult(p, model.OutputFormat, resp)
},
}
return cmd
}

func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
machineType := inputArgs[0]

globalFlags := globalflags.Parse(p, cmd)
if globalFlags.ProjectId == "" {
return nil, &errors.ProjectIdError{}
}

model := inputModel{
GlobalFlagModel: globalFlags,
MachineType: machineType,
}

if p.IsVerbosityDebug() {
modelStr, err := print.BuildDebugStrFromInputModel(model)
if err != nil {
p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err)
} else {
p.Debug(print.DebugLevel, "parsed input values: %s", modelStr)
}
}

return &model, nil
}

func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiGetMachineTypeRequest {
return apiClient.GetMachineType(ctx, model.ProjectId, model.MachineType)
}

func outputResult(p *print.Printer, outputFormat string, machineType *iaas.MachineType) error {
switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(machineType, "", " ")
if err != nil {
return fmt.Errorf("marshal server machine type: %w", err)
}
p.Outputln(string(details))

return nil
case print.YAMLOutputFormat:
details, err := yaml.MarshalWithOptions(machineType, yaml.IndentSequence(true))
if err != nil {
return fmt.Errorf("marshal server machine type: %w", err)
}
p.Outputln(string(details))

return nil
default:
table := tables.NewTable()
table.AddRow("NAME", utils.PtrString(machineType.Name))
table.AddSeparator()
table.AddRow("VCPUS", utils.PtrString(machineType.Vcpus))
table.AddSeparator()
table.AddRow("RAM (in MB)", utils.PtrString(machineType.Ram))
table.AddSeparator()
table.AddRow("DISK SIZE (in GB)", utils.PtrString(machineType.Disk))
table.AddSeparator()
table.AddRow("DESCRIPTION", utils.PtrString(machineType.Description))
table.AddSeparator()

err := table.Display(p)
if err != nil {
return fmt.Errorf("render table: %w", err)
}
return nil
}
}
Loading