diff --git a/google-beta/data_source_google_compute_router.go b/google-beta/data_source_google_compute_router.go new file mode 100644 index 0000000000..b1249473f7 --- /dev/null +++ b/google-beta/data_source_google_compute_router.go @@ -0,0 +1,25 @@ +package google + +import ( + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" +) + +func dataSourceGoogleComputeRouter() *schema.Resource { + dsSchema := datasourceSchemaFromResourceSchema(resourceComputeRouter().Schema) + addRequiredFieldsToSchema(dsSchema, "name") + addRequiredFieldsToSchema(dsSchema, "network") + addOptionalFieldsToSchema(dsSchema, "region") + addOptionalFieldsToSchema(dsSchema, "project") + + return &schema.Resource{ + Read: dataSourceComputeRouterRead, + Schema: dsSchema, + } +} + +func dataSourceComputeRouterRead(d *schema.ResourceData, meta interface{}) error { + routerName := d.Get("name").(string) + + d.SetId(routerName) + return resourceComputeRouterRead(d, meta) +} \ No newline at end of file diff --git a/google-beta/data_source_google_compute_router_test.go b/google-beta/data_source_google_compute_router_test.go new file mode 100644 index 0000000000..8ac2345599 --- /dev/null +++ b/google-beta/data_source_google_compute_router_test.go @@ -0,0 +1,49 @@ +package google + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" +) + +func TestAccDataSourceComputeRouter(t *testing.T) { + t.Parallel() + name := acctest.RandomWithPrefix("router-test") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceComputeRouterConfig(name), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.google_compute_router.myrouter", "id", name), + resource.TestCheckResourceAttr("data.google_compute_router.myrouter", "name", name), + resource.TestCheckResourceAttr("data.google_compute_router.myrouter", "network", fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/global/networks/%s", getTestProjectFromEnv(), name)), + ), + }, + }, + }) +} + +func testAccDataSourceComputeRouterConfig(name string) string { + return fmt.Sprintf(` +resource "google_compute_network" "foobar" { + name = "%s" + auto_create_subnetworks = false +} +resource "google_compute_router" "foobar" { + name = "%s" + network = "${google_compute_network.foobar.name}" + bgp { + asn = 64514 + } +} +data "google_compute_router" "myrouter" { + name = "${google_compute_router.foobar.name}" + network = "${google_compute_network.foobar.name}" +} +`, name, name) +} \ No newline at end of file diff --git a/google-beta/provider.go b/google-beta/provider.go index cba314279e..78b11582ab 100644 --- a/google-beta/provider.go +++ b/google-beta/provider.go @@ -422,6 +422,7 @@ func Provider() terraform.ResourceProvider { "google_compute_regions": dataSourceGoogleComputeRegions(), "google_compute_region_instance_group": dataSourceGoogleComputeRegionInstanceGroup(), "google_compute_resource_policy": dataSourceGoogleComputeResourcePolicy(), + "google_compute_router": dataSourceGoogleComputeRouter(), "google_compute_ssl_certificate": dataSourceGoogleComputeSslCertificate(), "google_compute_ssl_policy": dataSourceGoogleComputeSslPolicy(), "google_compute_subnetwork": dataSourceGoogleComputeSubnetwork(), diff --git a/website/docs/d/datasource_compute_router.html.markdown b/website/docs/d/datasource_compute_router.html.markdown new file mode 100644 index 0000000000..8c197a095e --- /dev/null +++ b/website/docs/d/datasource_compute_router.html.markdown @@ -0,0 +1,39 @@ +--- +layout: "google" +page_title: "Google: google_compute_router" +sidebar_current: "docs-google-datasource-compute-router" +description: |- + Get a Cloud Router within GCE. +--- + +# google\_compute\_router + +Get a router within GCE from its name and VPC. + +## Example Usage + +```hcl +data "google_compute_router" "my-router" { + name = "myrouter-us-east1" + network = "my-network" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) The name of the router. + +* `network` - (Required) The VPC network on which this router lives. + +* `project` - (Optional) The ID of the project in which the resource + belongs. If it is not provided, the provider project is used. + +* `region` - (Optional) The region this router has been created in. If + unspecified, this defaults to the region configured in the provider. + + +## Attributes Reference + +See [google_compute_router](https://www.terraform.io/docs/providers/google/r/compute_router.html) resource for details of the available attributes. \ No newline at end of file diff --git a/website/google.erb b/website/google.erb index 1f86879e5f..f1c42485c1 100644 --- a/website/google.erb +++ b/website/google.erb @@ -96,6 +96,9 @@ > google_compute_resource_policy + > + google_compute_router + > google_compute_ssl_certificate