github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/google/resource_compute_target_https_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 resourceComputeTargetHttpsProxy() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceComputeTargetHttpsProxyCreate, 16 Read: resourceComputeTargetHttpsProxyRead, 17 Delete: resourceComputeTargetHttpsProxyDelete, 18 Update: resourceComputeTargetHttpsProxyUpdate, 19 20 Schema: map[string]*schema.Schema{ 21 "name": &schema.Schema{ 22 Type: schema.TypeString, 23 Required: true, 24 ForceNew: true, 25 }, 26 27 "ssl_certificates": &schema.Schema{ 28 Type: schema.TypeList, 29 Required: true, 30 Elem: &schema.Schema{Type: schema.TypeString}, 31 }, 32 33 "url_map": &schema.Schema{ 34 Type: schema.TypeString, 35 Required: true, 36 }, 37 38 "description": &schema.Schema{ 39 Type: schema.TypeString, 40 Optional: true, 41 ForceNew: true, 42 }, 43 44 "self_link": &schema.Schema{ 45 Type: schema.TypeString, 46 Computed: true, 47 }, 48 49 "id": &schema.Schema{ 50 Type: schema.TypeString, 51 Computed: true, 52 }, 53 54 "project": &schema.Schema{ 55 Type: schema.TypeString, 56 Optional: true, 57 ForceNew: true, 58 }, 59 }, 60 } 61 } 62 63 func resourceComputeTargetHttpsProxyCreate(d *schema.ResourceData, meta interface{}) error { 64 config := meta.(*Config) 65 66 project, err := getProject(d, config) 67 if err != nil { 68 return err 69 } 70 71 _sslCertificates := d.Get("ssl_certificates").([]interface{}) 72 sslCertificates := make([]string, len(_sslCertificates)) 73 74 for i, v := range _sslCertificates { 75 sslCertificates[i] = v.(string) 76 } 77 78 proxy := &compute.TargetHttpsProxy{ 79 Name: d.Get("name").(string), 80 UrlMap: d.Get("url_map").(string), 81 SslCertificates: sslCertificates, 82 } 83 84 if v, ok := d.GetOk("description"); ok { 85 proxy.Description = v.(string) 86 } 87 88 log.Printf("[DEBUG] TargetHttpsProxy insert request: %#v", proxy) 89 op, err := config.clientCompute.TargetHttpsProxies.Insert( 90 project, proxy).Do() 91 if err != nil { 92 return fmt.Errorf("Error creating TargetHttpsProxy: %s", err) 93 } 94 95 err = computeOperationWaitGlobal(config, op, project, "Creating Target Https Proxy") 96 if err != nil { 97 return err 98 } 99 100 d.SetId(proxy.Name) 101 102 return resourceComputeTargetHttpsProxyRead(d, meta) 103 } 104 105 func resourceComputeTargetHttpsProxyUpdate(d *schema.ResourceData, meta interface{}) error { 106 config := meta.(*Config) 107 108 project, err := getProject(d, config) 109 if err != nil { 110 return err 111 } 112 113 d.Partial(true) 114 115 if d.HasChange("url_map") { 116 url_map := d.Get("url_map").(string) 117 url_map_ref := &compute.UrlMapReference{UrlMap: url_map} 118 op, err := config.clientCompute.TargetHttpsProxies.SetUrlMap( 119 project, d.Id(), url_map_ref).Do() 120 if err != nil { 121 return fmt.Errorf("Error updating Target HTTPS proxy URL map: %s", err) 122 } 123 124 err = computeOperationWaitGlobal(config, op, project, "Updating Target Https Proxy URL Map") 125 if err != nil { 126 return err 127 } 128 129 d.SetPartial("url_map") 130 } 131 132 if d.HasChange("ssl_certificates") { 133 proxy, err := config.clientCompute.TargetHttpsProxies.Get( 134 project, d.Id()).Do() 135 136 _old, _new := d.GetChange("ssl_certificates") 137 _oldCerts := _old.([]interface{}) 138 _newCerts := _new.([]interface{}) 139 current := proxy.SslCertificates 140 141 _oldMap := make(map[string]bool) 142 _newMap := make(map[string]bool) 143 144 for _, v := range _oldCerts { 145 _oldMap[v.(string)] = true 146 } 147 148 for _, v := range _newCerts { 149 _newMap[v.(string)] = true 150 } 151 152 sslCertificates := make([]string, 0) 153 // Only modify certificates in one of our old or new states 154 for _, v := range current { 155 _, okOld := _oldMap[v] 156 _, okNew := _newMap[v] 157 158 // we deleted the certificate 159 if okOld && !okNew { 160 continue 161 } 162 163 sslCertificates = append(sslCertificates, v) 164 165 // Keep track of the fact that we have added this certificate 166 if okNew { 167 delete(_newMap, v) 168 } 169 } 170 171 // Add fresh certificates 172 for k, _ := range _newMap { 173 sslCertificates = append(sslCertificates, k) 174 } 175 176 cert_ref := &compute.TargetHttpsProxiesSetSslCertificatesRequest{ 177 SslCertificates: sslCertificates, 178 } 179 op, err := config.clientCompute.TargetHttpsProxies.SetSslCertificates( 180 project, d.Id(), cert_ref).Do() 181 if err != nil { 182 return fmt.Errorf("Error updating Target Https Proxy SSL Certificates: %s", err) 183 } 184 185 err = computeOperationWaitGlobal(config, op, project, "Updating Target Https Proxy SSL certificates") 186 if err != nil { 187 return err 188 } 189 190 d.SetPartial("ssl_certificate") 191 } 192 193 d.Partial(false) 194 195 return resourceComputeTargetHttpsProxyRead(d, meta) 196 } 197 198 func resourceComputeTargetHttpsProxyRead(d *schema.ResourceData, meta interface{}) error { 199 config := meta.(*Config) 200 201 project, err := getProject(d, config) 202 if err != nil { 203 return err 204 } 205 206 proxy, err := config.clientCompute.TargetHttpsProxies.Get( 207 project, d.Id()).Do() 208 if err != nil { 209 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { 210 log.Printf("[WARN] Removing Target HTTPS Proxy %q because it's gone", d.Get("name").(string)) 211 // The resource doesn't exist anymore 212 d.SetId("") 213 214 return nil 215 } 216 217 return fmt.Errorf("Error reading TargetHttpsProxy: %s", err) 218 } 219 220 _certs := d.Get("ssl_certificates").([]interface{}) 221 current := proxy.SslCertificates 222 223 _certMap := make(map[string]bool) 224 _newCerts := make([]interface{}, 0) 225 226 for _, v := range _certs { 227 _certMap[v.(string)] = true 228 } 229 230 // Store intersection of server certificates and user defined certificates 231 for _, v := range current { 232 if _, ok := _certMap[v]; ok { 233 _newCerts = append(_newCerts, v) 234 } 235 } 236 237 d.Set("ssl_certificates", _newCerts) 238 d.Set("self_link", proxy.SelfLink) 239 d.Set("id", strconv.FormatUint(proxy.Id, 10)) 240 241 return nil 242 } 243 244 func resourceComputeTargetHttpsProxyDelete(d *schema.ResourceData, meta interface{}) error { 245 config := meta.(*Config) 246 247 project, err := getProject(d, config) 248 if err != nil { 249 return err 250 } 251 252 // Delete the TargetHttpsProxy 253 log.Printf("[DEBUG] TargetHttpsProxy delete request") 254 op, err := config.clientCompute.TargetHttpsProxies.Delete( 255 project, d.Id()).Do() 256 if err != nil { 257 return fmt.Errorf("Error deleting TargetHttpsProxy: %s", err) 258 } 259 260 err = computeOperationWaitGlobal(config, op, project, "Deleting Target Https Proxy") 261 if err != nil { 262 return err 263 } 264 265 d.SetId("") 266 return nil 267 }