Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parametrize cloud-init bus type #1027

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions libvirt/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func domainGetIfacesInfo(virConn *libvirt.Libvirt, domain libvirt.Domain, rd *sc
return interfaces, nil
}

func newDiskForCloudInit(virConn *libvirt.Libvirt, volumeKey string) (libvirtxml.DomainDisk, error) {
func newDiskForCloudInit(virConn *libvirt.Libvirt, volumeKey string, bus string) (libvirtxml.DomainDisk, error) {
disk := libvirtxml.DomainDisk{
// HACK mark the disk as belonging to the cloudinit
// resource so we can ignore it
Expand All @@ -188,7 +188,7 @@ func newDiskForCloudInit(virConn *libvirt.Libvirt, volumeKey string) (libvirtxml
Target: &libvirtxml.DomainDiskTarget{
// Last device letter possible with a single IDE controller on i440FX
Dev: "hdd",
Bus: "ide",
Bus: bus,
},
Driver: &libvirtxml.DomainDiskDriver{
Name: "qemu",
Expand Down Expand Up @@ -648,7 +648,10 @@ func setCloudinit(d *schema.ResourceData, domainDef *libvirtxml.Domain, virConn
if err != nil {
return err
}
disk, err := newDiskForCloudInit(virConn, cloudinitID)

cloudinitBus := d.Get("cloudinit_bus").(string)

disk, err := newDiskForCloudInit(virConn, cloudinitID, cloudinitBus)
if err != nil {
return err
}
Expand Down
11 changes: 10 additions & 1 deletion libvirt/resource_libvirt_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ func resourceLibvirtDomain() *schema.Resource {
Optional: true,
ForceNew: false,
},
"cloudinit_bus": {
Type: schema.TypeString,
Optional: true,
Default: "ide",
ForceNew: true,
Required: false,
},
"coreos_ignition": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -688,7 +695,9 @@ func resourceLibvirtDomainUpdate(ctx context.Context, d *schema.ResourceData, me
return diag.FromErr(err)
}

disk, err := newDiskForCloudInit(virConn, cloudinitID)
cloudinitBus := d.Get("cloudinit_bus").(string)

disk, err := newDiskForCloudInit(virConn, cloudinitID, cloudinitBus)
if err != nil {
return diag.FromErr(err)
}
Expand Down
42 changes: 42 additions & 0 deletions libvirt/resource_libvirt_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func TestAccLibvirtDomain_Basic(t *testing.T) {
"libvirt_domain."+randomResourceName, "memory", "512"),
resource.TestCheckResourceAttr(
"libvirt_domain."+randomResourceName, "vcpu", "1"),
resource.TestCheckResourceAttr(
"libvirt_domain."+randomResourceName, "cloudinit_bus", "ide"),
),
},
},
Expand Down Expand Up @@ -988,6 +990,46 @@ func TestAccLibvirtDomain_Filesystems(t *testing.T) {
})
}

func TestAccLibvirtDomain_CloudinitBus(t *testing.T) {
var domain libvirt.Domain
randomDomainName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)

var initialConfig = fmt.Sprintf(`
resource "libvirt_domain" "%s" {
name = "%s"

cloudinit_bus = "ide"
}`, randomDomainName, randomDomainName)

var updatedConfig = fmt.Sprintf(`
resource "libvirt_domain" "%s" {
name = "%s"

cloudinit_bus = "sata"
}`, randomDomainName, randomDomainName)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLibvirtDomainDestroy,
Steps: []resource.TestStep{
{
Config: initialConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckLibvirtDomainExists("libvirt_domain."+randomDomainName, &domain),
resource.TestCheckResourceAttr(
"libvirt_domain." + randomDomainName, "cloudinit_bus", "ide"),
),
},
{
Config: updatedConfig,
Check: resource.TestCheckResourceAttr(
"libvirt_domain." + randomDomainName, "cloudinit_bus", "sata"),
},
},
})
}

func testAccCheckLibvirtDomainExists(name string, domain *libvirt.Domain) resource.TestCheckFunc {
return func(state *terraform.State) error {
rs, err := getResourceFromTerraformState(name, state)
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/domain.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ The following arguments are supported:
the domain. This is going to be attached as a CDROM ISO. Changing the
cloud-init won't cause the domain to be recreated, however the change will
have effect on the next reboot.
* `cloudinit_bus` - (Optional, default: `ide`) The bus type which will be used for
the cloud-init disk. This is mainly to allow the use of cloud-init with Q35 machines.
* `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
Expand Down