@@ -252,9 +252,12 @@ func resourceCloudStackInstanceCreate(d *schema.ResourceData, meta interface{})
252
252
p .SetKeypair (keypair .(string ))
253
253
}
254
254
255
- if ud , err := getUserData (d , cs ); err != nil {
256
- return err
257
- } else if len (ud ) > 0 {
255
+ if userData , ok := d .GetOk ("user_data" ); ok {
256
+ ud , err := getUserData (userData .(string ), cs .HTTPGETOnly )
257
+ if err != nil {
258
+ return err
259
+ }
260
+
258
261
p .SetUserdata (ud )
259
262
}
260
263
@@ -438,6 +441,7 @@ func resourceCloudStackInstanceUpdate(d *schema.ResourceData, meta interface{})
438
441
d .SetPartial ("service_offering" )
439
442
}
440
443
444
+ // Check if the affinity group IDs have changed and if so, update the IDs
441
445
if d .HasChange ("affinity_group_ids" ) {
442
446
p := cs .AffinityGroup .NewUpdateVMAffinityGroupParams (d .Id ())
443
447
groups := []string {}
@@ -451,6 +455,7 @@ func resourceCloudStackInstanceUpdate(d *schema.ResourceData, meta interface{})
451
455
p .SetAffinitygroupids (groups )
452
456
}
453
457
458
+ // Check if the affinity group names have changed and if so, update the names
454
459
if d .HasChange ("affinity_group_names" ) {
455
460
p := cs .AffinityGroup .NewUpdateVMAffinityGroupParams (d .Id ())
456
461
groups := []string {}
@@ -464,6 +469,7 @@ func resourceCloudStackInstanceUpdate(d *schema.ResourceData, meta interface{})
464
469
p .SetAffinitygroupids (groups )
465
470
}
466
471
472
+ // Check if the keypair has changed and if so, update the keypair
467
473
if d .HasChange ("keypair" ) {
468
474
log .Printf ("[DEBUG] SSH keypair changed for %s, starting update" , name )
469
475
@@ -478,10 +484,11 @@ func resourceCloudStackInstanceUpdate(d *schema.ResourceData, meta interface{})
478
484
d .SetPartial ("keypair" )
479
485
}
480
486
487
+ // Check if the user data has changed and if so, update the user data
481
488
if d .HasChange ("user_data" ) {
482
489
log .Printf ("[DEBUG] user_data changed for %s, starting update" , name )
483
490
484
- ud , err := getUserData (d , cs )
491
+ ud , err := getUserData (d . Get ( "user_data" ).( string ) , cs . HTTPGETOnly )
485
492
if err != nil {
486
493
return err
487
494
}
@@ -534,28 +541,23 @@ func resourceCloudStackInstanceDelete(d *schema.ResourceData, meta interface{})
534
541
return nil
535
542
}
536
543
537
- // getUserData returns user_data as a base64 encoded string. An empty
538
- // string is returned if unset.
539
- func getUserData (d * schema.ResourceData , cs * cloudstack.CloudStackClient ) (string , error ) {
540
- if userData , ok := d .GetOk ("user_data" ); ok {
541
- ud := base64 .StdEncoding .EncodeToString ([]byte (userData .(string )))
542
-
543
- // deployVirtualMachine uses POST by default, so max userdata is 32K
544
- maxUD := 32768
544
+ // getUserData returns the user data as a base64 encoded string
545
+ func getUserData (userData string , httpGetOnly bool ) (string , error ) {
546
+ ud := base64 .StdEncoding .EncodeToString ([]byte (userData ))
545
547
546
- if cs .HTTPGETOnly {
547
- // deployVirtualMachine using GET instead, so max userdata is 2K
548
- maxUD = 2048
549
- }
548
+ // deployVirtualMachine uses POST by default, so max userdata is 32K
549
+ maxUD := 32768
550
550
551
- if len (ud ) > maxUD {
552
- return "" , fmt .Errorf (
553
- "The supplied user_data contains %d bytes after encoding, " +
554
- "this exeeds the limit of %d bytes" , len (ud ), maxUD )
555
- }
551
+ if httpGetOnly {
552
+ // deployVirtualMachine using GET instead, so max userdata is 2K
553
+ maxUD = 2048
554
+ }
556
555
557
- return ud , nil
556
+ if len (ud ) > maxUD {
557
+ return "" , fmt .Errorf (
558
+ "The supplied user_data contains %d bytes after encoding, " +
559
+ "this exeeds the limit of %d bytes" , len (ud ), maxUD )
558
560
}
559
561
560
- return "" , nil
562
+ return ud , nil
561
563
}
0 commit comments