github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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  			"url_map": &schema.Schema{
    28  				Type:     schema.TypeString,
    29  				Required: true,
    30  			},
    31  
    32  			"description": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Optional: true,
    35  				ForceNew: true,
    36  			},
    37  
    38  			"id": &schema.Schema{
    39  				Type:     schema.TypeString,
    40  				Computed: true,
    41  			},
    42  
    43  			"project": &schema.Schema{
    44  				Type:     schema.TypeString,
    45  				Optional: true,
    46  				ForceNew: true,
    47  			},
    48  
    49  			"self_link": &schema.Schema{
    50  				Type:     schema.TypeString,
    51  				Computed: true,
    52  			},
    53  		},
    54  	}
    55  }
    56  
    57  func resourceComputeTargetHttpProxyCreate(d *schema.ResourceData, meta interface{}) error {
    58  	config := meta.(*Config)
    59  
    60  	project, err := getProject(d, config)
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	proxy := &compute.TargetHttpProxy{
    66  		Name:   d.Get("name").(string),
    67  		UrlMap: d.Get("url_map").(string),
    68  	}
    69  
    70  	if v, ok := d.GetOk("description"); ok {
    71  		proxy.Description = v.(string)
    72  	}
    73  
    74  	log.Printf("[DEBUG] TargetHttpProxy insert request: %#v", proxy)
    75  	op, err := config.clientCompute.TargetHttpProxies.Insert(
    76  		project, proxy).Do()
    77  	if err != nil {
    78  		return fmt.Errorf("Error creating TargetHttpProxy: %s", err)
    79  	}
    80  
    81  	err = computeOperationWaitGlobal(config, op, project, "Creating Target Http Proxy")
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	d.SetId(proxy.Name)
    87  
    88  	return resourceComputeTargetHttpProxyRead(d, meta)
    89  }
    90  
    91  func resourceComputeTargetHttpProxyUpdate(d *schema.ResourceData, meta interface{}) error {
    92  	config := meta.(*Config)
    93  
    94  	project, err := getProject(d, config)
    95  	if err != nil {
    96  		return err
    97  	}
    98  
    99  	d.Partial(true)
   100  
   101  	if d.HasChange("url_map") {
   102  		url_map := d.Get("url_map").(string)
   103  		url_map_ref := &compute.UrlMapReference{UrlMap: url_map}
   104  		op, err := config.clientCompute.TargetHttpProxies.SetUrlMap(
   105  			project, d.Id(), url_map_ref).Do()
   106  		if err != nil {
   107  			return fmt.Errorf("Error updating target: %s", err)
   108  		}
   109  
   110  		err = computeOperationWaitGlobal(config, op, project, "Updating Target Http Proxy")
   111  		if err != nil {
   112  			return err
   113  		}
   114  
   115  		d.SetPartial("url_map")
   116  	}
   117  
   118  	d.Partial(false)
   119  
   120  	return resourceComputeTargetHttpProxyRead(d, meta)
   121  }
   122  
   123  func resourceComputeTargetHttpProxyRead(d *schema.ResourceData, meta interface{}) error {
   124  	config := meta.(*Config)
   125  
   126  	project, err := getProject(d, config)
   127  	if err != nil {
   128  		return err
   129  	}
   130  
   131  	proxy, err := config.clientCompute.TargetHttpProxies.Get(
   132  		project, d.Id()).Do()
   133  	if err != nil {
   134  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
   135  			log.Printf("[WARN] Removing Target HTTP Proxy %q because it's gone", d.Get("name").(string))
   136  			// The resource doesn't exist anymore
   137  			d.SetId("")
   138  
   139  			return nil
   140  		}
   141  
   142  		return fmt.Errorf("Error reading TargetHttpProxy: %s", err)
   143  	}
   144  
   145  	d.Set("self_link", proxy.SelfLink)
   146  	d.Set("id", strconv.FormatUint(proxy.Id, 10))
   147  
   148  	return nil
   149  }
   150  
   151  func resourceComputeTargetHttpProxyDelete(d *schema.ResourceData, meta interface{}) error {
   152  	config := meta.(*Config)
   153  
   154  	project, err := getProject(d, config)
   155  	if err != nil {
   156  		return err
   157  	}
   158  
   159  	// Delete the TargetHttpProxy
   160  	log.Printf("[DEBUG] TargetHttpProxy delete request")
   161  	op, err := config.clientCompute.TargetHttpProxies.Delete(
   162  		project, d.Id()).Do()
   163  	if err != nil {
   164  		return fmt.Errorf("Error deleting TargetHttpProxy: %s", err)
   165  	}
   166  
   167  	err = computeOperationWaitGlobal(config, op, project, "Deleting Target Http Proxy")
   168  	if err != nil {
   169  		return err
   170  	}
   171  
   172  	d.SetId("")
   173  	return nil
   174  }