Skip to content

Commit 8b98d40

Browse files
authored
Merge pull request #68 from maveonair/storage-computed-keys
storage: Combine the inherited storage pool volume keys with the computed keys for storage pool volume
2 parents 582e761 + 0075a63 commit 8b98d40

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

internal/storage/resource_storage_volume.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package storage
33
import (
44
"context"
55
"fmt"
6+
"strings"
67

78
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
89
"github.com/hashicorp/terraform-plugin-framework/attr"
@@ -349,7 +350,14 @@ func (r StorageVolumeResource) SyncState(ctx context.Context, tfState *tfsdk.Sta
349350
}
350351

351352
// Extract user defined config and merge it with current config state.
352-
stateConfig := common.StripConfig(vol.Config, m.Config, m.ComputedKeys())
353+
inheritedPoolVolumeKeys, err := m.InheritedStoragePoolVolumeKeys(server, poolName)
354+
if err != nil {
355+
respDiags.AddError(fmt.Sprintf("Failed to retrieve storage pool config %q", volName), err.Error())
356+
return respDiags
357+
}
358+
359+
combinedComputedKeys := append(inheritedPoolVolumeKeys, m.ComputedKeys()...)
360+
stateConfig := common.StripConfig(vol.Config, m.Config, combinedComputedKeys)
353361

354362
config, diags := common.ToConfigMapType(ctx, stateConfig, m.Config)
355363
respDiags.Append(diags...)
@@ -381,3 +389,22 @@ func (_ StorageVolumeModel) ComputedKeys() []string {
381389
"volatile.",
382390
}
383391
}
392+
393+
func (_ StorageVolumeModel) InheritedStoragePoolVolumeKeys(server incus.InstanceServer, poolName string) ([]string, error) {
394+
volumePrefix := "volume."
395+
inheritedKeys := make([]string, 0)
396+
397+
pool, _, err := server.GetStoragePool(poolName)
398+
if err != nil {
399+
return nil, err
400+
}
401+
402+
for key := range pool.Config {
403+
if strings.HasPrefix(key, volumePrefix) {
404+
inheritedKey := strings.TrimPrefix(key, volumePrefix)
405+
inheritedKeys = append(inheritedKeys, inheritedKey)
406+
}
407+
}
408+
409+
return inheritedKeys, nil
410+
}

internal/storage/resource_storage_volume_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,35 @@ func TestAccStorageVolume_importProject(t *testing.T) {
173173
})
174174
}
175175

176+
func TestAccStorageVolume_inheritedStoragePoolKeys(t *testing.T) {
177+
poolName := petname.Generate(2, "-")
178+
volumeName := petname.Generate(2, "-")
179+
180+
resource.Test(t, resource.TestCase{
181+
PreCheck: func() { acctest.PreCheck(t) },
182+
ProtoV6ProviderFactories: acctest.ProtoV6ProviderFactories,
183+
Steps: []resource.TestStep{
184+
{
185+
Config: testAccStorageVolume_inheritedStoragePoolVolumeKeys(poolName, volumeName),
186+
Check: resource.ComposeTestCheckFunc(
187+
resource.TestCheckResourceAttr("incus_storage_pool.pool1", "name", poolName),
188+
resource.TestCheckResourceAttr("incus_storage_pool.pool1", "driver", "zfs"),
189+
resource.TestCheckResourceAttr("incus_storage_pool.pool1", "config.volume.zfs.remove_snapshots", "true"),
190+
resource.TestCheckResourceAttr("incus_storage_pool.pool1", "config.volume.zfs.use_refquota", "true"),
191+
resource.TestCheckResourceAttr("incus_storage_volume.volume1", "name", volumeName),
192+
resource.TestCheckResourceAttr("incus_storage_volume.volume1", "pool", poolName),
193+
resource.TestCheckResourceAttr("incus_storage_volume.volume1", "type", "custom"),
194+
resource.TestCheckResourceAttr("incus_storage_volume.volume1", "content_type", "block"),
195+
196+
// Ensure computed keys are not tracked.
197+
resource.TestCheckNoResourceAttr("incus_storage_volume.volume1", "config.zfs.remove_snapshots"),
198+
resource.TestCheckNoResourceAttr("incus_storage_volume.volume1", "config.zfs.use_refquota"),
199+
),
200+
},
201+
},
202+
})
203+
}
204+
176205
func testAccStorageVolume_basic(poolName, volumeName string) string {
177206
return fmt.Sprintf(`
178207
resource "incus_storage_pool" "pool1" {
@@ -258,3 +287,25 @@ resource "incus_storage_volume" "volume1" {
258287
}
259288
`, poolName, volumeName)
260289
}
290+
291+
func testAccStorageVolume_inheritedStoragePoolVolumeKeys(poolName, volumeName string) string {
292+
return fmt.Sprintf(`
293+
resource "incus_storage_pool" "pool1" {
294+
name = "%s"
295+
driver = "zfs"
296+
config = {
297+
"volume.zfs.remove_snapshots" = "true",
298+
"volume.zfs.use_refquota" = "true"
299+
}
300+
}
301+
302+
resource "incus_storage_volume" "volume1" {
303+
name = "%s"
304+
pool = incus_storage_pool.pool1.name
305+
content_type = "block"
306+
config = {
307+
"size" = "1GiB"
308+
}
309+
}
310+
`, poolName, volumeName)
311+
}

0 commit comments

Comments
 (0)