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