Skip to content

Commit

Permalink
Refactor labelSelector schema and its flatteners/expanders functions …
Browse files Browse the repository at this point in the history
…for reuse (#140)

* Move labelSelector schema and its flatteners/expanders functions out of persistant volume claims files to their own files for reuse

* Simplify code
  • Loading branch information
pdecat authored and radeksimko committed Mar 23, 2018
1 parent 8f6bff5 commit 90abb2d
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 96 deletions.
39 changes: 1 addition & 38 deletions kubernetes/resource_kubernetes_persistent_volume_claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,44 +80,7 @@ func resourceKubernetesPersistentVolumeClaim() *schema.Resource {
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"match_expressions": {
Type: schema.TypeList,
Description: "A list of label selector requirements. The requirements are ANDed.",
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Description: "The label key that the selector applies to.",
Optional: true,
ForceNew: true,
},
"operator": {
Type: schema.TypeString,
Description: "A key's relationship to a set of values. Valid operators ard `In`, `NotIn`, `Exists` and `DoesNotExist`.",
Optional: true,
ForceNew: true,
},
"values": {
Type: schema.TypeSet,
Description: "An array of string values. If the operator is `In` or `NotIn`, the values array must be non-empty. If the operator is `Exists` or `DoesNotExist`, the values array must be empty. This array is replaced during a strategic merge patch.",
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
},
},
"match_labels": {
Type: schema.TypeMap,
Description: "A map of {key,value} pairs. A single {key,value} in the matchLabels map is equivalent to an element of `match_expressions`, whose key field is \"key\", the operator is \"In\", and the values array contains only \"value\". The requirements are ANDed.",
Optional: true,
ForceNew: true,
},
},
Schema: labelSelectorFields(),
},
},
"volume_name": {
Expand Down
46 changes: 46 additions & 0 deletions kubernetes/schema_label_selector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package kubernetes

import (
"github.com/hashicorp/terraform/helper/schema"
)

func labelSelectorFields() map[string]*schema.Schema {
return map[string]*schema.Schema{
"match_expressions": {
Type: schema.TypeList,
Description: "A list of label selector requirements. The requirements are ANDed.",
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Description: "The label key that the selector applies to.",
Optional: true,
ForceNew: true,
},
"operator": {
Type: schema.TypeString,
Description: "A key's relationship to a set of values. Valid operators ard `In`, `NotIn`, `Exists` and `DoesNotExist`.",
Optional: true,
ForceNew: true,
},
"values": {
Type: schema.TypeSet,
Description: "An array of string values. If the operator is `In` or `NotIn`, the values array must be non-empty. If the operator is `Exists` or `DoesNotExist`, the values array must be empty. This array is replaced during a strategic merge patch.",
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
},
},
"match_labels": {
Type: schema.TypeMap,
Description: "A map of {key,value} pairs. A single {key,value} in the matchLabels map is equivalent to an element of `match_expressions`, whose key field is \"key\", the operator is \"In\", and the values array contains only \"value\". The requirements are ANDed.",
Optional: true,
ForceNew: true,
},
}
}
67 changes: 67 additions & 0 deletions kubernetes/structure_label_selector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package kubernetes

