From 4bf71329261f27fc6f8c70fbe60910529e8f4a83 Mon Sep 17 00:00:00 2001 From: Dario Maiocchi Date: Thu, 16 Nov 2017 16:49:53 +0100 Subject: remove duplicata declaration and simplify code. This fix is with the cmd "gofmt -w -s *" --- libvirt/domain_def.go | 6 ++-- libvirt/provider.go | 2 +- libvirt/qemu_agent_test.go | 18 +++++------ libvirt/resource_cloud_init.go | 10 +++---- libvirt/resource_libvirt_coreos_ignition.go | 6 ++-- libvirt/resource_libvirt_domain.go | 46 ++++++++++++++--------------- libvirt/resource_libvirt_domain_console.go | 8 ++--- libvirt/resource_libvirt_domain_netiface.go | 20 ++++++------- libvirt/resource_libvirt_domain_test.go | 34 ++++++++++----------- libvirt/resource_libvirt_network.go | 20 ++++++------- libvirt/resource_libvirt_volume.go | 16 +++++----- libvirt/resource_libvirt_volume_test.go | 6 ++-- 12 files changed, 96 insertions(+), 96 deletions(-) diff --git a/libvirt/domain_def.go b/libvirt/domain_def.go index 85004051..5f443a62 100644 --- a/libvirt/domain_def.go +++ b/libvirt/domain_def.go @@ -36,13 +36,13 @@ func newDomainDef() libvirtxml.Domain { CPU: &libvirtxml.DomainCPU{}, Devices: &libvirtxml.DomainDeviceList{ Graphics: []libvirtxml.DomainGraphic{ - libvirtxml.DomainGraphic{ + { Type: "spice", AutoPort: "yes", }, }, Channels: []libvirtxml.DomainChannel{ - libvirtxml.DomainChannel{ + { Type: "unix", Target: &libvirtxml.DomainChannelTarget{ Type: "virtio", @@ -51,7 +51,7 @@ func newDomainDef() libvirtxml.Domain { }, }, RNGs: []libvirtxml.DomainRNG{ - libvirtxml.DomainRNG{ + { Model: "virtio", Backend: &libvirtxml.DomainRNGBackend{ Model: "random", diff --git a/libvirt/provider.go b/libvirt/provider.go index e1ebaeb4..07b0c72e 100644 --- a/libvirt/provider.go +++ b/libvirt/provider.go @@ -13,7 +13,7 @@ var poolMutexKV = mutexkv.NewMutexKV() func Provider() terraform.ResourceProvider { return &schema.Provider{ Schema: map[string]*schema.Schema{ - "uri": &schema.Schema{ + "uri": { Type: schema.TypeString, Required: true, DefaultFunc: schema.EnvDefaultFunc("LIBVIRT_DEFAULT_URI", nil), diff --git a/libvirt/qemu_agent_test.go b/libvirt/qemu_agent_test.go index b38cd9d3..03546054 100644 --- a/libvirt/qemu_agent_test.go +++ b/libvirt/qemu_agent_test.go @@ -38,11 +38,11 @@ func TestGetDomainInterfacesViaQemuAgentNoInterfaces(t *testing.T) { func TestGetDomainInterfacesViaQemuAgentIgnoreLoopbackDevice(t *testing.T) { response := QemuAgentInterfacesResponse{ Interfaces: []QemuAgentInterface{ - QemuAgentInterface{ + { Name: "lo", Hwaddr: "ho:me", IPAddresses: []QemuAgentInterfaceIPAddress{ - QemuAgentInterfaceIPAddress{ + { Type: "ipv4", Address: "127.0.0.1", Prefix: 1, @@ -69,11 +69,11 @@ func TestGetDomainInterfacesViaQemuAgentIgnoreLoopbackDevice(t *testing.T) { func TestGetDomainInterfacesViaQemuAgentIgnoreDevicesWithoutAddress(t *testing.T) { response := QemuAgentInterfacesResponse{ Interfaces: []QemuAgentInterface{ - QemuAgentInterface{ + { Name: "eth1", Hwaddr: "xy:yy:zz", IPAddresses: []QemuAgentInterfaceIPAddress{ - QemuAgentInterfaceIPAddress{ + { Type: "ipv4", Address: "", Prefix: 1, @@ -100,11 +100,11 @@ func TestGetDomainInterfacesViaQemuAgentIgnoreDevicesWithoutAddress(t *testing.T func TestGetDomainInterfacesViaQemuAgentUnknownIpAddressType(t *testing.T) { response := QemuAgentInterfacesResponse{ Interfaces: []QemuAgentInterface{ - QemuAgentInterface{ + { Name: "eth2", Hwaddr: "zy:yy:zz", IPAddresses: []QemuAgentInterfaceIPAddress{ - QemuAgentInterfaceIPAddress{ + { Type: "ipv8", Address: "i don't exist", Prefix: 1, @@ -136,16 +136,16 @@ func TestGetDomainInterfacesViaQemuAgent(t *testing.T) { response := QemuAgentInterfacesResponse{ Interfaces: []QemuAgentInterface{ - QemuAgentInterface{ + { Name: device, Hwaddr: mac, IPAddresses: []QemuAgentInterfaceIPAddress{ - QemuAgentInterfaceIPAddress{ + { Type: "ipv4", Address: ipv4Addr, Prefix: 1, }, - QemuAgentInterfaceIPAddress{ + { Type: "ipv6", Address: ipv6Addr, Prefix: 1, diff --git a/libvirt/resource_cloud_init.go b/libvirt/resource_cloud_init.go index 23639546..be52e02d 100644 --- a/libvirt/resource_cloud_init.go +++ b/libvirt/resource_cloud_init.go @@ -13,28 +13,28 @@ func resourceCloudInit() *schema.Resource { Read: resourceCloudInitRead, Delete: resourceCloudInitDelete, Schema: map[string]*schema.Schema{ - "name": &schema.Schema{ + "name": { Type: schema.TypeString, Required: true, ForceNew: true, }, - "pool": &schema.Schema{ + "pool": { Type: schema.TypeString, Optional: true, Default: "default", ForceNew: true, }, - "local_hostname": &schema.Schema{ + "local_hostname": { Type: schema.TypeString, Optional: true, ForceNew: true, }, - "user_data": &schema.Schema{ + "user_data": { Type: schema.TypeString, Optional: true, ForceNew: true, }, - "ssh_authorized_key": &schema.Schema{ + "ssh_authorized_key": { Type: schema.TypeString, Optional: true, ForceNew: true, diff --git a/libvirt/resource_libvirt_coreos_ignition.go b/libvirt/resource_libvirt_coreos_ignition.go index 01312663..5c14886d 100644 --- a/libvirt/resource_libvirt_coreos_ignition.go +++ b/libvirt/resource_libvirt_coreos_ignition.go @@ -12,18 +12,18 @@ func resourceIgnition() *schema.Resource { Read: resourceIgnitionRead, Delete: resourceIgnitionDelete, Schema: map[string]*schema.Schema{ - "name": &schema.Schema{ + "name": { Type: schema.TypeString, Required: true, ForceNew: true, }, - "pool": &schema.Schema{ + "pool": { Type: schema.TypeString, Optional: true, Default: "default", ForceNew: true, }, - "content": &schema.Schema{ + "content": { Type: schema.TypeString, Required: true, ForceNew: true, diff --git a/libvirt/resource_libvirt_domain.go b/libvirt/resource_libvirt_domain.go index 7266923b..1925b3ac 100644 --- a/libvirt/resource_libvirt_domain.go +++ b/libvirt/resource_libvirt_domain.go @@ -46,58 +46,58 @@ func resourceLibvirtDomain() *schema.Resource { Create: schema.DefaultTimeout(5 * time.Minute), }, Schema: map[string]*schema.Schema{ - "name": &schema.Schema{ + "name": { Type: schema.TypeString, Required: true, ForceNew: true, }, - "metadata": &schema.Schema{ + "metadata": { Type: schema.TypeString, Required: false, Optional: true, ForceNew: false, }, - "vcpu": &schema.Schema{ + "vcpu": { Type: schema.TypeInt, Optional: true, Default: 1, ForceNew: true, }, - "memory": &schema.Schema{ + "memory": { Type: schema.TypeInt, Optional: true, Default: 512, ForceNew: true, }, - "firmware": &schema.Schema{ + "firmware": { Type: schema.TypeString, Optional: true, ForceNew: true, }, - "nvram": &schema.Schema{ + "nvram": { Type: schema.TypeMap, Optional: true, ForceNew: true, }, - "running": &schema.Schema{ + "running": { Type: schema.TypeBool, Optional: true, Default: true, ForceNew: false, }, - "cloudinit": &schema.Schema{ + "cloudinit": { Type: schema.TypeString, Required: false, Optional: true, ForceNew: false, }, - "coreos_ignition": &schema.Schema{ + "coreos_ignition": { Type: schema.TypeString, Optional: true, ForceNew: true, Default: "", }, - "filesystem": &schema.Schema{ + "filesystem": { Type: schema.TypeList, Optional: true, Required: false, @@ -106,7 +106,7 @@ func resourceLibvirtDomain() *schema.Resource { Type: schema.TypeMap, }, }, - "disk": &schema.Schema{ + "disk": { Type: schema.TypeList, Optional: true, Required: false, @@ -115,7 +115,7 @@ func resourceLibvirtDomain() *schema.Resource { Type: schema.TypeMap, }, }, - "network_interface": &schema.Schema{ + "network_interface": { Type: schema.TypeList, Optional: true, Required: false, @@ -123,12 +123,12 @@ func resourceLibvirtDomain() *schema.Resource { Schema: networkInterfaceCommonSchema(), }, }, - "graphics": &schema.Schema{ + "graphics": { Type: schema.TypeMap, Optional: true, Required: false, }, - "console": &schema.Schema{ + "console": { Type: schema.TypeList, Optional: true, Required: false, @@ -136,28 +136,28 @@ func resourceLibvirtDomain() *schema.Resource { Schema: consoleSchema(), }, }, - "cpu": &schema.Schema{ + "cpu": { Type: schema.TypeMap, Optional: true, Required: false, ForceNew: true, }, - "autostart": &schema.Schema{ + "autostart": { Type: schema.TypeBool, Optional: true, Required: false, }, - "machine": &schema.Schema{ + "machine": { Type: schema.TypeString, Optional: true, Default: "pc", }, - "arch": &schema.Schema{ + "arch": { Type: schema.TypeString, Optional: true, Default: "x86_64", }, - "boot_device": &schema.Schema{ + "boot_device": { Type: schema.TypeList, Optional: true, Required: false, @@ -165,7 +165,7 @@ func resourceLibvirtDomain() *schema.Resource { Schema: bootDeviceSchema(), }, }, - "emulator": &schema.Schema{ + "emulator": { Type: schema.TypeString, Default: "/usr/bin/qemu-system-x86_64", Optional: true, @@ -176,7 +176,7 @@ func resourceLibvirtDomain() *schema.Resource { func bootDeviceSchema() map[string]*schema.Schema { return map[string]*schema.Schema{ - "dev": &schema.Schema{ + "dev": { Type: schema.TypeList, Optional: true, Required: false, @@ -249,7 +249,7 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error if graphics, ok := d.GetOk("graphics"); ok { graphicsMap := graphics.(map[string]interface{}) domainDef.Devices.Graphics = []libvirtxml.DomainGraphic{ - libvirtxml.DomainGraphic{}, + {}, } if graphicsType, ok := graphicsMap["type"]; ok { domainDef.Devices.Graphics[0].Type = graphicsType.(string) @@ -259,7 +259,7 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error } if listenType, ok := graphicsMap["listen_type"]; ok { domainDef.Devices.Graphics[0].Listeners = []libvirtxml.DomainGraphicListener{ - libvirtxml.DomainGraphicListener{ + { Type: listenType.(string), }, } diff --git a/libvirt/resource_libvirt_domain_console.go b/libvirt/resource_libvirt_domain_console.go index 6159a82a..d8e11248 100644 --- a/libvirt/resource_libvirt_domain_console.go +++ b/libvirt/resource_libvirt_domain_console.go @@ -6,25 +6,25 @@ import ( func consoleSchema() map[string]*schema.Schema { return map[string]*schema.Schema{ - "type": &schema.Schema{ + "type": { Type: schema.TypeString, Optional: false, Required: true, ForceNew: true, }, - "source_path": &schema.Schema{ + "source_path": { Type: schema.TypeString, Optional: true, Required: false, ForceNew: true, }, - "target_port": &schema.Schema{ + "target_port": { Type: schema.TypeString, Optional: false, Required: true, ForceNew: true, }, - "target_type": &schema.Schema{ + "target_type": { Type: schema.TypeString, Optional: true, Required: false, diff --git a/libvirt/resource_libvirt_domain_netiface.go b/libvirt/resource_libvirt_domain_netiface.go index 40fac691..7ffc948a 100644 --- a/libvirt/resource_libvirt_domain_netiface.go +++ b/libvirt/resource_libvirt_domain_netiface.go @@ -6,55 +6,55 @@ import ( func networkInterfaceCommonSchema() map[string]*schema.Schema { return map[string]*schema.Schema{ - "network_id": &schema.Schema{ + "network_id": { Type: schema.TypeString, Optional: true, ForceNew: true, Computed: true, }, - "network_name": &schema.Schema{ + "network_name": { Type: schema.TypeString, Optional: true, ForceNew: true, Computed: true, }, - "bridge": &schema.Schema{ + "bridge": { Type: schema.TypeString, Optional: true, ForceNew: true, }, - "vepa": &schema.Schema{ + "vepa": { Type: schema.TypeString, Optional: true, ForceNew: true, }, - "macvtap": &schema.Schema{ + "macvtap": { Type: schema.TypeString, Optional: true, ForceNew: true, }, - "passthrough": &schema.Schema{ + "passthrough": { Type: schema.TypeString, Optional: true, ForceNew: true, }, - "hostname": &schema.Schema{ + "hostname": { Type: schema.TypeString, Optional: true, Computed: true, ForceNew: false, }, - "mac": &schema.Schema{ + "mac": { Type: schema.TypeString, Optional: true, Computed: true, ForceNew: true, }, - "wait_for_lease": &schema.Schema{ + "wait_for_lease": { Type: schema.TypeBool, Optional: true, }, - "addresses": &schema.Schema{ + "addresses": { Type: schema.TypeList, Optional: true, Computed: true, diff --git a/libvirt/resource_libvirt_domain_test.go b/libvirt/resource_libvirt_domain_test.go index 65d9f898..2c9f1b52 100644 --- a/libvirt/resource_libvirt_domain_test.go +++ b/libvirt/resource_libvirt_domain_test.go @@ -26,7 +26,7 @@ func TestAccLibvirtDomain_Basic(t *testing.T) { Providers: testAccProviders, CheckDestroy: testAccCheckLibvirtDomainDestroy, Steps: []resource.TestStep{ - resource.TestStep{ + { Config: config, Check: resource.ComposeTestCheckFunc( testAccCheckLibvirtDomainExists("libvirt_domain.acceptance-test-domain-1", &domain), @@ -56,7 +56,7 @@ func TestAccLibvirtDomain_Detailed(t *testing.T) { Providers: testAccProviders, CheckDestroy: testAccCheckLibvirtDomainDestroy, Steps: []resource.TestStep{ - resource.TestStep{ + { Config: config, Check: resource.ComposeTestCheckFunc( testAccCheckLibvirtDomainExists("libvirt_domain.acceptance-test-domain-2", &domain), @@ -98,14 +98,14 @@ func TestAccLibvirtDomain_Volume(t *testing.T) { Providers: testAccProviders, CheckDestroy: testAccCheckLibvirtDomainDestroy, Steps: []resource.TestStep{ - resource.TestStep{ + { Config: configVolAttached, Check: resource.ComposeTestCheckFunc( testAccCheckLibvirtDomainExists("libvirt_domain.acceptance-test-domain", &domain), testAccCheckLibvirtVolumeExists("libvirt_volume.acceptance-test-volume", &volume), ), }, - resource.TestStep{ + { Config: configVolDettached, Check: resource.ComposeTestCheckFunc( testAccCheckLibvirtDomainExists("libvirt_domain.acceptance-test-domain", &domain), @@ -150,7 +150,7 @@ func TestAccLibvirtDomain_VolumeTwoDisks(t *testing.T) { Providers: testAccProviders, CheckDestroy: testAccCheckLibvirtDomainDestroy, Steps: []resource.TestStep{ - resource.TestStep{ + { Config: configVolAttached, Check: resource.ComposeTestCheckFunc( testAccCheckLibvirtDomainExists("libvirt_domain.acceptance-test-domain", &domain), @@ -158,7 +158,7 @@ func TestAccLibvirtDomain_VolumeTwoDisks(t *testing.T) { testAccCheckLibvirtVolumeExists("libvirt_volume.acceptance-test-volume2", &volume), ), }, - resource.TestStep{ + { Config: configVolDettached, Check: resource.ComposeTestCheckFunc( testAccCheckLibvirtDomainExists("libvirt_domain.acceptance-test-domain", &domain), @@ -191,7 +191,7 @@ func TestAccLibvirtDomain_ScsiDisk(t *testing.T) { Providers: testAccProviders, CheckDestroy: testAccCheckLibvirtDomainDestroy, Steps: []resource.TestStep{ - resource.TestStep{ + { Config: configScsi, Check: resource.ComposeTestCheckFunc( testAccCheckLibvirtDomainExists("libvirt_domain.acceptance-test-domain", &domain), @@ -262,7 +262,7 @@ func TestAccLibvirtDomain_NetworkInterface(t *testing.T) { Providers: testAccProviders, CheckDestroy: testAccCheckLibvirtDomainDestroy, Steps: []resource.TestStep{ - resource.TestStep{ + { Config: config, Check: resource.ComposeTestCheckFunc( testAccCheckLibvirtDomainExists("libvirt_domain.acceptance-test-domain", &domain), @@ -298,7 +298,7 @@ func TestAccLibvirtDomain_Graphics(t *testing.T) { Providers: testAccProviders, CheckDestroy: testAccCheckLibvirtDomainDestroy, Steps: []resource.TestStep{ - resource.TestStep{ + { Config: config, Check: resource.ComposeTestCheckFunc( testAccCheckLibvirtDomainExists("libvirt_domain.acceptance-test-domain", &domain), @@ -374,7 +374,7 @@ func TestAccLibvirtDomain_Cpu(t *testing.T) { Providers: testAccProviders, CheckDestroy: testAccCheckLibvirtDomainDestroy, Steps: []resource.TestStep{ - resource.TestStep{ + { Config: config, Check: resource.ComposeTestCheckFunc( testAccCheckLibvirtDomainExists("libvirt_domain.acceptance-test-domain", &domain), @@ -399,7 +399,7 @@ func TestAccLibvirtDomain_Autostart(t *testing.T) { Providers: testAccProviders, CheckDestroy: testAccCheckLibvirtDomainDestroy, Steps: []resource.TestStep{ - resource.TestStep{ + { Config: config, Check: resource.ComposeTestCheckFunc( testAccCheckLibvirtDomainExists("libvirt_domain.acceptance-test-domain", &domain), @@ -433,7 +433,7 @@ func TestAccLibvirtDomain_Filesystems(t *testing.T) { Providers: testAccProviders, CheckDestroy: testAccCheckLibvirtDomainDestroy, Steps: []resource.TestStep{ - resource.TestStep{ + { Config: config, Check: resource.ComposeTestCheckFunc( testAccCheckLibvirtDomainExists("libvirt_domain.acceptance-test-domain", &domain), @@ -479,7 +479,7 @@ func TestAccLibvirtDomain_Consoles(t *testing.T) { Providers: testAccProviders, CheckDestroy: testAccCheckLibvirtDomainDestroy, Steps: []resource.TestStep{ - resource.TestStep{ + { Config: config, Check: resource.ComposeTestCheckFunc( testAccCheckLibvirtDomainExists("libvirt_domain.acceptance-test-domain", &domain), @@ -722,7 +722,7 @@ func subtestAccLibvirtDomainFirmwareNoTemplate(t *testing.T, NVRAMPath string, f Providers: testAccProviders, CheckDestroy: testAccCheckLibvirtDomainDestroy, Steps: []resource.TestStep{ - resource.TestStep{ + { Config: config, Check: resource.ComposeTestCheckFunc( testAccCheckLibvirtDomainExists("libvirt_domain.acceptance-test-domain", &domain), @@ -760,7 +760,7 @@ func subtestAccLibvirtDomainFirmwareTemplate(t *testing.T, NVRAMPath string, fir Providers: testAccProviders, CheckDestroy: testAccCheckLibvirtDomainDestroy, Steps: []resource.TestStep{ - resource.TestStep{ + { Config: config, Check: resource.ComposeTestCheckFunc( testAccCheckLibvirtDomainExists("libvirt_domain.acceptance-test-domain", &domain), @@ -793,7 +793,7 @@ func TestAccLibvirtDomain_MachineType(t *testing.T) { Providers: testAccProviders, CheckDestroy: testAccCheckLibvirtDomainDestroy, Steps: []resource.TestStep{ - resource.TestStep{ + { Config: config, Check: resource.ComposeTestCheckFunc( testAccCheckLibvirtDomainExists("libvirt_domain.acceptance-test-domain", &domain), @@ -819,7 +819,7 @@ func TestAccLibvirtDomain_ArchType(t *testing.T) { Providers: testAccProviders, CheckDestroy: testAccCheckLibvirtDomainDestroy, Steps: []resource.TestStep{ - resource.TestStep{ + { Config: config, Check: resource.ComposeTestCheckFunc( testAccCheckLibvirtDomainExists("libvirt_domain.acceptance-test-domain", &domain), diff --git a/libvirt/resource_libvirt_network.go b/libvirt/resource_libvirt_network.go index 50bcedf8..7bf93c75 100644 --- a/libvirt/resource_libvirt_network.go +++ b/libvirt/resource_libvirt_network.go @@ -43,29 +43,29 @@ func resourceLibvirtNetwork() *schema.Resource { Exists: resourceLibvirtNetworkExists, Update: resourceLibvirtNetworkUpdate, Schema: map[string]*schema.Schema{ - "name": &schema.Schema{ + "name": { Type: schema.TypeString, Required: true, ForceNew: true, }, - "domain": &schema.Schema{ + "domain": { Type: schema.TypeString, Optional: true, ForceNew: true, }, - "mode": &schema.Schema{ // can be "none", "nat" (default), "route", "bridge" + "mode": { // can be "none", "nat" (default), "route", "bridge" Type: schema.TypeString, Optional: true, ForceNew: true, Default: netModeNat, }, - "bridge": &schema.Schema{ + "bridge": { Type: schema.TypeString, Optional: true, Computed: true, ForceNew: true, }, - "addresses": &schema.Schema{ + "addresses": { Type: schema.TypeList, Optional: true, Required: false, @@ -74,13 +74,13 @@ func resourceLibvirtNetwork() *schema.Resource { Type: schema.TypeString, }, }, - "running": &schema.Schema{ + "running": { Type: schema.TypeBool, Optional: true, Default: true, ForceNew: false, }, - "dns_forwarder": &schema.Schema{ + "dns_forwarder": { Type: schema.TypeList, Optional: true, Required: false, @@ -95,13 +95,13 @@ func resourceLibvirtNetwork() *schema.Resource { func dnsForwarderSchema() map[string]*schema.Schema { return map[string]*schema.Schema{ - "address": &schema.Schema{ + "address": { Type: schema.TypeString, Optional: true, Required: false, ForceNew: true, }, - "domain": &schema.Schema{ + "domain": { Type: schema.TypeString, Optional: true, Required: false, @@ -237,7 +237,7 @@ func resourceLibvirtNetworkCreate(d *schema.ResourceData, meta interface{}) erro dni.DHCP = &libvirtxml.NetworkDHCP{ Ranges: []libvirtxml.NetworkDHCPRange{ - libvirtxml.NetworkDHCPRange{ + { Start: start.String(), End: end.String(), }, diff --git a/libvirt/resource_libvirt_volume.go b/libvirt/resource_libvirt_volume.go index bc890e3c..7a7f6a40 100644 --- a/libvirt/resource_libvirt_volume.go +++ b/libvirt/resource_libvirt_volume.go @@ -13,44 +13,44 @@ import ( func volumeCommonSchema() map[string]*schema.Schema { return map[string]*schema.Schema{ - "name": &schema.Schema{ + "name": { Type: schema.TypeString, Required: true, ForceNew: true, }, - "pool": &schema.Schema{ + "pool": { Type: schema.TypeString, Optional: true, Default: "default", ForceNew: true, }, - "source": &schema.Schema{ + "source": { Type: schema.TypeString, Optional: true, ForceNew: true, }, - "size": &schema.Schema{ + "size": { Type: schema.TypeInt, Optional: true, Computed: true, ForceNew: true, }, - "format": &schema.Schema{ + "format": { Type: schema.TypeString, Optional: true, ForceNew: true, }, - "base_volume_id": &schema.Schema{ + "base_volume_id": { Type: schema.TypeString, Optional: true, ForceNew: true, }, - "base_volume_pool": &schema.Schema{ + "base_volume_pool": { Type: schema.TypeString, Optional: true, ForceNew: true, }, - "base_volume_name": &schema.Schema{ + "base_volume_name": { Type: schema.TypeString, Optional: true, ForceNew: true, diff --git a/libvirt/resource_libvirt_volume_test.go b/libvirt/resource_libvirt_volume_test.go index 9f047a67..277b2990 100644 --- a/libvirt/resource_libvirt_volume_test.go +++ b/libvirt/resource_libvirt_volume_test.go @@ -95,7 +95,7 @@ func TestAccLibvirtVolume_Basic(t *testing.T) { Providers: testAccProviders, CheckDestroy: testAccCheckLibvirtVolumeDestroy, Steps: []resource.TestStep{ - resource.TestStep{ + { Config: testAccCheckLibvirtVolumeConfigBasic, Check: resource.ComposeTestCheckFunc( testAccCheckLibvirtVolumeExists("libvirt_volume.terraform-acceptance-test-1", &volume), @@ -136,7 +136,7 @@ func TestAccLibvirtVolume_DownloadFromSource(t *testing.T) { Providers: testAccProviders, CheckDestroy: testAccCheckLibvirtVolumeDestroy, Steps: []resource.TestStep{ - resource.TestStep{ + { Config: config, Check: resource.ComposeTestCheckFunc( testAccCheckLibvirtVolumeExists("libvirt_volume.terraform-acceptance-test-2", &volume), @@ -163,7 +163,7 @@ func TestAccLibvirtVolume_Format(t *testing.T) { Providers: testAccProviders, CheckDestroy: testAccCheckLibvirtVolumeDestroy, Steps: []resource.TestStep{ - resource.TestStep{ + { Config: testAccCheckLibvirtVolumeConfigFormat, Check: resource.ComposeTestCheckFunc( testAccCheckLibvirtVolumeExists("libvirt_volume.terraform-acceptance-test-3", &volume), -- cgit v1.2.3 From 8d80cf73c79c5c4bdd15dc28690bb06dfa364f1b Mon Sep 17 00:00:00 2001 From: MalloZup Date: Fri, 17 Nov 2017 11:54:11 +0100 Subject: don't allocate memory with 0 make variables --- libvirt/resource_libvirt_domain.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/libvirt/resource_libvirt_domain.go b/libvirt/resource_libvirt_domain.go index 1925b3ac..a3b16792 100644 --- a/libvirt/resource_libvirt_domain.go +++ b/libvirt/resource_libvirt_domain.go @@ -884,7 +884,7 @@ func resourceLibvirtDomainRead(d *schema.ResourceData, meta interface{}) error { } d.Set("running", running) - disks := make([]map[string]interface{}, 0) + var disks []map[string]interface{} for _, diskDef := range domainDef.Devices.Disks { // network drives do not have a volume associated if diskDef.Type == "network" { @@ -934,8 +934,7 @@ func resourceLibvirtDomainRead(d *schema.ResourceData, meta interface{}) error { } } d.Set("disks", disks) - - filesystems := make([]map[string]interface{}, 0) + var filesystems []map[string]interface{} for _, fsDef := range domainDef.Devices.Filesystems { fs := map[string]interface{}{ "accessmode": fsDef.AccessMode, @@ -956,7 +955,7 @@ func resourceLibvirtDomainRead(d *schema.ResourceData, meta interface{}) error { addressesForMac := func(mac string) []string { // look for an ip address and try to match it with the mac address // not sure if using the target device name is a better idea here - addrs := make([]string, 0) + var addrs []string for _, ifaceWithAddr := range ifacesWithAddr { if strings.ToUpper(ifaceWithAddr.Hwaddr) == mac { for _, addr := range ifaceWithAddr.Addrs { @@ -967,7 +966,7 @@ func resourceLibvirtDomainRead(d *schema.ResourceData, meta interface{}) error { return addrs } - netIfaces := make([]map[string]interface{}, 0) + var netIfaces []map[string]interface{} for i, networkInterfaceDef := range domainDef.Devices.Interfaces { // we need it to read old values prefix := fmt.Sprintf("network_interface.%d", i) -- cgit v1.2.3 From 8e1bce8a4c28e69f4c84ae33bec7afd70aa7f7aa Mon Sep 17 00:00:00 2001 From: Duncan Mac-Vicar P Date: Thu, 16 Nov 2017 18:05:01 +0100 Subject: Add support for kernel/initrd/cmdline --- libvirt/resource_libvirt_domain.go | 39 ++++++++++++++ libvirt/resource_libvirt_domain_test.go | 60 +++++++++++++++++++++ website/docs/r/domain.html.markdown | 95 ++++++++++++++++++++++++++++++++- 3 files changed, 193 insertions(+), 1 deletion(-) diff --git a/libvirt/resource_libvirt_domain.go b/libvirt/resource_libvirt_domain.go index a3b16792..81cc2d70 100644 --- a/libvirt/resource_libvirt_domain.go +++ b/libvirt/resource_libvirt_domain.go @@ -170,6 +170,27 @@ func resourceLibvirtDomain() *schema.Resource { Default: "/usr/bin/qemu-system-x86_64", Optional: true, }, + "kernel": &schema.Schema{ + Type: schema.TypeString, + Required: false, + Optional: true, + ForceNew: false, + }, + "initrd": &schema.Schema{ + Type: schema.TypeString, + Required: false, + Optional: true, + ForceNew: false, + }, + "cmdline": &schema.Schema{ + Type: schema.TypeList, + Optional: true, + Required: false, + ForceNew: true, + Elem: &schema.Schema{ + Type: schema.TypeMap, + }, + }, }, } } @@ -267,6 +288,24 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error } } + if kernel, ok := d.GetOk("kernel"); ok { + domainDef.OS.Kernel = kernel.(string) + } + if initrd, ok := d.GetOk("initrd"); ok { + domainDef.OS.Initrd = initrd.(string) + } + + cmdlinesCount := d.Get("cmdline.#").(int) + cmdlineArgs := make([]string, 0) + for i := 0; i < cmdlinesCount; i++ { + cmdlineKey := fmt.Sprintf("cmdline.%d", i) + cmdlineMap := d.Get(cmdlineKey).(map[string]interface{}) + for k, v := range cmdlineMap { + cmdlineArgs = append(cmdlineArgs, fmt.Sprintf("%s=%v", k, v)) + } + } + domainDef.OS.KernelArgs = strings.Join(cmdlineArgs, " ") + if cpu, ok := d.GetOk("cpu"); ok { cpuMap := cpu.(map[string]interface{}) if cpuMode, ok := cpuMap["mode"]; ok { diff --git a/libvirt/resource_libvirt_domain_test.go b/libvirt/resource_libvirt_domain_test.go index 2c9f1b52..29e01095 100644 --- a/libvirt/resource_libvirt_domain_test.go +++ b/libvirt/resource_libvirt_domain_test.go @@ -235,6 +235,40 @@ func TestAccLibvirtDomainURLDisk(t *testing.T) { } +func TestAccLibvirtDomainKernelInitrdCmdline(t *testing.T) { + var domain libvirt.Domain + + var config = fmt.Sprintf(` + resource "libvirt_domain" "acceptance-test-domain" { + name = "terraform-test-domain" + kernel = "/boot/vmlinuz" + initrd = "/boot/initrd" + cmdline { + foo = 1 + bar = "bye" + } + cmdline { + foo = 2 + } + }`) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckLibvirtDomainDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: config, + Check: resource.ComposeTestCheckFunc( + testAccCheckLibvirtDomainExists("libvirt_domain.acceptance-test-domain", &domain), + testAccCheckLibvirtDomainKernelInitrdCmdline(&domain), + ), + }, + }, + }) + +} + func TestAccLibvirtDomain_NetworkInterface(t *testing.T) { var domain libvirt.Domain @@ -658,6 +692,32 @@ func testAccCheckLibvirtURLDisk(u *url.URL, domain *libvirt.Domain) resource.Tes } } +func testAccCheckLibvirtDomainKernelInitrdCmdline(domain *libvirt.Domain) resource.TestCheckFunc { + return func(s *terraform.State) error { + xmlDesc, err := domain.GetXMLDesc(0) + if err != nil { + return fmt.Errorf("Error retrieving libvirt domain XML description: %s", err) + } + + domainDef := newDomainDef() + err = xml.Unmarshal([]byte(xmlDesc), &domainDef) + if err != nil { + return fmt.Errorf("Error reading libvirt domain XML description: %s", err) + } + + if domainDef.OS.Kernel != "/boot/vmlinuz" { + return fmt.Errorf("Kernel is not set correctly") + } + if domainDef.OS.Initrd != "/boot/initrd" { + return fmt.Errorf("Initrd is not set correctly") + } + if domainDef.OS.KernelArgs != "bar=bye foo=1 foo=2" { + return fmt.Errorf("Kernel args not set correctly") + } + return nil + } +} + func createNvramFile() (string, error) { // size of an accepted, valid, nvram backing store NVRAMDummyBuffer := make([]byte, 131072) diff --git a/website/docs/r/domain.html.markdown b/website/docs/r/domain.html.markdown index b047a6e7..a1e60fcb 100644 --- a/website/docs/r/domain.html.markdown +++ b/website/docs/r/domain.html.markdown @@ -59,6 +59,99 @@ The following arguments are supported: [below](#define-boot-device-order). * `emulator` - (Optional) The path of the emulator to use +### Kernel and boot arguments + +* `kernel` - (Optional) The path of the kernel to boot + +If you are using a qcow2 volume, you can pass the id of the volume (eg. `${libvirt_volume.kernel.id}`) +as they are local to the hypervisor. + +Given that you can define a volume from a remote http file, this means, you can also have remote kernels. + +```hcl +resource "libvirt_volume" "kernel" { + source = "http://download.opensuse.org/tumbleweed/repo/oss/boot/x86_64/loader/linux" + name = "kernel" + pool = "default" + format = "raw" +} + +resource "libvirt_domain" "domain-suse" { + name = "suse" + memory = "1024" + vcpu = 1 + + kernel = "${libvirt_volume.kernel.id}" + + // ... +} +``` + +* `kernel` - (Optional) The path of the initrd to boot. + +You can use it in the same way as the kernel. + +* `cmdlin` - (Optional) Arguments to the kernel + +resource "libvirt_domain" "domain-suse" { + name = "suse" + memory = "1024" + vcpu = 1 + + kernel = "${libvirt_volume.kernel.id}" + + cmdline { + arg1 = "value1" + arg2 = "value2" + } +} +``` + +Also note that the `cmd` block is actually a list of maps, so it is possible to +declare several of them by using either the literal list and map syntax as in +the following examples: + +```hcl +resource "libvirt_domain" "my_machine" { + //... + + cmdline { + arg1 = "value1" + } + cmdline { + arg2 = "value2" + } +} +``` + +```hcl +resource "libvirt_domain" "my_machine" { + ... + cmdline = [ + { + arg1 = "value1" + }, + { + arg2 = "value2" + } + ] +} +``` +The kernel supports passing the same option multiple times. If you need this, use separate cmdline blocks. + +```hcl +resource "libvirt_domain" "my_machine" { + //... + + cmdline { + arg1 = "value1" + } + cmdline { + arg1 = "value2" + } +} +``` + ### UEFI images Some extra arguments are also provided for using UEFI images: @@ -73,7 +166,7 @@ read-only. domain. However, `libvirt` can manage this automatically (and this is the recommended solution) if a mapping for the firmware to a _variables file_ exists in `/etc/libvirt/qemu.conf:nvram`. In that case, `libvirt` will copy that variables file into a file specific for this domain. - * `template` - (Optional) path to the file used to override variables from the master NVRAM +s * `template` - (Optional) path to the file used to override variables from the master NVRAM store. So you should typically use the firmware as this, -- cgit v1.2.3 From 06f312e1ca81074d3806816535d3039251691fb4 Mon Sep 17 00:00:00 2001 From: Duncan Mac-Vicar P Date: Fri, 17 Nov 2017 15:00:37 +0100 Subject: Fix tests by adding fixture for kernel and initrd --- libvirt/resource_libvirt_domain.go | 3 +++ libvirt/resource_libvirt_domain_test.go | 45 +++++++++++++++++++++++++------- libvirt/testdata/README.md | 13 +++++++++ libvirt/testdata/initrd.img | Bin 0 -> 98 bytes libvirt/testdata/tetris.elf | Bin 0 -> 19384 bytes 5 files changed, 52 insertions(+), 9 deletions(-) create mode 100644 libvirt/testdata/README.md create mode 100644 libvirt/testdata/initrd.img create mode 100755 libvirt/testdata/tetris.elf diff --git a/libvirt/resource_libvirt_domain.go b/libvirt/resource_libvirt_domain.go index 81cc2d70..272d1eec 100644 --- a/libvirt/resource_libvirt_domain.go +++ b/libvirt/resource_libvirt_domain.go @@ -9,6 +9,7 @@ import ( "net" "net/url" "os" + "sort" "strconv" "strings" "time" @@ -304,6 +305,8 @@ 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, " ") if cpu, ok := d.GetOk("cpu"); ok { diff --git a/libvirt/resource_libvirt_domain_test.go b/libvirt/resource_libvirt_domain_test.go index 29e01095..055c67c4 100644 --- a/libvirt/resource_libvirt_domain_test.go +++ b/libvirt/resource_libvirt_domain_test.go @@ -237,12 +237,28 @@ func TestAccLibvirtDomainURLDisk(t *testing.T) { func TestAccLibvirtDomainKernelInitrdCmdline(t *testing.T) { var domain libvirt.Domain + var kernel libvirt.StorageVol + var initrd libvirt.StorageVol var config = fmt.Sprintf(` + resource "libvirt_volume" "kernel" { + source = "testdata/tetris.elf" + name = "kernel" + pool = "default" + format = "raw" + } + + resource "libvirt_volume" "initrd" { + source = "testdata/initrd.img" + name = "initrd" + pool = "default" + format = "raw" + } + resource "libvirt_domain" "acceptance-test-domain" { name = "terraform-test-domain" - kernel = "/boot/vmlinuz" - initrd = "/boot/initrd" + kernel = "${libvirt_volume.kernel.id}" + initrd = "${libvirt_volume.initrd.id}" cmdline { foo = 1 bar = "bye" @@ -260,8 +276,10 @@ func TestAccLibvirtDomainKernelInitrdCmdline(t *testing.T) { resource.TestStep{ Config: config, Check: resource.ComposeTestCheckFunc( + testAccCheckLibvirtVolumeExists("libvirt_volume.kernel", &kernel), + testAccCheckLibvirtVolumeExists("libvirt_volume.initrd", &initrd), testAccCheckLibvirtDomainExists("libvirt_domain.acceptance-test-domain", &domain), - testAccCheckLibvirtDomainKernelInitrdCmdline(&domain), + testAccCheckLibvirtDomainKernelInitrdCmdline(&domain, &kernel, &initrd), ), }, }, @@ -692,7 +710,7 @@ func testAccCheckLibvirtURLDisk(u *url.URL, domain *libvirt.Domain) resource.Tes } } -func testAccCheckLibvirtDomainKernelInitrdCmdline(domain *libvirt.Domain) resource.TestCheckFunc { +func testAccCheckLibvirtDomainKernelInitrdCmdline(domain *libvirt.Domain, kernel *libvirt.StorageVol, initrd *libvirt.StorageVol) resource.TestCheckFunc { return func(s *terraform.State) error { xmlDesc, err := domain.GetXMLDesc(0) if err != nil { @@ -705,14 +723,23 @@ func testAccCheckLibvirtDomainKernelInitrdCmdline(domain *libvirt.Domain) resour return fmt.Errorf("Error reading libvirt domain XML description: %s", err) } - if domainDef.OS.Kernel != "/boot/vmlinuz" { - return fmt.Errorf("Kernel is not set correctly") + key, err := kernel.GetKey() + if err != nil { + return fmt.Errorf("Can't get kernel volume id") + } + if domainDef.OS.Kernel != key { + return fmt.Errorf("Kernel is not set correctly: '%s' vs '%s'", domainDef.OS.Kernel, key) + } + + key, err = initrd.GetKey() + if err != nil { + return fmt.Errorf("Can't get initrd volume id") } - if domainDef.OS.Initrd != "/boot/initrd" { - return fmt.Errorf("Initrd is not set correctly") + if domainDef.OS.Initrd != key { + return fmt.Errorf("Initrd is not set correctly: '%s' vs '%s'", domainDef.OS, key) } if domainDef.OS.KernelArgs != "bar=bye foo=1 foo=2" { - return fmt.Errorf("Kernel args not set correctly") + return fmt.Errorf("Kernel args not set correctly: '%s'", domainDef.OS.KernelArgs) } return nil } diff --git a/libvirt/testdata/README.md b/libvirt/testdata/README.md new file mode 100644 index 00000000..d571154f --- /dev/null +++ b/libvirt/testdata/README.md @@ -0,0 +1,13 @@ +# Test data + +* tetris.elf: https://github.com/programble/bare-metal-tetris + +``` +License + +Copyright © 2013–2014, Curtis McEnroe programble@gmail.com + +Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +``` diff --git a/libvirt/testdata/initrd.img b/libvirt/testdata/initrd.img new file mode 100644 index 00000000..d2f6e345 Binary files /dev/null and b/libvirt/testdata/initrd.img differ diff --git a/libvirt/testdata/tetris.elf b/libvirt/testdata/tetris.elf new file mode 100755 index 00000000..6bbc9c7c Binary files /dev/null and b/libvirt/testdata/tetris.elf differ -- cgit v1.2.3 From a1145d25f878d0fc70229428c8076c27423d5a07 Mon Sep 17 00:00:00 2001 From: Duncan Mac-Vicar P Date: Thu, 23 Nov 2017 14:23:15 +0100 Subject: Read back cmdline, initrd and kernel attributes from the resource --- libvirt/resource_libvirt_domain.go | 9 ++++++++- libvirt/utils_domain_def.go | 27 +++++++++++++++++++++++++++ libvirt/utils_domain_def_test.go | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 libvirt/utils_domain_def_test.go 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) + } + } +} -- cgit v1.2.3 From 1cbfe466f0fb8c2320c54cec9e54fbdb1d5bd4ac Mon Sep 17 00:00:00 2001 From: Duncan Mac-Vicar P Date: Thu, 23 Nov 2017 14:37:52 +0100 Subject: Split kernel cmdline test --- libvirt/utils_domain_def_test.go | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/libvirt/utils_domain_def_test.go b/libvirt/utils_domain_def_test.go index b497c02c..4880804e 100644 --- a/libvirt/utils_domain_def_test.go +++ b/libvirt/utils_domain_def_test.go @@ -12,25 +12,23 @@ func init() { } 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) - } + 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) - } +func TestSplitKernelInvalidCmdLine(t *testing.T) { + 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) } } -- cgit v1.2.3 From a05679a1e2d4ae29e496131a88ba1a129d7f331e Mon Sep 17 00:00:00 2001 From: Duncan Mac-Vicar P Date: Thu, 23 Nov 2017 14:50:27 +0100 Subject: Do not choke on empty kernel cmdline --- libvirt/utils_domain_def.go | 4 ++++ libvirt/utils_domain_def_test.go | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/libvirt/utils_domain_def.go b/libvirt/utils_domain_def.go index 4e46cb62..670d8b02 100644 --- a/libvirt/utils_domain_def.go +++ b/libvirt/utils_domain_def.go @@ -54,6 +54,10 @@ func getOriginalMachineName(caps libvirtxml.Caps, arch string, virttype string, // key func splitKernelCmdLine(cmdLine string) ([]map[string]string, error) { var cmdLines []map[string]string + if len(cmdLine) == 0 { + return cmdLines, nil + } + currCmdLine := make(map[string]string) argVals := strings.Split(cmdLine, " ") for _, argVal := range argVals { diff --git a/libvirt/utils_domain_def_test.go b/libvirt/utils_domain_def_test.go index 4880804e..7ff57203 100644 --- a/libvirt/utils_domain_def_test.go +++ b/libvirt/utils_domain_def_test.go @@ -32,3 +32,14 @@ func TestSplitKernelInvalidCmdLine(t *testing.T) { t.Errorf("Expected error for parsing '%s'", v) } } + +func TestSplitKernelEmptyCmdLine(t *testing.T) { + var e []map[string]string + r, err := splitKernelCmdLine("") + if !reflect.DeepEqual(r, e) { + t.Fatalf("got='%s' expected='%s'", spew.Sdump(r), spew.Sdump(e)) + } + if err != nil { + t.Error(err) + } +} -- cgit v1.2.3 From 26cc59911233190debece753b8e9e9b91ded4bc8 Mon Sep 17 00:00:00 2001 From: Duncan Mac-Vicar P Date: Thu, 23 Nov 2017 15:40:17 +0100 Subject: No need to make a 0 len slice --- libvirt/resource_libvirt_domain.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libvirt/resource_libvirt_domain.go b/libvirt/resource_libvirt_domain.go index 4dae7c29..b19f23f0 100644 --- a/libvirt/resource_libvirt_domain.go +++ b/libvirt/resource_libvirt_domain.go @@ -297,7 +297,7 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error } cmdlinesCount := d.Get("cmdline.#").(int) - cmdlineArgs := make([]string, 0) + var cmdlineArgs []string for i := 0; i < cmdlinesCount; i++ { cmdlineKey := fmt.Sprintf("cmdline.%d", i) cmdlineMap := d.Get(cmdlineKey).(map[string]interface{}) -- cgit v1.2.3 From 3ee58ad92b04d1f7f1a086e6c1e2cfba5111995c Mon Sep 17 00:00:00 2001 From: Duncan Mac-Vicar P Date: Fri, 24 Nov 2017 17:03:50 +0100 Subject: Type not needed in literal anymore --- libvirt/resource_libvirt_domain.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libvirt/resource_libvirt_domain.go b/libvirt/resource_libvirt_domain.go index b19f23f0..829a9d9f 100644 --- a/libvirt/resource_libvirt_domain.go +++ b/libvirt/resource_libvirt_domain.go @@ -171,19 +171,19 @@ func resourceLibvirtDomain() *schema.Resource { Default: "/usr/bin/qemu-system-x86_64", Optional: true, }, - "kernel": &schema.Schema{ + "kernel": { Type: schema.TypeString, Required: false, Optional: true, ForceNew: false, }, - "initrd": &schema.Schema{ + "initrd": { Type: schema.TypeString, Required: false, Optional: true, ForceNew: false, }, - "cmdline": &schema.Schema{ + "cmdline": { Type: schema.TypeList, Optional: true, Required: false, -- cgit v1.2.3 From 81de2c8cea6a09b2112acfeade2120c2337bf366 Mon Sep 17 00:00:00 2001 From: Duncan Mac-Vicar P Date: Fri, 24 Nov 2017 17:05:00 +0100 Subject: Typo in cmdline docs --- website/docs/r/domain.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/domain.html.markdown b/website/docs/r/domain.html.markdown index a1e60fcb..98b85a39 100644 --- a/website/docs/r/domain.html.markdown +++ b/website/docs/r/domain.html.markdown @@ -91,7 +91,7 @@ resource "libvirt_domain" "domain-suse" { You can use it in the same way as the kernel. -* `cmdlin` - (Optional) Arguments to the kernel +* `cmdline` - (Optional) Arguments to the kernel resource "libvirt_domain" "domain-suse" { name = "suse" -- cgit v1.2.3 From 1f6b7508dac80715b794bdd1ab17e8f6141c404d Mon Sep 17 00:00:00 2001 From: Duncan Mac-Vicar P Date: Fri, 24 Nov 2017 17:08:15 +0100 Subject: Simplify for loop --- libvirt/resource_libvirt_domain.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libvirt/resource_libvirt_domain.go b/libvirt/resource_libvirt_domain.go index 829a9d9f..16b618e2 100644 --- a/libvirt/resource_libvirt_domain.go +++ b/libvirt/resource_libvirt_domain.go @@ -296,9 +296,8 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error domainDef.OS.Initrd = initrd.(string) } - cmdlinesCount := d.Get("cmdline.#").(int) var cmdlineArgs []string - for i := 0; i < cmdlinesCount; i++ { + for i := 0; i < d.Get("cmdline.#").(int); i++ { cmdlineKey := fmt.Sprintf("cmdline.%d", i) cmdlineMap := d.Get(cmdlineKey).(map[string]interface{}) for k, v := range cmdlineMap { -- cgit v1.2.3 From 4fea0870bf634945d4b1f101e49d1193651600b3 Mon Sep 17 00:00:00 2001 From: Duncan Mac-Vicar P Date: Fri, 24 Nov 2017 17:09:50 +0100 Subject: Remove intermediate variables in map iteration --- libvirt/resource_libvirt_domain.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/libvirt/resource_libvirt_domain.go b/libvirt/resource_libvirt_domain.go index 16b618e2..b5d0fb6d 100644 --- a/libvirt/resource_libvirt_domain.go +++ b/libvirt/resource_libvirt_domain.go @@ -298,9 +298,7 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error var cmdlineArgs []string for i := 0; i < d.Get("cmdline.#").(int); i++ { - cmdlineKey := fmt.Sprintf("cmdline.%d", i) - cmdlineMap := d.Get(cmdlineKey).(map[string]interface{}) - for k, v := range cmdlineMap { + for k, v := range d.Get(fmt.Sprintf("cmdline.%d", i)).(map[string]interface{}) { cmdlineArgs = append(cmdlineArgs, fmt.Sprintf("%s=%v", k, v)) } } -- cgit v1.2.3 From c3aad57b10355410bc3e897ba47ac916992cf626 Mon Sep 17 00:00:00 2001 From: Duncan Mac-Vicar P Date: Fri, 24 Nov 2017 17:10:41 +0100 Subject: Add missing hcl markdown fence --- website/docs/r/domain.html.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/website/docs/r/domain.html.markdown b/website/docs/r/domain.html.markdown index 98b85a39..2a0e50e8 100644 --- a/website/docs/r/domain.html.markdown +++ b/website/docs/r/domain.html.markdown @@ -93,6 +93,7 @@ You can use it in the same way as the kernel. * `cmdline` - (Optional) Arguments to the kernel +```hcl resource "libvirt_domain" "domain-suse" { name = "suse" memory = "1024" -- cgit v1.2.3 From 9f7f0253a320a3fe96c2970fa486064fd7cb3963 Mon Sep 17 00:00:00 2001 From: Duncan Mac-Vicar P Date: Fri, 24 Nov 2017 17:11:39 +0100 Subject: Typo --- website/docs/r/domain.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/domain.html.markdown b/website/docs/r/domain.html.markdown index 2a0e50e8..f008501e 100644 --- a/website/docs/r/domain.html.markdown +++ b/website/docs/r/domain.html.markdown @@ -167,7 +167,7 @@ read-only. domain. However, `libvirt` can manage this automatically (and this is the recommended solution) if a mapping for the firmware to a _variables file_ exists in `/etc/libvirt/qemu.conf:nvram`. In that case, `libvirt` will copy that variables file into a file specific for this domain. -s * `template` - (Optional) path to the file used to override variables from the master NVRAM + * `template` - (Optional) path to the file used to override variables from the master NVRAM store. So you should typically use the firmware as this, -- cgit v1.2.3