use MarshalJSON
diff --git a/openstack/compute/v2/servers/requests.go b/openstack/compute/v2/servers/requests.go
index 18a2237..da0c737 100644
--- a/openstack/compute/v2/servers/requests.go
+++ b/openstack/compute/v2/servers/requests.go
@@ -2,6 +2,7 @@
import (
"encoding/base64"
+ "encoding/json"
"errors"
"fmt"
@@ -97,32 +98,26 @@
}
// Personality is an array of files that are injected into the server at launch.
-type Personality []File
-
-// Marshal marshals the personality, marshalling each of the files.
-func (p Personality) Marshal() []map[string]string {
- personality := make([]map[string]string, len(p))
- for i, file := range p {
- personality[i] = file.Marshal()
- }
-
- return personality
-}
+type Personality []*File
// File is used within CreateOpts and RebuildOpts to inject a file into the server at launch.
type File struct {
// Path of the file
- Path string `json:"path"`
+ Path string
// Contents of the file. Maximum content size is 255 bytes.
- Contents []byte `json:"contents"`
+ Contents []byte
}
-// Marshal marshals the file, base64 encoding the contents.
-func (f File) Marshal() map[string]string {
- return map[string]string{
- "path": f.Path,
- "contents": base64.StdEncoding.EncodeToString(f.Contents),
+// MarshalJSON marshals the escaped file, base64 encoding the contents.
+func (f *File) MarshalJSON() ([]byte, error) {
+ file := struct {
+ Path string `json:"path"`
+ Contents string `json:"contents"`
+ }{
+ Path: f.Path,
+ Contents: base64.StdEncoding.EncodeToString(f.Contents),
}
+ return json.Marshal(file)
}
// CreateOpts specifies server creation parameters.
@@ -229,7 +224,7 @@
}
if len(opts.Personality) > 0 {
- server["personality"] = opts.Personality.Marshal()
+ server["personality"] = opts.Personality
}
return map[string]interface{}{"server": server}, nil
@@ -460,7 +455,7 @@
}
if len(opts.Personality) > 0 {
- server["personality"] = opts.Personality.Marshal()
+ server["personality"] = opts.Personality
}
return map[string]interface{}{"rebuild": server}, nil
diff --git a/openstack/compute/v2/servers/requests_test.go b/openstack/compute/v2/servers/requests_test.go
index 83fcdb0..d878bd0 100644
--- a/openstack/compute/v2/servers/requests_test.go
+++ b/openstack/compute/v2/servers/requests_test.go
@@ -2,6 +2,7 @@
import (
"encoding/base64"
+ "encoding/json"
"net/http"
"testing"
@@ -328,17 +329,30 @@
}
func TestMarshalPersonality(t *testing.T) {
- name := "test"
+ name := "/etc/test"
contents := []byte("asdfasdf")
personality := Personality{
- File{
+ &File{
Path: name,
Contents: contents,
},
}
- actual := personality.Marshal()
+ data, err := json.Marshal(personality)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ var actual []map[string]string
+ err = json.Unmarshal(data, &actual)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if len(actual) != 1 {
+ t.Fatal("expected personality length 1")
+ }
if actual[0]["path"] != name {
t.Fatal("file path incorrect")