Skip to content

Commit

Permalink
Add metadataFilters to GlobalForwardingRule (#1160)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored and Ty Larrabee committed Sep 19, 2019
1 parent 1de12fe commit c3763f6
Show file tree
Hide file tree
Showing 4 changed files with 361 additions and 0 deletions.
165 changes: 165 additions & 0 deletions google-beta/resource_compute_global_forwarding_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,42 @@ func resourceComputeGlobalForwardingRule() *schema.Resource {
ValidateFunc: validation.StringInSlice([]string{"INTERNAL_SELF_MANAGED", "EXTERNAL", ""}, false),
Default: "EXTERNAL",
},
"metadata_filters": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"filter_labels": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
MinItems: 1,
MaxItems: 64,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"value": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
},
},
"filter_match_criteria": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"MATCH_ANY", "MATCH_ALL"}, false),
},
},
},
},
"network": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -167,6 +203,12 @@ func resourceComputeGlobalForwardingRuleCreate(d *schema.ResourceData, meta inte
} else if v, ok := d.GetOkExists("load_balancing_scheme"); !isEmptyValue(reflect.ValueOf(loadBalancingSchemeProp)) && (ok || !reflect.DeepEqual(v, loadBalancingSchemeProp)) {
obj["loadBalancingScheme"] = loadBalancingSchemeProp
}
metadataFiltersProp, err := expandComputeGlobalForwardingRuleMetadataFilters(d.Get("metadata_filters"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("metadata_filters"); !isEmptyValue(reflect.ValueOf(metadataFiltersProp)) && (ok || !reflect.DeepEqual(v, metadataFiltersProp)) {
obj["metadataFilters"] = metadataFiltersProp
}
nameProp, err := expandComputeGlobalForwardingRuleName(d.Get("name"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -318,6 +360,9 @@ func resourceComputeGlobalForwardingRuleRead(d *schema.ResourceData, meta interf
if err := d.Set("load_balancing_scheme", flattenComputeGlobalForwardingRuleLoadBalancingScheme(res["loadBalancingScheme"], d)); err != nil {
return fmt.Errorf("Error reading GlobalForwardingRule: %s", err)
}
if err := d.Set("metadata_filters", flattenComputeGlobalForwardingRuleMetadataFilters(res["metadataFilters"], d)); err != nil {
return fmt.Errorf("Error reading GlobalForwardingRule: %s", err)
}
if err := d.Set("name", flattenComputeGlobalForwardingRuleName(res["name"], d)); err != nil {
return fmt.Errorf("Error reading GlobalForwardingRule: %s", err)
}
Expand Down Expand Up @@ -515,6 +560,56 @@ func flattenComputeGlobalForwardingRuleLoadBalancingScheme(v interface{}, d *sch
return v
}

func flattenComputeGlobalForwardingRuleMetadataFilters(v interface{}, d *schema.ResourceData) interface{} {
if v == nil {
return v
}
l := v.([]interface{})
transformed := make([]interface{}, 0, len(l))
for _, raw := range l {
original := raw.(map[string]interface{})
if len(original) < 1 {
// Do not include empty json objects coming back from the api
continue
}
transformed = append(transformed, map[string]interface{}{
"filter_match_criteria": flattenComputeGlobalForwardingRuleMetadataFiltersFilterMatchCriteria(original["filterMatchCriteria"], d),
"filter_labels": flattenComputeGlobalForwardingRuleMetadataFiltersFilterLabels(original["filterLabels"], d),
})
}
return transformed
}
func flattenComputeGlobalForwardingRuleMetadataFiltersFilterMatchCriteria(v interface{}, d *schema.ResourceData) interface{} {
return v
}

func flattenComputeGlobalForwardingRuleMetadataFiltersFilterLabels(v interface{}, d *schema.ResourceData) interface{} {
if v == nil {
return v
}
l := v.([]interface{})
transformed := make([]interface{}, 0, len(l))
for _, raw := range l {
original := raw.(map[string]interface{})
if len(original) < 1 {
// Do not include empty json objects coming back from the api
continue
}
transformed = append(transformed, map[string]interface{}{
"name": flattenComputeGlobalForwardingRuleMetadataFiltersFilterLabelsName(original["name"], d),
"value": flattenComputeGlobalForwardingRuleMetadataFiltersFilterLabelsValue(original["value"], d),
})
}
return transformed
}
func flattenComputeGlobalForwardingRuleMetadataFiltersFilterLabelsName(v interface{}, d *schema.ResourceData) interface{} {
return v
}

func flattenComputeGlobalForwardingRuleMetadataFiltersFilterLabelsValue(v interface{}, d *schema.ResourceData) interface{} {
return v
}

func flattenComputeGlobalForwardingRuleName(v interface{}, d *schema.ResourceData) interface{} {
return v
}
Expand Down Expand Up @@ -569,6 +664,76 @@ func expandComputeGlobalForwardingRuleLoadBalancingScheme(v interface{}, d Terra
return v, nil
}

func expandComputeGlobalForwardingRuleMetadataFilters(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
req := make([]interface{}, 0, len(l))
for _, raw := range l {
if raw == nil {
continue
}
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedFilterMatchCriteria, err := expandComputeGlobalForwardingRuleMetadataFiltersFilterMatchCriteria(original["filter_match_criteria"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedFilterMatchCriteria); val.IsValid() && !isEmptyValue(val) {
transformed["filterMatchCriteria"] = transformedFilterMatchCriteria
}

transformedFilterLabels, err := expandComputeGlobalForwardingRuleMetadataFiltersFilterLabels(original["filter_labels"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedFilterLabels); val.IsValid() && !isEmptyValue(val) {
transformed["filterLabels"] = transformedFilterLabels
}

req = append(req, transformed)
}
return req, nil
}

func expandComputeGlobalForwardingRuleMetadataFiltersFilterMatchCriteria(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeGlobalForwardingRuleMetadataFiltersFilterLabels(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
req := make([]interface{}, 0, len(l))
for _, raw := range l {
if raw == nil {
continue
}
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedName, err := expandComputeGlobalForwardingRuleMetadataFiltersFilterLabelsName(original["name"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedName); val.IsValid() && !isEmptyValue(val) {
transformed["name"] = transformedName
}

transformedValue, err := expandComputeGlobalForwardingRuleMetadataFiltersFilterLabelsValue(original["value"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedValue); val.IsValid() && !isEmptyValue(val) {
transformed["value"] = transformedValue
}

req = append(req, transformed)
}
return req, nil
}

func expandComputeGlobalForwardingRuleMetadataFiltersFilterLabelsName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeGlobalForwardingRuleMetadataFiltersFilterLabelsValue(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeGlobalForwardingRuleName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ resource "google_compute_global_forwarding_rule" "default" {
port_range = "80"
load_balancing_scheme = "INTERNAL_SELF_MANAGED"
ip_address = "0.0.0.0"
metadata_filters {
filter_match_criteria = "MATCH_ANY"
filter_labels {
name = "PLANET"
value = "MARS"
}
}
}
resource "google_compute_target_http_proxy" "default" {
Expand Down
133 changes: 133 additions & 0 deletions google-beta/resource_compute_global_forwarding_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ func TestAccComputeGlobalForwardingRule_internalLoadBalancing(t *testing.T) {
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccComputeGlobalForwardingRule_internalLoadBalancingUpdate(fr, proxy, backend, hc, urlmap, igm, it),
},
{
ResourceName: "google_compute_global_forwarding_rule.forwarding_rule",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down Expand Up @@ -369,6 +377,131 @@ resource "google_compute_global_forwarding_rule" "forwarding_rule" {
port_range = "80"
load_balancing_scheme = "INTERNAL_SELF_MANAGED"
ip_address = "0.0.0.0"
metadata_filters {
filter_match_criteria = "MATCH_ANY"
filter_labels {
name = "PLANET"
value = "NEPTUNE"
}
}
}
resource "google_compute_target_http_proxy" "default" {
name = "%s"
description = "a description"
url_map = "${google_compute_url_map.default.self_link}"
}
resource "google_compute_backend_service" "default" {
name = "%s"
port_name = "http"
protocol = "HTTP"
timeout_sec = 10
load_balancing_scheme = "INTERNAL_SELF_MANAGED"
backend {
group = "${google_compute_instance_group_manager.igm.instance_group}"
balancing_mode = "RATE"
capacity_scaler = 0.4
max_rate_per_instance = 50
}
health_checks = ["${google_compute_health_check.default.self_link}"]
}
resource "google_compute_health_check" "default" {
name = "%s"
check_interval_sec = 1
timeout_sec = 1
tcp_health_check {
port = "80"
}
}
resource "google_compute_url_map" "default" {
name = "%s"
description = "a description"
default_service = "${google_compute_backend_service.default.self_link}"
host_rule {
hosts = ["mysite.com"]
path_matcher = "allpaths"
}
path_matcher {
name = "allpaths"
default_service = "${google_compute_backend_service.default.self_link}"
path_rule {
paths = ["/*"]
service = "${google_compute_backend_service.default.self_link}"
}
}
}
data "google_compute_image" "debian_image" {
family = "debian-9"
project = "debian-cloud"
}
resource "google_compute_instance_group_manager" "igm" {
name = "%s"
version {
instance_template = "${google_compute_instance_template.instance_template.self_link}"
name = "primary"
}
base_instance_name = "internal-igm"
zone = "us-central1-f"
target_size = 1
}
resource "google_compute_instance_template" "instance_template" {
name = "%s"
machine_type = "n1-standard-1"
network_interface {
network = "default"
}
disk {
source_image = "${data.google_compute_image.debian_image.self_link}"
auto_delete = true
boot = true
}
}`, fr, proxy, backend, hc, urlmap, igm, it)
}

func testAccComputeGlobalForwardingRule_internalLoadBalancingUpdate(fr, proxy, backend, hc, urlmap, igm, it string) string {
return fmt.Sprintf(`
resource "google_compute_global_forwarding_rule" "forwarding_rule" {
name = "%s"
target = "${google_compute_target_http_proxy.default.self_link}"
port_range = "80"
load_balancing_scheme = "INTERNAL_SELF_MANAGED"
ip_address = "0.0.0.0"
metadata_filters {
filter_match_criteria = "MATCH_ANY"
filter_labels {
name = "PLANET"
value = "NEPTUNE"
}
filter_labels {
name = "PLANET"
value = "JUPITER"
}
}
metadata_filters {
filter_match_criteria = "MATCH_ALL"
filter_labels {
name = "STAR"
value = "PROXIMA CENTAURI"
}
filter_labels {
name = "SPECIES"
value = "ALIEN"
}
}
}
resource "google_compute_target_http_proxy" "default" {
Expand Down
Loading

0 comments on commit c3763f6

Please sign in to comment.