Skip to content

Commit

Permalink
Adding google_compute_network_peering datasource (#7051) (#13476)
Browse files Browse the repository at this point in the history
* Adding google_compute_network_peering datasource

* Removing unused diffSuppress

Signed-off-by: Modular Magician <magic-modules@google.com>

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Jan 12, 2023
1 parent 3105cd0 commit 82fee5e
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/7051.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-datasource
`google_compute_network_peering`
```
38 changes: 38 additions & 0 deletions google/data_source_compute_network_peering.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package google

import (
"fmt"
"time"

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

const regexGCEName = "^(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)$"

func dataSourceComputeNetworkPeering() *schema.Resource {

dsSchema := datasourceSchemaFromResourceSchema(resourceComputeNetworkPeering().Schema)
addRequiredFieldsToSchema(dsSchema, "name", "network")

dsSchema["name"].ValidateFunc = validateRegexp(regexGCEName)
dsSchema["network"].ValidateFunc = validateRegexp(peerNetworkLinkRegex)
return &schema.Resource{
Read: dataSourceComputeNetworkPeeringRead,
Schema: dsSchema,
Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(4 * time.Minute),
},
}
}

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

networkFieldValue, err := ParseNetworkFieldValue(d.Get("network").(string), d, config)
if err != nil {
return err
}
d.SetId(fmt.Sprintf("%s/%s", networkFieldValue.Name, d.Get("name").(string)))

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

import (
"testing"

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

func TestAccDataSourceComputeNetworkPeering_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: testAccComputeNetworkPeeringDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccDataSourceComputeNetworkPeering_basic(context),
Check: resource.ComposeTestCheckFunc(
checkDataSourceStateMatchesResourceState("data.google_compute_network_peering.peering1_ds", "google_compute_network_peering.peering1"),
),
},
},
})
}

func testAccDataSourceComputeNetworkPeering_basic(context map[string]interface{}) string {
return Nprintf(`
resource "google_compute_network_peering" "peering1" {
name = "peering1-%{random_suffix}"
network = google_compute_network.default.self_link
peer_network = google_compute_network.other.self_link
}
resource "google_compute_network_peering" "peering2" {
name = "peering2-%{random_suffix}"
network = google_compute_network.other.self_link
peer_network = google_compute_network.default.self_link
}
resource "google_compute_network" "default" {
name = "foobar-%{random_suffix}"
auto_create_subnetworks = "false"
}
resource "google_compute_network" "other" {
name = "other-%{random_suffix}"
auto_create_subnetworks = "false"
}
data "google_compute_network_peering" "peering1_ds" {
name = google_compute_network_peering.peering1.name
network = google_compute_network_peering.peering1.network
}
`, context)
}
1 change: 1 addition & 0 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,7 @@ func Provider() *schema.Provider {
"google_compute_lb_ip_ranges": dataSourceGoogleComputeLbIpRanges(),
"google_compute_network": dataSourceGoogleComputeNetwork(),
"google_compute_network_endpoint_group": dataSourceGoogleComputeNetworkEndpointGroup(),
"google_compute_network_peering": dataSourceComputeNetworkPeering(),
"google_compute_node_types": dataSourceGoogleComputeNodeTypes(),
"google_compute_regions": dataSourceGoogleComputeRegions(),
"google_compute_region_network_endpoint_group": dataSourceGoogleComputeRegionNetworkEndpointGroup(),
Expand Down
62 changes: 62 additions & 0 deletions website/docs/d/compute_network_peering.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
subcategory: "Compute Engine"
page_title: "Google: google_compute_network_peering"
description: |-
Get information of a specified compute network peering.
---

# google\_compute\_network\_peering

Get information of a specified compute network peering. For more information see
[the official documentation](https://cloud.google.com/compute/docs/vpc/vpc-peering)
and
[API](https://cloud.google.com/compute/docs/reference/latest/networks).

## Example Usage

```hcl
resource "google_compute_network_peering" "peering1" {
name = "peering1"
network = google_compute_network.default.self_link
peer_network = google_compute_network.other.self_link
}
resource "google_compute_network_peering" "peering2" {
name = "peering2"
network = google_compute_network.other.self_link
peer_network = google_compute_network.default.self_link
}
resource "google_compute_network" "default" {
name = "foobar"
auto_create_subnetworks = "false"
}
resource "google_compute_network" "other" {
name = "other"
auto_create_subnetworks = "false"
}
data "google_compute_network_peering" "peering1_ds" {
name = google_compute_network_peering.peering1.name
network = google_compute_network_peering.peering1.network
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) Name of the peering.

* `network` - (Required) The primary network of the peering.

## Attributes Reference

See [google_compute_network_peering](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_network_peering#argument-reference) resource for details of the available attributes.

## Timeouts

This datasource provides the following
[Timeouts](/docs/configuration/resources.html#timeouts) configuration options:

- `read` - Default is 4 minutes.

0 comments on commit 82fee5e

Please sign in to comment.