Add support for name_prefix to google_compute_ssl_certificate

This commit is contained in:
Christoph Blecker 2016-12-12 15:57:58 -08:00
parent 06d17ca75f
commit 2b62f40b7f
2 changed files with 90 additions and 2 deletions

View File

@ -5,6 +5,7 @@ import (
"log"
"strconv"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/compute/v1"
"google.golang.org/api/googleapi"
@ -24,9 +25,36 @@ func resourceComputeSslCertificate() *schema.Resource {
},
"name": &schema.Schema{
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{
Type: schema.TypeString,
Required: true,
Optional: true,
ForceNew: true,
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{
@ -68,9 +96,18 @@ func resourceComputeSslCertificateCreate(d *schema.ResourceData, meta interface{
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: d.Get("name").(string),
Name: certName,
Certificate: d.Get("certificate").(string),
PrivateKey: d.Get("private_key").(string),
}

View File

@ -26,6 +26,40 @@ func TestAccComputeSslCertificate_basic(t *testing.T) {
})
}
func TestAccComputeSslCertificate_no_name(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeSslCertificateDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeSslCertificate_no_name,
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeSslCertificateExists(
"google_compute_ssl_certificate.foobar"),
),
},
},
})
}
func TestAccComputeSslCertificate_name_prefix(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeSslCertificateDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeSslCertificate_name_prefix,
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeSslCertificateExists(
"google_compute_ssl_certificate.foobar"),
),
},
},
})
}
func testAccCheckComputeSslCertificateDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
@ -79,3 +113,20 @@ resource "google_compute_ssl_certificate" "foobar" {
certificate = "${file("test-fixtures/ssl_cert/test.crt")}"
}
`, acctest.RandString(10))
var testAccComputeSslCertificate_no_name = fmt.Sprintf(`
resource "google_compute_ssl_certificate" "foobar" {
description = "really descriptive"
private_key = "${file("test-fixtures/ssl_cert/test.key")}"
certificate = "${file("test-fixtures/ssl_cert/test.crt")}"
}
`)
var testAccComputeSslCertificate_name_prefix = fmt.Sprintf(`
resource "google_compute_ssl_certificate" "foobar" {
name_prefix = "sslcert-test-%s-"
description = "extremely descriptive"
private_key = "${file("test-fixtures/ssl_cert/test.key")}"
certificate = "${file("test-fixtures/ssl_cert/test.crt")}"
}
`, acctest.RandString(10))