github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/kubernetes/structure_service_spec.go (about) 1 package kubernetes 2 3 import ( 4 "github.com/hashicorp/terraform/helper/schema" 5 "k8s.io/apimachinery/pkg/util/intstr" 6 "k8s.io/kubernetes/pkg/api/v1" 7 ) 8 9 // Flatteners 10 11 func flattenIntOrString(in intstr.IntOrString) int { 12 return in.IntValue() 13 } 14 15 func flattenServicePort(in []v1.ServicePort) []interface{} { 16 att := make([]interface{}, len(in), len(in)) 17 for i, n := range in { 18 m := make(map[string]interface{}) 19 m["name"] = n.Name 20 m["protocol"] = string(n.Protocol) 21 m["port"] = int(n.Port) 22 m["target_port"] = flattenIntOrString(n.TargetPort) 23 m["node_port"] = int(n.NodePort) 24 25 att[i] = m 26 } 27 return att 28 } 29 30 func flattenServiceSpec(in v1.ServiceSpec) []interface{} { 31 att := make(map[string]interface{}) 32 if len(in.Ports) > 0 { 33 att["port"] = flattenServicePort(in.Ports) 34 } 35 if len(in.Selector) > 0 { 36 att["selector"] = in.Selector 37 } 38 if in.ClusterIP != "" { 39 att["cluster_ip"] = in.ClusterIP 40 } 41 if in.Type != "" { 42 att["type"] = string(in.Type) 43 } 44 if len(in.ExternalIPs) > 0 { 45 att["external_ips"] = newStringSet(schema.HashString, in.ExternalIPs) 46 } 47 if in.SessionAffinity != "" { 48 att["session_affinity"] = string(in.SessionAffinity) 49 } 50 if in.LoadBalancerIP != "" { 51 att["load_balancer_ip"] = in.LoadBalancerIP 52 } 53 if len(in.LoadBalancerSourceRanges) > 0 { 54 att["load_balancer_source_ranges"] = newStringSet(schema.HashString, in.LoadBalancerSourceRanges) 55 } 56 if in.ExternalName != "" { 57 att["external_name"] = in.ExternalName 58 } 59 return []interface{}{att} 60 } 61 62 // Expanders 63 64 func expandIntOrString(in int) intstr.IntOrString { 65 return intstr.FromInt(in) 66 } 67 68 func expandServicePort(l []interface{}) []v1.ServicePort { 69 if len(l) == 0 || l[0] == nil { 70 return []v1.ServicePort{} 71 } 72 obj := make([]v1.ServicePort, len(l), len(l)) 73 for i, n := range l { 74 cfg := n.(map[string]interface{}) 75 obj[i] = v1.ServicePort{ 76 Port: int32(cfg["port"].(int)), 77 TargetPort: expandIntOrString(cfg["target_port"].(int)), 78 } 79 if v, ok := cfg["name"].(string); ok { 80 obj[i].Name = v 81 } 82 if v, ok := cfg["protocol"].(string); ok { 83 obj[i].Protocol = v1.Protocol(v) 84 } 85 if v, ok := cfg["node_port"].(int); ok { 86 obj[i].NodePort = int32(v) 87 } 88 } 89 return obj 90 } 91 92 func expandServiceSpec(l []interface{}) v1.ServiceSpec { 93 if len(l) == 0 || l[0] == nil { 94 return v1.ServiceSpec{} 95 } 96 in := l[0].(map[string]interface{}) 97 obj := v1.ServiceSpec{} 98 99 if v, ok := in["port"].([]interface{}); ok && len(v) > 0 { 100 obj.Ports = expandServicePort(v) 101 } 102 if v, ok := in["selector"].(map[string]interface{}); ok && len(v) > 0 { 103 obj.Selector = expandStringMap(v) 104 } 105 if v, ok := in["cluster_ip"].(string); ok { 106 obj.ClusterIP = v 107 } 108 if v, ok := in["type"].(string); ok { 109 obj.Type = v1.ServiceType(v) 110 } 111 if v, ok := in["external_ips"].(*schema.Set); ok && v.Len() > 0 { 112 obj.ExternalIPs = sliceOfString(v.List()) 113 } 114 if v, ok := in["session_affinity"].(string); ok { 115 obj.SessionAffinity = v1.ServiceAffinity(v) 116 } 117 if v, ok := in["load_balancer_ip"].(string); ok { 118 obj.LoadBalancerIP = v 119 } 120 if v, ok := in["load_balancer_source_ranges"].(*schema.Set); ok && v.Len() > 0 { 121 obj.LoadBalancerSourceRanges = sliceOfString(v.List()) 122 } 123 if v, ok := in["external_name"].(string); ok { 124 obj.ExternalName = v 125 } 126 return obj 127 } 128 129 // Patch Ops 130 131 func patchServiceSpec(keyPrefix, pathPrefix string, d *schema.ResourceData) PatchOperations { 132 ops := make([]PatchOperation, 0, 0) 133 if d.HasChange(keyPrefix + "selector") { 134 ops = append(ops, &ReplaceOperation{ 135 Path: pathPrefix + "selector", 136 Value: d.Get(keyPrefix + "selector").(map[string]interface{}), 137 }) 138 } 139 if d.HasChange(keyPrefix + "type") { 140 ops = append(ops, &ReplaceOperation{ 141 Path: pathPrefix + "type", 142 Value: d.Get(keyPrefix + "type").(string), 143 }) 144 } 145 if d.HasChange(keyPrefix + "session_affinity") { 146 ops = append(ops, &ReplaceOperation{ 147 Path: pathPrefix + "sessionAffinity", 148 Value: d.Get(keyPrefix + "session_affinity").(string), 149 }) 150 } 151 if d.HasChange(keyPrefix + "load_balancer_ip") { 152 ops = append(ops, &ReplaceOperation{ 153 Path: pathPrefix + "loadBalancerIP", 154 Value: d.Get(keyPrefix + "load_balancer_ip").(string), 155 }) 156 } 157 if d.HasChange(keyPrefix + "load_balancer_source_ranges") { 158 ops = append(ops, &ReplaceOperation{ 159 Path: pathPrefix + "loadBalancerSourceRanges", 160 Value: d.Get(keyPrefix + "load_balancer_source_ranges").(*schema.Set).List(), 161 }) 162 } 163 if d.HasChange(keyPrefix + "port") { 164 ops = append(ops, &ReplaceOperation{ 165 Path: pathPrefix + "ports", 166 Value: expandServicePort(d.Get(keyPrefix + "port").([]interface{})), 167 }) 168 } 169 if d.HasChange(keyPrefix + "external_ips") { 170 // If we haven't done this the deprecated field would have priority 171 ops = append(ops, &ReplaceOperation{ 172 Path: pathPrefix + "deprecatedPublicIPs", 173 Value: nil, 174 }) 175 176 ops = append(ops, &ReplaceOperation{ 177 Path: pathPrefix + "externalIPs", 178 Value: d.Get(keyPrefix + "external_ips").(*schema.Set).List(), 179 }) 180 } 181 if d.HasChange(keyPrefix + "external_name") { 182 ops = append(ops, &ReplaceOperation{ 183 Path: pathPrefix + "externalName", 184 Value: d.Get(keyPrefix + "external_name").(string), 185 }) 186 } 187 return ops 188 }