Skip to content

Commit d298e62

Browse files
authored
Add datasource for google_compute_region_security_policy (#15902)
1 parent 66dac1e commit d298e62

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed

mmv1/third_party/terraform/provider/provider_mmv1_resources.go.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
127127
"google_compute_region_instance_group_manager": compute.DataSourceGoogleComputeRegionInstanceGroupManager(),
128128
"google_compute_region_instance_template": compute.DataSourceGoogleComputeRegionInstanceTemplate(),
129129
"google_compute_region_network_endpoint_group": compute.DataSourceGoogleComputeRegionNetworkEndpointGroup(),
130+
"google_compute_region_security_policy": compute.DataSourceGoogleComputeRegionSecurityPolicy(),
130131
"google_compute_region_ssl_certificate": compute.DataSourceGoogleRegionComputeSslCertificate(),
131132
"google_compute_region_ssl_policy": compute.DataSourceGoogleRegionComputeSslPolicy(),
132133
"google_compute_reservation": compute.DataSourceGoogleComputeReservation(),
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package compute
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
8+
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
9+
)
10+
11+
func DataSourceGoogleComputeRegionSecurityPolicy() *schema.Resource {
12+
// Generate datasource schema from resource
13+
dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourceComputeRegionSecurityPolicy().Schema)
14+
15+
// Set 'Required' schema elements
16+
tpgresource.AddRequiredFieldsToSchema(dsSchema, "name")
17+
// Set 'Optional' schema elements
18+
tpgresource.AddOptionalFieldsToSchema(dsSchema, "project", "region")
19+
20+
return &schema.Resource{
21+
Read: dataSourceGoogleComputeRegionSecurityPolicyRead,
22+
Schema: dsSchema,
23+
}
24+
}
25+
26+
func dataSourceGoogleComputeRegionSecurityPolicyRead(d *schema.ResourceData, meta interface{}) error {
27+
id, err := tpgresource.ReplaceVars(d, meta.(*transport_tpg.Config), "projects/{{project}}/regions/{{region}}/securityPolicies/{{name}}")
28+
if err != nil {
29+
return fmt.Errorf("Error constructing id: %s", err)
30+
}
31+
d.SetId(id)
32+
33+
err = resourceComputeRegionSecurityPolicyRead(d, meta)
34+
if err != nil {
35+
return err
36+
}
37+
38+
if err := tpgresource.SetDataSourceLabels(d); err != nil {
39+
return err
40+
}
41+
42+
if d.Id() == "" {
43+
return fmt.Errorf("%s not found", id)
44+
}
45+
return nil
46+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package compute_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
7+
"github.com/hashicorp/terraform-provider-google/google/acctest"
8+
)
9+
10+
func TestAccComputeRegionSecurityPolicyDatasource(t *testing.T) {
11+
t.Parallel()
12+
13+
context := map[string]interface{}{
14+
"random_suffix": acctest.RandString(t, 10),
15+
}
16+
17+
acctest.VcrTest(t, resource.TestCase{
18+
PreCheck: func() { acctest.AccTestPreCheck(t) },
19+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
20+
Steps: []resource.TestStep{
21+
{
22+
Config: testAccComputeRegionSecurityPolicyDatasourceConfig(context),
23+
Check: resource.ComposeTestCheckFunc(
24+
acctest.CheckDataSourceStateMatchesResourceState("data.google_compute_region_security_policy.default", "google_compute_region_security_policy.default"),
25+
),
26+
},
27+
},
28+
})
29+
}
30+
31+
func testAccComputeRegionSecurityPolicyDatasourceConfig(context map[string]interface{}) string {
32+
return acctest.Nprintf(`
33+
resource "google_compute_region_security_policy" "default" {
34+
name = "tf-test-region-sec-policy-%{random_suffix}"
35+
region = "us-west2"
36+
description = "basic region security policy"
37+
type = "CLOUD_ARMOR"
38+
}
39+
40+
data "google_compute_region_security_policy" "default" {
41+
name = google_compute_region_security_policy.default.name
42+
region = "us-west2"
43+
}
44+
`, context)
45+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
subcategory: "Compute Engine"
3+
description: |-
4+
Fetches the details of a Compute Region Security Policy.
5+
---
6+
7+
# google_compute_region_security_policy
8+
9+
Use this data source to get information about a Compute Region Security Policy. For more details, see the [API documentation](https://cloud.google.com/compute/docs/reference/rest/v1/regionSecurityPolicies).
10+
11+
## Example Usage
12+
13+
```hcl
14+
data "google_compute_region_security_policy" "default" {
15+
name = "my-region-security-policy"
16+
region = "us-west2"
17+
}
18+
```
19+
20+
## Argument Reference
21+
22+
The following arguments are supported:
23+
24+
* `name` -
25+
(Required)
26+
The name of the Region Security Policy.
27+
28+
* `region` -
29+
(Optional)
30+
The region in which the Region Security Policy resides. If not specified, the provider region is used.
31+
32+
* `project` -
33+
(Optional)
34+
The ID of the project in which the resource belongs. If it is not provided, the provider project is used.
35+
36+
## Attributes Reference
37+
38+
See [google_compute_region_security_policy](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_region_security_policy) resource for details of all the available attributes.

0 commit comments

Comments
 (0)