computer/v2/servers: Check if opts.UserData is already Base64 Encoded (#170)
* computer/v2/servers: Check if opts.UserData is already Base64 Encoded
* Tweaks following review
* Add tests for UserData generation using both string and Base64 encoded values
diff --git a/openstack/compute/v2/servers/requests.go b/openstack/compute/v2/servers/requests.go
index 4ec2cf0..0ec5b0f 100644
--- a/openstack/compute/v2/servers/requests.go
+++ b/openstack/compute/v2/servers/requests.go
@@ -145,7 +145,7 @@
SecurityGroups []string `json:"-"`
// UserData contains configuration information or scripts to use upon launch.
- // Create will base64-encode it for you.
+ // Create will base64-encode it for you, if it isn't already.
UserData []byte `json:"-"`
// AvailabilityZone in which to launch the server.
@@ -190,8 +190,13 @@
}
if opts.UserData != nil {
- encoded := base64.StdEncoding.EncodeToString(opts.UserData)
- b["user_data"] = &encoded
+ var userData string
+ if _, err := base64.StdEncoding.DecodeString(string(opts.UserData)); err != nil {
+ userData = base64.StdEncoding.EncodeToString(opts.UserData)
+ } else {
+ userData = string(opts.UserData)
+ }
+ b["user_data"] = &userData
}
if len(opts.SecurityGroups) > 0 {