Skip to content

Commit 5925a10

Browse files
authored
Merge pull request #32226 from joshjluo/f-aws_opensearchserverless_security_policy-data-source
Add aws_opensearchserverless_security_policy data source
2 parents 174ab20 + eaa8ea9 commit 5925a10

File tree

5 files changed

+207
-1
lines changed

5 files changed

+207
-1
lines changed

.changelog/32226.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-data-source
2+
aws_opensearchserverless_security_policy
3+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package opensearchserverless
2+
3+
import (
4+
"context"
5+
"regexp"
6+
"time"
7+
8+
"github.com/aws/aws-sdk-go-v2/aws"
9+
"github.com/aws/aws-sdk-go-v2/service/opensearchserverless/types"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
13+
"github.com/hashicorp/terraform-provider-aws/internal/conns"
14+
"github.com/hashicorp/terraform-provider-aws/internal/enum"
15+
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
16+
)
17+
18+
// @SDKDataSource("aws_opensearchserverless_security_policy")
19+
func DataSourceSecurityPolicy() *schema.Resource {
20+
return &schema.Resource{
21+
ReadWithoutTimeout: dataSourceSecurityPolicyRead,
22+
23+
Schema: map[string]*schema.Schema{
24+
"created_date": {
25+
Type: schema.TypeString,
26+
Computed: true,
27+
},
28+
"description": {
29+
Type: schema.TypeString,
30+
Computed: true,
31+
},
32+
"last_modified_date": {
33+
Type: schema.TypeString,
34+
Computed: true,
35+
},
36+
"name": {
37+
Type: schema.TypeString,
38+
Required: true,
39+
ValidateFunc: validation.All(
40+
validation.StringLenBetween(3, 32),
41+
validation.StringMatch(regexp.MustCompile(`^[a-z][a-z0-9-]+$`), `must start with any lower case letter and can include any lower case letter, number, or "-"`),
42+
),
43+
},
44+
"policy": {
45+
Type: schema.TypeString,
46+
Computed: true,
47+
},
48+
"policy_version": {
49+
Type: schema.TypeString,
50+
Computed: true,
51+
},
52+
"type": {
53+
Type: schema.TypeString,
54+
Required: true,
55+
ValidateDiagFunc: enum.Validate[types.SecurityPolicyType](),
56+
},
57+
},
58+
}
59+
}
60+
61+
func dataSourceSecurityPolicyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
62+
var diags diag.Diagnostics
63+
conn := meta.(*conns.AWSClient).OpenSearchServerlessClient(ctx)
64+
65+
securityPolicyName := d.Get("name").(string)
66+
securityPolicyType := d.Get("type").(string)
67+
securityPolicy, err := FindSecurityPolicyByNameAndType(ctx, conn, securityPolicyName, securityPolicyType)
68+
69+
if err != nil {
70+
return sdkdiag.AppendErrorf(diags, "reading OpenSearch Security Policy with name (%s) and type (%s): %s", securityPolicyName, securityPolicyType, err)
71+
}
72+
73+
policyBytes, err := securityPolicy.Policy.MarshalSmithyDocument()
74+
if err != nil {
75+
return sdkdiag.AppendErrorf(diags, "reading JSON policy document for OpenSearch Security Policy with name %s and type %s: %s", securityPolicyName, securityPolicyType, err)
76+
}
77+
78+
d.SetId(aws.ToString(securityPolicy.Name))
79+
d.Set("description", securityPolicy.Description)
80+
d.Set("name", securityPolicy.Name)
81+
d.Set("policy", string(policyBytes))
82+
d.Set("policy_version", securityPolicy.PolicyVersion)
83+
d.Set("type", securityPolicy.Type)
84+
85+
createdDate := time.UnixMilli(aws.ToInt64(securityPolicy.CreatedDate))
86+
d.Set("created_date", createdDate.Format(time.RFC3339))
87+
88+
lastModifiedDate := time.UnixMilli(aws.ToInt64(securityPolicy.LastModifiedDate))
89+
d.Set("last_modified_date", lastModifiedDate.Format(time.RFC3339))
90+
91+
return diags
92+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package opensearchserverless_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
9+
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
10+
"github.com/hashicorp/terraform-provider-aws/names"
11+
)
12+
13+
func TestAccOpenSearchServerlessSecurityPolicyDataSource_basic(t *testing.T) {
14+
ctx := acctest.Context(t)
15+
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
16+
resourceName := "aws_opensearchserverless_security_policy.test"
17+
dataSourceName := "data.aws_opensearchserverless_security_policy.test"
18+
19+
resource.ParallelTest(t, resource.TestCase{
20+
PreCheck: func() {
21+
acctest.PreCheck(ctx, t)
22+
acctest.PreCheckPartitionHasService(t, names.OpenSearchServerlessEndpointID)
23+
},
24+
ErrorCheck: acctest.ErrorCheck(t, names.OpenSearchServerlessEndpointID),
25+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
26+
CheckDestroy: testAccCheckSecurityPolicyDestroy(ctx),
27+
Steps: []resource.TestStep{
28+
{
29+
Config: testAccSecurityPolicyDataSourceConfig_basic(rName),
30+
Check: resource.ComposeTestCheckFunc(
31+
resource.TestCheckResourceAttrPair(dataSourceName, "name", resourceName, "name"),
32+
resource.TestCheckResourceAttrPair(dataSourceName, "type", resourceName, "type"),
33+
resource.TestCheckResourceAttrPair(dataSourceName, "description", resourceName, "description"),
34+
resource.TestCheckResourceAttrPair(dataSourceName, "policy", resourceName, "policy"),
35+
resource.TestCheckResourceAttrPair(dataSourceName, "policy_version", resourceName, "policy_version"),
36+
resource.TestCheckResourceAttrSet(dataSourceName, "created_date"),
37+
resource.TestCheckResourceAttrSet(dataSourceName, "last_modified_date"),
38+
),
39+
},
40+
},
41+
})
42+
}
43+
44+
func testAccSecurityPolicyDataSourceConfig_basic(rName string) string {
45+
collection := fmt.Sprintf("collection/%s", rName)
46+
return fmt.Sprintf(`
47+
resource "aws_opensearchserverless_security_policy" "test" {
48+
name = %[1]q
49+
type = "encryption"
50+
description = %[1]q
51+
policy = jsonencode({
52+
"Rules" = [
53+
{
54+
"Resource" = [
55+
%[2]q
56+
],
57+
"ResourceType" = "collection"
58+
}
59+
],
60+
"AWSOwnedKey" = true
61+
})
62+
}
63+
64+
data "aws_opensearchserverless_security_policy" "test" {
65+
name = aws_opensearchserverless_security_policy.test.name
66+
type = "encryption"
67+
}
68+
`, rName, collection)
69+
}

