@@ -252,9 +252,12 @@ func resourceCloudStackInstanceCreate(d *schema.ResourceData, meta interface{})
252252 p .SetKeypair (keypair .(string ))
253253 }
254254
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+
258261 p .SetUserdata (ud )
259262 }
260263
@@ -438,6 +441,7 @@ func resourceCloudStackInstanceUpdate(d *schema.ResourceData, meta interface{})
438441 d .SetPartial ("service_offering" )
439442 }
440443
444+ // Check if the affinity group IDs have changed and if so, update the IDs
441445 if d .HasChange ("affinity_group_ids" ) {
442446 p := cs .AffinityGroup .NewUpdateVMAffinityGroupParams (d .Id ())
443447 groups := []string {}
@@ -451,6 +455,7 @@ func resourceCloudStackInstanceUpdate(d *schema.ResourceData, meta interface{})
451455 p .SetAffinitygroupids (groups )
452456 }
453457
458+ // Check if the affinity group names have changed and if so, update the names
454459 if d .HasChange ("affinity_group_names" ) {
455460 p := cs .AffinityGroup .NewUpdateVMAffinityGroupParams (d .Id ())
456461 groups := []string {}
@@ -464,6 +469,7 @@ func resourceCloudStackInstanceUpdate(d *schema.ResourceData, meta interface{})
464469 p .SetAffinitygroupids (groups )
465470 }
466471
472+ // Check if the keypair has changed and if so, update the keypair
467473 if d .HasChange ("keypair" ) {
468474 log .Printf ("[DEBUG] SSH keypair changed for %s, starting update" , name )
469475
@@ -478,10 +484,11 @@ func resourceCloudStackInstanceUpdate(d *schema.ResourceData, meta interface{})
478484 d .SetPartial ("keypair" )
479485 }
480486
487+ // Check if the user data has changed and if so, update the user data
481488 if d .HasChange ("user_data" ) {
482489 log .Printf ("[DEBUG] user_data changed for %s, starting update" , name )
483490
484- ud , err := getUserData (d , cs )
491+ ud , err := getUserData (d . Get ( "user_data" ).( string ) , cs . HTTPGETOnly )
485492 if err != nil {
486493 return err
487494 }
@@ -534,28 +541,23 @@ func resourceCloudStackInstanceDelete(d *schema.ResourceData, meta interface{})
534541 return nil
535542}
536543
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 ))
545547
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
550550
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+ }
556555
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 )
558560 }
559561
560- return "" , nil
562+ return ud , nil
561563}
0 commit comments