github.com/leeprovoost/terraform@v0.6.10-0.20160119085442-96f3f76118e7/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 log.Printf("[WARN] Removing Target HTTP Proxy %q because it's gone", d.Get("name").(string)) 115 // The resource doesn't exist anymore 116 d.SetId("") 117 118 return nil 119 } 120 121 return fmt.Errorf("Error reading TargetHttpProxy: %s", err) 122 } 123 124 d.Set("self_link", proxy.SelfLink) 125 d.Set("id", strconv.FormatUint(proxy.Id, 10)) 126 127 return nil 128 } 129 130 func resourceComputeTargetHttpProxyDelete(d *schema.ResourceData, meta interface{}) error { 131 config := meta.(*Config) 132 133 // Delete the TargetHttpProxy 134 log.Printf("[DEBUG] TargetHttpProxy delete request") 135 op, err := config.clientCompute.TargetHttpProxies.Delete( 136 config.Project, d.Id()).Do() 137 if err != nil { 138 return fmt.Errorf("Error deleting TargetHttpProxy: %s", err) 139 } 140 141 err = computeOperationWaitGlobal(config, op, "Deleting Target Http Proxy") 142 if err != nil { 143 return err 144 } 145 146 d.SetId("") 147 return nil 148 }