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