Repository navigation
feat(compute): add security-group update command #981
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
Merged
Merged
Changes from all commits
Commits
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,119 @@ | ||
| package security_group | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| exocmd "github.com/exoscale/cli/cmd" | ||
| "github.com/exoscale/cli/pkg/account" | ||
| "github.com/exoscale/cli/pkg/clicmd" | ||
| "github.com/exoscale/cli/pkg/globalstate" | ||
| "github.com/exoscale/cli/pkg/output" | ||
| "github.com/exoscale/cli/utils" | ||
| v3 "github.com/exoscale/egoscale/v3" | ||
| ) | ||
|
|
||
| type securityGroupUpdateCmd struct { | ||
| clicmd.CliCommandSettings `cli-cmd:"-"` | ||
|
|
||
| _ bool `cli-cmd:"update"` | ||
|
|
||
| SecurityGroup string `cli-arg:"#" cli-usage:"NAME|ID"` | ||
|
|
||
| Name string `cli-usage:"Security Group name"` | ||
| Description string `cli-usage:"Security Group description"` | ||
| } | ||
|
|
||
| func (c *securityGroupUpdateCmd) CmdAliases() []string { return clicmd.GUpdateAlias } | ||
|
|
||
| func (c *securityGroupUpdateCmd) CmdShort() string { | ||
| return "Update a Security Group" | ||
| } | ||
|
|
||
| func (c *securityGroupUpdateCmd) CmdLong() string { | ||
| return fmt.Sprintf(`This command updates a Compute instance Security Group name and/or description. | ||
|
|
||
| Supported output template annotations: %s`, | ||
| strings.Join(output.TemplateAnnotations(&securityGroupShowOutput{}), ", ")) | ||
| } | ||
|
|
||
| func (c *securityGroupUpdateCmd) CmdPreRun(cmd *cobra.Command, args []string) error { | ||
| if err := clicmd.CliCommandDefaultPreRun(c, cmd, args); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if !cmd.Flags().Changed(clicmd.MustCLICommandFlagName(c, &c.Name)) && | ||
| !cmd.Flags().Changed(clicmd.MustCLICommandFlagName(c, &c.Description)) { | ||
| clicmd.CmdExitOnUsageError(cmd, "either --name or --description is required") | ||
| } | ||
|
|
||
| // TODO: remove this validation once the spec marks these fields as nullable | ||
| // and egoscale can send empty values, instead of dropping them via omitempty. | ||
| if cmd.Flags().Changed(clicmd.MustCLICommandFlagName(c, &c.Name)) && c.Name == "" { | ||
| clicmd.CmdExitOnUsageError(cmd, "--name must not be empty") | ||
| } | ||
| if cmd.Flags().Changed(clicmd.MustCLICommandFlagName(c, &c.Description)) && c.Description == "" { | ||
| clicmd.CmdExitOnUsageError(cmd, "clearing the description is not supported yet") | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (c *securityGroupUpdateCmd) CmdRun(cmd *cobra.Command, _ []string) error { | ||
| ctx := exocmd.GContext | ||
| client, err := exocmd.SwitchClientZoneV3(ctx, globalstate.EgoscaleV3Client, v3.ZoneName(account.CurrentAccount.DefaultZone)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| securityGroups, err := client.ListSecurityGroups(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| securityGroup, err := securityGroups.FindSecurityGroup(c.SecurityGroup) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| request := v3.UpdateSecurityGroupRequest{ | ||
| Name: securityGroup.Name, | ||
| Description: securityGroup.Description, | ||
| } | ||
|
|
||
| if cmd.Flags().Changed(clicmd.MustCLICommandFlagName(c, &c.Name)) { | ||
| request.Name = c.Name | ||
| } | ||
|
|
||
| if cmd.Flags().Changed(clicmd.MustCLICommandFlagName(c, &c.Description)) { | ||
| request.Description = c.Description | ||
| } | ||
|
|
||
| op, err := client.UpdateSecurityGroup(ctx, securityGroup.ID, request) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| utils.DecorateAsyncOperation(fmt.Sprintf("Updating Security Group %q...", c.SecurityGroup), func() { | ||
| op, err = client.Wait(ctx, op, v3.OperationStateSuccess) | ||
| }) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if !globalstate.Quiet { | ||
| return (&securityGroupShowCmd{ | ||
| CliCommandSettings: c.CliCommandSettings, | ||
| SecurityGroup: securityGroup.ID.String(), | ||
| }).CmdRun(nil, nil) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func init() { | ||
| cobra.CheckErr(clicmd.RegisterCLICommand(securityGroupCmd, &securityGroupUpdateCmd{ | ||
| CliCommandSettings: clicmd.DefaultCLICmdSettings(), | ||
| })) | ||
| } | ||
47 changes: 47 additions & 0 deletions
47
tests/e2e/scenarios/with-api/compute/security_group_update.txtar
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,47 @@ | ||
| # Test: exo compute security-group update renames SGs and changes their | ||
| # description in place. | ||
| # Full lifecycle: create two SGs, update each field alone and both at once, | ||
| # test name conflict and not-found errors, delete both. | ||
| # TEST_ZONE and TEST_RUN_ID are injected by the API test runner. | ||
|
|
||
| # security-group commands have no --zone flag, they use the default zone | ||
| env EXOSCALE_ZONE=$TEST_ZONE | ||
|
|
||
| # Setup: create the primary Security Group with a description | ||
| exec exo --output-format json compute security-group create $TEST_RUN_ID-sg-a --description 'e2e initial description' | ||
| json-setenv SG_A_ID id stdout | ||
| defer exo compute security-group delete -f $SG_A_ID | ||
|
|
||
| # Setup: create a second SG whose name will be used for the conflict test | ||
| exec exo --output-format json compute security-group create $TEST_RUN_ID-sg-b | ||
| json-setenv SG_B_ID id stdout | ||
| defer exo compute security-group delete -f $SG_B_ID | ||
|
|
||
| # Update --description only: the name must be left unchanged | ||
| exec exo --output-format json compute security-group update $SG_A_ID --description 'e2e updated description' | ||
| stdout '"description": ?"e2e updated description"' | ||
| stdout $TEST_RUN_ID-sg-a | ||
|
|
||
| # Update --name only: the description must be left unchanged | ||
| exec exo --output-format json compute security-group update $SG_A_ID --name $TEST_RUN_ID-sg-a-renamed | ||
| stdout $TEST_RUN_ID-sg-a-renamed | ||
| stdout '"description": ?"e2e updated description"' | ||
|
|
||
| exec exo --output-format json compute security-group show $TEST_RUN_ID-sg-a-renamed | ||
| stdout $SG_A_ID | ||
|
|
||
| # Both flags at once, addressed by name | ||
| exec exo --output-format json compute security-group update $TEST_RUN_ID-sg-a-renamed --name $TEST_RUN_ID-sg-a --description 'e2e final description' | ||
| stdout '"name": ?"'$TEST_RUN_ID-sg-a'"' | ||
| stdout '"description": ?"e2e final description"' | ||
|
|
||
| # --quiet prints nothing | ||
| exec exo --quiet compute security-group update $SG_A_ID --description 'e2e quiet description' | ||
| ! stdout . | ||
|
|
||
| # Conflict: renaming to an already-existing name must surface the API error | ||
| ! exec exo compute security-group update $SG_A_ID --name $TEST_RUN_ID-sg-b | ||
| stderr 'already exists' | ||
|
|
||
| ! exec exo compute security-group update no-such-sg --name whatever | ||
| stderr 'not found' |
49 changes: 49 additions & 0 deletions
49
tests/e2e/scenarios/without-api/compute_security_group_update_flags.txtar
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,49 @@ | ||
| # Test: compute security-group update --help shows the --name and --description | ||
| # flags and the "set" alias, and invocations rejected before any API call fail | ||
| # with a usage error. The config is passed with --config: XDG_CONFIG_HOME is | ||
| # ignored on macOS. | ||
|
|
||
| exec exo compute security-group update --help | ||
| stdout '--description string' | ||
| stdout '--name string' | ||
| stdout 'This command updates a Compute instance Security Group' | ||
|
|
||
| exec exo compute security-group set --help | ||
| stdout 'updates a Compute instance Security Group' | ||
|
|
||
| exec exo compute security-group --help | ||
| stdout ' +update +Update a Security Group' | ||
|
|
||
| # At least one of --name/--description must be set, checked before any API call | ||
| ! exec exo --config exoscale.toml compute security-group update my-sg | ||
| stderr 'either --name or --description is required' | ||
| stderr 'Usage:' | ||
|
|
||
| ! exec exo --config exoscale.toml compute security-group set my-sg | ||
| stderr 'either --name or --description is required' | ||
|
|
||
| ! exec exo --config exoscale.toml compute security-group update --name foo | ||
| stderr 'missing arguments' | ||
|
|
||
| ! exec exo --config exoscale.toml compute security-group update my-sg --name '' | ||
| stderr '--name must not be empty' | ||
| stderr 'Usage:' | ||
|
|
||
| ! exec exo --config exoscale.toml compute security-group update my-sg --description '' | ||
| stderr 'clearing the description is not supported yet' | ||
| stderr 'Usage:' | ||
|
|
||
| ! exec exo --config exoscale.toml compute security-group update my-sg --name '' --description 'whatever' | ||
| stderr '--name must not be empty' | ||
|
|
||
| ! exec exo --config exoscale.toml compute security-group update my-sg --name whatever --description '' | ||
| stderr 'clearing the description is not supported yet' | ||
|
|
||
| -- exoscale.toml -- | ||
| defaultaccount = "test" | ||
|
|
||
| [[accounts]] | ||
| name = "test" | ||
| key = "EXOtest123" | ||
| secret = "testsecret123" | ||
| defaultZone = "ch-gva-2" |
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.
--description ''(and--name '') silently does nothing: both fields areomitemptyinUpdateSecurityGroupRequest, so the empty value never reaches the API, and the command still reports success with the old value. Clearing a description looks like a natural use of this command, so I'd at least reject empty values inCmdPreRunwith a clear error rather than pretend it worked.Repro
Uh oh!
There was an error while loading. Please reload this page.
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.
Nice catch!
This should be changed in the OpenAPI spec and exposed by egoscale, but in the meantime I added some validation like you suggested.
BTW, this is not the only place with this issue. It also happens with EIP updates, SKS nodepool updates, and privnet updates. It is implemented correctly in NLB updates, though.
I'll discuss this internally with relevant teams and maybe propose a fix.