github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/google/resource_compute_ssl_certificate.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strconv"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  	"google.golang.org/api/compute/v1"
    11  	"google.golang.org/api/googleapi"
    12  )
    13  
    14  func resourceComputeSslCertificate() *schema.Resource {
    15  	return &schema.Resource{
    16  		Create: resourceComputeSslCertificateCreate,
    17  		Read:   resourceComputeSslCertificateRead,
    18  		Delete: resourceComputeSslCertificateDelete,
    19  
    20  		Schema: map[string]*schema.Schema{
    21  			"certificate": &schema.Schema{
    22  				Type:     schema.TypeString,
    23  				Required: true,
    24  				ForceNew: true,
    25  			},
    26  
    27  			"name": &schema.Schema{
    28  				Type:          schema.TypeString,
    29  				Optional:      true,
    30  				Computed:      true,
    31  				ForceNew:      true,
    32  				ConflictsWith: []string{"name_prefix"},
    33  				ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
    34  					// https://cloud.google.com/compute/docs/reference/latest/sslCertificates#resource
    35  					value := v.(string)
    36  					if len(value) > 63 {
    37  						errors = append(errors, fmt.Errorf(
    38  							"%q cannot be longer than 63 characters", k))
    39  					}
    40  					return
    41  				},
    42  			},
    43  
    44  			"name_prefix": &schema.Schema{
    45  				Type:     schema.TypeString,
    46  				Optional: true,
    47  				ForceNew: true,
    48  				ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
    49  					// https://cloud.google.com/compute/docs/reference/latest/sslCertificates#resource
    50  					// uuid is 26 characters, limit the prefix to 37.
    51  					value := v.(string)
    52  					if len(value) > 37 {
    53  						errors = append(errors, fmt.Errorf(
    54  							"%q cannot be longer than 37 characters, name is limited to 63", k))
    55  					}
    56  					return
    57  				},
    58  			},
    59  
    60  			"private_key": &schema.Schema{
    61  				Type:     schema.TypeString,
    62  				Required: true,
    63  				ForceNew: true,
    64  			},
    65  
    66  			"description": &schema.Schema{
    67  				Type:     schema.TypeString,
    68  				Optional: true,
    69  				ForceNew: true,
    70  			},
    71  
    72  			"id": &schema.Schema{
    73  				Type:     schema.TypeString,
    74  				Computed: true,
    75  			},
    76  
    77  			"project": &schema.Schema{
    78  				Type:     schema.TypeString,
    79  				Optional: true,
    80  				ForceNew: true,
    81  			},
    82  
    83  			"self_link": &schema.Schema{
    84  				Type:     schema.TypeString,
    85  				Computed: true,
    86  			},
    87  		},
    88  	}
    89  }
    90  
    91  func resourceComputeSslCertificateCreate(d *schema.ResourceData, meta interface{}) error {
    92  	config := meta.(*Config)
    93  
    94  	project, err := getProject(d, config)
    95  	if err != nil {
    96  		return err
    97  	}
    98  
    99  	var certName string
   100  	if v, ok := d.GetOk("name"); ok {
   101  		certName = v.(string)
   102  	} else if v, ok := d.GetOk("name_prefix"); ok {
   103  		certName = resource.PrefixedUniqueId(v.(string))
   104  	} else {
   105  		certName = resource.UniqueId()
   106  	}
   107  
   108  	// Build the certificate parameter
   109  	cert := &compute.SslCertificate{
   110  		Name:        certName,
   111  		Certificate: d.Get("certificate").(string),
   112  		PrivateKey:  d.Get("private_key").(string),
   113  	}
   114  
   115  	if v, ok := d.GetOk("description"); ok {
   116  		cert.Description = v.(string)
   117  	}
   118  
   119  	op, err := config.clientCompute.SslCertificates.Insert(
   120  		project, cert).Do()
   121  
   122  	if err != nil {
   123  		return fmt.Errorf("Error creating ssl certificate: %s", err)
   124  	}
   125  
   126  	err = computeOperationWaitGlobal(config, op, project, "Creating SslCertificate")
   127  	if err != nil {
   128  		return err
   129  	}
   130  
   131  	d.SetId(cert.Name)
   132  
   133  	return resourceComputeSslCertificateRead(d, meta)
   134  }
   135  
   136  func resourceComputeSslCertificateRead(d *schema.ResourceData, meta interface{}) error {
   137  	config := meta.(*Config)
   138  
   139  	project, err := getProject(d, config)
   140  	if err != nil {
   141  		return err
   142  	}
   143  
   144  	cert, err := config.clientCompute.SslCertificates.Get(
   145  		project, d.Id()).Do()
   146  	if err != nil {
   147  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
   148  			log.Printf("[WARN] Removing SSL Certificate %q because it's gone", d.Get("name").(string))
   149  			// The resource doesn't exist anymore
   150  			d.SetId("")
   151  
   152  			return nil
   153  		}
   154  
   155  		return fmt.Errorf("Error reading ssl certificate: %s", err)
   156  	}
   157  
   158  	d.Set("self_link", cert.SelfLink)
   159  	d.Set("id", strconv.FormatUint(cert.Id, 10))
   160  
   161  	return nil
   162  }
   163  
   164  func resourceComputeSslCertificateDelete(d *schema.ResourceData, meta interface{}) error {
   165  	config := meta.(*Config)
   166  
   167  	project, err := getProject(d, config)
   168  	if err != nil {
   169  		return err
   170  	}
   171  
   172  	op, err := config.clientCompute.SslCertificates.Delete(
   173  		project, d.Id()).Do()
   174  	if err != nil {
   175  		return fmt.Errorf("Error deleting ssl certificate: %s", err)
   176  	}
   177  
   178  	err = computeOperationWaitGlobal(config, op, project, "Deleting SslCertificate")
   179  	if err != nil {
   180  		return err
   181  	}
   182  
   183  	d.SetId("")
   184  	return nil
   185  }