Skip to content

Commit

Permalink
Fix 1322: allow_volume_expansion fix (hashicorp#1519)
Browse files Browse the repository at this point in the history
* fix src

* add tests

Co-authored-by: Mauricio Alvarez Leon <[email protected]>
  • Loading branch information
AnisimoffNikita and BBBmau authored Jan 23, 2023
1 parent fbd2190 commit c9182af
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
9 changes: 9 additions & 0 deletions kubernetes/resource_kubernetes_storage_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,19 @@ func resourceKubernetesStorageClassUpdate(ctx context.Context, d *schema.Resourc

name := d.Id()
ops := patchMetadata("metadata.0.", "/metadata/", d)

if d.HasChange("allow_volume_expansion") {
newVal := d.Get("allow_volume_expansion").(bool)
ops = append(ops, &ReplaceOperation{
Path: "/allowVolumeExpansion",
Value: newVal,
})
}
data, err := ops.MarshalJSON()
if err != nil {
return diag.Errorf("Failed to marshal update operations: %s", err)
}

log.Printf("[INFO] Updating storage class %q: %v", name, string(data))
out, err := conn.StorageV1().StorageClasses().Patch(ctx, name, pkgApi.JSONPatchType, data, metav1.PatchOptions{})
if err != nil {
Expand Down
66 changes: 66 additions & 0 deletions kubernetes/resource_kubernetes_storage_class_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,48 @@ func TestAccKubernetesStorageClass_minikube(t *testing.T) {
})
}

func TestAccKubernetesStorageClass_volumeExpansion(t *testing.T) {
var conf api.StorageClass
name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
resourceName := "kubernetes_storage_class.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); skipIfNotRunningInMinikube(t) },
IDRefreshName: resourceName,
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckKubernetesStorageClassDestroy,
Steps: []resource.TestStep{
{
Config: testAccKubernetesStorageClassConfig_volumeExpansion(name, "k8s.io/minikube-hostpath"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesStorageClassExists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "metadata.0.name", name),
resource.TestCheckResourceAttrSet(resourceName, "metadata.0.generation"),
resource.TestCheckResourceAttrSet(resourceName, "metadata.0.resource_version"),
resource.TestCheckResourceAttrSet(resourceName, "metadata.0.uid"),
resource.TestCheckResourceAttr(resourceName, "storage_provisioner", "k8s.io/minikube-hostpath"),
resource.TestCheckResourceAttr(resourceName, "reclaim_policy", "Delete"),
resource.TestCheckResourceAttr(resourceName, "volume_binding_mode", "Immediate"),
resource.TestCheckResourceAttr(resourceName, "allow_volume_expansion", "true"),
),
},
{
Config: testAccKubernetesStorageClassConfig_volumeExpansionModified(name, "k8s.io/minikube-hostpath"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesStorageClassExists(resourceName, &conf),
resource.TestCheckResourceAttrSet(resourceName, "metadata.0.generation"),
resource.TestCheckResourceAttrSet(resourceName, "metadata.0.resource_version"),
resource.TestCheckResourceAttrSet(resourceName, "metadata.0.uid"),
resource.TestCheckResourceAttr(resourceName, "storage_provisioner", "k8s.io/minikube-hostpath"),
resource.TestCheckResourceAttr(resourceName, "reclaim_policy", "Delete"),
resource.TestCheckResourceAttr(resourceName, "volume_binding_mode", "Immediate"),
resource.TestCheckResourceAttr(resourceName, "allow_volume_expansion", "false"),
),
},
},
})
}

func TestAccKubernetesStorageClass_basic(t *testing.T) {
var conf api.StorageClass
name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
Expand Down Expand Up @@ -364,6 +406,30 @@ func testAccKubernetesStorageClassConfig_modified(name, provisioner string) stri
`, name, provisioner)
}

func testAccKubernetesStorageClassConfig_volumeExpansion(name, provisioner string) string {
return fmt.Sprintf(`resource "kubernetes_storage_class" "test" {
metadata {
name = "%s"
}
storage_provisioner = "%s"
allow_volume_expansion = true
}
`, name, provisioner)
}

func testAccKubernetesStorageClassConfig_volumeExpansionModified(name, provisioner string) string {
return fmt.Sprintf(`resource "kubernetes_storage_class" "test" {
metadata {
name = "%s"
}
storage_provisioner = "%s"
allow_volume_expansion = false
}
`, name, provisioner)
}

func testAccKubernetesStorageClassConfig_noParameters(name, provisioner string) string {
return fmt.Sprintf(`resource "kubernetes_storage_class" "test" {
metadata {
Expand Down

0 comments on commit c9182af

Please sign in to comment.