aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlocalghost <zkostrzewa@gmail.com>2017-06-23 22:19:22 +0200
committerFlavio Castelli <flavio@castelli.me>2017-06-26 22:19:52 +0200
commit8b8c66e3bd53aeab3806179aefaec2b453dbd4ad (patch)
tree25f90fa43201bfa16fe7a721bf7c2c64cab0e086
parent7bab91695709f07227e18079651d7a2063986268 (diff)
downloadterraform-provider-libvirt-8b8c66e3bd53aeab3806179aefaec2b453dbd4ad.tar
terraform-provider-libvirt-8b8c66e3bd53aeab3806179aefaec2b453dbd4ad.tar.gz
Add autostart option for domain resource.
-rw-r--r--docs/providers/libvirt/r/domain.html.markdown8
-rw-r--r--libvirt/resource_libvirt_domain.go26
-rw-r--r--libvirt/resource_libvirt_domain_test.go24
3 files changed, 58 insertions, 0 deletions
diff --git a/docs/providers/libvirt/r/domain.html.markdown b/docs/providers/libvirt/r/domain.html.markdown
index a78b68d3..35feb4c8 100644
--- a/docs/providers/libvirt/r/domain.html.markdown
+++ b/docs/providers/libvirt/r/domain.html.markdown
@@ -41,6 +41,7 @@ The following arguments are supported:
cloud-init won't cause the domain to be recreated, however the change will
have effect on the next reboot.
* `cpu` - (Optional) Configures CPU mode.
+* `autostart` - (Optional) Set to `true` to start the domain on host boot up. If not specified `false` is assumed.
* `filesystem` - (Optional) An array of one or more host filesystems to attach to the domain. The `filesystem` object structure is documented below.
There is an optional `coreos_ignition` parameter:
@@ -297,6 +298,13 @@ resource "libvirt_domain" "my_machine" {
}
```
+To start the domain on host boot up set `autostart` to `true` like so:
+```
+resource "libvirt_domain" "my_machine" {
+ ...
+ autostart = true
+}
+```
The optional `filesystem` block allows to define one or more [filesytem](https://libvirt.org/formatdomain.html#elementsFilesystems)
entries to be added to the domain. This allows to share a directory of the libvirtd
diff --git a/libvirt/resource_libvirt_domain.go b/libvirt/resource_libvirt_domain.go
index e94edb1b..5861eeea 100644
--- a/libvirt/resource_libvirt_domain.go
+++ b/libvirt/resource_libvirt_domain.go
@@ -140,6 +140,11 @@ func resourceLibvirtDomain() *schema.Resource {
Required: false,
ForceNew: true,
},
+ "autostart": &schema.Schema{
+ Type: schema.TypeBool,
+ Optional: true,
+ Required: false,
+ },
},
}
}
@@ -479,6 +484,13 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("Error defining libvirt domain: %s", err)
}
+ if autostart, ok := d.GetOk("autostart"); ok {
+ err = domain.SetAutostart(autostart.(bool))
+ if err != nil {
+ return fmt.Errorf("Error setting autostart for domain: %s", err)
+ }
+ }
+
err = domain.Create()
if err != nil {
return fmt.Errorf("Error creating libvirt domain: %s", err)
@@ -623,6 +635,14 @@ func resourceLibvirtDomainUpdate(d *schema.ResourceData, meta interface{}) error
d.SetPartial("cloudinit")
}
+ if d.HasChange("autostart") {
+ err = domain.SetAutostart(d.Get("autostart").(bool))
+ if err != nil {
+ return fmt.Errorf("Error setting autostart for domain: %s", err)
+ }
+ d.SetPartial("autostart")
+ }
+
netIfacesCount := d.Get("network_interface.#").(int)
for i := 0; i < netIfacesCount; i++ {
prefix := fmt.Sprintf("network_interface.%d", i)
@@ -691,6 +711,11 @@ func resourceLibvirtDomainRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error reading libvirt domain XML description: %s", err)
}
+ autostart, err := domain.GetAutostart()
+ if err != nil {
+ return fmt.Errorf("Error reading domain autostart setting: %s", err)
+ }
+
d.Set("name", domainDef.Name)
d.Set("metadata", domainDef.Metadata.TerraformLibvirt.Xml)
d.Set("vpu", domainDef.VCpu)
@@ -698,6 +723,7 @@ func resourceLibvirtDomainRead(d *schema.ResourceData, meta interface{}) error {
d.Set("firmware", domainDef.Os.Loader)
d.Set("nvram", domainDef.Os.NvRam)
d.Set("cpu", domainDef.Cpu)
+ d.Set("autostart", autostart)
running, err := isDomainRunning(*domain)
if err != nil {
diff --git a/libvirt/resource_libvirt_domain_test.go b/libvirt/resource_libvirt_domain_test.go
index e8897f3d..7eec6e23 100644
--- a/libvirt/resource_libvirt_domain_test.go
+++ b/libvirt/resource_libvirt_domain_test.go
@@ -351,6 +351,30 @@ func TestAccLibvirtDomain_Cpu(t *testing.T) {
})
}
+func TestAccLibvirtDomain_Autostart(t *testing.T) {
+ var domain libvirt.Domain
+
+ var config = fmt.Sprintf(`
+ resource "libvirt_domain" "acceptance-test-domain" {
+ name = "terraform-test"
+ autostart = true
+ }`)
+ 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),
+ resource.TestCheckResourceAttr("libvirt_domain.acceptance-test-domain", "autostart", "true"),
+ ),
+ },
+ },
+ })
+}
+
func TestAccLibvirtDomain_Filesystems(t *testing.T) {
var domain libvirt.Domain