import (
"github.com/hashicorp/terraform/helper/schema"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// Flatteners

func flattenLabelSelector(in *metav1.LabelSelector) []interface{} {
att := make(map[string]interface{})
if len(in.MatchLabels) > 0 {
att["match_labels"] = in.MatchLabels
}
if len(in.MatchExpressions) > 0 {
att["match_expressions"] = flattenLabelSelectorRequirement(in.MatchExpressions)
}
if len(att) > 0 {
return []interface{}{att}
}
return []interface{}{}
}

func flattenLabelSelectorRequirement(in []metav1.LabelSelectorRequirement) []interface{} {
att := make([]interface{}, len(in), len(in))
for i, n := range in {
m := make(map[string]interface{})
m["key"] = n.Key
m["operator"] = n.Operator
m["values"] = newStringSet(schema.HashString, n.Values)
att[i] = m
}
return att
}

// Expanders

func expandLabelSelector(l []interface{}) *metav1.LabelSelector {
if len(l) == 0 || l[0] == nil {
return &metav1.LabelSelector{}
}
in := l[0].(map[string]interface{})
obj := &metav1.LabelSelector{}
if v, ok := in["match_labels"].(map[string]interface{}); ok && len(v) > 0 {
obj.MatchLabels = expandStringMap(v)
}
if v, ok := in["match_expressions"].([]interface{}); ok && len(v) > 0 {
obj.MatchExpressions = expandLabelSelectorRequirement(v)
}
return obj
}

func expandLabelSelectorRequirement(l []interface{}) []metav1.LabelSelectorRequirement {
if len(l) == 0 || l[0] == nil {
return []metav1.LabelSelectorRequirement{}
}
obj := make([]metav1.LabelSelectorRequirement, len(l), len(l))
for i, n := range l {
in := n.(map[string]interface{})
obj[i] = metav1.LabelSelectorRequirement{
Key: in["key"].(string),
Operator: metav1.LabelSelectorOperator(in["operator"].(string)),
Values: sliceOfString(in["values"].(*schema.Set).List()),
}
}
return obj
}
File renamed without changes.
58 changes: 0 additions & 58 deletions kubernetes/structure_persistent_volume_claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,11 @@ package kubernetes

import (
"github.com/hashicorp/terraform/helper/schema"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/pkg/api/v1"
)

// Flatteners

func flattenLabelSelector(in *metav1.LabelSelector) []interface{} {
att := make(map[string]interface{})
if len(in.MatchLabels) > 0 {
att["match_labels"] = in.MatchLabels
}
if len(in.MatchExpressions) > 0 {
att["match_expressions"] = flattenLabelSelectorRequirement(in.MatchExpressions)
}
if len(att) > 0 {
return []interface{}{att}
}
return []interface{}{}
}

func flattenLabelSelectorRequirement(in []metav1.LabelSelectorRequirement) []interface{} {
att := make([]interface{}, len(in), len(in))
for i, n := range in {
m := make(map[string]interface{})
m["key"] = n.Key
m["operator"] = n.Operator
m["values"] = newStringSet(schema.HashString, n.Values)
att[i] = m
}
return att
}

func flattenPersistentVolumeClaimSpec(in v1.PersistentVolumeClaimSpec) []interface{} {
att := make(map[string]interface{})
att["access_modes"] = flattenPersistentVolumeAccessModes(in.AccessModes)
Expand Down Expand Up @@ -63,37 +36,6 @@ func flattenResourceRequirements(in v1.ResourceRequirements) []interface{} {

// Expanders

func expandLabelSelector(l []interface{}) *metav1.LabelSelector {
if len(l) == 0 || l[0] == nil {
return &metav1.LabelSelector{}
}
in := l[0].(map[string]interface{})
obj := &metav1.LabelSelector{}
if v, ok := in["match_labels"].(map[string]interface{}); ok && len(v) > 0 {
obj.MatchLabels = expandStringMap(v)
}
if v, ok := in["match_expressions"].([]interface{}); ok && len(v) > 0 {
obj.MatchExpressions = expandLabelSelectorRequirement(v)
}
return obj
}

func expandLabelSelectorRequirement(l []interface{}) []metav1.LabelSelectorRequirement {
if len(l) == 0 || l[0] == nil {
return []metav1.LabelSelectorRequirement{}
}
obj := make([]metav1.LabelSelectorRequirement, len(l), len(l))
for i, n := range l {
in := n.(map[string]interface{})
obj[i] = metav1.LabelSelectorRequirement{
Key: in["key"].(string),
Operator: metav1.LabelSelectorOperator(in["operator"].(string)),
Values: sliceOfString(in["values"].(*schema.Set).List()),
}
}
return obj
}

func expandPersistentVolumeClaimSpec(l []interface{}) (v1.PersistentVolumeClaimSpec, error) {
if len(l) == 0 || l[0] == nil {
return v1.PersistentVolumeClaimSpec{}, nil
Expand Down

0 comments on commit 90abb2d

Please sign in to comment.