mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-09-07 09:36:44 +00:00
![Paddy](/assets/img/avatar_default.png)
As discussed in #1326, we're not going to remove name_prefix for compute_ssl_certificate, because it makes the common use case more ergonomic by a good amount, and the only cost is it's harder to maintain the autogenerated code, and we've decided the benefits outweigh the costs in this circumstance.
181 lines
4.2 KiB
Go
181 lines
4.2 KiB
Go
package google
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
"google.golang.org/api/compute/v1"
|
|
)
|
|
|
|
func resourceComputeSslCertificate() *schema.Resource {
|
|
return &schema.Resource{
|
|
Create: resourceComputeSslCertificateCreate,
|
|
Read: resourceComputeSslCertificateRead,
|
|
Delete: resourceComputeSslCertificateDelete,
|
|
|
|
Importer: &schema.ResourceImporter{
|
|
State: schema.ImportStatePassthrough,
|
|
},
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
"certificate": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Required: true,
|
|
ForceNew: true,
|
|
Sensitive: true,
|
|
},
|
|
|
|
"name": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Optional: true,
|
|
Computed: true,
|
|
ForceNew: true,
|
|
ConflictsWith: []string{"name_prefix"},
|
|
ValidateFunc: validateGCPName,
|
|
},
|
|
|
|
"name_prefix": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Optional: true,
|
|
Computed: true,
|
|
ForceNew: true,
|
|
ConflictsWith: []string{"name"},
|
|
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
|
|
},
|
|
},
|
|
|
|
"private_key": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Required: true,
|
|
ForceNew: true,
|
|
Sensitive: true,
|
|
},
|
|
|
|
"description": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Optional: true,
|
|
ForceNew: true,
|
|
},
|
|
|
|
"certificate_id": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Computed: true,
|
|
},
|
|
|
|
"project": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Optional: true,
|
|
Computed: true,
|
|
ForceNew: true,
|
|
},
|
|
|
|
"self_link": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Computed: true,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func resourceComputeSslCertificateCreate(d *schema.ResourceData, meta interface{}) error {
|
|
config := meta.(*Config)
|
|
|
|
project, err := getProject(d, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
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()
|
|
}
|
|
|
|
// Build the certificate parameter
|
|
cert := &compute.SslCertificate{
|
|
Name: certName,
|
|
Certificate: d.Get("certificate").(string),
|
|
PrivateKey: d.Get("private_key").(string),
|
|
}
|
|
|
|
if v, ok := d.GetOk("description"); ok {
|
|
cert.Description = v.(string)
|
|
}
|
|
|
|
op, err := config.clientCompute.SslCertificates.Insert(
|
|
project, cert).Do()
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("Error creating ssl certificate: %s", err)
|
|
}
|
|
|
|
err = computeOperationWait(config.clientCompute, op, project, "Creating SslCertificate")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
d.SetId(cert.Name)
|
|
|
|
return resourceComputeSslCertificateRead(d, meta)
|
|
}
|
|
|
|
func resourceComputeSslCertificateRead(d *schema.ResourceData, meta interface{}) error {
|
|
config := meta.(*Config)
|
|
|
|
project, err := getProject(d, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cert, err := config.clientCompute.SslCertificates.Get(
|
|
project, d.Id()).Do()
|
|
if err != nil {
|
|
return handleNotFoundError(err, d, fmt.Sprintf("SSL Certificate %q", d.Get("name").(string)))
|
|
}
|
|
|
|
d.Set("self_link", cert.SelfLink)
|
|
d.Set("certificate_id", strconv.FormatUint(cert.Id, 10))
|
|
d.Set("description", cert.Description)
|
|
d.Set("name", cert.Name)
|
|
d.Set("certificate", cert.Certificate)
|
|
d.Set("project", project)
|
|
|
|
return nil
|
|
}
|
|
|
|
func resourceComputeSslCertificateDelete(d *schema.ResourceData, meta interface{}) error {
|
|
config := meta.(*Config)
|
|
|
|
project, err := getProject(d, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
op, err := config.clientCompute.SslCertificates.Delete(
|
|
project, d.Id()).Do()
|
|
if err != nil {
|
|
return fmt.Errorf("Error deleting ssl certificate: %s", err)
|
|
}
|
|
|
|
err = computeOperationWait(config.clientCompute, op, project, "Deleting SslCertificate")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
d.SetId("")
|
|
return nil
|
|
}
|