Skip to content

Commit f9f3cdf

Browse files
compute: data source for google_compute_network_attachment (#14396) (#23570)
[upstream:6ea8fe9784d7b5de37528606a0c8bea078e54060] Signed-off-by: Modular Magician <[email protected]>
1 parent f7ce3c3 commit f9f3cdf

File tree

5 files changed

+360
-0
lines changed

5 files changed

+360
-0
lines changed

.changelog/14396.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-datasource
2+
`google_compute_network_attachment`
3+
```

google/provider/provider_mmv1_resources.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
250250
"google_compute_machine_types": compute.DataSourceGoogleComputeMachineTypes(),
251251
"google_compute_network": compute.DataSourceGoogleComputeNetwork(),
252252
"google_compute_networks": compute.DataSourceGoogleComputeNetworks(),
253+
"google_compute_network_attachment": compute.DataSourceGoogleComputeNetworkAttachment(),
253254
"google_compute_network_endpoint_group": compute.DataSourceGoogleComputeNetworkEndpointGroup(),
254255
"google_compute_network_peering": compute.DataSourceComputeNetworkPeering(),
255256
"google_compute_node_types": compute.DataSourceGoogleComputeNodeTypes(),
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
// ----------------------------------------------------------------------------
4+
//
5+
// *** AUTO GENERATED CODE *** Type: Handwritten ***
6+
//
7+
// ----------------------------------------------------------------------------
8+
//
9+
// This code is generated by Magic Modules using the following:
10+
//
11+
// Source file: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/third_party/terraform/services/compute/data_source_google_compute_network_attachment.go
12+
//
13+
// DO NOT EDIT this file directly. Any changes made to this file will be
14+
// overwritten during the next generation cycle.
15+
//
16+
// ----------------------------------------------------------------------------
17+
package compute
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
23+
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
24+
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
25+
)
26+
27+
func DataSourceGoogleComputeNetworkAttachment() *schema.Resource {
28+
dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourceComputeNetworkAttachment().Schema)
29+
30+
tpgresource.AddRequiredFieldsToSchema(dsSchema, "name", "region")
31+
tpgresource.AddOptionalFieldsToSchema(dsSchema, "project")
32+
33+
return &schema.Resource{
34+
Read: dataSourceComputeNetworkAttachmentRead,
35+
Schema: dsSchema,
36+
}
37+
}
38+
39+
func dataSourceComputeNetworkAttachmentRead(d *schema.ResourceData, meta interface{}) error {
40+
config := meta.(*transport_tpg.Config)
41+
project, err := tpgresource.GetProject(d, config)
42+
if err != nil {
43+
return fmt.Errorf("Error fetching project: %s", err)
44+
}
45+
46+
name := d.Get("name").(string)
47+
region := d.Get("region").(string)
48+
49+
id := fmt.Sprintf("projects/%s/regions/%s/networkAttachments/%s", project, region, name)
50+
d.SetId(id)
51+
52+
err = resourceComputeNetworkAttachmentRead(d, meta)
53+
if err != nil {
54+
return fmt.Errorf("Error reading Network Attachment %q: %s", id, err)
55+
}
56+
57+
// normalize fields to ensure they are in the correct format
58+
// the API returns a full URL here for fields such as `network` and `region` and not just the resource name
59+
if v, ok := d.Get("network").(string); ok && v != "" {
60+
d.Set("network", tpgresource.GetResourceNameFromSelfLink(v))
61+
}
62+
63+
if v, ok := d.Get("region").(string); ok && v != "" {
64+
d.Set("region", tpgresource.GetResourceNameFromSelfLink(v))
65+
}
66+
67+
if v, ok := d.Get("subnetworks").([]interface{}); ok && len(v) > 0 {
68+
var subnetworks []string
69+
for _, s := range v {
70+
subnetworks = append(subnetworks, tpgresource.GetResourceNameFromSelfLink(s.(string)))
71+
}
72+
if err := d.Set("subnetworks", subnetworks); err != nil {
73+
return fmt.Errorf("Error setting subnetworks: %s", err)
74+
}
75+
}
76+
77+
if err := tpgresource.SetDataSourceLabels(d); err != nil {
78+
return err
79+
}
80+
81+
if d.Id() == "" {
82+
return fmt.Errorf("%s not found", id)
83+
}
84+
85+
return nil
86+
}
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
// ----------------------------------------------------------------------------
4+
//
5+
// *** AUTO GENERATED CODE *** Type: Handwritten ***
6+
//
7+
// ----------------------------------------------------------------------------
8+
//
9+
// This code is generated by Magic Modules using the following:
10+
//
11+
// Source file: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/third_party/terraform/services/compute/data_source_google_compute_network_attachment_test.go
12+
//
13+
// DO NOT EDIT this file directly. Any changes made to this file will be
14+
// overwritten during the next generation cycle.
15+
//
16+
// ----------------------------------------------------------------------------
17+
// Copyright (c) HashiCorp, Inc.
18+
// SPDX-License-Identifier: MPL-2.0
19+
package compute_test
20+
21+
import (
22+
"fmt"
23+
"testing"
24+
25+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
26+
"github.com/hashicorp/terraform-provider-google/google/acctest"
27+
"github.com/hashicorp/terraform-provider-google/google/envvar"
28+
)
29+
30+
func TestAccDataSourceComputeNetworkAttachment_basic(t *testing.T) {
31+
t.Parallel()
32+
33+
context := map[string]interface{}{
34+
"random_suffix": acctest.RandString(t, 10),
35+
}
36+
37+
acctest.VcrTest(t, resource.TestCase{
38+
PreCheck: func() { acctest.AccTestPreCheck(t) },
39+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
40+
Steps: []resource.TestStep{
41+
{
42+
Config: testAccComputeNetworkAttachment_basic(context),
43+
Check: resource.ComposeTestCheckFunc(
44+
resource.TestCheckResourceAttr("data.google_compute_network_attachment.default", "name", fmt.Sprintf("tf-test-basic-network-attachment-%s", context["random_suffix"])),
45+
resource.TestCheckResourceAttr("data.google_compute_network_attachment.default", "region", "us-central1"),
46+
resource.TestCheckResourceAttr("data.google_compute_network_attachment.default", "description", "my basic network attachment"),
47+
resource.TestCheckResourceAttr("data.google_compute_network_attachment.default", "connection_preference", "ACCEPT_AUTOMATIC"),
48+
resource.TestCheckResourceAttr("data.google_compute_network_attachment.default", "subnetworks.#", "1"),
49+
),
50+
},
51+
},
52+
})
53+
}
54+
55+
func TestAccDataSourceComputeNetworkAttachment_full(t *testing.T) {
56+
t.Parallel()
57+
58+
context := map[string]interface{}{
59+
"billing_account": envvar.GetTestBillingAccountFromEnv(t),
60+
"org_id": envvar.GetTestOrgFromEnv(t),
61+
"random_suffix": acctest.RandString(t, 10),
62+
}
63+
64+
acctest.VcrTest(t, resource.TestCase{
65+
PreCheck: func() { acctest.AccTestPreCheck(t) },
66+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
67+
Steps: []resource.TestStep{
68+
{
69+
Config: testAccDataSourceComputeNetworkAttachment_full(context),
70+
Check: resource.ComposeTestCheckFunc(
71+
resource.TestCheckResourceAttr("data.google_compute_network_attachment.default", "name", fmt.Sprintf("tf-test-basic-network-attachment-%s", context["random_suffix"])),
72+
resource.TestCheckResourceAttr("data.google_compute_network_attachment.default", "region", "us-central1"),
73+
resource.TestCheckResourceAttr("data.google_compute_network_attachment.default", "description", "basic network attachment description"),
74+
resource.TestCheckResourceAttr("data.google_compute_network_attachment.default", "connection_preference", "ACCEPT_MANUAL"),
75+
resource.TestCheckResourceAttr("data.google_compute_network_attachment.default", "subnetworks.#", "1"),
76+
resource.TestCheckResourceAttr("data.google_compute_network_attachment.default", "subnetworks.0", fmt.Sprintf("tf-test-basic-subnetwork1-%s", context["random_suffix"])),
77+
resource.TestCheckResourceAttr("data.google_compute_network_attachment.default", "producer_accept_lists.#", "2"),
78+
resource.TestCheckResourceAttr("data.google_compute_network_attachment.default", "producer_accept_lists.0", fmt.Sprintf("tf-test-prj-accept1-%s", context["random_suffix"])),
79+
resource.TestCheckResourceAttr("data.google_compute_network_attachment.default", "producer_accept_lists.1", fmt.Sprintf("tf-test-prj-accept2-%s", context["random_suffix"])),
80+
resource.TestCheckResourceAttr("data.google_compute_network_attachment.default", "producer_reject_lists.#", "2"),
81+
resource.TestCheckResourceAttr("data.google_compute_network_attachment.default", "producer_reject_lists.0", fmt.Sprintf("tf-test-prj-reject1-%s", context["random_suffix"])),
82+
resource.TestCheckResourceAttr("data.google_compute_network_attachment.default", "producer_reject_lists.1", fmt.Sprintf("tf-test-prj-reject2-%s", context["random_suffix"])),
83+
),
84+
},
85+
},
86+
})
87+
}
88+
89+
func testAccComputeNetworkAttachment_basic(context map[string]interface{}) string {
90+
return acctest.Nprintf(`
91+
resource "google_compute_network" "default" {
92+
name = "tf-test-basic-network%{random_suffix}"
93+
auto_create_subnetworks = false
94+
}
95+
96+
resource "google_compute_subnetwork" "default" {
97+
name = "tf-test-basic-subnetwork%{random_suffix}"
98+
region = "us-central1"
99+
100+
network = google_compute_network.default.id
101+
ip_cidr_range = "10.0.0.0/16"
102+
}
103+
104+
resource "google_compute_network_attachment" "default" {
105+
name = "tf-test-basic-network-attachment-%{random_suffix}"
106+
region = "us-central1"
107+
description = "my basic network attachment"
108+
109+
subnetworks = [google_compute_subnetwork.default.id]
110+
connection_preference = "ACCEPT_AUTOMATIC"
111+
}
112+
113+
data "google_compute_network_attachment" "default" {
114+
name = google_compute_network_attachment.default.name
115+
region = google_compute_network_attachment.default.region
116+
project = google_compute_network_attachment.default.project
117+
depends_on = [
118+
google_compute_network.default,
119+
google_compute_subnetwork.default,
120+
google_compute_network_attachment.default,
121+
]
122+
}
123+
`, context)
124+
}
125+
126+
func testAccDataSourceComputeNetworkAttachment_full(context map[string]interface{}) string {
127+
return acctest.Nprintf(`
128+
resource "google_compute_network_attachment" "default" {
129+
name = "tf-test-basic-network-attachment-%{random_suffix}"
130+
region = "us-central1"
131+
description = "basic network attachment description"
132+
connection_preference = "ACCEPT_MANUAL"
133+
134+
subnetworks = [
135+
google_compute_subnetwork.net1.self_link
136+
]
137+
138+
producer_accept_lists = [
139+
google_project.accepted_producer_project1.project_id,
140+
google_project.accepted_producer_project2.project_id
141+
]
142+
143+
producer_reject_lists = [
144+
google_project.rejected_producer_project1.project_id,
145+
google_project.rejected_producer_project2.project_id
146+
]
147+
}
148+
149+
resource "google_compute_network" "default" {
150+
name = "tf-test-basic-network-%{random_suffix}"
151+
auto_create_subnetworks = false
152+
}
153+
154+
resource "google_compute_subnetwork" "net1" {
155+
name = "tf-test-basic-subnetwork1-%{random_suffix}"
156+
region = "us-central1"
157+
158+
network = google_compute_network.default.id
159+
ip_cidr_range = "10.0.0.0/16"
160+
}
161+
162+
resource "google_compute_subnetwork" "net2" {
163+
name = "tf-test-basic-subnetwork2-%{random_suffix}"
164+
region = "us-central1"
165+
166+
network = google_compute_network.default.id
167+
ip_cidr_range = "10.1.0.0/16"
168+
}
169+
170+
resource "google_project" "rejected_producer_project1" {
171+
project_id = "tf-test-prj-reject1-%{random_suffix}"
172+
name = "tf-test-prj-reject1-%{random_suffix}"
173+
org_id = "%{org_id}"
174+
billing_account = "%{billing_account}"
175+
deletion_policy = "DELETE"
176+
}
177+
178+
resource "google_project" "rejected_producer_project2" {
179+
project_id = "tf-test-prj-reject2-%{random_suffix}"
180+
name = "tf-test-prj-reject2-%{random_suffix}"
181+
org_id = "%{org_id}"
182+
billing_account = "%{billing_account}"
183+
deletion_policy = "DELETE"
184+
}
185+
186+
resource "google_project" "accepted_producer_project1" {
187+
project_id = "tf-test-prj-accept1-%{random_suffix}"
188+
name = "tf-test-prj-accept1-%{random_suffix}"
189+
org_id = "%{org_id}"
190+
billing_account = "%{billing_account}"
191+
deletion_policy = "DELETE"
192+
}
193+
194+
resource "google_project" "accepted_producer_project2" {
195+
project_id = "tf-test-prj-accept2-%{random_suffix}"
196+
name = "tf-test-prj-accept2-%{random_suffix}"
197+
org_id = "%{org_id}"
198+
billing_account = "%{billing_account}"
199+
deletion_policy = "DELETE"
200+
}
201+
202+
data "google_compute_network_attachment" "default" {
203+
name = google_compute_network_attachment.default.name
204+
region = google_compute_network_attachment.default.region
205+
project = google_compute_network_attachment.default.project
206+
depends_on = [
207+
google_compute_network_attachment.default,
208+
google_compute_network.default,
209+
google_compute_subnetwork.net1,
210+
google_compute_subnetwork.net2,
211+
google_project.accepted_producer_project1,
212+
google_project.accepted_producer_project2,
213+
google_project.rejected_producer_project1,
214+
google_project.rejected_producer_project2,
215+
]
216+
}
217+
`, context)
218+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
# ----------------------------------------------------------------------------
3+
#
4+
# *** AUTO GENERATED CODE *** Type: Handwritten ***
5+
#
6+
# ----------------------------------------------------------------------------
7+
#
8+
# This code is generated by Magic Modules using the following:
9+
#
10+
# Source file: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/third_party/terraform/website/docs/d/compute_network_attachment.html.markdown
11+
#
12+
# DO NOT EDIT this file directly. Any changes made to this file will be
13+
# overwritten during the next generation cycle.
14+
#
15+
# ----------------------------------------------------------------------------
16+
subcategory: "Compute Engine"
17+
description: |-
18+
A data source to retrieve a network attachment
19+
---
20+
21+
# `google_compute_network_attachment`
22+
23+
Get a specific network attachment within a region. For more information see
24+
the [official documentation](https://cloud.google.com/vpc/docs/about-network-attachments)
25+
and [API](https://cloud.google.com/compute/docs/reference/rest/v1/networkAttachments/get).
26+
27+
## Example Usage
28+
29+
```hcl
30+
data "google_compute_network_attachment" "default" {
31+
project = "my-project"
32+
name = "my-network-attachment"
33+
region = "europe-west1"
34+
}
35+
```
36+
37+
## Argument Reference
38+
39+
The following arguments are supported:
40+
41+
* `name` - (Required) The name of the network attachment to retrieve.
42+
The name must be unique within the region.
43+
44+
* `region` - (Required) The region in which the network attachment resides.
45+
For example, `europe-west1`.
46+
47+
* `project` - (Optional) The ID of the project in which the resource belongs.
48+
If it is not provided, the provider project is used.
49+
50+
## Attributes Reference
51+
52+
See [google_compute_network_attachment](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/bigquery_table#attributes-reference) resource for details of the available attributes.

0 commit comments

Comments
 (0)