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.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ stackit beta [flags]
### SEE ALSO

* [stackit](./stackit.md) - Manage STACKIT resources using the command line
* [stackit beta affinity-group](./stackit_beta_affinity-group.md) - Manage server affinity groups
* [stackit beta image](./stackit_beta_image.md) - Manage server images
* [stackit beta key-pair](./stackit_beta_key-pair.md) - Provides functionality for SSH key pairs
* [stackit beta network](./stackit_beta_network.md) - Provides functionality for networks
Expand Down
37 changes: 37 additions & 0 deletions docs/stackit_beta_affinity-group.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## stackit beta affinity-group

Manage server affinity groups

### Synopsis

Manage the lifecycle of server affinity groups.

```
stackit beta affinity-group [flags]
```

### Options

```
-h, --help Help for "stackit beta affinity-group"
```

### 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](./stackit_beta.md) - Contains beta STACKIT CLI commands
* [stackit beta affinity-group create](./stackit_beta_affinity-group_create.md) - Creates an affinity groups
* [stackit beta affinity-group delete](./stackit_beta_affinity-group_delete.md) - Deletes an affinity group
* [stackit beta affinity-group describe](./stackit_beta_affinity-group_describe.md) - Show details of an affinity group
* [stackit beta affinity-group list](./stackit_beta_affinity-group_list.md) - Lists affinity groups

42 changes: 42 additions & 0 deletions docs/stackit_beta_affinity-group_create.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
## stackit beta affinity-group create

Creates an affinity groups

### Synopsis

Creates an affinity groups.

```
stackit beta affinity-group create [flags]
```

### Examples

```
Create an affinity group with name "AFFINITY_GROUP_NAME" and policy "soft-affinity"
$ stackit beta affinity-group create --name AFFINITY_GROUP_NAME --policy soft-affinity
```

### Options

```
-h, --help Help for "stackit beta affinity-group create"
--name string The name of the affinity group.
--policy string The policy for the affinity group. Valid values for the policy are: "hard-affinity", "hard-anti-affinity", "soft-affinity", "soft-anti-affinity"
```

### 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 affinity-group](./stackit_beta_affinity-group.md) - Manage server affinity groups

40 changes: 40 additions & 0 deletions docs/stackit_beta_affinity-group_delete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## stackit beta affinity-group delete

Deletes an affinity group

### Synopsis

Deletes an affinity group.

```
stackit beta affinity-group delete AFFINITY_GROUP [flags]
```

### Examples

```
Delete an affinity group with ID "xxx"
$ stackit beta affinity-group delete xxx
```

### Options

```
-h, --help Help for "stackit beta affinity-group delete"
```

### 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 affinity-group](./stackit_beta_affinity-group.md) - Manage server affinity groups

40 changes: 40 additions & 0 deletions docs/stackit_beta_affinity-group_describe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## stackit beta affinity-group describe

Show details of an affinity group

### Synopsis

Show details of an affinity group.

```
stackit beta affinity-group describe AFFINITY_GROUP_ID [flags]
```

### Examples

```
Get details about an affinity group with the ID "xxx"
$ stackit beta affinity-group describe xxx
```

### Options

```
-h, --help Help for "stackit beta affinity-group 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 affinity-group](./stackit_beta_affinity-group.md) - Manage server affinity groups

44 changes: 44 additions & 0 deletions docs/stackit_beta_affinity-group_list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
## stackit beta affinity-group list

Lists affinity groups

### Synopsis

Lists affinity groups.

```
stackit beta affinity-group list [flags]
```

### Examples

```
Lists all affinity groups
$ stackit beta affinity-group list

Lists up to 10 affinity groups
$ stackit beta affinity-group list --limit=10
```

### Options

```
-h, --help Help for "stackit beta affinity-group 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 affinity-group](./stackit_beta_affinity-group.md) - Manage server affinity groups

33 changes: 33 additions & 0 deletions internal/cmd/beta/affinity-groups/affinity-groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package affinity_groups

