@@ -14,27 +14,29 @@ import (
1414 "github.com/stackitcloud/stackit-cli/internal/pkg/print"
1515 "github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
1616 "github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client"
17+ "github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1718 "github.com/stackitcloud/stackit-sdk-go/services/iaas"
1819)
1920
2021type inputModel struct {
2122 * globalflags.GlobalFlagModel
22- Labels map [string ]any
23- Description string
24- Name string
25- Stateful bool
23+ Labels * map [string ]any
24+ Description * string
25+ Name * string
2626 SecurityGroupId string
2727}
2828
29+ const argNameGroupId = "argGroupId"
30+
2931func NewCmd (p * print.Printer ) * cobra.Command {
3032 cmd := & cobra.Command {
3133 Use : "Update" ,
3234 Short : "Update a security group" ,
33- Long : "Update a security group" ,
34- Args : args .NoArgs ,
35+ Long : "Update a named security group" ,
36+ Args : args .SingleArg ( argNameGroupId , utils . ValidateUUID ) ,
3537 Example : examples .Build (
36- examples .NewExample (`Update a named group` , `$ stackit beta security-group Update -- name my-new-group ` ),
37- examples .NewExample (`Update a named group with labels ` , `$ stackit beta security-group Update --name my-new-group --labels label1=value1,label2=value2` ),
38+ examples .NewExample (`Update the name of a group` , `$ stackit beta security-group update 541d122f-0a5f-4bb0-94b9-b1ccbd7ba776 -- name my-new-name ` ),
39+ examples .NewExample (`Update the labels of a group ` , `$ stackit beta security-group update 541d122f-0a5f-4bb0-94b9-b1ccbd7ba776 --labels label1=value1,label2=value2` ),
3840 ),
3941 RunE : func (cmd * cobra.Command , args []string ) error {
4042 return executeUpdate (cmd , p , args )
@@ -46,20 +48,16 @@ func NewCmd(p *print.Printer) *cobra.Command {
4648}
4749
4850func configureFlags (cmd * cobra.Command ) {
51+ globalflags .Configure (cmd .Flags ())
4952 cmd .Flags ().String ("name" , "" , "the name of the security group. Must be <= 63 chars" )
5053 cmd .Flags ().String ("description" , "" , "an optional description of the security group. Must be <= 127 chars" )
51- cmd .Flags ().Bool ("stateful" , false , "Update a stateful or a stateless security group" )
5254 cmd .Flags ().StringSlice ("labels" , nil , "a list of labels in the form <key>=<value>" )
53-
54- if err := flags .MarkFlagsRequired (cmd , "name" ); err != nil {
55- cobra .CheckErr (err )
56- }
5755}
5856
59- func executeUpdate (cmd * cobra.Command , p * print.Printer , _ []string ) error {
60- p .Info ("executing Update command" )
57+ func executeUpdate (cmd * cobra.Command , p * print.Printer , args []string ) error {
58+ p .Info ("executing update command" )
6159 ctx := context .Background ()
62- model , err := parseInput (p , cmd )
60+ model , err := parseInput (p , cmd , args )
6361 if err != nil {
6462 return err
6563 }
@@ -80,56 +78,75 @@ func executeUpdate(cmd *cobra.Command, p *print.Printer, _ []string) error {
8078 req := buildRequest (ctx , model , apiClient )
8179 _ , err = req .Execute ()
8280 if err != nil {
83- return fmt .Errorf ("Update security group: %w" , err )
81+ return fmt .Errorf ("^date security group: %w" , err )
8482 }
8583
8684 operationState := "Enabled"
8785 if model .Async {
8886 operationState = "Triggered enablement of"
8987 }
90- p .Info ("%s security group %q for %q\n " , operationState , model .Name , projectLabel )
88+ p .Info ("%s security group \" %v \" for %q\n " , operationState , model .Name , projectLabel )
9189 return nil
9290}
9391
94- func parseInput (p * print.Printer , cmd * cobra.Command ) (* inputModel , error ) {
92+ func parseInput (p * print.Printer , cmd * cobra.Command , args [] string ) (* inputModel , error ) {
9593 globalFlags := globalflags .Parse (p , cmd )
9694 if globalFlags .ProjectId == "" {
9795 return nil , & errors.ProjectIdError {}
9896 }
99- name := flags .FlagToStringValue (p , cmd , "name" )
100- if len (name ) >= 64 {
97+
98+ if err := cmd .ValidateArgs (args ); err != nil {
99+ return nil , & errors.ArgValidationError {
100+ Arg : argNameGroupId ,
101+ Details : fmt .Sprintf ("argument validation failed: %v" , err ),
102+ }
103+ }
104+
105+ model := inputModel {
106+ GlobalFlagModel : globalFlags ,
107+ }
108+ if len (args ) != 1 {
101109 return nil , & errors.ArgValidationError {
102- Arg : "invalid name" ,
103- Details : "name exceeds 63 characters in length " ,
110+ Arg : argNameGroupId ,
111+ Details : "wrong number of arguments " ,
104112 }
105113 }
114+ model .SecurityGroupId = args [0 ]
106115
107- labels := make (map [string ]any )
108- for _ , label := range flags .FlagToStringSliceValue (p , cmd , "labels" ) {
109- parts := strings .Split (label , "=" )
110- if len (parts ) != 2 {
116+ if cmd .Flags ().Lookup ("name" ).Changed {
117+ name := flags .FlagToStringValue (p , cmd , "name" )
118+ if len (name ) >= 64 {
111119 return nil , & errors.ArgValidationError {
112- Arg : "labels " ,
113- Details : "invalid label declaration. Must be in the form <key>=<value> " ,
120+ Arg : "invalid name " ,
121+ Details : "name exceeds 63 characters in length " ,
114122 }
115123 }
116- labels [parts [0 ]] = parts [1 ]
117-
124+ model .Name = & name
118125 }
119- description := flags .FlagToStringValue (p , cmd , "description" )
120- if len (description ) >= 128 {
121- return nil , & errors.ArgValidationError {
122- Arg : "invalid description" ,
123- Details : "description exceeds 127 characters in length" ,
126+
127+ if cmd .Flags ().Lookup ("labels" ).Changed {
128+ labels := make (map [string ]any )
129+ for _ , label := range flags .FlagToStringSliceValue (p , cmd , "labels" ) {
130+ parts := strings .Split (label , "=" )
131+ if len (parts ) != 2 {
132+ return nil , & errors.ArgValidationError {
133+ Arg : "labels" ,
134+ Details : "invalid label declaration. Must be in the form <key>=<value>" ,
135+ }
136+ }
137+ labels [parts [0 ]] = parts [1 ]
124138 }
139+ model .Labels = & labels
125140 }
126- model := inputModel {
127- GlobalFlagModel : globalFlags ,
128- Name : name ,
129-
130- Labels : labels ,
131- Description : description ,
132- Stateful : flags .FlagToBoolValue (p , cmd , "stateful" ),
141+ if cmd .Flags ().Lookup ("description" ).Changed {
142+ description := flags .FlagToStringValue (p , cmd , "description" )
143+ if len (description ) >= 128 {
144+ return nil , & errors.ArgValidationError {
145+ Arg : "invalid description" ,
146+ Details : "description exceeds 127 characters in length" ,
147+ }
148+ }
149+ model .Description = & description
133150 }
134151
135152 if p .IsVerbosityDebug () {
@@ -147,13 +164,9 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
147164func buildRequest (ctx context.Context , model * inputModel , apiClient * iaas.APIClient ) iaas.ApiUpdateSecurityGroupRequest {
148165 request := apiClient .UpdateSecurityGroup (ctx , model .ProjectId , model .SecurityGroupId )
149166 payload := iaas .NewUpdateSecurityGroupPayload ()
150- payload .Description = & model .Description
151- if model .Labels != nil {
152- // this check assure that we don't end up with a pointer to nil
153- // which is a thing in go!
154- payload .Labels = & model .Labels
155- }
156- payload .Name = & model .Name
167+ payload .Description = model .Description
168+ payload .Labels = model .Labels
169+ payload .Name = model .Name
157170 request = request .UpdateSecurityGroupPayload (* payload )
158171
159172 return request
0 commit comments