Skip to content

Commit

Permalink
Added the data_source_compute_router_nat (#7058) (#13475)
Browse files Browse the repository at this point in the history
* added the data_source_compute_router_nat with test and docs

* changes in test file

* minor fix

Signed-off-by: Modular Magician <[email protected]>

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Jan 12, 2023
1 parent ef7a097 commit 3105cd0
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/7058.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-datasource
google_compute_router_nat
```
33 changes: 33 additions & 0 deletions google/data_source_google_compute_router_nat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package google

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceGoogleComputeRouterNat() *schema.Resource {

dsSchema := datasourceSchemaFromResourceSchema(resourceComputeRouterNat().Schema)

addRequiredFieldsToSchema(dsSchema, "name", "router")
addOptionalFieldsToSchema(dsSchema, "project", "region")

return &schema.Resource{
Read: dataSourceGoogleComputeRouterNatRead,
Schema: dsSchema,
}

}

func dataSourceGoogleComputeRouterNatRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

id, err := replaceVars(d, config, "{{project}}/{{region}}/{{router}}/{{name}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

return resourceComputeRouterNatRead(d, meta)
}
73 changes: 73 additions & 0 deletions google/data_source_google_compute_router_nat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package google

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceGoogleComputeRouterNat_basic(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": randString(t, 10),
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeRouterNatDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccDataSourceGoogleComputeRouterNat_basic(context),
Check: resource.ComposeTestCheckFunc(
checkDataSourceStateMatchesResourceState("data.google_compute_router_nat.foo", "google_compute_router_nat.nat"),
),
},
},
})
}

func testAccDataSourceGoogleComputeRouterNat_basic(context map[string]interface{}) string {
return Nprintf(`
resource "google_compute_network" "net" {
name = "my-network%{random_suffix}"
}
resource "google_compute_subnetwork" "subnet" {
name = "my-subnetwork%{random_suffix}"
network = google_compute_network.net.id
ip_cidr_range = "10.0.0.0/16"
region = "us-central1"
}
resource "google_compute_router" "router" {
name = "my-router%{random_suffix}"
region = google_compute_subnetwork.subnet.region
network = google_compute_network.net.id
bgp {
asn = 64514
}
}
resource "google_compute_router_nat" "nat" {
name = "my-router-nat%{random_suffix}"
router = google_compute_router.router.name
region = google_compute_router.router.region
nat_ip_allocate_option = "AUTO_ONLY"
source_subnetwork_ip_ranges_to_nat = "ALL_SUBNETWORKS_ALL_IP_RANGES"
log_config {
enable = true
filter = "ERRORS_ONLY"
}
}
data "google_compute_router_nat" "foo" {
name = google_compute_router_nat.nat.name
router = google_compute_router_nat.nat.router
region = google_compute_router.router.region
}`, context)

}
1 change: 1 addition & 0 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,7 @@ func Provider() *schema.Provider {
"google_compute_region_ssl_certificate": dataSourceGoogleRegionComputeSslCertificate(),
"google_compute_resource_policy": dataSourceGoogleComputeResourcePolicy(),
"google_compute_router": dataSourceGoogleComputeRouter(),
"google_compute_router_nat": dataSourceGoogleComputeRouterNat(),
"google_compute_router_status": dataSourceGoogleComputeRouterStatus(),
"google_compute_snapshot": dataSourceGoogleComputeSnapshot(),
"google_compute_ssl_certificate": dataSourceGoogleComputeSslCertificate(),
Expand Down
44 changes: 44 additions & 0 deletions website/docs/d/compute_router_nat.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
subcategory: "Compute Engine"
page_title: "Google: google_compute_router_nat"
description: |-
Get information about a Google Compute Router NAT.
---

# google\_compute\_router\_nat

To get more information about Snapshot, see:

* [API documentation](https://cloud.google.com/compute/docs/reference/rest/v1/routers)
* How-to Guides
* [Official Documentation](https://cloud.google.com/router/docs/)

## Example Usage

```hcl
data "google_compute_router_nat" "foo" {
name = "my-nat"
router = "my-router"
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) Name of the NAT service. The name must be 1-63 characters long and
comply with RFC1035.

* `router` - (Required)
The name of the Cloud Router in which this NAT will be configured.

- - -

* `project` - (Optional) The ID of the project in which the resource belongs.
If it is not provided, the provider project is used.

* `region` - (Optional) Region where the router and NAT reside.

## Attributes Reference

See [google_compute_router_nat](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_router_nat) resource for details of the available attributes.

0 comments on commit 3105cd0

Please sign in to comment.