-
Notifications
You must be signed in to change notification settings - Fork 35
feat(server): Implement and document security group attach/detach commands #1341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
s-inter
wants to merge
8
commits into
main
Choose a base branch
from
feat/server-security-group-attach-and-detach
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5aa1caa
feat(server): create security-group attach and detach command scaffolds
s-inter 0b406a0
fix(server): fix typo
s-inter c45dcbc
feat(server): implement attach security group command
s-inter 879b341
feat(server): implement detach security group command
s-inter 8a32a6c
feat(server): refactor attach command to use flags instead of positio…
s-inter 57d004d
feat(server): refactor detach command to use flags instead of positio…
s-inter 902777c
fix(server): fix unused-parameter linter warning, update parseInput f…
s-inter a107df6
feat(server): add documentation for security group attach and detach …
s-inter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| ## stackit server security-group | ||
|
|
||
| Allows attaching/detaching security groups to servers | ||
|
|
||
| ### Synopsis | ||
|
|
||
| Allows attaching/detaching security groups to servers. | ||
|
|
||
| ``` | ||
| stackit server security-group [flags] | ||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -h, --help Help for "stackit server security-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 server](./stackit_server.md) - Provides functionality for servers | ||
| * [stackit server security-group attach](./stackit_server_security-group_attach.md) - Attaches a security group to a server | ||
| * [stackit server security-group detach](./stackit_server_security-group_detach.md) - Detaches a security group from a server | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| ## stackit server security-group attach | ||
|
|
||
| Attaches a security group to a server | ||
|
|
||
| ### Synopsis | ||
|
|
||
| Attaches a security group to a server. | ||
|
|
||
| ``` | ||
| stackit server security-group attach [flags] | ||
| ``` | ||
|
|
||
| ### Examples | ||
|
|
||
| ``` | ||
| Attach a security group with ID "xxx" to a server with ID "yyy" | ||
| $ stackit server security-group attach --server-id yyy --security-group-id xxx | ||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -h, --help Help for "stackit server security-group attach" | ||
| --security-group-id string Security Group ID | ||
| --server-id string Server ID | ||
| ``` | ||
|
|
||
| ### 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 server security-group](./stackit_server_security-group.md) - Allows attaching/detaching security groups to servers | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| ## stackit server security-group detach | ||
|
|
||
| Detaches a security group from a server | ||
|
|
||
| ### Synopsis | ||
|
|
||
| Detaches a security group from a server. | ||
|
|
||
| ``` | ||
| stackit server security-group detach [flags] | ||
| ``` | ||
|
|
||
| ### Examples | ||
|
|
||
| ``` | ||
| Detach a security group with ID "xxx" from a server with ID "yyy" | ||
| $ stackit server security-group detach --server-id yyy --security-group-id xxx | ||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -h, --help Help for "stackit server security-group detach" | ||
| --security-group-id string Security Group ID | ||
| --server-id string Server ID | ||
| ``` | ||
|
|
||
| ### 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 server security-group](./stackit_server_security-group.md) - Allows attaching/detaching security groups to servers | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package attach | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/stackitcloud/stackit-cli/internal/pkg/types" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/args" | ||
| cliErr "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" | ||
| iaasUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/utils" | ||
| "github.com/stackitcloud/stackit-sdk-go/services/iaas" | ||
| ) | ||
|
|
||
| const ( | ||
| serverIdFlag = "server-id" | ||
| securityGroupIdFlag = "security-group-id" | ||
| ) | ||
|
|
||
| type inputModel struct { | ||
| *globalflags.GlobalFlagModel | ||
| ServerId string | ||
| SecurityGroupId string | ||
| } | ||
|
|
||
| func NewCmd(params *types.CmdParams) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "attach", | ||
| Short: "Attaches a security group to a server", | ||
| Long: "Attaches a security group to a server.", | ||
| Args: args.NoArgs, | ||
| Example: examples.Build( | ||
| examples.NewExample( | ||
| `Attach a security group with ID "xxx" to a server with ID "yyy"`, | ||
| `$ stackit server security-group attach --server-id yyy --security-group-id xxx`, | ||
| ), | ||
| ), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| ctx := context.Background() | ||
| model, err := parseInput(params.Printer, cmd, args) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Configure API client | ||
| apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| serverLabel, err := iaasUtils.GetServerName(ctx, apiClient, model.ProjectId, model.Region, model.ServerId) | ||
| if err != nil { | ||
| params.Printer.Debug(print.ErrorLevel, "get server name: %v", err) | ||
| serverLabel = model.ServerId | ||
| } else if serverLabel == "" { | ||
| serverLabel = model.ServerId | ||
| } | ||
|
|
||
| securityGroupLabel, err := iaasUtils.GetSecurityGroupName(ctx, apiClient, model.ProjectId, model.Region, model.SecurityGroupId) | ||
| if err != nil { | ||
| params.Printer.Debug(print.ErrorLevel, "get security group name: %v", err) | ||
| securityGroupLabel = model.SecurityGroupId | ||
| } | ||
|
|
||
| prompt := fmt.Sprintf("Are you sure you want to attach security group %q to server %q?", securityGroupLabel, serverLabel) | ||
| err = params.Printer.PromptForConfirmation(prompt) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Call API | ||
| req := buildRequest(ctx, model, apiClient) | ||
| if err := req.Execute(); err != nil { | ||
| return fmt.Errorf("attach security group to server: %w", err) | ||
| } | ||
|
|
||
| params.Printer.Info("Attached security group %q to server %q\n", securityGroupLabel, serverLabel) | ||
|
|
||
| return nil | ||
| }, | ||
| } | ||
| configureFlags(cmd) | ||
| return cmd | ||
| } | ||
|
|
||
| func configureFlags(cmd *cobra.Command) { | ||
| cmd.Flags().Var(flags.UUIDFlag(), serverIdFlag, "Server ID") | ||
| cmd.Flags().Var(flags.UUIDFlag(), securityGroupIdFlag, "Security Group ID") | ||
|
|
||
| err := flags.MarkFlagsRequired(cmd, serverIdFlag, securityGroupIdFlag) | ||
| cobra.CheckErr(err) | ||
| } | ||
|
|
||
| func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) { | ||
| globalFlags := globalflags.Parse(p, cmd) | ||
| if globalFlags.ProjectId == "" { | ||
| return nil, &cliErr.ProjectIdError{} | ||
| } | ||
|
|
||
| model := inputModel{ | ||
| GlobalFlagModel: globalFlags, | ||
| ServerId: flags.FlagToStringValue(p, cmd, serverIdFlag), | ||
| SecurityGroupId: flags.FlagToStringValue(p, cmd, securityGroupIdFlag), | ||
| } | ||
|
|
||
| p.DebugInputModel(model) | ||
| return &model, nil | ||
| } | ||
|
|
||
| func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiAddSecurityGroupToServerRequest { | ||
| req := apiClient.AddSecurityGroupToServer(ctx, model.ProjectId, model.Region, model.ServerId, model.SecurityGroupId) | ||
| return req | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is often handled wrong in the codebase, that's why I'll explain it in detail. Please make sure to remember this in the future and especially look out for it in code reviews you will do in the future. 😅
Outputfprints to stdout,Infoprints to stderr. See the code examples below for details.Since this output here is a success message and no error message, it belongs into the stdout output from my perspective.
stackit-cli/internal/pkg/print/print.go
Lines 68 to 76 in 26bc5f1
stackit-cli/internal/pkg/print/print.go
Lines 107 to 114 in 26bc5f1