diff options
author | Duncan Mac-Vicar P <dmacvicar@suse.de> | 2017-11-23 14:23:15 +0100 |
---|---|---|
committer | Duncan Mac-Vicar P <dmacvicar@suse.de> | 2017-11-24 17:56:44 +0100 |
commit | a1145d25f878d0fc70229428c8076c27423d5a07 (patch) | |
tree | d1bbc6df6939755902d53f616c6ae7dc06650b9c | |
parent | 06f312e1ca81074d3806816535d3039251691fb4 (diff) | |
download | terraform-provider-libvirt-a1145d25f878d0fc70229428c8076c27423d5a07.tar terraform-provider-libvirt-a1145d25f878d0fc70229428c8076c27423d5a07.tar.gz |
Read back cmdline, initrd and kernel attributes from the resource
-rw-r--r-- | libvirt/resource_libvirt_domain.go | 9 | ||||
-rw-r--r-- | libvirt/utils_domain_def.go | 27 | ||||
-rw-r--r-- | libvirt/utils_domain_def_test.go | 36 |
3 files changed, 71 insertions, 1 deletions
diff --git a/libvirt/resource_libvirt_domain.go b/libvirt/resource_libvirt_domain.go index 272d1eec..4dae7c29 100644 --- a/libvirt/resource_libvirt_domain.go +++ b/libvirt/resource_libvirt_domain.go @@ -305,7 +305,6 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error cmdlineArgs = append(cmdlineArgs, fmt.Sprintf("%s=%v", k, v)) } } - sort.Strings(cmdlineArgs) domainDef.OS.KernelArgs = strings.Join(cmdlineArgs, " ") @@ -905,6 +904,14 @@ func resourceLibvirtDomainRead(d *schema.ResourceData, meta interface{}) error { d.Set("autostart", autostart) d.Set("arch", domainDef.OS.Type.Arch) + cmdLines, err := splitKernelCmdLine(domainDef.OS.KernelArgs) + if err != nil { + return err + } + d.Set("cmdline", cmdLines) + d.Set("kernel", domainDef.OS.Kernel) + d.Set("initrd", domainDef.OS.Initrd) + caps, err := getHostCapabilities(virConn) if err != nil { return err diff --git a/libvirt/utils_domain_def.go b/libvirt/utils_domain_def.go index a712f145..4e46cb62 100644 --- a/libvirt/utils_domain_def.go +++ b/libvirt/utils_domain_def.go @@ -4,6 +4,7 @@ import ( "fmt" libvirtxml "github.com/libvirt/libvirt-go-xml" "log" + "strings" ) func getGuestForArchType(caps libvirtxml.Caps, arch string, virttype string) (libvirtxml.CapsGuest, error) { @@ -47,3 +48,29 @@ func getOriginalMachineName(caps libvirtxml.Caps, arch string, virttype string, } return targetmachine, nil // There wasn't a canonical mapping to this } + +// as kernal args allow duplicate keys, we use a list of maps +// we jump to a next map as soon as we find a duplicate +// key +func splitKernelCmdLine(cmdLine string) ([]map[string]string, error) { + var cmdLines []map[string]string + currCmdLine := make(map[string]string) + argVals := strings.Split(cmdLine, " ") + for _, argVal := range argVals { + kv := strings.Split(argVal, "=") + if len(kv) != 2 { + return nil, fmt.Errorf("Can't parse kernel command line: '%s'", cmdLine) + } + k, v := kv[0], kv[1] + // if the key is duplicate, start a new map + if _, ok := currCmdLine[k]; ok { + cmdLines = append(cmdLines, currCmdLine) + currCmdLine = make(map[string]string) + } + currCmdLine[k] = v + } + if len(currCmdLine) > 0 { + cmdLines = append(cmdLines, currCmdLine) + } + return cmdLines, nil +} diff --git a/libvirt/utils_domain_def_test.go b/libvirt/utils_domain_def_test.go new file mode 100644 index 00000000..b497c02c --- /dev/null +++ b/libvirt/utils_domain_def_test.go @@ -0,0 +1,36 @@ +package libvirt + +import ( + "reflect" + "testing" + + "github.com/davecgh/go-spew/spew" +) + +func init() { + spew.Config.Indent = "\t" +} + +func TestSplitKernelCmdLine(t *testing.T) { + { + e := []map[string]string{{"foo": "bar"}, {"foo": "bar", "key": "val"}} + r, err := splitKernelCmdLine("foo=bar foo=bar key=val") + if !reflect.DeepEqual(r, e) { + t.Fatalf("got='%s' expected='%s'", spew.Sdump(r), spew.Sdump(e)) + } + if err != nil { + t.Error(err) + } + } + + { + v := "foo=barfoo=bar" + r, err := splitKernelCmdLine(v) + if r != nil { + t.Fatalf("got='%s' expected='%s'", spew.Sdump(r), err) + } + if err == nil { + t.Errorf("Expected error for parsing '%s'", v) + } + } +} |