mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-09-06 09:06:44 +00:00
96 lines
2.6 KiB
Go
96 lines
2.6 KiB
Go
![]() |
package google
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/hashicorp/terraform/helper/acctest"
|
||
|
"github.com/hashicorp/terraform/helper/resource"
|
||
|
"github.com/hashicorp/terraform/terraform"
|
||
|
)
|
||
|
|
||
|
func TestAccDataSourceGoogleForwardingRule(t *testing.T) {
|
||
|
t.Parallel()
|
||
|
|
||
|
poolName := fmt.Sprintf("tf-%s", acctest.RandString(10))
|
||
|
ruleName := fmt.Sprintf("tf-%s", acctest.RandString(10))
|
||
|
|
||
|
resource.Test(t, resource.TestCase{
|
||
|
PreCheck: func() { testAccPreCheck(t) },
|
||
|
Providers: testAccProviders,
|
||
|
Steps: []resource.TestStep{
|
||
|
resource.TestStep{
|
||
|
Config: testAccDataSourceGoogleForwardingRuleConfig(poolName, ruleName),
|
||
|
Check: resource.ComposeTestCheckFunc(
|
||
|
testAccDataSourceGoogleForwardingRuleCheck("data.google_compute_forwarding_rule.my_forwarding_rule", "google_compute_forwarding_rule.foobar-fr"),
|
||
|
),
|
||
|
},
|
||
|
},
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func testAccDataSourceGoogleForwardingRuleCheck(data_source_name string, resource_name string) resource.TestCheckFunc {
|
||
|
return func(s *terraform.State) error {
|
||
|
ds, ok := s.RootModule().Resources[data_source_name]
|
||
|
if !ok {
|
||
|
return fmt.Errorf("root module has no resource called %s", data_source_name)
|
||
|
}
|
||
|
|
||
|
rs, ok := s.RootModule().Resources[resource_name]
|
||
|
if !ok {
|
||
|
return fmt.Errorf("can't find %s in state", resource_name)
|
||
|
}
|
||
|
|
||
|
ds_attr := ds.Primary.Attributes
|
||
|
rs_attr := rs.Primary.Attributes
|
||
|
forwarding_rule_attrs_to_test := []string{
|
||
|
"id",
|
||
|
"self_link",
|
||
|
"name",
|
||
|
"description",
|
||
|
"region",
|
||
|
"port_range",
|
||
|
"ports",
|
||
|
"target",
|
||
|
"ip_address",
|
||
|
"ip_protocol",
|
||
|
"load_balancing_scheme",
|
||
|
"backend_service",
|
||
|
"network",
|
||
|
"subnetwork",
|
||
|
}
|
||
|
|
||
|
for _, attr_to_check := range forwarding_rule_attrs_to_test {
|
||
|
if ds_attr[attr_to_check] != rs_attr[attr_to_check] {
|
||
|
return fmt.Errorf(
|
||
|
"%s is %s; want %s",
|
||
|
attr_to_check,
|
||
|
ds_attr[attr_to_check],
|
||
|
rs_attr[attr_to_check],
|
||
|
)
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func testAccDataSourceGoogleForwardingRuleConfig(poolName, ruleName string) string {
|
||
|
return fmt.Sprintf(`
|
||
|
resource "google_compute_target_pool" "foobar-tp" {
|
||
|
description = "Resource created for Terraform acceptance testing"
|
||
|
instances = ["us-central1-a/foo", "us-central1-b/bar"]
|
||
|
name = "%s"
|
||
|
}
|
||
|
resource "google_compute_forwarding_rule" "foobar-fr" {
|
||
|
description = "Resource created for Terraform acceptance testing"
|
||
|
ip_protocol = "UDP"
|
||
|
name = "%s"
|
||
|
port_range = "80-81"
|
||
|
target = "${google_compute_target_pool.foobar-tp.self_link}"
|
||
|
}
|
||
|
data "google_compute_forwarding_rule" "my_forwarding_rule" {
|
||
|
name = "${google_compute_forwarding_rule.foobar-fr.name}"
|
||
|
}
|
||
|
`, poolName, ruleName)
|
||
|
}
|