github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/google/resource_compute_target_http_proxy.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strconv"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  	"google.golang.org/api/compute/v1"
    10  	"google.golang.org/api/googleapi"
    11  )
    12  
    13  func resourceComputeTargetHttpProxy() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceComputeTargetHttpProxyCreate,
    16  		Read:   resourceComputeTargetHttpProxyRead,
    17  		Delete: resourceComputeTargetHttpProxyDelete,
    18  		Update: resourceComputeTargetHttpProxyUpdate,
    19  
    20  		Schema: map[string]*schema.Schema{
    21  			"name": &schema.Schema{
    22  				Type:     schema.TypeString,
    23  				Required: true,
    24  				ForceNew: true,
    25  			},
    26  
    27  			"description": &schema.Schema{
    28  				Type:     schema.TypeString,
    29  				Optional: true,
    30  				ForceNew: true,
    31  			},
    32  
    33  			"self_link": &schema.Schema{
    34  				Type:     schema.TypeString,
    35  				Computed: true,
    36  			},
    37  
    38  			"id": &schema.Schema{
    39  				Type:     schema.TypeString,
    40  				Computed: true,
    41  			},
    42  
    43  			"url_map": &schema.Schema{
    44  				Type:     schema.TypeString,
    45  				Required: true,
    46  			},
    47  		},
    48  	}
    49  }
    50  
    51  func resourceComputeTargetHttpProxyCreate(d *schema.ResourceData, meta interface{}) error {
    52  	config := meta.(*Config)
    53  
    54  	proxy := &compute.TargetHttpProxy{
    55  		Name:   d.Get("name").(string),
    56  		UrlMap: d.Get("url_map").(string),
    57  	}
    58  
    59  	if v, ok := d.GetOk("description"); ok {
    60  		proxy.Description = v.(string)
    61  	}
    62  
    63  	log.Printf("[DEBUG] TargetHttpProxy insert request: %#v", proxy)
    64  	op, err := config.clientCompute.TargetHttpProxies.Insert(
    65  		config.Project, proxy).Do()
    66  	if err != nil {
    67  		return fmt.Errorf("Error creating TargetHttpProxy: %s", err)
    68  	}
    69  
    70  	err = computeOperationWaitGlobal(config, op, "Creating Target Http Proxy")
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	d.SetId(proxy.Name)
    76  
    77  	return resourceComputeTargetHttpProxyRead(d, meta)
    78  }
    79  
    80  func resourceComputeTargetHttpProxyUpdate(d *schema.ResourceData, meta interface{}) error {
    81  	config := meta.(*Config)
    82  
    83  	d.Partial(true)
    84  
    85  	if d.HasChange("url_map") {
    86  		url_map := d.Get("url_map").(string)
    87  		url_map_ref := &compute.UrlMapReference{UrlMap: url_map}
    88  		op, err := config.clientCompute.TargetHttpProxies.SetUrlMap(
    89  			config.Project, d.Id(), url_map_ref).Do()
    90  		if err != nil {
    91  			return fmt.Errorf("Error updating target: %s", err)
    92  		}
    93  
    94  		err = computeOperationWaitGlobal(config, op, "Updating Target Http Proxy")
    95  		if err != nil {
    96  			return err
    97  		}
    98  
    99  		d.SetPartial("url_map")
   100  	}
   101  
   102  	d.Partial(false)
   103  
   104  	return resourceComputeTargetHttpProxyRead(d, meta)
   105  }
   106  
   107  func resourceComputeTargetHttpProxyRead(d *schema.ResourceData, meta interface{}) error {
   108  	config := meta.(*Config)
   109  
   110  	proxy, err := config.clientCompute.TargetHttpProxies.Get(
   111  		config.Project, d.Id()).Do()
   112  	if err != nil {
   113  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
   114  			// The resource doesn't exist anymore
   115  			d.SetId("")
   116  
   117  			return nil
   118  		}
   119  
   120  		return fmt.Errorf("Error reading TargetHttpProxy: %s", err)
   121  	}
   122  
   123  	d.Set("self_link", proxy.SelfLink)
   124  	d.Set("id", strconv.FormatUint(proxy.Id, 10))
   125  
   126  	return nil
   127  }
   128  
   129  func resourceComputeTargetHttpProxyDelete(d *schema.ResourceData, meta interface{}) error {
   130  	config := meta.(*Config)
   131  
   132  	// Delete the TargetHttpProxy
   133  	log.Printf("[DEBUG] TargetHttpProxy delete request")
   134  	op, err := config.clientCompute.TargetHttpProxies.Delete(
   135  		config.Project, d.Id()).Do()
   136  	if err != nil {
   137  		return fmt.Errorf("Error deleting TargetHttpProxy: %s", err)
   138  	}
   139  
   140  	err = computeOperationWaitGlobal(config, op, "Deleting Target Http Proxy")
   141  	if err != nil {
   142  		return err
   143  	}
   144  
   145  	d.SetId("")
   146  	return nil
   147  }