Skip to content

Commit

Permalink
Add new resource google_compute_target_ssl_proxy (hashicorp#569)
Browse files Browse the repository at this point in the history
* Add target ssl proxy
* Add documentation
  • Loading branch information
rosbo authored and Nic Cope committed Oct 16, 2017
1 parent 4f4f983 commit 0adcce1
Show file tree
Hide file tree
Showing 7 changed files with 570 additions and 0 deletions.
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 @@ -102,6 +102,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
249 changes: 249 additions & 0 deletions google/resource_compute_target_ssl_proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
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,
MaxItems: 1,
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, 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") {
proxyHeader := d.Get("proxy_header").(string)
proxyHeaderPayload := &compute.TargetSslProxiesSetProxyHeaderRequest{
ProxyHeader: proxyHeader,
}
op, err := config.clientCompute.TargetSslProxies.SetProxyHeader(
project, d.Id(), proxyHeaderPayload).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, 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(d *schema.ResourceData, config *Config) ([]string, error) {
configured := d.Get("ssl_certificates").([]interface{})
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

0 comments on commit 0adcce1

Please sign in to comment.