Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor labelSelector schema and its flatteners/expanders functions for reuse #140

Merged
merged 2 commits into from
Mar 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
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