Skip to content

Commit fa8921f

Browse files
author
Christoph Blecker
committed
Add support for name_prefix to google_compute_ssl_certificate
1 parent c1637f2 commit fa8921f

3 files changed

Lines changed: 97 additions & 6 deletions

File tree

builtin/providers/google/resource_compute_ssl_certificate.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"log"
66
"strconv"
77

8+
"github.com/hashicorp/terraform/helper/resource"
89
"github.com/hashicorp/terraform/helper/schema"
910
"google.golang.org/api/compute/v1"
1011
"google.golang.org/api/googleapi"
@@ -24,9 +25,36 @@ func resourceComputeSslCertificate() *schema.Resource {
2425
},
2526

2627
"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{
2745
Type: schema.TypeString,
28-
Required: true,
46+
Optional: true,
2947
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+
},
3058
},
3159

3260
"private_key": &schema.Schema{
@@ -68,9 +96,18 @@ func resourceComputeSslCertificateCreate(d *schema.ResourceData, meta interface{
6896
return err
6997
}
7098

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+
71108
// Build the certificate parameter
72109
cert := &compute.SslCertificate{
73-
Name: d.Get("name").(string),
110+
Name: certName,
74111
Certificate: d.Get("certificate").(string),
75112
PrivateKey: d.Get("private_key").(string),
76113
}

builtin/providers/google/resource_compute_ssl_certificate_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,40 @@ func TestAccComputeSslCertificate_basic(t *testing.T) {
2626
})
2727
}
2828

29+
func TestAccComputeSslCertificate_no_name(t *testing.T) {
30+
resource.Test(t, resource.TestCase{
31+
PreCheck: func() { testAccPreCheck(t) },
32+
Providers: testAccProviders,
33+
CheckDestroy: testAccCheckComputeSslCertificateDestroy,
34+
Steps: []resource.TestStep{
35+
resource.TestStep{
36+
Config: testAccComputeSslCertificate_no_name,
37+
Check: resource.ComposeTestCheckFunc(
38+
testAccCheckComputeSslCertificateExists(
39+
"google_compute_ssl_certificate.foobar"),
40+
),
41+
},
42+
},
43+
})
44+
}
45+
46+
func TestAccComputeSslCertificate_name_prefix(t *testing.T) {
47+
resource.Test(t, resource.TestCase{
48+
PreCheck: func() { testAccPreCheck(t) },
49+
Providers: testAccProviders,
50+
CheckDestroy: testAccCheckComputeSslCertificateDestroy,
51+
Steps: []resource.TestStep{
52+
resource.TestStep{
53+
Config: testAccComputeSslCertificate_name_prefix,
54+
Check: resource.ComposeTestCheckFunc(
55+
testAccCheckComputeSslCertificateExists(
56+
"google_compute_ssl_certificate.foobar"),
57+
),
58+
},
59+
},
60+
})
61+
}
62+
2963
func testAccCheckComputeSslCertificateDestroy(s *terraform.State) error {
3064
config := testAccProvider.Meta().(*Config)
3165

@@ -79,3 +113,20 @@ resource "google_compute_ssl_certificate" "foobar" {
79113
certificate = "${file("test-fixtures/ssl_cert/test.crt")}"
80114
}
81115
`, acctest.RandString(10))
116+
117+
var testAccComputeSslCertificate_no_name = fmt.Sprintf(`
118+
resource "google_compute_ssl_certificate" "foobar" {
119+
description = "really descriptive"
120+
private_key = "${file("test-fixtures/ssl_cert/test.key")}"
121+
certificate = "${file("test-fixtures/ssl_cert/test.crt")}"
122+
}
123+
`)
124+
125+
var testAccComputeSslCertificate_name_prefix = fmt.Sprintf(`
126+
resource "google_compute_ssl_certificate" "foobar" {
127+
name_prefix = "sslcert-test-%s-"
128+
description = "extremely descriptive"
129+
private_key = "${file("test-fixtures/ssl_cert/test.key")}"
130+
certificate = "${file("test-fixtures/ssl_cert/test.crt")}"
131+
}
132+
`, acctest.RandString(10))

website/source/docs/providers/google/r/compute_ssl_certificate.html.markdown

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ For more information see
1818

1919
```js
2020
resource "google_compute_ssl_certificate" "default" {
21-
name = "my-certificate"
21+
name_prefix = "my-certificate-"
2222
description = "a description"
2323
private_key = "${file("path/to/private.key")}"
2424
certificate = "${file("path/to/certificate.crt")}"
@@ -33,14 +33,17 @@ The following arguments are supported:
3333
may be at most 5 certs long, and must include at least one intermediate
3434
cert. Changing this forces a new resource to be created.
3535

36-
* `name` - (Required) A unique name for the resource, required by GCE.
37-
Changing this forces a new resource to be created.
38-
3936
* `private_key` - (Required) Write only private key in PEM format.
4037
Changing this forces a new resource to be created.
4138

4239
- - -
4340

41+
* `name` - (Optional) A unique name for the SSL certificate. If you leave
42+
this blank, Terraform will auto-generate a unique name.
43+
44+
* `name_prefix` - (Optional) Creates a unique name beginning with the specified
45+
prefix. Conflicts with `name`.
46+
4447
* `description` - (Optional) An optional description of this resource.
4548
Changing this forces a new resource to be created.
4649

0 commit comments

Comments
 (0)