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

     1  package kubernetes
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  	pkgApi "k8s.io/kubernetes/pkg/api"
    11  	"k8s.io/kubernetes/pkg/api/errors"
    12  	api "k8s.io/kubernetes/pkg/api/v1"
    13  	kubernetes "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_5"
    14  )
    15  
    16  func resourceKubernetesNamespace() *schema.Resource {
    17  	return &schema.Resource{
    18  		Create: resourceKubernetesNamespaceCreate,
    19  		Read:   resourceKubernetesNamespaceRead,
    20  		Exists: resourceKubernetesNamespaceExists,
    21  		Update: resourceKubernetesNamespaceUpdate,
    22  		Delete: resourceKubernetesNamespaceDelete,
    23  		Importer: &schema.ResourceImporter{
    24  			State: schema.ImportStatePassthrough,
    25  		},
    26  
    27  		Schema: map[string]*schema.Schema{
    28  			"metadata": metadataSchema("namespace", true),
    29  		},
    30  	}
    31  }
    32  
    33  func resourceKubernetesNamespaceCreate(d *schema.ResourceData, meta interface{}) error {
    34  	conn := meta.(*kubernetes.Clientset)
    35  
    36  	metadata := expandMetadata(d.Get("metadata").([]interface{}))
    37  	namespace := api.Namespace{
    38  		ObjectMeta: metadata,
    39  	}
    40  	log.Printf("[INFO] Creating new namespace: %#v", namespace)
    41  	out, err := conn.CoreV1().Namespaces().Create(&namespace)
    42  	if err != nil {
    43  		return err
    44  	}
    45  	log.Printf("[INFO] Submitted new namespace: %#v", out)
    46  	d.SetId(out.Name)
    47  
    48  	return resourceKubernetesNamespaceRead(d, meta)
    49  }
    50  
    51  func resourceKubernetesNamespaceRead(d *schema.ResourceData, meta interface{}) error {
    52  	conn := meta.(*kubernetes.Clientset)
    53  
    54  	name := d.Id()
    55  	log.Printf("[INFO] Reading namespace %s", name)
    56  	namespace, err := conn.CoreV1().Namespaces().Get(name)
    57  	if err != nil {
    58  		log.Printf("[DEBUG] Received error: %#v", err)
    59  		return err
    60  	}
    61  	log.Printf("[INFO] Received namespace: %#v", namespace)
    62  	err = d.Set("metadata", flattenMetadata(namespace.ObjectMeta))
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	return nil
    68  }
    69  
    70  func resourceKubernetesNamespaceUpdate(d *schema.ResourceData, meta interface{}) error {
    71  	conn := meta.(*kubernetes.Clientset)
    72  
    73  	ops := patchMetadata("metadata.0.", "/metadata/", d)
    74  	data, err := ops.MarshalJSON()
    75  	if err != nil {
    76  		return fmt.Errorf("Failed to marshal update operations: %s", err)
    77  	}
    78  
    79  	log.Printf("[INFO] Updating namespace: %s", ops)
    80  	out, err := conn.CoreV1().Namespaces().Patch(d.Id(), pkgApi.JSONPatchType, data)
    81  	if err != nil {
    82  		return err
    83  	}
    84  	log.Printf("[INFO] Submitted updated namespace: %#v", out)
    85  	d.SetId(out.Name)
    86  
    87  	return resourceKubernetesNamespaceRead(d, meta)
    88  }
    89  
    90  func resourceKubernetesNamespaceDelete(d *schema.ResourceData, meta interface{}) error {
    91  	conn := meta.(*kubernetes.Clientset)
    92  
    93  	name := d.Id()
    94  	log.Printf("[INFO] Deleting namespace: %#v", name)
    95  	err := conn.CoreV1().Namespaces().Delete(name, &api.DeleteOptions{})
    96  	if err != nil {
    97  		return err
    98  	}
    99  
   100  	stateConf := &resource.StateChangeConf{
   101  		Target:  []string{},
   102  		Pending: []string{"Terminating"},
   103  		Timeout: 5 * time.Minute,
   104  		Refresh: func() (interface{}, string, error) {
   105  			out, err := conn.CoreV1().Namespaces().Get(name)
   106  			if err != nil {
   107  				if statusErr, ok := err.(*errors.StatusError); ok && statusErr.ErrStatus.Code == 404 {
   108  					return nil, "", nil
   109  				}
   110  				log.Printf("[ERROR] Received error: %#v", err)
   111  				return out, "Error", err
   112  			}
   113  
   114  			statusPhase := fmt.Sprintf("%v", out.Status.Phase)
   115  			log.Printf("[DEBUG] Namespace %s status received: %#v", out.Name, statusPhase)
   116  			return out, statusPhase, nil
   117  		},
   118  	}
   119  	_, err = stateConf.WaitForState()
   120  	if err != nil {
   121  		return err
   122  	}
   123  	log.Printf("[INFO] Namespace %s deleted", name)
   124  
   125  	d.SetId("")
   126  	return nil
   127  }
   128  
   129  func resourceKubernetesNamespaceExists(d *schema.ResourceData, meta interface{}) (bool, error) {
   130  	conn := meta.(*kubernetes.Clientset)
   131  
   132  	name := d.Id()
   133  	log.Printf("[INFO] Checking namespace %s", name)
   134  	_, err := conn.CoreV1().Namespaces().Get(name)
   135  	if err != nil {
   136  		if statusErr, ok := err.(*errors.StatusError); ok && statusErr.ErrStatus.Code == 404 {
   137  			return false, nil
   138  		}
   139  		log.Printf("[DEBUG] Received error: %#v", err)
   140  	}
   141  	log.Printf("[INFO] Namespace %s exists", name)
   142  	return true, err
   143  }