Skip to content

Commit

Permalink
Merge pull request #824 from RouxAntoine/fix/pool-type
Browse files Browse the repository at this point in the history
Fix/pool type
  • Loading branch information
dmacvicar authored Jun 30, 2021
2 parents 7a5cbe5 + 175947f commit 7471f52
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
10 changes: 9 additions & 1 deletion libvirt/resource_libvirt_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,14 @@ func resourceLibvirtPoolRead(d *schema.ResourceData, meta interface{}) error {
d.Set("path", poolPath)
}

poolType := poolDef.Type
if poolType == "" {
log.Printf("Pool %s has no type specified", pool.Name)
} else {
log.Printf("[DEBUG] Pool %s type: %s", pool.Name, poolType)
d.Set("type", poolType)
}

return nil
}

Expand All @@ -232,7 +240,7 @@ func resourceLibvirtPoolDelete(d *schema.ResourceData, meta interface{}) error {
}

func resourceLibvirtPoolExists(d *schema.ResourceData, meta interface{}) (bool, error) {
log.Printf("[DEBUG] Check if resource libvirt_pool exists")
log.Printf("[DEBUG] Check if resource (id : %s) libvirt_pool exists", d.Id())
client := meta.(*Client)
virConn := client.libvirt
if virConn == nil {
Expand Down
79 changes: 79 additions & 0 deletions libvirt/resource_libvirt_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,85 @@ func testAccCheckLibvirtPoolDoesNotExists(n string, pool *libvirt.StoragePool) r
}
}

func TestAccLibvirtPool_Import(t *testing.T) {
var pool libvirt.StoragePool
randomPoolResource := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
randomPoolName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
poolPath := "/tmp/terraform-provider-libvirt-pool-" + randomPoolName
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLibvirtPoolDestroy,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "libvirt_pool" "%s" {
name = "%s"
type = "dir"
path = "%s"
}`, randomPoolResource, randomPoolName, poolPath),
Check: testAccCheckLibvirtPoolExists("libvirt_pool."+randomPoolResource, &pool),
Destroy: false,
},
{
ResourceName: "libvirt_pool." + randomPoolResource,
ImportState: true,
ImportStateCheck: func(instanceState []*terraform.InstanceState) error {
// check all instance state imported with same assert
for i, f := range instanceState {
if err := composeTestImportStateCheckFunc(
testImportStateCheckResourceAttr("libvirt_pool."+randomPoolResource, "name", randomPoolName),
testImportStateCheckResourceAttr("libvirt_pool."+randomPoolResource, "type", "dir"),
testImportStateCheckResourceAttr("libvirt_pool."+randomPoolResource, "path", poolPath),
)(f); err != nil {
return fmt.Errorf("Check InstanceState n°%d / %d error: %s", i+1, len(instanceState), err)
}
}

return nil
},
},
},
})
}

// ImportStateCheckFunc one import instance state check function
// differ from github.com/hashicorp/terraform-plugin-sdk/helper/resource.ImportStateCheckFunc
// which is multiple import Instance State check function
type ImportStateCheckFunc func(is *terraform.InstanceState) error

// composeTestImportStateCheckFunc compose multiple InstanceState check
func composeTestImportStateCheckFunc(fs ...ImportStateCheckFunc) ImportStateCheckFunc {
return func(is *terraform.InstanceState) error {
for i, f := range fs {
if err := f(is); err != nil {
return fmt.Errorf("Check %d/%d error: %s", i+1, len(fs), err)
}
}

return nil
}
}

// testImportStateCheckResourceAttr assert if a terraform.InstanceState as attribute name[key] with value
func testImportStateCheckResourceAttr(name string, key string, value string) ImportStateCheckFunc {
return func(instanceState *terraform.InstanceState) error {
if v, ok := instanceState.Attributes[key]; !ok || v != value {
if !ok {
return fmt.Errorf("%s: Attribute '%s' not found", name, key)
}

return fmt.Errorf(
"%s: Attribute '%s' expected %#v, got %#v",
name,
key,
value,
v)
}
return nil
}
}

func TestAccLibvirtPool_Basic(t *testing.T) {
var pool libvirt.StoragePool
randomPoolResource := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
Expand Down

0 comments on commit 7471f52

Please sign in to comment.