Skip to content
Closed
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
24 changes: 24 additions & 0 deletions libvirt/resource_libvirt_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func resourceLibvirtDomain() *schema.Resource {
ReadContext: resourceLibvirtDomainRead,
DeleteContext: resourceLibvirtDomainDelete,
UpdateContext: resourceLibvirtDomainUpdate,
CustomizeDiff: resourceLibvirtDomainCustomDiff,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Expand All @@ -48,6 +49,11 @@ func resourceLibvirtDomain() *schema.Resource {
Required: true,
ForceNew: true,
},
"title": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -494,6 +500,13 @@ func resourceLibvirtDomainCreate(ctx context.Context, d *schema.ResourceData, me
domainDef.Name = name.(string)
}

if title, ok := d.GetOk("title"); ok {
if strings.Contains(title.(string), "\n") {
return diag.Errorf("title attribute should not contain newline characters")
}
domainDef.Title = title.(string)
}

if cpuMode, ok := d.GetOk("cpu.0.mode"); ok {
domainDef.CPU = &libvirtxml.DomainCPU{
Mode: cpuMode.(string),
Expand Down Expand Up @@ -807,6 +820,7 @@ func resourceLibvirtDomainRead(ctx context.Context, d *schema.ResourceData, meta
}

d.Set("name", domainDef.Name)
d.Set("title", domainDef.Title)
d.Set("description", domainDef.Description)
d.Set("vcpu", domainDef.VCPU.Value)

Expand Down Expand Up @@ -1123,3 +1137,13 @@ func resourceLibvirtDomainDelete(ctx context.Context, d *schema.ResourceData, me

return nil
}

func resourceLibvirtDomainCustomDiff(ctx context.Context, d *schema.ResourceDiff, meta interface{}) error {
if d.HasChange("title") {
_, newTitle := d.GetChange("title")
if strings.Contains(newTitle.(string), "\n") {
return fmt.Errorf("title attribute should not contain newline characters")
}
}
return nil
}
47 changes: 47 additions & 0 deletions libvirt/resource_libvirt_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/url"
"os"
"path/filepath"
"regexp"
"testing"

testhelper "github.com/dmacvicar/terraform-provider-libvirt/libvirt/helper/test"
Expand Down Expand Up @@ -73,6 +74,52 @@ func TestAccLibvirtDomain_Description(t *testing.T) {
})
}

func TestAccLibvirtDomain_Title(t *testing.T) {
var domain libvirt.Domain
randomResourceName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
randomDomainName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLibvirtDomainDestroy,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "libvirt_domain" "%s" {
name = "%s"
title = "invalid\n unit test title"
}`, randomResourceName, randomDomainName),
ResourceName: "libvirt_domain." + randomResourceName,
ExpectError: regexp.MustCompile("title attribute should not contain newline characters"),
},
{
Config: fmt.Sprintf(`
resource "libvirt_domain" "%s" {
name = "%s"
title = "unit test title"
}`, randomResourceName, randomDomainName),
Check: resource.ComposeTestCheckFunc(
testAccCheckLibvirtDomainExists("libvirt_domain."+randomResourceName, &domain),
resource.TestCheckResourceAttr(
"libvirt_domain."+randomResourceName, "name", randomDomainName),
resource.TestCheckResourceAttr(
"libvirt_domain."+randomResourceName, "title", "unit test title"),
),
ResourceName: "libvirt_domain." + randomDomainName,
},
{
Config: fmt.Sprintf(`
resource "libvirt_domain" "%s" {
name = "%s"
title = "unit test title\n update failure"
}`, randomResourceName, randomDomainName),
ResourceName: "libvirt_domain." + randomDomainName,
ExpectError: regexp.MustCompile("title attribute should not contain newline characters"),
},
},
})
}

func TestAccLibvirtDomain_Detailed(t *testing.T) {
var domain libvirt.Domain
randomResourceName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
Expand Down