Skip to content

Commit

Permalink
Adding Router datasource
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
maxlip authored and modular-magician committed Nov 1, 2019
1 parent ea6ea1d commit 14d6266
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
25 changes: 25 additions & 0 deletions google-beta/data_source_google_compute_router.go
Original file line number Diff line number Diff line change
@@ -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)
}
49 changes: 49 additions & 0 deletions google-beta/data_source_google_compute_router_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions google-beta/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
39 changes: 39 additions & 0 deletions website/docs/d/datasource_compute_router.html.markdown
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 3 additions & 0 deletions website/google.erb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@
<li<%= sidebar_current("docs-google-datasource-compute-resource-policy") %>>
<a href="/docs/providers/google/d/google_compute_resource_policy.html">google_compute_resource_policy</a>
</li>
<li<%= sidebar_current("docs-google-datasource-compute-router") %>>
<a href="/docs/providers/google/d/datasource_compute_router.html">google_compute_router</a>
</li>
<li<%= sidebar_current("docs-google-datasource-compute-ssl-certificate") %>>
<a href="/docs/providers/google/d/datasource_compute_ssl_certificate.html">google_compute_ssl_certificate</a>
</li>
Expand Down

0 comments on commit 14d6266

Please sign in to comment.