diff options
author | Thomas Hipp <thipp@suse.de> | 2017-12-19 15:26:13 +0100 |
---|---|---|
committer | Flavio Castelli <fcastelli@suse.com> | 2018-02-19 13:25:29 +0100 |
commit | 474cfb2214f15c076bdef941583d91d06c0180ed (patch) | |
tree | dcff23876744d7310ee0bc5cb0af3cb38f8427d6 | |
parent | 75ef90f02d79562d5e921b93dd9ead126dd3d83b (diff) | |
download | terraform-provider-libvirt-474cfb2214f15c076bdef941583d91d06c0180ed.tar terraform-provider-libvirt-474cfb2214f15c076bdef941583d91d06c0180ed.tar.gz |
refactor cloudinit
Signed-off-by: Thomas Hipp <thipp@suse.de>
-rw-r--r-- | libvirt/cloudinit_def.go | 56 | ||||
-rw-r--r-- | libvirt/cloudinit_def_test.go | 12 | ||||
-rw-r--r-- | libvirt/resource_cloud_init.go | 4 |
3 files changed, 37 insertions, 35 deletions
diff --git a/libvirt/cloudinit_def.go b/libvirt/cloudinit_def.go index b1cd2c27..1f6fd629 100644 --- a/libvirt/cloudinit_def.go +++ b/libvirt/cloudinit_def.go @@ -19,35 +19,37 @@ import ( "gopkg.in/yaml.v2" ) -// USERDATA is the filename expected by cloud-init -const USERDATA string = "user-data" +// userData is the filename expected by cloud-init +const userData string = "user-data" -// METADATA is the filename expected by cloud-init -const METADATA string = "meta-data" +// metaData is the filename expected by cloud-init +const metaData string = "meta-data" -// CloudInitUserData struct -type CloudInitUserData struct { +type defCloudInitUserData struct { SSHAuthorizedKeys []string `yaml:"ssh_authorized_keys"` } +type defCloudInitMetaData struct { + LocalHostname string `yaml:"local-hostname,omitempty"` + InstanceID string `yaml:"instance-id"` +} + type defCloudInit struct { - Name string - PoolName string - Metadata struct { - LocalHostname string `yaml:"local-hostname,omitempty"` - InstanceID string `yaml:"instance-id"` - } + Name string + PoolName string + MetaData defCloudInitMetaData UserDataRaw string `yaml:"user_data"` - UserData CloudInitUserData + UserData defCloudInitUserData } // Creates a new cloudinit with the defaults // the provider uses func newCloudInitDef() defCloudInit { - ci := defCloudInit{} - ci.Metadata.InstanceID = fmt.Sprintf("created-at-%s", time.Now().String()) - - return ci + return defCloudInit{ + MetaData: defCloudInitMetaData{ + InstanceID: fmt.Sprintf("created-at-%s", time.Now().String()), + }, + } } // Create a ISO file based on the contents of the CloudInit instance and @@ -157,8 +159,8 @@ func (ci *defCloudInit) createISO() (string, error) { "cidata", "-joliet", "-rock", - filepath.Join(tmpDir, USERDATA), - filepath.Join(tmpDir, METADATA)) + filepath.Join(tmpDir, userData), + filepath.Join(tmpDir, metaData)) log.Printf("About to execute cmd: %+v", cmd) if err = cmd.Run(); err != nil { @@ -189,17 +191,17 @@ func (ci *defCloudInit) createFiles() (string, error) { userdata := fmt.Sprintf("#cloud-config\n%s", mergedUserData) if err = ioutil.WriteFile( - filepath.Join(tmpDir, USERDATA), + filepath.Join(tmpDir, userData), []byte(userdata), os.ModePerm); err != nil { return "", fmt.Errorf("Error while writing user-data to file: %s", err) } - metadata, err := yaml.Marshal(&ci.Metadata) + metadata, err := yaml.Marshal(&ci.MetaData) if err != nil { return "", fmt.Errorf("Error dumping cloudinit's meta data: %s", err) } - if err = ioutil.WriteFile(filepath.Join(tmpDir, METADATA), metadata, os.ModePerm); err != nil { + if err = ioutil.WriteFile(filepath.Join(tmpDir, metaData), metadata, os.ModePerm); err != nil { return "", fmt.Errorf("Error while writing meta-data to file: %s", err) } @@ -271,7 +273,7 @@ func newCloudInitDefFromRemoteISO(virConn *libvirt.Connect, id string) (defCloud if f.Name() == "/user_dat." { data, err := ioutil.ReadAll(f.Sys().(io.Reader)) if err != nil { - return ci, fmt.Errorf("Error while reading %s: %s", USERDATA, err) + return ci, fmt.Errorf("Error while reading %s: %s", userData, err) } if err := yaml.Unmarshal(data, &ci.UserData); err != nil { return ci, fmt.Errorf("Error while unmarshalling user-data: %s", err) @@ -288,9 +290,9 @@ func newCloudInitDefFromRemoteISO(virConn *libvirt.Connect, id string) (defCloud if f.Name() == "/meta_dat." { data, err := ioutil.ReadAll(f.Sys().(io.Reader)) if err != nil { - return ci, fmt.Errorf("Error while reading %s: %s", METADATA, err) + return ci, fmt.Errorf("Error while reading %s: %s", metaData, err) } - if err := yaml.Unmarshal(data, &ci.Metadata); err != nil { + if err := yaml.Unmarshal(data, &ci.MetaData); err != nil { return ci, fmt.Errorf("Error while unmarshalling user-data: %s", err) } } @@ -339,7 +341,7 @@ func downloadISO(virConn *libvirt.Connect, volume libvirt.StorageVol) (*os.File, } // Convert a UserData instance to a map with string as key and interface as value -func convertUserDataToMap(data CloudInitUserData) (map[string]interface{}, error) { +func convertUserDataToMap(data defCloudInitUserData) (map[string]interface{}, error) { userDataMap := make(map[string]interface{}) // This is required to get the right names expected by cloud-init @@ -353,7 +355,7 @@ func convertUserDataToMap(data CloudInitUserData) (map[string]interface{}, error return userDataMap, err } -func mergeUserDataIntoUserDataRaw(userData CloudInitUserData, userDataRaw string) (string, error) { +func mergeUserDataIntoUserDataRaw(userData defCloudInitUserData, userDataRaw string) (string, error) { userDataMap, err := convertUserDataToMap(userData) if err != nil { return "", err diff --git a/libvirt/cloudinit_def_test.go b/libvirt/cloudinit_def_test.go index 4f8091dc..b040d80f 100644 --- a/libvirt/cloudinit_def_test.go +++ b/libvirt/cloudinit_def_test.go @@ -17,7 +17,7 @@ import ( func TestNewCloudInitDef(t *testing.T) { ci := newCloudInitDef() - if ci.Metadata.InstanceID == "" { + if ci.MetaData.InstanceID == "" { t.Error("Expected metadata InstanceID not to be empty") } } @@ -47,7 +47,7 @@ func TestCreateFiles(t *testing.T) { } defer os.RemoveAll(dir) - for _, file := range []string{USERDATA, METADATA} { + for _, file := range []string{userData, metaData} { check, err := exists(filepath.Join(dir, file)) if !check { t.Errorf("%s not found: %v", file, err) @@ -74,7 +74,7 @@ func TestCreateISONoExteralTool(t *testing.T) { } func TestConvertUserDataToMapPreservesCloudInitNames(t *testing.T) { - ud := CloudInitUserData{ + ud := defCloudInitUserData{ SSHAuthorizedKeys: []string{"key1"}, } @@ -90,7 +90,7 @@ func TestConvertUserDataToMapPreservesCloudInitNames(t *testing.T) { } func TestMergeEmptyUserDataIntoUserDataRaw(t *testing.T) { - ud := CloudInitUserData{} + ud := defCloudInitUserData{} var userDataRaw = ` new-key: new-value-set-by-extra @@ -119,7 +119,7 @@ ssh_authorized_keys: } func TestMergeUserDataIntoEmptyUserDataRaw(t *testing.T) { - ud := CloudInitUserData{ + ud := defCloudInitUserData{ SSHAuthorizedKeys: []string{"key1"}, } var userDataRaw string @@ -142,7 +142,7 @@ func TestMergeUserDataIntoEmptyUserDataRaw(t *testing.T) { func TestMergeUserDataIntoUserDataRawGivesPrecedenceToRawData(t *testing.T) { udKey := "user-data-key" - ud := CloudInitUserData{ + ud := defCloudInitUserData{ SSHAuthorizedKeys: []string{udKey}, } diff --git a/libvirt/resource_cloud_init.go b/libvirt/resource_cloud_init.go index be52e02d..97193803 100644 --- a/libvirt/resource_cloud_init.go +++ b/libvirt/resource_cloud_init.go @@ -51,7 +51,7 @@ func resourceCloudInitCreate(d *schema.ResourceData, meta interface{}) error { } cloudInit := newCloudInitDef() - cloudInit.Metadata.LocalHostname = d.Get("local_hostname").(string) + cloudInit.MetaData.LocalHostname = d.Get("local_hostname").(string) cloudInit.UserDataRaw = d.Get("user_data").(string) if _, ok := d.GetOk("ssh_authorized_key"); ok { @@ -94,7 +94,7 @@ func resourceCloudInitRead(d *schema.ResourceData, meta interface{}) error { } d.Set("pool", ci.PoolName) d.Set("name", ci.Name) - d.Set("local_hostname", ci.Metadata.LocalHostname) + d.Set("local_hostname", ci.MetaData.LocalHostname) d.Set("user_data", ci.UserDataRaw) if len(ci.UserData.SSHAuthorizedKeys) == 1 { |