github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/kubernetes/structures.go (about) 1 package kubernetes 2 3 import ( 4 "fmt" 5 "net/url" 6 "strings" 7 8 "encoding/base64" 9 "github.com/hashicorp/terraform/helper/schema" 10 "k8s.io/kubernetes/pkg/api/resource" 11 api "k8s.io/kubernetes/pkg/api/v1" 12 ) 13 14 func idParts(id string) (string, string) { 15 parts := strings.Split(id, "/") 16 return parts[0], parts[1] 17 } 18 19 func buildId(meta api.ObjectMeta) string { 20 return meta.Namespace + "/" + meta.Name 21 } 22 23 func expandMetadata(in []interface{}) api.ObjectMeta { 24 meta := api.ObjectMeta{} 25 if len(in) < 1 { 26 return meta 27 } 28 m := in[0].(map[string]interface{}) 29 30 meta.Annotations = expandStringMap(m["annotations"].(map[string]interface{})) 31 meta.Labels = expandStringMap(m["labels"].(map[string]interface{})) 32 33 if v, ok := m["generate_name"]; ok { 34 meta.GenerateName = v.(string) 35 } 36 if v, ok := m["name"]; ok { 37 meta.Name = v.(string) 38 } 39 if v, ok := m["namespace"]; ok { 40 meta.Namespace = v.(string) 41 } 42 43 return meta 44 } 45 46 func patchMetadata(keyPrefix, pathPrefix string, d *schema.ResourceData) PatchOperations { 47 ops := make([]PatchOperation, 0, 0) 48 if d.HasChange(keyPrefix + "annotations") { 49 oldV, newV := d.GetChange(keyPrefix + "annotations") 50 diffOps := diffStringMap(pathPrefix+"annotations", oldV.(map[string]interface{}), newV.(map[string]interface{})) 51 ops = append(ops, diffOps...) 52 } 53 if d.HasChange(keyPrefix + "labels") { 54 oldV, newV := d.GetChange(keyPrefix + "labels") 55 diffOps := diffStringMap(pathPrefix+"labels", oldV.(map[string]interface{}), newV.(map[string]interface{})) 56 ops = append(ops, diffOps...) 57 } 58 return ops 59 } 60 61 func expandStringMap(m map[string]interface{}) map[string]string { 62 result := make(map[string]string) 63 for k, v := range m { 64 result[k] = v.(string) 65 } 66 return result 67 } 68 69 func expandStringSlice(s []interface{}) []string { 70 result := make([]string, len(s), len(s)) 71 for k, v := range s { 72 result[k] = v.(string) 73 } 74 return result 75 } 76 77 func flattenMetadata(meta api.ObjectMeta) []map[string]interface{} { 78 m := make(map[string]interface{}) 79 m["annotations"] = filterAnnotations(meta.Annotations) 80 if meta.GenerateName != "" { 81 m["generate_name"] = meta.GenerateName 82 } 83 m["labels"] = meta.Labels 84 m["name"] = meta.Name 85 m["resource_version"] = meta.ResourceVersion 86 m["self_link"] = meta.SelfLink 87 m["uid"] = fmt.Sprintf("%v", meta.UID) 88 m["generation"] = meta.Generation 89 90 if meta.Namespace != "" { 91 m["namespace"] = meta.Namespace 92 } 93 94 return []map[string]interface{}{m} 95 } 96 97 func filterAnnotations(m map[string]string) map[string]string { 98 for k, _ := range m { 99 if isInternalAnnotationKey(k) { 100 delete(m, k) 101 } 102 } 103 return m 104 } 105 106 func isInternalAnnotationKey(annotationKey string) bool { 107 u, err := url.Parse("//" + annotationKey) 108 if err == nil && strings.HasSuffix(u.Hostname(), "kubernetes.io") { 109 return true 110 } 111 112 return false 113 } 114 115 func byteMapToStringMap(m map[string][]byte) map[string]string { 116 result := make(map[string]string) 117 for k, v := range m { 118 result[k] = string(v) 119 } 120 return result 121 } 122 123 func ptrToString(s string) *string { 124 return &s 125 } 126 127 func ptrToInt(i int) *int { 128 return &i 129 } 130 131 func ptrToBool(b bool) *bool { 132 return &b 133 } 134 135 func ptrToInt32(i int32) *int32 { 136 return &i 137 } 138 139 func sliceOfString(slice []interface{}) []string { 140 result := make([]string, len(slice), len(slice)) 141 for i, s := range slice { 142 result[i] = s.(string) 143 } 144 return result 145 } 146 147 func base64EncodeStringMap(m map[string]interface{}) map[string]interface{} { 148 result := make(map[string]interface{}) 149 for k, v := range m { 150 value := v.(string) 151 result[k] = (base64.StdEncoding.EncodeToString([]byte(value))) 152 } 153 return result 154 } 155 156 func flattenResourceList(l api.ResourceList) map[string]string { 157 m := make(map[string]string) 158 for k, v := range l { 159 m[string(k)] = v.String() 160 } 161 return m 162 } 163 164 func expandMapToResourceList(m map[string]interface{}) (api.ResourceList, error) { 165 out := make(map[api.ResourceName]resource.Quantity) 166 for stringKey, v := range m { 167 key := api.ResourceName(stringKey) 168 value, err := resource.ParseQuantity(v.(string)) 169 if err != nil { 170 return out, err 171 } 172 173 out[key] = value 174 } 175 return out, nil 176 } 177 178 func flattenPersistentVolumeAccessModes(in []api.PersistentVolumeAccessMode) *schema.Set { 179 var out = make([]interface{}, len(in), len(in)) 180 for i, v := range in { 181 out[i] = string(v) 182 } 183 return schema.NewSet(schema.HashString, out) 184 } 185 186 func expandPersistentVolumeAccessModes(s []interface{}) []api.PersistentVolumeAccessMode { 187 out := make([]api.PersistentVolumeAccessMode, len(s), len(s)) 188 for i, v := range s { 189 out[i] = api.PersistentVolumeAccessMode(v.(string)) 190 } 191 return out 192 } 193 194 func newStringSet(f schema.SchemaSetFunc, in []string) *schema.Set { 195 var out = make([]interface{}, len(in), len(in)) 196 for i, v := range in { 197 out[i] = v 198 } 199 return schema.NewSet(f, out) 200 }