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