github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/kubernetes/structure_persistent_volume_claim.go (about)

     1  package kubernetes
     2  
     3  import (
     4  	"github.com/hashicorp/terraform/helper/schema"
     5  	"k8s.io/kubernetes/pkg/api/unversioned"
     6  	"k8s.io/kubernetes/pkg/api/v1"
     7  )
     8  
     9  // Flatteners
    10  
    11  func flattenLabelSelector(in *unversioned.LabelSelector) []interface{} {
    12  	att := make(map[string]interface{})
    13  	if len(in.MatchLabels) > 0 {
    14  		att["match_labels"] = in.MatchLabels
    15  	}
    16  	if len(in.MatchExpressions) > 0 {
    17  		att["match_expressions"] = flattenLabelSelectorRequirement(in.MatchExpressions)
    18  	}
    19  	return []interface{}{att}
    20  }
    21  
    22  func flattenLabelSelectorRequirement(in []unversioned.LabelSelectorRequirement) []interface{} {
    23  	att := make([]interface{}, len(in), len(in))
    24  	for i, n := range in {
    25  		m := make(map[string]interface{})
    26  		m["key"] = n.Key
    27  		m["operator"] = n.Operator
    28  		m["values"] = newStringSet(schema.HashString, n.Values)
    29  		att[i] = m
    30  	}
    31  	return att
    32  }
    33  
    34  func flattenPersistentVolumeClaimSpec(in v1.PersistentVolumeClaimSpec) []interface{} {
    35  	att := make(map[string]interface{})
    36  	att["access_modes"] = flattenPersistentVolumeAccessModes(in.AccessModes)
    37  	att["resources"] = flattenResourceRequirements(in.Resources)
    38  	if in.Selector != nil {
    39  		att["selector"] = flattenLabelSelector(in.Selector)
    40  	}
    41  	if in.VolumeName != "" {
    42  		att["volume_name"] = in.VolumeName
    43  	}
    44  	return []interface{}{att}
    45  }
    46  
    47  func flattenResourceRequirements(in v1.ResourceRequirements) []interface{} {
    48  	att := make(map[string]interface{})
    49  	if len(in.Limits) > 0 {
    50  		att["limits"] = flattenResourceList(in.Limits)
    51  	}
    52  	if len(in.Requests) > 0 {
    53  		att["requests"] = flattenResourceList(in.Requests)
    54  	}
    55  	return []interface{}{att}
    56  }
    57  
    58  // Expanders
    59  
    60  func expandLabelSelector(l []interface{}) *unversioned.LabelSelector {
    61  	if len(l) == 0 || l[0] == nil {
    62  		return &unversioned.LabelSelector{}
    63  	}
    64  	in := l[0].(map[string]interface{})
    65  	obj := &unversioned.LabelSelector{}
    66  	if v, ok := in["match_labels"].(map[string]interface{}); ok && len(v) > 0 {
    67  		obj.MatchLabels = expandStringMap(v)
    68  	}
    69  	if v, ok := in["match_expressions"].([]interface{}); ok && len(v) > 0 {
    70  		obj.MatchExpressions = expandLabelSelectorRequirement(v)
    71  	}
    72  	return obj
    73  }
    74  
    75  func expandLabelSelectorRequirement(l []interface{}) []unversioned.LabelSelectorRequirement {
    76  	if len(l) == 0 || l[0] == nil {
    77  		return []unversioned.LabelSelectorRequirement{}
    78  	}
    79  	obj := make([]unversioned.LabelSelectorRequirement, len(l), len(l))
    80  	for i, n := range l {
    81  		in := n.(map[string]interface{})
    82  		obj[i] = unversioned.LabelSelectorRequirement{
    83  			Key:      in["key"].(string),
    84  			Operator: unversioned.LabelSelectorOperator(in["operator"].(string)),
    85  			Values:   sliceOfString(in["values"].(*schema.Set).List()),
    86  		}
    87  	}
    88  	return obj
    89  }
    90  
    91  func expandPersistentVolumeClaimSpec(l []interface{}) (v1.PersistentVolumeClaimSpec, error) {
    92  	if len(l) == 0 || l[0] == nil {
    93  		return v1.PersistentVolumeClaimSpec{}, nil
    94  	}
    95  	in := l[0].(map[string]interface{})
    96  	resourceRequirements, err := expandResourceRequirements(in["resources"].([]interface{}))
    97  	if err != nil {
    98  		return v1.PersistentVolumeClaimSpec{}, err
    99  	}
   100  	obj := v1.PersistentVolumeClaimSpec{
   101  		AccessModes: expandPersistentVolumeAccessModes(in["access_modes"].(*schema.Set).List()),
   102  		Resources:   resourceRequirements,
   103  	}
   104  	if v, ok := in["selector"].([]interface{}); ok && len(v) > 0 {
   105  		obj.Selector = expandLabelSelector(v)
   106  	}
   107  	if v, ok := in["volume_name"].(string); ok {
   108  		obj.VolumeName = v
   109  	}
   110  	return obj, nil
   111  }
   112  
   113  func expandResourceRequirements(l []interface{}) (v1.ResourceRequirements, error) {
   114  	if len(l) == 0 || l[0] == nil {
   115  		return v1.ResourceRequirements{}, nil
   116  	}
   117  	in := l[0].(map[string]interface{})
   118  	obj := v1.ResourceRequirements{}
   119  	if v, ok := in["limits"].(map[string]interface{}); ok && len(v) > 0 {
   120  		var err error
   121  		obj.Limits, err = expandMapToResourceList(v)
   122  		if err != nil {
   123  			return obj, err
   124  		}
   125  	}
   126  	if v, ok := in["requests"].(map[string]interface{}); ok && len(v) > 0 {
   127  		var err error
   128  		obj.Requests, err = expandMapToResourceList(v)
   129  		if err != nil {
   130  			return obj, err
   131  		}
   132  	}
   133  	return obj, nil
   134  }