import (
"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/affinity-groups/create"
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/affinity-groups/delete"
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/affinity-groups/describe"
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/affinity-groups/list"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
)

func NewCmd(p *print.Printer) *cobra.Command {
cmd := &cobra.Command{
Use: "affinity-group",
Short: "Manage server affinity groups",
Long: "Manage the lifecycle of server affinity groups.",
Args: args.NoArgs,
Run: utils.CmdHelp,
}
addSubcommands(cmd, p)
return cmd
}

func addSubcommands(cmd *cobra.Command, p *print.Printer) {
cmd.AddCommand(
create.NewCmd(p),
delete.NewCmd(p),
describe.NewCmd(p),
list.NewCmd(p),
)
}
148 changes: 148 additions & 0 deletions internal/cmd/beta/affinity-groups/create/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package create

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

"github.com/goccy/go-yaml"
"github.com/spf13/cobra"
"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/flags"
"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/utils"
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
)

const (
nameFlag = "name"
policyFlag = "policy"
)

type inputModel struct {
*globalflags.GlobalFlagModel
Name string
Policy string
}

func NewCmd(p *print.Printer) *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Creates an affinity groups",
Long: `Creates an affinity groups.`,
Args: args.NoArgs,
Example: examples.Build(
examples.NewExample(
`Create an affinity group with name "AFFINITY_GROUP_NAME" and policy "soft-affinity"`,
"$ stackit beta affinity-group create --name AFFINITY_GROUP_NAME --policy soft-affinity",
),
),
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := context.Background()
model, err := parseInput(p, cmd)
if err != nil {
return err
}

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

if !model.AssumeYes {
prompt := fmt.Sprintf("Are you sure you want to create the affinity group %q?", model.Name)
err = p.PromptForConfirmation(prompt)
if err != nil {
return err
}
}

// Call API
request := buildRequest(ctx, *model, apiClient)

result, err := request.Execute()
if err != nil {
return fmt.Errorf("create affinity group: %w", err)
}
if resp := result; resp != nil {
return outputResult(p, *model, *resp)
}
return fmt.Errorf("create affinity group: nil result")
},
}
configureFlags(cmd)
return cmd
}

func configureFlags(cmd *cobra.Command) {
cmd.Flags().String(nameFlag, "", "The name of the affinity group.")
cmd.Flags().String(policyFlag, "", `The policy for the affinity group. Valid values for the policy are: "hard-affinity", "hard-anti-affinity", "soft-affinity", "soft-anti-affinity"`)

if err := flags.MarkFlagsRequired(cmd, nameFlag, policyFlag); err != nil {
cobra.CheckErr(err)
}
}

func buildRequest(ctx context.Context, model inputModel, apiClient *iaas.APIClient) iaas.ApiCreateAffinityGroupRequest {
req := apiClient.CreateAffinityGroup(ctx, model.ProjectId)
req = req.CreateAffinityGroupPayload(
iaas.CreateAffinityGroupPayload{
Name: utils.Ptr(model.Name),
Policy: utils.Ptr(model.Policy),
},
)
return req
}

func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
globalFlags := globalflags.Parse(p, cmd)
if globalFlags.ProjectId == "" {
return nil, &errors.ProjectIdError{}
}

model := inputModel{
GlobalFlagModel: globalFlags,
Name: flags.FlagToStringValue(p, cmd, nameFlag),
Policy: flags.FlagToStringValue(p, cmd, policyFlag),
}

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 outputResult(p *print.Printer, model inputModel, resp iaas.AffinityGroup) error {
outputFormat := ""
if model.GlobalFlagModel != nil {
outputFormat = model.GlobalFlagModel.OutputFormat
}
switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(resp, "", " ")
if err != nil {
return fmt.Errorf("marshal affinity group: %w", err)
}
p.Outputln(string(details))
case print.YAMLOutputFormat:
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
if err != nil {
return fmt.Errorf("marshal affinity group: %w", err)
}
p.Outputln(string(details))
default:
p.Outputf("Created affinity group %q with id %s\n", model.Name, utils.PtrString(resp.Id))
}
return nil
}
Loading