internal/service/opensearchserverless/service_package_gen.go

+6-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
subcategory: "OpenSearch Serverless"
3+
layout: "aws"
4+
page_title: "AWS: aws_opensearchserverless_security_policy"
5+
description: |-
6+
Get information on an OpenSearch Serverless Security Policy.
7+
---
8+
9+
# Data Source: aws_opensearchserverless_security_policy
10+
11+
Use this data source to get information about an AWS OpenSearch Serverless Security Policy.
12+
13+
## Example Usage
14+
15+
```terraform
16+
data "aws_opensearchserverless_security_policy" "example" {
17+
name = "example-security-policy"
18+
type = "encryption"
19+
}
20+
```
21+
22+
## Argument Reference
23+
24+
The following arguments are supported:
25+
26+
* `name` - (Required) Name of the policy
27+
* `type` - (Required) Type of security policy. One of `encryption` or `network`.
28+
29+
## Attributes Reference
30+
31+
In addition to all arguments above, the following attributes are exported:
32+
33+
* `created_date` - The date the security policy was created.
34+
* `description` - Description of the security policy.
35+
* `last_modified_date` - The date the security policy was last modified.
36+
* `policy` - The JSON policy document without any whitespaces.
37+
* `policy_version` - Version of the policy.

0 commit comments

Comments
 (0)