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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Features

- compute: add `exo compute security-group update` command (#981)

## 1.106.0

### Features
Expand Down
119 changes: 119 additions & 0 deletions cmd/compute/security_group/security_group_update.go
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

Copy link
Copy Markdown
Member

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 are omitempty in UpdateSecurityGroupRequest, 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 in CmdPreRun with a clear error rather than pretend it worked.

Repro
exo compute security-group create sg-test --description 'initial desc'
exo -O json compute security-group update sg-test --description '' | jq -c '{name,description}'
# {"name":"sg-test","description":"initial desc"}
exo -O json compute security-group update sg-test --name '' | jq -c '{name,description}'
# {"name":"sg-test","description":"initial desc"}

@imiric imiric Dec 3, 2026 •

Copy link
Copy Markdown
Member Author

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.

}

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 tests/e2e/scenarios/with-api/compute/security_group_update.txtar
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'
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"
Loading