-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
}, | ||
}, | ||
}) | ||
} |
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) | ||
proxy_header_payload := &compute.TargetSslProxiesSetProxyHeaderRequest{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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!