66 "net"
77 "reflect"
88 "sort"
9+ "strconv"
910 "time"
1011
1112 "github.com/fatih/color"
@@ -178,28 +179,43 @@ func serverUpdateBuilder(c *core.Command) *core.Command {
178179 * instance.UpdateServerRequest
179180 IP * instance.NullableStringValue
180181 PlacementGroupID * instance.NullableStringValue
181- SecurityGroupID string
182+ SecurityGroupID * string
183+ VolumeIDs * []string
182184 }
183185
184- IPArgSpec := & core.ArgSpec {
185- Name : "ip" ,
186- Short : `IP that should be attached to the server (use ip=none to remove)` ,
187- }
188- c .ArgSpecs .GetByName ("placement-group" ).Name = "placement-group-id"
189-
190186 c .ArgsType = reflect .TypeOf (instanceUpdateServerRequestCustom {})
191187
192- c . ArgSpecs = append ( c . ArgSpecs , IPArgSpec )
193- c .ArgSpecs .DeleteByName ( "security -group.name" )
188+ // Rename modified arg specs.
189+ c .ArgSpecs .GetByName ( "placement -group" ). Name = "placement-group-id"
194190 c .ArgSpecs .GetByName ("security-group.id" ).Name = "security-group-id"
195191
192+ // Delete unused arg specs.
193+ c .ArgSpecs .DeleteByName ("security-group.name" )
194+ c .ArgSpecs .DeleteByName ("volumes.{key}.name" )
195+ c .ArgSpecs .DeleteByName ("volumes.{key}.size" )
196+ c .ArgSpecs .DeleteByName ("volumes.{key}.id" )
197+ c .ArgSpecs .DeleteByName ("volumes.{key}.volume-type" )
198+ c .ArgSpecs .DeleteByName ("volumes.{key}.organization" )
199+
200+ // Add new arg specs.
201+ c .ArgSpecs .AddBefore ("placement-group-id" , & core.ArgSpec {
202+ Name : "volume-ids.{index}" ,
203+ Short : "Will update ALL volume IDs at once, including the root volume of the server (use volume-ids=none to detach all volumes)" ,
204+ })
205+ c .ArgSpecs .AddBefore ("boot-type" , & core.ArgSpec {
206+ Name : "ip" ,
207+ Short : `IP that should be attached to the server (use ip=none to detach)` ,
208+ })
209+
196210 c .Run = func (ctx context.Context , argsI interface {}) (i interface {}, e error ) {
197211 customRequest := argsI .(* instanceUpdateServerRequestCustom )
198212
199213 updateServerRequest := customRequest .UpdateServerRequest
200214 updateServerRequest .PlacementGroup = customRequest .PlacementGroupID
201- updateServerRequest .SecurityGroup = & instance.SecurityGroupTemplate {
202- ID : customRequest .SecurityGroupID ,
215+ if customRequest .SecurityGroupID != nil {
216+ updateServerRequest .SecurityGroup = & instance.SecurityGroupTemplate {
217+ ID : * customRequest .SecurityGroupID ,
218+ }
203219 }
204220
205221 attachIPRequest := (* instance .UpdateIPRequest )(nil )
@@ -210,11 +226,11 @@ func serverUpdateBuilder(c *core.Command) *core.Command {
210226 api := instance .NewAPI (client )
211227
212228 getServerResponse , err := api .GetServer (& instance.GetServerRequest {
213- Zone : "" ,
229+ Zone : updateServerRequest . Zone ,
214230 ServerID : customRequest .ServerID ,
215231 })
216232 if err != nil {
217- return "" , err
233+ return nil , err
218234 }
219235
220236 switch {
@@ -255,20 +271,30 @@ func serverUpdateBuilder(c *core.Command) *core.Command {
255271 },
256272 })
257273 if err != nil {
258- return "" , err
274+ return nil , err
259275 }
260276 }
261277
262278 if attachIPRequest != nil {
263279 _ , err = api .UpdateIP (attachIPRequest )
264280 if err != nil {
265- return "" , err
281+ return nil , err
282+ }
283+ }
284+
285+ // Update all volume IDs at once.
286+ if customRequest .VolumeIDs != nil {
287+ volumes := make (map [string ]* instance.VolumeTemplate )
288+ for i , volumeID := range * customRequest .VolumeIDs {
289+ index := strconv .Itoa (i )
290+ volumes [index ] = & instance.VolumeTemplate {ID : volumeID , Name : getServerResponse .Server .Name + "-" + index }
266291 }
292+ customRequest .Volumes = & volumes
267293 }
268294
269295 updateServerResponse , err := api .UpdateServer (updateServerRequest )
270296 if err != nil {
271- return "" , err
297+ return nil , err
272298 }
273299
274300 return updateServerResponse , nil
0 commit comments