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 certificate_map to compute_target_https_proxy #4061

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
60 changes: 60 additions & 0 deletions google-beta/field_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
const (
globalLinkTemplate = "projects/%s/global/%s/%s"
globalLinkBasePattern = "projects/(.+)/global/%s/(.+)"
locationLinkTemplate = "projects/%s/locations/%s/%s/%s"
locationLinkBasePattern = "projects/(.+)/locations/(.+)/%s/(.+)"
zonalLinkTemplate = "projects/%s/zones/%s/%s/%s"
zonalLinkBasePattern = "projects/(.+)/zones/(.+)/%s/(.+)"
zonalPartialLinkBasePattern = "zones/(.+)/%s/(.+)"
Expand Down Expand Up @@ -146,6 +148,64 @@ func parseGlobalFieldValue(resourceType, fieldValue, projectSchemaField string,
}, nil
}

type LocationFieldValue struct {
Project string
Location string
Name string

resourceType string
}

func (f LocationFieldValue) RelativeLink() string {
if len(f.Name) == 0 {
return ""
}

return fmt.Sprintf(locationLinkTemplate, f.Project, f.Location, f.resourceType, f.Name)
}

// Parses a location field supporting 5 different formats:
// - https://www.googleapis.com/compute/ANY_VERSION/projects/{my_project}/locations/{location}/{resource_type}/{resource_name}
// - projects/{my_project}/locations/{location}/{resource_type}/{resource_name}
// - {location}/{resource_type}/{resource_name}
// - resource_name
// - "" (empty string). RelativeLink() returns empty if isEmptyValid is true.
//
// If the project is not specified, it first tries to get the project from the `projectSchemaField` and then fallback on the default project.
// If the location is not specified, it will assume 'global' location.
func parseLocationFieldValue(resourceType, fieldValue, projectSchemaField string, d TerraformResourceData, config *Config, isEmptyValid bool) (*LocationFieldValue, error) {
if len(fieldValue) == 0 {
if isEmptyValid {
return &LocationFieldValue{resourceType: resourceType}, nil
}
return nil, fmt.Errorf("The location field for resource %s cannot be empty", resourceType)
}

r := regexp.MustCompile(fmt.Sprintf(locationLinkBasePattern, resourceType))
if parts := r.FindStringSubmatch(fieldValue); parts != nil {
return &LocationFieldValue{
Project: parts[1],
Location: parts[2],
Name: parts[3],

resourceType: resourceType,
}, nil
}

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

return &LocationFieldValue{
Project: project,
Location: "global",
Name: GetResourceNameFromSelfLink(fieldValue),

resourceType: resourceType,
}, nil
}

type ZonalFieldValue struct {
Project string
Zone string
Expand Down
79 changes: 79 additions & 0 deletions google-beta/field_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,85 @@ func TestParseGlobalFieldValue(t *testing.T) {
}
}

func TestParseLocationFieldValue(t *testing.T) {
const resourceType = "certificateMaps"
cases := map[string]struct {
FieldValue string
ExpectedRelativeLink string
ExpectedError bool
IsEmptyValid bool
ProjectSchemaField string
ProjectSchemaValue string
Config *Config
}{
"certificateMap is a full self link": {
FieldValue: "//certificatemanager.googleapis.com/projects/myproject/locations/mylocation/certificateMaps/mymap",
ExpectedRelativeLink: "projects/myproject/locations/mylocation/certificateMaps/mymap",
},
"certificateMap is a relative self link": {
FieldValue: "projects/myproject/locations/mylocation/certificateMaps/mymap",
ExpectedRelativeLink: "projects/myproject/locations/mylocation/certificateMaps/mymap",
},
"certificateMap is a partial relative self link": {
FieldValue: "global/certificateMaps/mymap",
Config: &Config{Project: "default-project"},
ExpectedRelativeLink: "projects/default-project/locations/global/certificateMaps/mymap",
},
"certificateMap is the name only": {
FieldValue: "mymap",
Config: &Config{Project: "default-project"},
ExpectedRelativeLink: "projects/default-project/locations/global/certificateMaps/mymap",
},
"certificateMap is the name only and has a project set in schema": {
FieldValue: "mymap",
ProjectSchemaField: "project",
ProjectSchemaValue: "schema-project",
Config: &Config{Project: "default-project"},
ExpectedRelativeLink: "projects/schema-project/locations/global/certificateMaps/mymap",
},
"certificateMap is the name only and has a project set in schema but the field is not specified.": {
FieldValue: "mymap",
ProjectSchemaValue: "schema-project",
Config: &Config{Project: "default-project"},
ExpectedRelativeLink: "projects/default-project/locations/global/certificateMaps/mymap",
},
"certificateMap is empty and it is valid": {
FieldValue: "",
IsEmptyValid: true,
ExpectedRelativeLink: "",
},
"certificateMap is empty and it is not valid": {
FieldValue: "",
IsEmptyValid: false,
ExpectedError: true,
},
}

for tn, tc := range cases {
fieldsInSchema := make(map[string]interface{})

if len(tc.ProjectSchemaValue) > 0 && len(tc.ProjectSchemaField) > 0 {
fieldsInSchema[tc.ProjectSchemaField] = tc.ProjectSchemaValue
}

d := &ResourceDataMock{
FieldsInSchema: fieldsInSchema,
}

v, err := parseLocationFieldValue(resourceType, tc.FieldValue, tc.ProjectSchemaField, d, tc.Config, tc.IsEmptyValid)

if err != nil {
if !tc.ExpectedError {
t.Errorf("bad: %s, did not expect an error. Error: %s", tn, err)
}
} else {
if v.RelativeLink() != tc.ExpectedRelativeLink {
t.Errorf("bad: %s, expected relative link to be '%s' but got '%s'", tn, tc.ExpectedRelativeLink, v.RelativeLink())
}
}
}
}

