2015-11-02 16:15:19 +01:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-11-13 21:36:03 +01:00
|
|
|
"log"
|
2015-11-02 16:15:19 +01:00
|
|
|
"strconv"
|
|
|
|
|
2016-12-13 00:57:58 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2015-11-02 16:15:19 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"google.golang.org/api/compute/v1"
|
|
|
|
"google.golang.org/api/googleapi"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceComputeSslCertificate() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceComputeSslCertificateCreate,
|
|
|
|
Read: resourceComputeSslCertificateRead,
|
|
|
|
Delete: resourceComputeSslCertificateDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-04-10 23:34:15 +02:00
|
|
|
"certificate": &schema.Schema{
|
2015-11-02 16:15:19 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"name": &schema.Schema{
|
2016-12-13 00:57:58 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
ConflictsWith: []string{"name_prefix"},
|
|
|
|
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
// https://cloud.google.com/compute/docs/reference/latest/sslCertificates#resource
|
|
|
|
value := v.(string)
|
|
|
|
if len(value) > 63 {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
|
|
|
"%q cannot be longer than 63 characters", k))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"name_prefix": &schema.Schema{
|
2015-11-02 16:15:19 +01:00
|
|
|
Type: schema.TypeString,
|
2016-12-13 00:57:58 +01:00
|
|
|
Optional: true,
|
2015-11-02 16:15:19 +01:00
|
|
|
ForceNew: true,
|
2016-12-13 00:57:58 +01:00
|
|
|
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
// https://cloud.google.com/compute/docs/reference/latest/sslCertificates#resource
|
|
|
|
// uuid is 26 characters, limit the prefix to 37.
|
|
|
|
value := v.(string)
|
|
|
|
if len(value) > 37 {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
|
|
|
"%q cannot be longer than 37 characters, name is limited to 63", k))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
},
|
2015-11-02 16:15:19 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"private_key": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"description": &schema.Schema{
|
2015-11-02 16:15:19 +01:00
|
|
|
Type: schema.TypeString,
|
2016-04-10 23:34:15 +02:00
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2015-11-02 16:15:19 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2016-04-10 18:59:57 +02:00
|
|
|
|
|
|
|
"project": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2016-04-10 23:34:15 +02:00
|
|
|
|
|
|
|
"self_link": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2015-11-02 16:15:19 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeSslCertificateCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-13 00:57:58 +01:00
|
|
|
var certName string
|
|
|
|
if v, ok := d.GetOk("name"); ok {
|
|
|
|
certName = v.(string)
|
|
|
|
} else if v, ok := d.GetOk("name_prefix"); ok {
|
|
|
|
certName = resource.PrefixedUniqueId(v.(string))
|
|
|
|
} else {
|
|
|
|
certName = resource.UniqueId()
|
|
|
|
}
|
|
|
|
|
2015-11-02 16:15:19 +01:00
|
|
|
// Build the certificate parameter
|
|
|
|
cert := &compute.SslCertificate{
|
2016-12-13 00:57:58 +01:00
|
|
|
Name: certName,
|
2015-11-02 16:15:19 +01:00
|
|
|
Certificate: d.Get("certificate").(string),
|
2015-11-02 20:32:07 +01:00
|
|
|
PrivateKey: d.Get("private_key").(string),
|
2015-11-02 16:15:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("description"); ok {
|
|
|
|
cert.Description = v.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
op, err := config.clientCompute.SslCertificates.Insert(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, cert).Do()
|
2015-11-02 16:15:19 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating ssl certificate: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitGlobal(config, op, project, "Creating SslCertificate")
|
2015-11-02 16:15:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(cert.Name)
|
|
|
|
|
|
|
|
return resourceComputeSslCertificateRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeSslCertificateRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-02 16:15:19 +01:00
|
|
|
cert, err := config.clientCompute.SslCertificates.Get(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Id()).Do()
|
2015-11-02 16:15:19 +01:00
|
|
|
if err != nil {
|
|
|
|
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
|
2015-11-13 21:36:03 +01:00
|
|
|
log.Printf("[WARN] Removing SSL Certificate %q because it's gone", d.Get("name").(string))
|
2015-11-02 16:15:19 +01:00
|
|
|
// The resource doesn't exist anymore
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Error reading ssl certificate: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("self_link", cert.SelfLink)
|
|
|
|
d.Set("id", strconv.FormatUint(cert.Id, 10))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeSslCertificateDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-02 16:15:19 +01:00
|
|
|
op, err := config.clientCompute.SslCertificates.Delete(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Id()).Do()
|
2015-11-02 16:15:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting ssl certificate: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitGlobal(config, op, project, "Deleting SslCertificate")
|
2015-11-02 16:15:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|