github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/kubernetes/resource_kubernetes_config_map.go (about) 1 package kubernetes 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/hashicorp/terraform/helper/schema" 8 pkgApi "k8s.io/kubernetes/pkg/api" 9 "k8s.io/kubernetes/pkg/api/errors" 10 api "k8s.io/kubernetes/pkg/api/v1" 11 kubernetes "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_5" 12 ) 13 14 func resourceKubernetesConfigMap() *schema.Resource { 15 return &schema.Resource{ 16 Create: resourceKubernetesConfigMapCreate, 17 Read: resourceKubernetesConfigMapRead, 18 Exists: resourceKubernetesConfigMapExists, 19 Update: resourceKubernetesConfigMapUpdate, 20 Delete: resourceKubernetesConfigMapDelete, 21 Importer: &schema.ResourceImporter{ 22 State: schema.ImportStatePassthrough, 23 }, 24 25 Schema: map[string]*schema.Schema{ 26 "metadata": namespacedMetadataSchema("config map", true), 27 "data": { 28 Type: schema.TypeMap, 29 Description: "A map of the configuration data.", 30 Optional: true, 31 }, 32 }, 33 } 34 } 35 36 func resourceKubernetesConfigMapCreate(d *schema.ResourceData, meta interface{}) error { 37 conn := meta.(*kubernetes.Clientset) 38 39 metadata := expandMetadata(d.Get("metadata").([]interface{})) 40 cfgMap := api.ConfigMap{ 41 ObjectMeta: metadata, 42 Data: expandStringMap(d.Get("data").(map[string]interface{})), 43 } 44 log.Printf("[INFO] Creating new config map: %#v", cfgMap) 45 out, err := conn.CoreV1().ConfigMaps(metadata.Namespace).Create(&cfgMap) 46 if err != nil { 47 return err 48 } 49 log.Printf("[INFO] Submitted new config map: %#v", out) 50 d.SetId(buildId(out.ObjectMeta)) 51 52 return resourceKubernetesConfigMapRead(d, meta) 53 } 54 55 func resourceKubernetesConfigMapRead(d *schema.ResourceData, meta interface{}) error { 56 conn := meta.(*kubernetes.Clientset) 57 58 namespace, name := idParts(d.Id()) 59 log.Printf("[INFO] Reading config map %s", name) 60 cfgMap, err := conn.CoreV1().ConfigMaps(namespace).Get(name) 61 if err != nil { 62 log.Printf("[DEBUG] Received error: %#v", err) 63 return err 64 } 65 log.Printf("[INFO] Received config map: %#v", cfgMap) 66 err = d.Set("metadata", flattenMetadata(cfgMap.ObjectMeta)) 67 if err != nil { 68 return err 69 } 70 d.Set("data", cfgMap.Data) 71 72 return nil 73 } 74 75 func resourceKubernetesConfigMapUpdate(d *schema.ResourceData, meta interface{}) error { 76 conn := meta.(*kubernetes.Clientset) 77 78 namespace, name := idParts(d.Id()) 79 80 ops := patchMetadata("metadata.0.", "/metadata/", d) 81 if d.HasChange("data") { 82 oldV, newV := d.GetChange("data") 83 diffOps := diffStringMap("/data/", oldV.(map[string]interface{}), newV.(map[string]interface{})) 84 ops = append(ops, diffOps...) 85 } 86 data, err := ops.MarshalJSON() 87 if err != nil { 88 return fmt.Errorf("Failed to marshal update operations: %s", err) 89 } 90 log.Printf("[INFO] Updating config map %q: %v", name, string(data)) 91 out, err := conn.CoreV1().ConfigMaps(namespace).Patch(name, pkgApi.JSONPatchType, data) 92 if err != nil { 93 return fmt.Errorf("Failed to update Config Map: %s", err) 94 } 95 log.Printf("[INFO] Submitted updated config map: %#v", out) 96 d.SetId(buildId(out.ObjectMeta)) 97 98 return resourceKubernetesConfigMapRead(d, meta) 99 } 100 101 func resourceKubernetesConfigMapDelete(d *schema.ResourceData, meta interface{}) error { 102 conn := meta.(*kubernetes.Clientset) 103 104 namespace, name := idParts(d.Id()) 105 log.Printf("[INFO] Deleting config map: %#v", name) 106 err := conn.CoreV1().ConfigMaps(namespace).Delete(name, &api.DeleteOptions{}) 107 if err != nil { 108 return err 109 } 110 111 log.Printf("[INFO] Config map %s deleted", name) 112 113 d.SetId("") 114 return nil 115 } 116 117 func resourceKubernetesConfigMapExists(d *schema.ResourceData, meta interface{}) (bool, error) { 118 conn := meta.(*kubernetes.Clientset) 119 120 namespace, name := idParts(d.Id()) 121 log.Printf("[INFO] Checking config map %s", name) 122 _, err := conn.CoreV1().ConfigMaps(namespace).Get(name) 123 if err != nil { 124 if statusErr, ok := err.(*errors.StatusError); ok && statusErr.ErrStatus.Code == 404 { 125 return false, nil 126 } 127 log.Printf("[DEBUG] Received error: %#v", err) 128 } 129 return true, err 130 }