Skip to content

Commit 7471f52

Browse files
authored
Merge pull request #824 from RouxAntoine/fix/pool-type
Fix/pool type
2 parents 7a5cbe5 + 175947f commit 7471f52

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

libvirt/resource_libvirt_pool.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,14 @@ func resourceLibvirtPoolRead(d *schema.ResourceData, meta interface{}) error {
219219
d.Set("path", poolPath)
220220
}
221221

222+
poolType := poolDef.Type
223+
if poolType == "" {
224+
log.Printf("Pool %s has no type specified", pool.Name)
225+
} else {
226+
log.Printf("[DEBUG] Pool %s type: %s", pool.Name, poolType)
227+
d.Set("type", poolType)
228+
}
229+
222230
return nil
223231
}
224232

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

234242
func resourceLibvirtPoolExists(d *schema.ResourceData, meta interface{}) (bool, error) {
235-
log.Printf("[DEBUG] Check if resource libvirt_pool exists")
243+
log.Printf("[DEBUG] Check if resource (id : %s) libvirt_pool exists", d.Id())
236244
client := meta.(*Client)
237245
virConn := client.libvirt
238246
if virConn == nil {

libvirt/resource_libvirt_pool_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,85 @@ func testAccCheckLibvirtPoolDoesNotExists(n string, pool *libvirt.StoragePool) r
5858
}
5959
}
6060

61+
func TestAccLibvirtPool_Import(t *testing.T) {
62+
var pool libvirt.StoragePool
63+
randomPoolResource := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
64+
randomPoolName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
65+
poolPath := "/tmp/terraform-provider-libvirt-pool-" + randomPoolName
66+
resource.Test(t, resource.TestCase{
67+
PreCheck: func() { testAccPreCheck(t) },
68+
Providers: testAccProviders,
69+
CheckDestroy: testAccCheckLibvirtPoolDestroy,
70+
Steps: []resource.TestStep{
71+
{
72+
Config: fmt.Sprintf(`
73+
resource "libvirt_pool" "%s" {
74+
name = "%s"
75+
type = "dir"
76+
path = "%s"
77+
}`, randomPoolResource, randomPoolName, poolPath),
78+
Check: testAccCheckLibvirtPoolExists("libvirt_pool."+randomPoolResource, &pool),
79+
Destroy: false,
80+
},
81+
{
82+
ResourceName: "libvirt_pool." + randomPoolResource,
83+
ImportState: true,
84+
ImportStateCheck: func(instanceState []*terraform.InstanceState) error {
85+
// check all instance state imported with same assert
86+
for i, f := range instanceState {
87+
if err := composeTestImportStateCheckFunc(
88+
testImportStateCheckResourceAttr("libvirt_pool."+randomPoolResource, "name", randomPoolName),
89+
testImportStateCheckResourceAttr("libvirt_pool."+randomPoolResource, "type", "dir"),
90+
testImportStateCheckResourceAttr("libvirt_pool."+randomPoolResource, "path", poolPath),
91+
)(f); err != nil {
92+
return fmt.Errorf("Check InstanceState n°%d / %d error: %s", i+1, len(instanceState), err)
93+
}
94+
}
95+
96+
return nil
97+
},
98+
},
99+
},
100+
})
101+
}
102+
103+
// ImportStateCheckFunc one import instance state check function
104+
// differ from github.com/hashicorp/terraform-plugin-sdk/helper/resource.ImportStateCheckFunc
105+
// which is multiple import Instance State check function
106+
type ImportStateCheckFunc func(is *terraform.InstanceState) error
107+
108+
// composeTestImportStateCheckFunc compose multiple InstanceState check
109+
func composeTestImportStateCheckFunc(fs ...ImportStateCheckFunc) ImportStateCheckFunc {
110+
return func(is *terraform.InstanceState) error {
111+
for i, f := range fs {
112+
if err := f(is); err != nil {
113+
return fmt.Errorf("Check %d/%d error: %s", i+1, len(fs), err)
114+
}
115+
}
116+
117+
return nil
118+
}
119+
}
120+
121+
// testImportStateCheckResourceAttr assert if a terraform.InstanceState as attribute name[key] with value
122+
func testImportStateCheckResourceAttr(name string, key string, value string) ImportStateCheckFunc {
123+
return func(instanceState *terraform.InstanceState) error {
124+
if v, ok := instanceState.Attributes[key]; !ok || v != value {
125+
if !ok {
126+
return fmt.Errorf("%s: Attribute '%s' not found", name, key)
127+
}
128+
129+
return fmt.Errorf(
130+
"%s: Attribute '%s' expected %#v, got %#v",
131+
name,
132+
key,
133+
value,
134+
v)
135+
}
136+
return nil
137+
}
138+
}
139+
61140
func TestAccLibvirtPool_Basic(t *testing.T) {
62141
var pool libvirt.StoragePool
63142
randomPoolResource := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)

0 commit comments

Comments
 (0)