-
Notifications
You must be signed in to change notification settings - Fork 986
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor labelSelector schema and its flatteners/expanders functions …
…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
1 parent
8f6bff5
commit 90abb2d
Showing
5 changed files
with
114 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters