diff --git a/cloudinit/cloud_config.go b/cloudinit/cloud_config.go index 1091df8..ac901bb 100644 --- a/cloudinit/cloud_config.go +++ b/cloudinit/cloud_config.go @@ -1,6 +1,7 @@ package cloudinit import ( + "fmt" "log" "launchpad.net/goyaml" @@ -26,11 +27,14 @@ func NewCloudConfig(contents []byte) (*CloudConfig, error) { func (cc CloudConfig) String() string { bytes, err := goyaml.Marshal(cc) - if err == nil { - return string(bytes) - } else { + if err != nil { return "" } + + stringified := string(bytes) + stringified = fmt.Sprintf("#cloud-config\n%s", stringified) + + return stringified } func ApplyCloudConfig(cfg CloudConfig, sshKeyName string) error { diff --git a/cloudinit/cloud_config_test.go b/cloudinit/cloud_config_test.go index 46a1e19..827d009 100644 --- a/cloudinit/cloud_config_test.go +++ b/cloudinit/cloud_config_test.go @@ -1,6 +1,7 @@ package cloudinit import ( + "strings" "testing" ) @@ -146,3 +147,12 @@ ssh_authorized_keys: t.Error("Parsed incorrect number of SSH keys") } } + +func TestCloudConfigSerializationHeader(t *testing.T) { + cfg, _ := NewCloudConfig([]byte{}) + contents := cfg.String() + header := strings.SplitN(contents, "\n", 2)[0] + if header != "#cloud-config" { + t.Fatalf("Serialized config did not have expected header") + } +}