Skip to content

Commit

Permalink
simplify data_source schema
Browse files Browse the repository at this point in the history
  • Loading branch information
JaylonmcShan03 committed Jan 9, 2025
1 parent 4877ac7 commit 1d6b7ac
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 45 deletions.
14 changes: 2 additions & 12 deletions docs/resources/persistent_volume_claim_v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,7 @@ Optional:
### Nested Schema for `spec.data_source`

Optional:

- `persistent_volume_claim` (Block List, Max: 1) Details of the source PersistentVolumeClaim. (see [below for nested schema](#nestedblock--spec--data_source--persistent_volume_claim))

<a id="nestedblock--spec--data_source--persistent_volume_claim"></a>
### Nested Schema for `spec.data_source.persistent_volume_claim`

Required:

- `claim_name` (String) The name of the source PersistentVolumeClaim.
- `name` (String) The name of the source PersistentVolumeClaim.

<a id="nestedblock--spec--selector"></a>
### Nested Schema for `spec.selector`
Expand Down Expand Up @@ -158,9 +150,7 @@ resource "kubernetes_persistent_volume_claim_v1" "example_clone" {
}
}
data_source {
persistent_volume_claim {
claim_name = "example-source-pvc"
}
name = "example-source-pvc"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ func TestAccKubernetesPersistentVolumeClaimV1_dataSource(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesPersistentVolumeClaimV1Exists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "metadata.0.name", clonedPVC),
resource.TestCheckResourceAttr(resourceName, "spec.0.data_source.0.persistent_volume_claim.0.claim_name", originalPVC),
resource.TestCheckResourceAttr(resourceName, "spec.0.data_source.0.name", originalPVC),
),
},
},
Expand Down Expand Up @@ -1055,9 +1055,7 @@ func testAccKubernetesPersistentVolumeClaimV1Config_dataSourceClone(sourceName,
}
data_source {
persistent_volume_claim {
claim_name = "%s"
}
name = "%s"
}
}
Expand Down
16 changes: 3 additions & 13 deletions kubernetes/schema_persistent_volume_claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,10 @@ func persistentVolumeClaimSpecFields() map[string]*schema.Schema {
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"persistent_volume_claim": {
Type: schema.TypeList,
Description: "Details of the source PersistentVolumeClaim.",
"name": {
Type: schema.TypeString,
Description: "The name of the source PersistentVolumeClaim.",
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"claim_name": {
Type: schema.TypeString,
Description: "The name of the source PersistentVolumeClaim.",
Required: true,
},
},
},
},
},
},
Expand Down
26 changes: 10 additions & 16 deletions kubernetes/structure_persistent_volume_claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,12 @@ func flattenPersistentVolumeClaimSpec(in corev1.PersistentVolumeClaimSpec) []int
if in.VolumeMode != nil {
att["volume_mode"] = in.VolumeMode
}
if in.DataSource != nil {
dataSource := make(map[string]interface{})
if in.DataSource.Kind == "PersistentVolumeClaim" && in.DataSource.Name != "" {
dataSource["persistent_volume_claim"] = []interface{}{
map[string]interface{}{
"claim_name": in.DataSource.Name,
},
}
if in.DataSource != nil && in.DataSource.Name != "" {
att["data_source"] = []interface{}{
map[string]interface{}{
"name": in.DataSource.Name,
},
}
att["data_source"] = []interface{}{dataSource}
}
return []interface{}{att}
}
Expand Down Expand Up @@ -104,13 +100,11 @@ func expandPersistentVolumeClaimSpec(l []interface{}) (*corev1.PersistentVolumeC
}
if v, ok := in["data_source"].([]interface{}); ok && len(v) > 0 && v[0] != nil {
dataSource := v[0].(map[string]interface{})
if pvcSource, ok := dataSource["persistent_volume_claim"].([]interface{}); ok && len(pvcSource) > 0 && pvcSource[0] != nil {
pvcData := pvcSource[0].(map[string]interface{})
if claimName, ok := pvcData["claim_name"].(string); ok && claimName != "" {
obj.DataSource = &corev1.TypedLocalObjectReference{
Kind: "PersistentVolumeClaim",
Name: claimName,
}
if name, ok := dataSource["name"].(string); ok && name != "" {
obj.DataSource = &corev1.TypedLocalObjectReference{
Kind: "PersistentVolumeClaim",
Name: name,
APIGroup: ptr.To(""),
}
}
}
Expand Down

0 comments on commit 1d6b7ac

Please sign in to comment.