github.com/bradfeehan/terraform@v0.7.0-rc3.0.20170529055808-34b45c5ad841/builtin/providers/kubernetes/structure_horizontal_pod_autoscaler.go (about) 1 package kubernetes 2 3 import ( 4 "github.com/hashicorp/terraform/helper/schema" 5 api "k8s.io/kubernetes/pkg/apis/autoscaling/v1" 6 ) 7 8 func expandHorizontalPodAutoscalerSpec(in []interface{}) api.HorizontalPodAutoscalerSpec { 9 if len(in) == 0 || in[0] == nil { 10 return api.HorizontalPodAutoscalerSpec{} 11 } 12 spec := api.HorizontalPodAutoscalerSpec{} 13 m := in[0].(map[string]interface{}) 14 if v, ok := m["max_replicas"]; ok { 15 spec.MaxReplicas = int32(v.(int)) 16 } 17 if v, ok := m["min_replicas"].(int); ok && v > 0 { 18 spec.MinReplicas = ptrToInt32(int32(v)) 19 } 20 if v, ok := m["scale_target_ref"]; ok { 21 spec.ScaleTargetRef = expandCrossVersionObjectReference(v.([]interface{})) 22 } 23 if v, ok := m["target_cpu_utilization_percentage"].(int); ok && v > 0 { 24 spec.TargetCPUUtilizationPercentage = ptrToInt32(int32(v)) 25 } 26 27 return spec 28 } 29 30 func expandCrossVersionObjectReference(in []interface{}) api.CrossVersionObjectReference { 31 if len(in) == 0 || in[0] == nil { 32 return api.CrossVersionObjectReference{} 33 } 34 ref := api.CrossVersionObjectReference{} 35 m := in[0].(map[string]interface{}) 36 37 if v, ok := m["api_version"]; ok { 38 ref.APIVersion = v.(string) 39 } 40 if v, ok := m["kind"]; ok { 41 ref.Kind = v.(string) 42 } 43 if v, ok := m["name"]; ok { 44 ref.Name = v.(string) 45 } 46 return ref 47 } 48 49 func flattenHorizontalPodAutoscalerSpec(spec api.HorizontalPodAutoscalerSpec) []interface{} { 50 m := make(map[string]interface{}, 0) 51 m["max_replicas"] = spec.MaxReplicas 52 if spec.MinReplicas != nil { 53 m["min_replicas"] = *spec.MinReplicas 54 } 55 m["scale_target_ref"] = flattenCrossVersionObjectReference(spec.ScaleTargetRef) 56 if spec.TargetCPUUtilizationPercentage != nil { 57 m["target_cpu_utilization_percentage"] = *spec.TargetCPUUtilizationPercentage 58 } 59 return []interface{}{m} 60 } 61 62 func flattenCrossVersionObjectReference(ref api.CrossVersionObjectReference) []interface{} { 63 m := make(map[string]interface{}, 0) 64 if ref.APIVersion != "" { 65 m["api_version"] = ref.APIVersion 66 } 67 if ref.Kind != "" { 68 m["kind"] = ref.Kind 69 } 70 if ref.Name != "" { 71 m["name"] = ref.Name 72 } 73 return []interface{}{m} 74 } 75 76 func patchHorizontalPodAutoscalerSpec(prefix string, pathPrefix string, d *schema.ResourceData) []PatchOperation { 77 ops := make([]PatchOperation, 0) 78 79 if d.HasChange(prefix + "max_replicas") { 80 ops = append(ops, &ReplaceOperation{ 81 Path: pathPrefix + "/maxReplicas", 82 Value: d.Get(prefix + "max_replicas").(int), 83 }) 84 } 85 if d.HasChange(prefix + "min_replicas") { 86 ops = append(ops, &ReplaceOperation{ 87 Path: pathPrefix + "/minReplicas", 88 Value: d.Get(prefix + "min_replicas").(int), 89 }) 90 } 91 if d.HasChange(prefix + "scale_target_ref") { 92 ops = append(ops, &ReplaceOperation{ 93 Path: pathPrefix + "/scaleTargetRef", 94 Value: expandCrossVersionObjectReference(d.Get(prefix + "scale_target_ref").([]interface{})), 95 }) 96 } 97 if d.HasChange(prefix + "target_cpu_utilization_percentage") { 98 ops = append(ops, &ReplaceOperation{ 99 Path: pathPrefix + "/targetCPUUtilizationPercentage", 100 Value: d.Get(prefix + "target_cpu_utilization_percentage").(int), 101 }) 102 } 103 104 return ops 105 }