Skip to content

Commit f70d407

Browse files
committed
provider/google: SSL Certificates resource + tests & documentation
1 parent 5302bec commit f70d407

5 files changed

Lines changed: 257 additions & 0 deletions

File tree

builtin/providers/google/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func Provider() terraform.ResourceProvider {
4646
"google_compute_network": resourceComputeNetwork(),
4747
"google_compute_project_metadata": resourceComputeProjectMetadata(),
4848
"google_compute_route": resourceComputeRoute(),
49+
"google_compute_ssl_certificate": resourceComputeSslCertificate(),
4950
"google_compute_target_pool": resourceComputeTargetPool(),
5051
"google_compute_vpn_gateway": resourceComputeVpnGateway(),
5152
"google_compute_vpn_tunnel": resourceComputeVpnTunnel(),
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package google
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform/helper/resource"
8+
"github.com/hashicorp/terraform/terraform"
9+
)
10+
11+
func TestAccComputeSslCertificate_basic(t *testing.T) {
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() { testAccPreCheck(t) },
14+
Providers: testAccProviders,
15+
CheckDestroy: testAccCheckComputeSslCertificateDestroy,
16+
Steps: []resource.TestStep{
17+
resource.TestStep{
18+
Config: testAccComputeSslCertificate_basic,
19+
Check: resource.ComposeTestCheckFunc(
20+
testAccCheckComputeSslCertificateExists(
21+
"google_compute_ssl_certificate.foobar"),
22+
),
23+
},
24+
},
25+
})
26+
}
27+
28+
func testAccCheckComputeSslCertificateDestroy(s *terraform.State) error {
29+
config := testAccProvider.Meta().(*Config)
30+
31+
for _, rs := range s.RootModule().Resources {
32+
if rs.Type != "google_compute_ssl_certificate" {
33+
continue
34+
}
35+
36+
_, err := config.clientCompute.SslCertificates.Get(
37+
config.Project, rs.Primary.ID).Do()
38+
if err == nil {
39+
return fmt.Errorf("SslCertificate still exists")
40+
}
41+
}
42+
43+
return nil
44+
}
45+
46+
func testAccCheckComputeSslCertificateExists(n string) resource.TestCheckFunc {
47+
return func(s *terraform.State) error {
48+
rs, ok := s.RootModule().Resources[n]
49+
if !ok {
50+
return fmt.Errorf("Not found: %s", n)
51+
}
52+
53+
if rs.Primary.ID == "" {
54+
return fmt.Errorf("No ID is set")
55+
}
56+
57+
config := testAccProvider.Meta().(*Config)
58+
59+
found, err := config.clientCompute.SslCertificates.Get(
60+
config.Project, rs.Primary.ID).Do()
61+
if err != nil {
62+
return err
63+
}
64+
65+
if found.Name != rs.Primary.ID {
66+
return fmt.Errorf("Certificate not found")
67+
}
68+
69+
return nil
70+
}
71+
}
72+
73+
const testAccComputeSslCertificate_basic = `
74+
resource "google_compute_ssl_certificate" "foobar" {
75+
name = "terraform-test"
76+
description = "very descriptive"
77+
private_key = "${file("~/cert/example.key")}"
78+
certificate = "${file("~/cert/example.crt")}"
79+
}
80+
`
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
layout: "google"
3+
page_title: "Google: google_compute_ssl_certificate"
4+
sidebar_current: "docs-google-compute-ssl-certificate"
5+
description: |-
6+
Creates an SSL certificate resource necessary for HTTPS load balancing in GCE.
7+
---
8+
9+
# google\_compute\_ssl\_certificate
10+
11+
Creates an SSL certificate resource necessary for HTTPS load balancing in GCE.
12+
For more information see
13+
[the official documentation](https://cloud.google.com/compute/docs/load-balancing/http/ssl-certificates) and
14+
[API](https://cloud.google.com/compute/docs/reference/latest/sslCertificates).
15+
16+
17+
## Example Usage
18+
19+
```
20+
resource "google_compute_ssl_certificate" "default" {
21+
name = "my-certificate"
22+
description = "a description"
23+
private_key = "${file("path/to/private.key")}"
24+
certificate = "${file("path/to/certificate.crt")}"
25+
}
26+
```
27+
28+
## Argument Reference
29+
30+
The following arguments are supported:
31+
32+
* `name` - (Required) A unique name for the resource, required by GCE.
33+
Changing this forces a new resource to be created.
34+
* `description` - (Optional) An optional description of this resource.
35+
Changing this forces a new resource to be created.
36+
* `private_key` - (Required) Write only private key in PEM format.
37+
Changing this forces a new resource to be created.
38+
* `description` - (Required) A local certificate file in PEM format. The chain
39+
may be at most 5 certs long, and must include at least one intermediate cert.
40+
Changing this forces a new resource to be created.
41+
42+
## Attributes Reference
43+
44+
The following attributes are exported:
45+
46+
* `self_link` - The URI of the created resource.
47+
* `id` - A unique ID assigned by GCE.

website/source/layouts/google.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@
6565
<a href="/docs/providers/google/r/compute_route.html">google_compute_route</a>
6666
</li>
6767

68+
<li<%= sidebar_current("docs-google-compute-ssl-certificate") %>>
69+
<a href="/docs/providers/google/r/compute_ssl_certificate.html">google_compute_ssl_certificate</a>
70+
</li>
71+
6872
<li<%= sidebar_current("docs-google-compute-target-pool") %>>
6973
<a href="/docs/providers/google/r/compute_target_pool.html">google_compute_target_pool</a>
7074
</li>

0 commit comments

Comments
 (0)