diff --git a/google/data_source_google_compute_vpn_gateway.go b/google/data_source_google_compute_vpn_gateway.go new file mode 100644 index 00000000..47cbe8cb --- /dev/null +++ b/google/data_source_google_compute_vpn_gateway.go @@ -0,0 +1,78 @@ +package google + +import ( + "fmt" + + "github.com/hashicorp/terraform/helper/schema" + "google.golang.org/api/compute/v1" +) + +func dataSourceGoogleComputeVpnGateway() *schema.Resource { + return &schema.Resource{ + Read: dataSourceGoogleComputeVpnGatewayRead, + + Schema: map[string]*schema.Schema{ + "name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + + "region": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + + "description": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + + "self_link": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + + "network": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + }, + }, + } +} + +func dataSourceGoogleComputeVpnGatewayRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + region, err := getRegion(d, config) + if err != nil { + return err + } + + project, err := getProject(d, config) + if err != nil { + return err + } + + name := d.Get("name").(string) + + vpnGatewaysService := compute.NewTargetVpnGatewaysService(config.clientCompute) + + gateway, err := vpnGatewaysService.Get(project, region, name).Do() + if err != nil { + return handleNotFoundError(err, d, fmt.Sprintf("VPN Gateway Not Found : %s", name)) + } + d.Set("network", gateway.Network) + d.Set("region", gateway.Region) + d.Set("self_link", gateway.SelfLink) + d.Set("description", gateway.Description) + d.Set("project", project) + d.SetId(gateway.Name) + return nil +} diff --git a/google/data_source_google_compute_vpn_gateway_test.go b/google/data_source_google_compute_vpn_gateway_test.go new file mode 100644 index 00000000..cec78918 --- /dev/null +++ b/google/data_source_google_compute_vpn_gateway_test.go @@ -0,0 +1,77 @@ +package google + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccDataSourceGoogleVpnGateway(t *testing.T) { + t.Parallel() + + vpnGatewayName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccDataSourceGoogleVpnGatewayConfig(vpnGatewayName), + Check: resource.ComposeTestCheckFunc( + testAccDataSourceGoogleVpnGatewayCheck("data.google_compute_vpn_gateway.my_vpn_gateway", "google_compute_vpn_gateway.foobar"), + ), + }, + }, + }) +} + +func testAccDataSourceGoogleVpnGatewayCheck(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 + vpn_gateway_attrs_to_test := []string{ + "id", + "self_link", + "name", + "description", + "network", + } + + for _, attr_to_check := range vpn_gateway_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 testAccDataSourceGoogleVpnGatewayConfig(name string) string { + return fmt.Sprintf(` +resource "google_compute_vpn_gateway" "foobar" { + name = "%s" + description = "my-description" + network = "default" +} + +data "google_compute_vpn_gateway" "my_vpn_gateway" { + name = "${google_compute_vpn_gateway.foobar.name}" +}`, name) +} diff --git a/google/provider.go b/google/provider.go index 4e1e0c0f..d16254b0 100644 --- a/google/provider.go +++ b/google/provider.go @@ -74,6 +74,7 @@ func Provider() terraform.ResourceProvider { "google_compute_zones": dataSourceGoogleComputeZones(), "google_compute_instance_group": dataSourceGoogleComputeInstanceGroup(), "google_compute_region_instance_group": dataSourceGoogleComputeRegionInstanceGroup(), + "google_compute_vpn_gateway": dataSourceGoogleComputeVpnGateway(), "google_container_cluster": dataSourceGoogleContainerCluster(), "google_container_engine_versions": dataSourceGoogleContainerEngineVersions(), "google_container_registry_repository": dataSourceGoogleContainerRepo(), diff --git a/website/docs/d/datasource_compute_vpn_gateway.html.markdown b/website/docs/d/datasource_compute_vpn_gateway.html.markdown new file mode 100644 index 00000000..a8a2ffd5 --- /dev/null +++ b/website/docs/d/datasource_compute_vpn_gateway.html.markdown @@ -0,0 +1,46 @@ +--- +layout: "google" +page_title: "Google: google_compute_vpn_gateway" +sidebar_current: "docs-google-datasource-compute-vpn-gateway" +description: |- + Get a VPN gateway within GCE. +--- + +# google\_compute\_vpn\_gateway + +Get a VPN gateway within GCE from its name. + +## Example Usage + +```tf +data "google_compute_vpn_gateway" "my-vpn-gateway" { + name = "vpn-gateway-us-east1" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) The name of the VPN gateway. + + +- - - + +* `project` - (Optional) The project in which the resource belongs. If it + is not provided, the provider project is used. + +* `region` - (Optional) The region in which the resource belongs. If it + is not provided, the project region is used. + +## Attributes Reference + +In addition to the arguments listed above, the following attributes are exported: + +* `network` - The network of this VPN gateway. + +* `description` - Description of this VPN gateway. + +* `region` - Region of this VPN gateway. + +* `self_link` - The URI of the resource. diff --git a/website/google.erb b/website/google.erb index 52cb2766..75aef816 100644 --- a/website/google.erb +++ b/website/google.erb @@ -37,6 +37,9 @@ > google_compute_subnetwork + > + google_compute_vpn_gateway + > google_compute_zones