Skip to content

Commit

Permalink
storage/volume: Combine the computed volume keys with the inherited s…
Browse files Browse the repository at this point in the history
…torage pool keys

Signed-off-by: Fabian Mettler <[email protected]>
(cherry picked from commit 0075a635761973e57b438d74630a06ed83f993fe)
Signed-off-by: Din Music <[email protected]>
  • Loading branch information
maveonair authored and MusicDin committed Sep 26, 2024
1 parent 40eacc7 commit d6b4f5c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
29 changes: 28 additions & 1 deletion internal/storage/resource_storage_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package storage
import (
"context"
"fmt"
"strings"

lxd "github.com/canonical/lxd/client"
"github.com/canonical/lxd/shared/api"
Expand Down Expand Up @@ -349,7 +350,14 @@ func (r StorageVolumeResource) SyncState(ctx context.Context, tfState *tfsdk.Sta
}

// Extract user defined config and merge it with current config state.
stateConfig := common.StripConfig(vol.Config, m.Config, m.ComputedKeys())
inheritedPoolVolumeKeys, err := m.InheritedStoragePoolVolumeKeys(server, poolName)
if err != nil {
respDiags.AddError(fmt.Sprintf("Failed to retrieve storage pool config %q", volName), err.Error())
return respDiags
}

combinedComputedKeys := append(inheritedPoolVolumeKeys, m.ComputedKeys()...)
stateConfig := common.StripConfig(vol.Config, m.Config, combinedComputedKeys)

config, diags := common.ToConfigMapType(ctx, stateConfig, m.Config)
respDiags.Append(diags...)
Expand Down Expand Up @@ -381,3 +389,22 @@ func (_ StorageVolumeModel) ComputedKeys() []string {
"volatile.",
}
}

func (_ StorageVolumeModel) InheritedStoragePoolVolumeKeys(server lxd.InstanceServer, poolName string) ([]string, error) {
pool, _, err := server.GetStoragePool(poolName)
if err != nil {
return nil, err
}

volumePrefix := "volume."
inheritedKeys := make([]string, 0)

for key := range pool.Config {
if strings.HasPrefix(key, volumePrefix) {
inheritedKey := strings.TrimPrefix(key, volumePrefix)
inheritedKeys = append(inheritedKeys, inheritedKey)
}
}

return inheritedKeys, nil
}
51 changes: 51 additions & 0 deletions internal/storage/resource_storage_volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,35 @@ func TestAccStorageVolume_importProject(t *testing.T) {
})
}

func TestAccStorageVolume_inheritedStoragePoolKeys(t *testing.T) {
poolName := acctest.GenerateName(2, "-")
volumeName := acctest.GenerateName(2, "-")

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProtoV6ProviderFactories: acctest.ProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccStorageVolume_inheritedStoragePoolVolumeKeys(poolName, volumeName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("lxd_storage_pool.pool1", "name", poolName),
resource.TestCheckResourceAttr("lxd_storage_pool.pool1", "driver", "zfs"),
resource.TestCheckResourceAttr("lxd_storage_pool.pool1", "config.volume.zfs.remove_snapshots", "true"),
resource.TestCheckResourceAttr("lxd_storage_pool.pool1", "config.volume.zfs.use_refquota", "true"),
resource.TestCheckResourceAttr("lxd_volume.volume1", "name", volumeName),
resource.TestCheckResourceAttr("lxd_volume.volume1", "pool", poolName),
resource.TestCheckResourceAttr("lxd_volume.volume1", "type", "custom"),
resource.TestCheckResourceAttr("lxd_volume.volume1", "content_type", "block"),

// Ensure computed keys are not tracked.
resource.TestCheckNoResourceAttr("lxd_volume.volume1", "config.zfs.remove_snapshots"),
resource.TestCheckNoResourceAttr("lxd_volume.volume1", "config.zfs.use_refquota"),
),
},
},
})
}

func testAccStorageVolume_basic(poolName, volumeName string) string {
return fmt.Sprintf(`
resource "lxd_storage_pool" "pool1" {
Expand Down Expand Up @@ -257,3 +286,25 @@ resource "lxd_volume" "volume1" {
}
`, poolName, volumeName)
}

func testAccStorageVolume_inheritedStoragePoolVolumeKeys(poolName, volumeName string) string {
return fmt.Sprintf(`
resource "lxd_storage_pool" "pool1" {
name = "%s"
driver = "zfs"
config = {
"volume.zfs.remove_snapshots" = "true",
"volume.zfs.use_refquota" = "true"
}
}
resource "lxd_volume" "volume1" {
name = "%s"
pool = lxd_storage_pool.pool1.name
content_type = "block"
config = {
"size" = "1GiB"
}
}
`, poolName, volumeName)
}

0 comments on commit d6b4f5c

Please sign in to comment.