2017-04-13 20:25:29 +00:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-04-27 15:52:02 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2017-04-13 20:25:29 +00:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccDataSourceGoogleNetwork(t *testing.T) {
|
2017-10-12 22:07:29 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-04-27 15:52:02 +00:00
|
|
|
networkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
|
2017-04-13 20:25:29 +00:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2017-04-27 15:52:02 +00:00
|
|
|
Config: testAccDataSourceGoogleNetworkConfig(networkName),
|
2017-04-13 20:25:29 +00:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccDataSourceGoogleNetworkCheck("data.google_compute_network.my_network", "google_compute_network.foobar"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccDataSourceGoogleNetworkCheck(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
|
|
|
|
network_attrs_to_test := []string{
|
|
|
|
"id",
|
|
|
|
"self_link",
|
|
|
|
"name",
|
|
|
|
"description",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, attr_to_check := range network_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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-27 15:52:02 +00:00
|
|
|
func testAccDataSourceGoogleNetworkConfig(name string) string {
|
|
|
|
return fmt.Sprintf(`
|
2017-04-13 20:25:29 +00:00
|
|
|
resource "google_compute_network" "foobar" {
|
2017-04-27 15:52:02 +00:00
|
|
|
name = "%s"
|
2017-04-13 20:25:29 +00:00
|
|
|
description = "my-description"
|
|
|
|
}
|
|
|
|
|
|
|
|
data "google_compute_network" "my_network" {
|
|
|
|
name = "${google_compute_network.foobar.name}"
|
2017-04-27 15:52:02 +00:00
|
|
|
}`, name)
|
|
|
|
}
|