Skip to content

Commit

Permalink
Add support for INTERNAL_SELF_MANAGED backend service
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
slevenick authored and modular-magician committed May 29, 2019
1 parent e76adf1 commit 3e24444
Show file tree
Hide file tree
Showing 7 changed files with 562 additions and 5 deletions.
2 changes: 1 addition & 1 deletion google-beta/resource_compute_backend_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func resourceComputeBackendService() *schema.Resource {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"EXTERNAL", ""}, false),
ValidateFunc: validation.StringInSlice([]string{"EXTERNAL", "INTERNAL_SELF_MANAGED", ""}, false),
Default: "EXTERNAL",
},
"port_name": {
Expand Down
125 changes: 125 additions & 0 deletions google-beta/resource_compute_backend_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,35 @@ func TestAccComputeBackendService_withCustomHeaders(t *testing.T) {
})
}

func TestAccComputeBackendService_internalLoadBalancing(t *testing.T) {
t.Parallel()

fr := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10))
proxy := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10))
backend := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10))
hc := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10))
urlmap := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeBackendServiceDestroy,
Steps: []resource.TestStep{
{
Config: testAccComputeBackendService_internalLoadBalancing(fr, proxy, backend, hc, urlmap),
},
{
ResourceName: "google_compute_backend_service.backend_service",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccComputeBackendService_internalLoadBalancing(fr, proxy, backend, hc, urlmap),
},
},
})
}

func testAccComputeBackendService_basic(serviceName, checkName string) string {
return fmt.Sprintf(`
resource "google_compute_backend_service" "foobar" {
Expand Down Expand Up @@ -1057,3 +1086,99 @@ resource "google_compute_http_health_check" "zero" {
}
`, serviceName, checkName)
}

