Skip to content

Commit 09d9a11

Browse files
add: support to google_network_security_address_groups datasource (#16697) (#11773)
[upstream:f1f4d8d64e98b25ab0826c0591e1bfd39a79d2bc] Signed-off-by: Modular Magician <[email protected]>
1 parent 272b27f commit 09d9a11

File tree

5 files changed

+285
-0
lines changed

5 files changed

+285
-0
lines changed

.changelog/16697.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_network_security_address_groups`
3+
```

google-beta/provider/provider_mmv1_resources.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
498498
"google_compute_region_backend_service": compute.DataSourceGoogleComputeRegionBackendService(),
499499
"google_network_management_connectivity_test_run": networkmanagement.DataSourceGoogleNetworkManagementTestRun(),
500500
"google_network_management_connectivity_tests": networkmanagement.DataSourceGoogleNetworkManagementConnectivityTests(),
501+
"google_network_security_address_groups": networksecurity.DataSourceNetworkSecurityAddressGroups(),
501502
// ####### END handwritten datasources ###########
502503
}
503504

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
// Copyright IBM Corp. 2014, 2026
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/networksecurity/data_source_google_network_security_address_groups.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 networksecurity
18+
19+
import (
20+
"fmt"
21+
"log"
22+
23+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
24+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
25+
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
26+
)
27+
28+
func DataSourceNetworkSecurityAddressGroups() *schema.Resource {
29+
return &schema.Resource{
30+
Read: dataSourceNetworkSecurityAddressGroups,
31+
32+
Schema: map[string]*schema.Schema{
33+
"project": {
34+
Type: schema.TypeString,
35+
Optional: true,
36+
Computed: true,
37+
},
38+
"location": {
39+
Type: schema.TypeString,
40+
Required: true,
41+
},
42+
"address_groups": {
43+
Type: schema.TypeList,
44+
Computed: true,
45+
Elem: &schema.Resource{
46+
Schema: map[string]*schema.Schema{
47+
"name": {
48+
Type: schema.TypeString,
49+
Computed: true,
50+
},
51+
"location": {
52+
Type: schema.TypeString,
53+
Computed: true,
54+
},
55+
"capacity": {
56+
Type: schema.TypeInt,
57+
Computed: true,
58+
},
59+
"items": {
60+
Type: schema.TypeList,
61+
Computed: true,
62+
Elem: &schema.Schema{Type: schema.TypeString},
63+
},
64+
},
65+
},
66+
},
67+
},
68+
}
69+
}
70+
71+
func dataSourceNetworkSecurityAddressGroups(d *schema.ResourceData, meta interface{}) error {
72+
config := meta.(*transport_tpg.Config)
73+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
74+
if err != nil {
75+
return err
76+
}
77+
78+
project, err := tpgresource.GetProject(d, config)
79+
if err != nil {
80+
return err
81+
}
82+
location := d.Get("location").(string)
83+
84+
url, err := tpgresource.ReplaceVars(d, config, "{{NetworkSecurityBasePath}}projects/{{project}}/locations/{{location}}/addressGroups")
85+
if err != nil {
86+
return err
87+
}
88+
89+
var allAddressGroups []interface{}
90+
91+
for {
92+
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
93+
Config: config,
94+
Method: "GET",
95+
Project: project,
96+
RawURL: url,
97+
UserAgent: userAgent,
98+
})
99+
if err != nil {
100+
return fmt.Errorf("error listing Address Groups: %s", err)
101+
}
102+
103+
if v, ok := res["addressGroups"]; ok {
104+
allAddressGroups = append(allAddressGroups, v.([]interface{})...)
105+
}
106+
107+
if nextPageToken, ok := res["nextPageToken"]; ok && nextPageToken.(string) != "" {
108+
url, err = transport_tpg.AddQueryParams(url, map[string]string{"pageToken": nextPageToken.(string)})
109+
if err != nil {
110+
return err
111+
}
112+
} else {
113+
break
114+
}
115+
}
116+
117+
if len(allAddressGroups) == 0 {
118+
log.Printf("[DEBUG] No Address Groups found for project %s in location %s", project, location)
119+
}
120+
121+
flattenedGroups := make([]map[string]interface{}, 0, len(allAddressGroups))
122+
for _, groupRaw := range allAddressGroups {
123+
group := groupRaw.(map[string]interface{})
124+
125+
flatGroup := map[string]interface{}{
126+
"name": group["name"],
127+
"location": location,
128+
"capacity": group["capacity"],
129+
"items": group["items"],
130+
}
131+
flattenedGroups = append(flattenedGroups, flatGroup)
132+
}
133+
134+
if err := d.Set("address_groups", flattenedGroups); err != nil {
135+
return fmt.Errorf("error setting address_groups state: %s", err)
136+
}
137+
138+
d.SetId(fmt.Sprintf("projects/%s/locations/%s", project, location))
139+
140+
return nil
141+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright IBM Corp. 2014, 2026
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/networksecurity/data_source_google_network_security_address_groups_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+
package networksecurity_test
18+
19+
import (
20+
"testing"
21+
22+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
23+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest"
24+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/envvar"
25+
)
26+
27+
func TestAccDataSourceNetworkSecurityAddressGroups_basic(t *testing.T) {
28+
t.Parallel()
29+
30+
context := map[string]interface{}{
31+
"random_suffix": acctest.RandString(t, 10),
32+
"project": envvar.GetTestProjectFromEnv(),
33+
"location": "us-central1",
34+
}
35+
36+
acctest.VcrTest(t, resource.TestCase{
37+
PreCheck: func() { acctest.AccTestPreCheck(t) },
38+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t),
39+
Steps: []resource.TestStep{
40+
{
41+
Config: testAccDataSourceNetworkSecurityAddressGroupsConfig(context),
42+
Check: resource.ComposeTestCheckFunc(
43+
resource.TestCheckResourceAttrSet("data.google_network_security_address_groups.all", "address_groups.#"),
44+
resource.TestCheckResourceAttr("data.google_network_security_address_groups.all", "address_groups.0.location", context["location"].(string)),
45+
),
46+
},
47+
},
48+
})
49+
}
50+
51+
func testAccDataSourceNetworkSecurityAddressGroupsConfig(context map[string]interface{}) string {
52+
return acctest.Nprintf(`
53+
provider "google-beta" {
54+
region = "%{location}"
55+
}
56+
57+
resource "google_network_security_address_group" "basic" {
58+
provider = google-beta
59+
name = "tf-test-ag-%{random_suffix}"
60+
parent = "projects/%{project}"
61+
location = "%{location}"
62+
type = "IPV4"
63+
capacity = 100
64+
items = ["208.80.154.224/32"]
65+
}
66+
67+
data "google_network_security_address_groups" "all" {
68+
provider = google-beta
69+
project = "%{project}"
70+
location = "%{location}"
71+
depends_on = [google_network_security_address_group.basic]
72+
}
73+
`, context)
74+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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/network_security_address_groups.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: "Network Security"
17+
description: |-
18+
AddressGroups are used to group IP addresses together for use in firewall policies.
19+
This data source allows you to list address groups in a project and location.
20+
---
21+
22+
# google_network_security_address_groups
23+
24+
AddressGroups are used to group IP addresses together for use in firewall policies. This data source allows you to list address groups in a project and location.
25+
26+
To get more information about Address Groups, see:
27+
28+
* [API documentation](https://cloud.google.com/compute/docs/reference/rest/beta/networkFirewallPolicies)
29+
* How-to Guides
30+
* [Official Documentation](https://cloud.google.com/firewall/docs/about-address-groups)
31+
32+
## Example Usage
33+
34+
```hcl
35+
data "google_network_security_address_groups" "all" {
36+
location = "us-central1"
37+
project = "my-project-id"
38+
}
39+
```
40+
41+
## Argument Reference
42+
43+
The following arguments are supported:
44+
45+
* `location` - (Required) The location of the Address Group.
46+
47+
* `project` - (Optional) The ID of the project.
48+
49+
## Attributes Reference
50+
51+
In addition to the arguments listed above, the following computed attributes are exported:
52+
53+
* `address_groups` - A list of Address Groups in the selected project and location. Structure is [defined below](#nested_address_groups).
54+
55+
<a name="nested_address_groups"></a>The `address_groups` block supports:
56+
57+
* `name` -
58+
The name of the Address Group.
59+
60+
* `description` -
61+
The description of the Address Group.
62+
63+
* `addresses` -
64+
A list of IP addresses in the Address Group.
65+
66+
* `project` - The ID of the project in which the resource belongs.

0 commit comments

Comments
 (0)