github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/google/resource_compute_ssl_certificate.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  	"google.golang.org/api/compute/v1"
     9  	"google.golang.org/api/googleapi"
    10  )
    11  
    12  func resourceComputeSslCertificate() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceComputeSslCertificateCreate,
    15  		Read:   resourceComputeSslCertificateRead,
    16  		Delete: resourceComputeSslCertificateDelete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"name": &schema.Schema{
    20  				Type:     schema.TypeString,
    21  				Required: true,
    22  				ForceNew: true,
    23  			},
    24  
    25  			"description": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Optional: true,
    28  				ForceNew: true,
    29  			},
    30  
    31  			"certificate": &schema.Schema{
    32  				Type:     schema.TypeString,
    33  				Required: true,
    34  				ForceNew: true,
    35  			},
    36  
    37  			"private_key": &schema.Schema{
    38  				Type:     schema.TypeString,
    39  				Required: 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  	}
    54  }
    55  
    56  func resourceComputeSslCertificateCreate(d *schema.ResourceData, meta interface{}) error {
    57  	config := meta.(*Config)
    58  
    59  	// Build the certificate parameter
    60  	cert := &compute.SslCertificate{
    61  		Name:        d.Get("name").(string),
    62  		Certificate: d.Get("certificate").(string),
    63  		PrivateKey:  d.Get("private_key").(string),
    64  	}
    65  
    66  	if v, ok := d.GetOk("description"); ok {
    67  		cert.Description = v.(string)
    68  	}
    69  
    70  	op, err := config.clientCompute.SslCertificates.Insert(
    71  		config.Project, cert).Do()
    72  
    73  	if err != nil {
    74  		return fmt.Errorf("Error creating ssl certificate: %s", err)
    75  	}
    76  
    77  	err = computeOperationWaitGlobal(config, op, "Creating SslCertificate")
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	d.SetId(cert.Name)
    83  
    84  	return resourceComputeSslCertificateRead(d, meta)
    85  }
    86  
    87  func resourceComputeSslCertificateRead(d *schema.ResourceData, meta interface{}) error {
    88  	config := meta.(*Config)
    89  
    90  	cert, err := config.clientCompute.SslCertificates.Get(
    91  		config.Project, d.Id()).Do()
    92  	if err != nil {
    93  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
    94  			// The resource doesn't exist anymore
    95  			d.SetId("")
    96  
    97  			return nil
    98  		}
    99  
   100  		return fmt.Errorf("Error reading ssl certificate: %s", err)
   101  	}
   102  
   103  	d.Set("self_link", cert.SelfLink)
   104  	d.Set("id", strconv.FormatUint(cert.Id, 10))
   105  
   106  	return nil
   107  }
   108  
   109  func resourceComputeSslCertificateDelete(d *schema.ResourceData, meta interface{}) error {
   110  	config := meta.(*Config)
   111  
   112  	op, err := config.clientCompute.SslCertificates.Delete(
   113  		config.Project, d.Id()).Do()
   114  	if err != nil {
   115  		return fmt.Errorf("Error deleting ssl certificate: %s", err)
   116  	}
   117  
   118  	err = computeOperationWaitGlobal(config, op, "Deleting SslCertificate")
   119  	if err != nil {
   120  		return err
   121  	}
   122  
   123  	d.SetId("")
   124  	return nil
   125  }