Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new resource google_compute_target_ssl_proxy #569

Merged
merged 5 commits into from
Oct 13, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions google/field_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ func ParseNetworkFieldValue(network string, d TerraformResourceData, config *Con
return parseGlobalFieldValue("networks", network, "project", d, config, true)
}

func ParseSslCertificateFieldValue(sslCertificate string, d TerraformResourceData, config *Config) (*GlobalFieldValue, error) {
return parseGlobalFieldValue("sslCertificates", sslCertificate, "project", d, config, false)
}

// ------------------------------------------------------------
// Base helpers used to create helpers for specific fields.
// ------------------------------------------------------------
Expand Down
31 changes: 31 additions & 0 deletions google/import_compute_target_ssl_proxy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package google

import (
"fmt"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"testing"
)

func TestAccComputeTargetSslProxy_import(t *testing.T) {
target := fmt.Sprintf("tssl-test-%s", acctest.RandString(10))
cert := fmt.Sprintf("tssl-test-%s", acctest.RandString(10))
backend := fmt.Sprintf("tssl-test-%s", acctest.RandString(10))
hc := fmt.Sprintf("tssl-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeTargetSslProxyDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeTargetSslProxy_basic1(target, cert, backend, hc),
},
resource.TestStep{
ResourceName: "google_compute_target_ssl_proxy.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
1 change: 1 addition & 0 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func Provider() terraform.ResourceProvider {
"google_compute_target_http_proxy": resourceComputeTargetHttpProxy(),
"google_compute_target_https_proxy": resourceComputeTargetHttpsProxy(),
"google_compute_target_tcp_proxy": resourceComputeTargetTcpProxy(),
"google_compute_target_ssl_proxy": resourceComputeTargetSslProxy(),
"google_compute_target_pool": resourceComputeTargetPool(),
"google_compute_url_map": resourceComputeUrlMap(),
"google_compute_vpn_gateway": resourceComputeVpnGateway(),
Expand Down
247 changes: 247 additions & 0 deletions google/resource_compute_target_ssl_proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
package google

import (
"fmt"
"log"
"strconv"

"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/compute/v1"
)

func resourceComputeTargetSslProxy() *schema.Resource {
return &schema.Resource{
Create: resourceComputeTargetSslProxyCreate,
Read: resourceComputeTargetSslProxyRead,
Delete: resourceComputeTargetSslProxyDelete,
Update: resourceComputeTargetSslProxyUpdate,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"backend_service": &schema.Schema{
Type: schema.TypeString,
Required: true,
},

"ssl_certificates": &schema.Schema{
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
DiffSuppressFunc: compareSelfLinkOrResourceName,
},
},

"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"proxy_header": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "NONE",
},

"project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"proxy_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},

"self_link": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceComputeTargetSslProxyCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

project, err := getProject(d, config)
if err != nil {
return err
}

sslCertificates, err := expandSslCertificates(d.Get("ssl_certificates").([]interface{}), d, config)
if err != nil {
return err
}

proxy := &compute.TargetSslProxy{
Name: d.Get("name").(string),
Service: d.Get("backend_service").(string),
ProxyHeader: d.Get("proxy_header").(string),
Description: d.Get("description").(string),
SslCertificates: sslCertificates,
}

log.Printf("[DEBUG] TargetSslProxy insert request: %#v", proxy)
op, err := config.clientCompute.TargetSslProxies.Insert(
project, proxy).Do()
if err != nil {
return fmt.Errorf("Error creating TargetSslProxy: %s", err)
}

err = computeOperationWait(config, op, project, "Creating Target Ssl Proxy")
if err != nil {
return err
}

d.SetId(proxy.Name)

return resourceComputeTargetSslProxyRead(d, meta)
}

func resourceComputeTargetSslProxyUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

project, err := getProject(d, config)
if err != nil {
return err
}

d.Partial(true)

if d.HasChange("proxy_header") {
proxy_header := d.Get("proxy_header").(string)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hate to be that guy but this is using snake case rather than camel case.

It's totally readable to me but I imagine others might find it bad style

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oupps, I let this one slip. Good catch!

proxy_header_payload := &compute.TargetSslProxiesSetProxyHeaderRequest{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

ProxyHeader: proxy_header,
}
op, err := config.clientCompute.TargetSslProxies.SetProxyHeader(
project, d.Id(), proxy_header_payload).Do()
if err != nil {
return fmt.Errorf("Error updating proxy_header: %s", err)
}

err = computeOperationWait(config, op, project, "Updating Target SSL Proxy")
if err != nil {
return err
}

d.SetPartial("proxy_header")
}

if d.HasChange("backend_service") {
op, err := config.clientCompute.TargetSslProxies.SetBackendService(project, d.Id(), &compute.TargetSslProxiesSetBackendServiceRequest{
Service: d.Get("backend_service").(string),
}).Do()

if err != nil {
return fmt.Errorf("Error updating backend_service: %s", err)
}

err = computeOperationWait(config, op, project, "Updating Target SSL Proxy")
if err != nil {
return err
}

d.SetPartial("backend_service")
}

if d.HasChange("ssl_certificates") {
sslCertificates, err := expandSslCertificates(d.Get("ssl_certificates").([]interface{}), d, config)
if err != nil {
return err
}

op, err := config.clientCompute.TargetSslProxies.SetSslCertificates(project, d.Id(), &compute.TargetSslProxiesSetSslCertificatesRequest{
SslCertificates: sslCertificates,
}).Do()

if err != nil {
return fmt.Errorf("Error updating backend_service: %s", err)
}

err = computeOperationWait(config, op, project, "Updating Target SSL Proxy")
if err != nil {
return err
}

d.SetPartial("ssl_certificates")
}

d.Partial(false)

return resourceComputeTargetSslProxyRead(d, meta)
}

func resourceComputeTargetSslProxyRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

project, err := getProject(d, config)
if err != nil {
return err
}

proxy, err := config.clientCompute.TargetSslProxies.Get(
project, d.Id()).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Target SSL Proxy %q", d.Get("name").(string)))
}

d.Set("name", proxy.Name)
d.Set("description", proxy.Description)
d.Set("proxy_header", proxy.ProxyHeader)
d.Set("backend_service", proxy.Service)
d.Set("ssl_certificates", proxy.SslCertificates)
d.Set("self_link", proxy.SelfLink)
d.Set("proxy_id", strconv.FormatUint(proxy.Id, 10))

return nil
}

func resourceComputeTargetSslProxyDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

project, err := getProject(d, config)
if err != nil {
return err
}

op, err := config.clientCompute.TargetSslProxies.Delete(
project, d.Id()).Do()
if err != nil {
return fmt.Errorf("Error deleting TargetSslProxy: %s", err)
}

err = computeOperationWait(config, op, project, "Deleting Target SSL Proxy")
if err != nil {
return err
}

d.SetId("")
return nil
}

func expandSslCertificates(configured []interface{}, d *schema.ResourceData, config *Config) ([]string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function would be a bit simpler if it didn't take the configured parameter (which is of a very confusing type). Instead you can use the d parameter to extract out the ssl_certificates field

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

certs := make([]string, 0, len(configured))

for _, sslCertificate := range configured {
sslCertificateFieldValue, err := ParseSslCertificateFieldValue(sslCertificate.(string), d, config)
if err != nil {
return nil, fmt.Errorf("Invalid ssl certificate: %s", err)
}

certs = append(certs, sslCertificateFieldValue.RelativeLink())
}

return certs, nil
}
Loading