func TestParseZonalFieldValue(t *testing.T) {
const resourceType = "instances"
cases := map[string]struct {
Expand Down
68 changes: 67 additions & 1 deletion google-beta/resource_compute_target_https_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,22 @@ character, which cannot be a dash.`,
},
"ssl_certificates": {
Type: schema.TypeList,
Required: true,
Optional: true,
Description: `A list of SslCertificate resources that are used to authenticate
connections between users and the load balancer. At least one SSL
certificate must be specified.`,
Elem: &schema.Schema{
Type: schema.TypeString,
DiffSuppressFunc: compareSelfLinkOrResourceName,
},
AtLeastOneOf: []string{"ssl_certificates", "certificate_map"},
},
"certificate_map": {
Type: schema.TypeString,
Optional: true,
Description: `A reference to the CrtificateMap resource that identifies a certificate map
associated with the given target proxy. This field can only be set for global target proxies.`,
AtLeastOneOf: []string{"ssl_certificates", "certificate_map"},
},
"url_map": {
Type: schema.TypeString,
Expand Down Expand Up @@ -161,6 +169,12 @@ func resourceComputeTargetHttpsProxyCreate(d *schema.ResourceData, meta interfac
} else if v, ok := d.GetOkExists("ssl_certificates"); !isEmptyValue(reflect.ValueOf(sslCertificatesProp)) && (ok || !reflect.DeepEqual(v, sslCertificatesProp)) {
obj["sslCertificates"] = sslCertificatesProp
}
certificateMapProp, err := expandComputeTargetHttpsProxyCertificateMap(d.Get("certificate_map"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("certificate_map"); !isEmptyValue(reflect.ValueOf(certificateMapProp)) && (ok || !reflect.DeepEqual(v, certificateMapProp)) {
obj["certificateMap"] = certificateMapProp
}
sslPolicyProp, err := expandComputeTargetHttpsProxySslPolicy(d.Get("ssl_policy"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -278,6 +292,9 @@ func resourceComputeTargetHttpsProxyRead(d *schema.ResourceData, meta interface{
if err := d.Set("ssl_certificates", flattenComputeTargetHttpsProxySslCertificates(res["sslCertificates"], d, config)); err != nil {
return fmt.Errorf("Error reading TargetHttpsProxy: %s", err)
}
if err := d.Set("certificate_map", flattenComputeTargetHttpsProxyCertificateMap(res["certificateMap"], d, config)); err != nil {
return fmt.Errorf("Error reading TargetHttpsProxy: %s", err)
}
if err := d.Set("ssl_policy", flattenComputeTargetHttpsProxySslPolicy(res["sslPolicy"], d, config)); err != nil {
return fmt.Errorf("Error reading TargetHttpsProxy: %s", err)
}
Expand Down Expand Up @@ -379,6 +396,40 @@ func resourceComputeTargetHttpsProxyUpdate(d *schema.ResourceData, meta interfac
return err
}
}
if d.HasChange("certificate_map") {
obj := make(map[string]interface{})

certificateMapProp, err := expandComputeTargetHttpsProxyCertificateMap(d.Get("certificate_map"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("certificate_map"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, certificateMapProp)) {
obj["certificateMap"] = certificateMapProp
}

url, err := replaceVars(d, config, "{{ComputeBasePath}}projects/{{project}}/global/targetHttpsProxies/{{name}}/setCertificateMap")
if err != nil {
return err
}

// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}

res, err := sendRequestWithTimeout(config, "POST", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return fmt.Errorf("Error updating TargetHttpsProxy %q: %s", d.Id(), err)
} else {
log.Printf("[DEBUG] Finished updating TargetHttpsProxy %q: %#v", d.Id(), res)
}

err = computeOperationWaitTime(
config, res, project, "Updating TargetHttpsProxy", userAgent,
d.Timeout(schema.TimeoutUpdate))
if err != nil {
return err
}
}
if d.HasChange("ssl_policy") {
obj := make(map[string]interface{})

Expand Down Expand Up @@ -562,6 +613,13 @@ func flattenComputeTargetHttpsProxySslCertificates(v interface{}, d *schema.Reso
return convertAndMapStringArr(v.([]interface{}), ConvertSelfLinkToV1)
}

func flattenComputeTargetHttpsProxyCertificateMap(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return v
}
return GetResourceNameFromSelfLink(v.(string))
}

func flattenComputeTargetHttpsProxySslPolicy(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return v
Expand Down Expand Up @@ -608,6 +666,14 @@ func expandComputeTargetHttpsProxySslCertificates(v interface{}, d TerraformReso
return req, nil
}

func expandComputeTargetHttpsProxyCertificateMap(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
f, err := parseLocationFieldValue("certificateMaps", v.(string), "project", d, config, true)
if err != nil {
return nil, fmt.Errorf("Invalid value for certificate_map: %s", err)
}
return "//certificatemanager.googleapis.com/" + f.RelativeLink(), nil
}

func expandComputeTargetHttpsProxySslPolicy(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
f, err := parseGlobalFieldValue("sslPolicies", v.(string), "project", d, config, true)
if err != nil {
Expand Down