func testAccComputeBackendService_internalLoadBalancing(fr, proxy, backend, hc, urlmap 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"
}
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" "backend_service" {
name = "%s"
port_name = "http"
protocol = "HTTP"
timeout_sec = 10
load_balancing_scheme = "INTERNAL_SELF_MANAGED"
backend {
group = "${google_compute_instance_group_manager.foobar.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.backend_service.self_link}"
host_rule {
hosts = ["mysite.com"]
path_matcher = "allpaths"
}
path_matcher {
name = "allpaths"
default_service = "${google_compute_backend_service.backend_service.self_link}"
path_rule {
paths = ["/*"]
service = "${google_compute_backend_service.backend_service.self_link}"
}
}
}
data "google_compute_image" "debian_image" {
family = "debian-9"
project = "debian-cloud"
}
resource "google_compute_instance_group_manager" "foobar" {
name = "igm-internal"
version {
instance_template = "${google_compute_instance_template.foobar.self_link}"
name = "primary"
}
base_instance_name = "foobar"
zone = "us-central1-f"
target_size = 1
}
resource "google_compute_instance_template" "foobar" {
name = "instance-template-internal"
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)
}
55 changes: 55 additions & 0 deletions google-beta/resource_compute_global_forwarding_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ func resourceComputeGlobalForwardingRule() *schema.Resource {
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"load_balancing_scheme": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"INTERNAL_SELF_MANAGED", "EXTERNAL", ""}, false),
Default: "EXTERNAL",
},
"network": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
},
"port_range": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -147,12 +161,24 @@ func resourceComputeGlobalForwardingRuleCreate(d *schema.ResourceData, meta inte
} else if v, ok := d.GetOkExists("label_fingerprint"); !isEmptyValue(reflect.ValueOf(labelFingerprintProp)) && (ok || !reflect.DeepEqual(v, labelFingerprintProp)) {
obj["labelFingerprint"] = labelFingerprintProp
}
loadBalancingSchemeProp, err := expandComputeGlobalForwardingRuleLoadBalancingScheme(d.Get("load_balancing_scheme"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("load_balancing_scheme"); !isEmptyValue(reflect.ValueOf(loadBalancingSchemeProp)) && (ok || !reflect.DeepEqual(v, loadBalancingSchemeProp)) {
obj["loadBalancingScheme"] = loadBalancingSchemeProp
}
nameProp, err := expandComputeGlobalForwardingRuleName(d.Get("name"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("name"); !isEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) {
obj["name"] = nameProp
}
networkProp, err := expandComputeGlobalForwardingRuleNetwork(d.Get("network"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("network"); !isEmptyValue(reflect.ValueOf(networkProp)) && (ok || !reflect.DeepEqual(v, networkProp)) {
obj["network"] = networkProp
}
portRangeProp, err := expandComputeGlobalForwardingRulePortRange(d.Get("port_range"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -289,9 +315,15 @@ func resourceComputeGlobalForwardingRuleRead(d *schema.ResourceData, meta interf
if err := d.Set("label_fingerprint", flattenComputeGlobalForwardingRuleLabelFingerprint(res["labelFingerprint"], d)); err != nil {
return fmt.Errorf("Error reading GlobalForwardingRule: %s", err)
}
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("name", flattenComputeGlobalForwardingRuleName(res["name"], d)); err != nil {
return fmt.Errorf("Error reading GlobalForwardingRule: %s", err)
}
if err := d.Set("network", flattenComputeGlobalForwardingRuleNetwork(res["network"], d)); err != nil {
return fmt.Errorf("Error reading GlobalForwardingRule: %s", err)
}
if err := d.Set("port_range", flattenComputeGlobalForwardingRulePortRange(res["portRange"], d)); err != nil {
return fmt.Errorf("Error reading GlobalForwardingRule: %s", err)
}
Expand Down Expand Up @@ -476,10 +508,21 @@ func flattenComputeGlobalForwardingRuleLabelFingerprint(v interface{}, d *schema
return v
}

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

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

func flattenComputeGlobalForwardingRuleNetwork(v interface{}, d *schema.ResourceData) interface{} {
if v == nil {
return v
}
return ConvertSelfLinkToV1(v.(string))
}

func flattenComputeGlobalForwardingRulePortRange(v interface{}, d *schema.ResourceData) interface{} {
return v
}
Expand Down Expand Up @@ -519,10 +562,22 @@ func expandComputeGlobalForwardingRuleLabelFingerprint(v interface{}, d Terrafor
return v, nil
}

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

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

func expandComputeGlobalForwardingRuleNetwork(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
f, err := parseGlobalFieldValue("networks", v.(string), "project", d, config, true)
if err != nil {
return nil, fmt.Errorf("Invalid value for network: %s", err)
}
return f.RelativeLink(), nil
}

func expandComputeGlobalForwardingRulePortRange(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down
124 changes: 124 additions & 0 deletions google-beta/resource_compute_global_forwarding_rule_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,130 @@ resource "google_compute_http_health_check" "default" {
`, context)
}

func TestAccComputeGlobalForwardingRule_globalForwardingRuleInternalExample(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": acctest.RandString(10),
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProvidersOiCS,
CheckDestroy: testAccCheckComputeGlobalForwardingRuleDestroy,
Steps: []resource.TestStep{
{
Config: testAccComputeGlobalForwardingRule_globalForwardingRuleInternalExample(context),
},
},
})
}

func testAccComputeGlobalForwardingRule_globalForwardingRuleInternalExample(context map[string]interface{}) string {
return Nprintf(`
resource "google_compute_global_forwarding_rule" "default" {
provider = "google-beta"
name = "global-rule-%{random_suffix}"
target = "${google_compute_target_http_proxy.default.self_link}"
port_range = "80"
load_balancing_scheme = "INTERNAL_SELF_MANAGED"
ip_address = "0.0.0.0"
}
resource "google_compute_target_http_proxy" "default" {
provider = "google-beta"
name = "target-proxy-%{random_suffix}"
description = "a description"
url_map = "${google_compute_url_map.default.self_link}"
}
resource "google_compute_url_map" "default" {
provider = "google-beta"
name = "url-map-target-proxy-%{random_suffix}"
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}"
}
}
}
resource "google_compute_backend_service" "default" {
provider = "google-beta"
name = "backend-%{random_suffix}"
port_name = "http"
protocol = "HTTP"
timeout_sec = 10
load_balancing_scheme = "INTERNAL_SELF_MANAGED"
backend {
group = "${google_compute_instance_group_manager.foobar.instance_group}"
balancing_mode = "RATE"
capacity_scaler = 0.4
max_rate_per_instance = 50
}
health_checks = ["${google_compute_health_check.default.self_link}"]
}
data "google_compute_image" "debian_image" {
provider = "google-beta"
family = "debian-9"
project = "debian-cloud"
}
resource "google_compute_instance_group_manager" "foobar" {
provider = "google-beta"
name = "igm-internal"
version {
instance_template = "${google_compute_instance_template.foobar.self_link}"
name = "primary"
}
base_instance_name = "foobar"
zone = "us-central1-f"
target_size = 1
}
resource "google_compute_instance_template" "foobar" {
provider = "google-beta"
name = "instance-template-internal"
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
}
}
resource "google_compute_health_check" "default" {
provider = "google-beta"
name = "check-backend-%{random_suffix}"
check_interval_sec = 1
timeout_sec = 1
tcp_health_check {
port = "80"
}
}
`, context)
}

func testAccCheckComputeGlobalForwardingRuleDestroy(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
if rs.Type != "google_compute_global_forwarding_rule" {
Expand Down
Loading

0 comments on commit 3e24444

Please sign in to comment.