Skip to content
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
49 changes: 44 additions & 5 deletions libvirt/resource_libvirt_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,31 @@ func resourceLibvirtDomain() *schema.Resource {
ForceNew: false,
},
"vcpu": {
Type: schema.TypeInt,
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Default: 1,
Computed: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"count": {
Type: schema.TypeInt,
Optional: true,
Default: 1,
ForceNew: true,
},
"cpuset": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"placement": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
},
},
"memory": {
Type: schema.TypeInt,
Expand Down Expand Up @@ -504,8 +525,18 @@ func resourceLibvirtDomainCreate(ctx context.Context, d *schema.ResourceData, me
Value: uint(d.Get("memory").(int)),
Unit: "MiB",
}
domainDef.VCPU = &libvirtxml.DomainVCPU{
Value: uint(d.Get("vcpu").(int)),
if vcpuCount, ok := d.GetOk("vcpu.0.count"); ok {
domainDef.VCPU = &libvirtxml.DomainVCPU{
CPUSet: d.Get("vcpu.0.cpuset").(string),
Placement: d.Get("vcpu.0.placement").(string),
Value: uint(vcpuCount.(int)),
}
} else {
domainDef.VCPU = &libvirtxml.DomainVCPU{
CPUSet: d.Get("vcpu.0.cpuset").(string),
Placement: d.Get("vcpu.0.placement").(string),
Value: 1,
}
}
domainDef.Description = d.Get("description").(string)

Expand Down Expand Up @@ -808,7 +839,15 @@ func resourceLibvirtDomainRead(ctx context.Context, d *schema.ResourceData, meta

d.Set("name", domainDef.Name)
d.Set("description", domainDef.Description)
d.Set("vcpu", domainDef.VCPU.Value)
if domainDef.VCPU != nil {
vcpu := make(map[string]interface{})
var vcpus []map[string]interface{}
vcpu["count"] = domainDef.VCPU.Value
vcpu["placement"] = domainDef.VCPU.Placement
vcpu["cpuset"] = domainDef.VCPU.CPUSet
vcpus = append(vcpus, vcpu)
d.Set("vcpu", vcpus)
}

switch domainDef.Memory.Unit {
case "KiB":
Expand Down
22 changes: 15 additions & 7 deletions libvirt/resource_libvirt_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestAccLibvirtDomain_Basic(t *testing.T) {
resource.TestCheckResourceAttr(
"libvirt_domain."+randomResourceName, "memory", "512"),
resource.TestCheckResourceAttr(
"libvirt_domain."+randomResourceName, "vcpu", "1"),
"libvirt_domain."+randomResourceName, "vcpu.0.count", "1"),
),
},
},
Expand Down Expand Up @@ -87,7 +87,9 @@ func TestAccLibvirtDomain_Detailed(t *testing.T) {
resource "libvirt_domain" "%s" {
name = "%s"
memory = 384
vcpu = 2
vcpu {
count = 2
}
}`, randomResourceName, randomDomainName),
Check: resource.ComposeTestCheckFunc(
testAccCheckLibvirtDomainExists("libvirt_domain."+randomResourceName, &domain),
Expand All @@ -96,7 +98,7 @@ func TestAccLibvirtDomain_Detailed(t *testing.T) {
resource.TestCheckResourceAttr(
"libvirt_domain."+randomResourceName, "memory", "384"),
resource.TestCheckResourceAttr(
"libvirt_domain."+randomResourceName, "vcpu", "2"),
"libvirt_domain."+randomResourceName, "vcpu.0.count", "2"),
),
},
},
Expand Down Expand Up @@ -1431,12 +1433,16 @@ func TestAccLibvirtDomain_ShutoffMultiDomainsRunning(t *testing.T) {
Config: `
resource "libvirt_domain" "domainoff" {
name = "domainfalse"
vcpu = 1
vcpu {
count = 1
}
running = false
}
resource "libvirt_domain" "domainok" {
name = "domaintrue"
vcpu = 1
vcpu {
count = 1
}
running = true
}`,
Check: resource.ComposeTestCheckFunc(
Expand Down Expand Up @@ -1526,7 +1532,9 @@ func TestAccLibvirtDomain_Import(t *testing.T) {
resource "libvirt_domain" "%s" {
name = "%s"
memory = 384
vcpu = 2
vcpu {
count = 2
}
}`, randomDomainName, randomDomainName),
},
{
Expand All @@ -1539,7 +1547,7 @@ func TestAccLibvirtDomain_Import(t *testing.T) {
resource.TestCheckResourceAttr(
"libvirt_domain.%s-2", "memory", "384"),
resource.TestCheckResourceAttr(
"libvirt_domain.%s-2", "vcpu", "2"),
"libvirt_domain.%s-2", "vcpu.0.count", "2"),
),
},
},
Expand Down