Skip to content

Commit 304e3fb

Browse files
author
Joshua Luo
committed
Add aws_opensearchserverless_security_policy data source
1 parent 322977a commit 304e3fb

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
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+
```

internal/service/opensearchserverless/security_policy_data_source.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ package opensearchserverless
22

33
import (
44
"context"
5+
"regexp"
56

67
"github.com/aws/aws-sdk-go-v2/aws"
8+
"github.com/aws/aws-sdk-go-v2/service/opensearchserverless/types"
79
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
810
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
912
"github.com/hashicorp/terraform-provider-aws/internal/conns"
13+
"github.com/hashicorp/terraform-provider-aws/internal/enum"
1014
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
1115
)
1216

@@ -27,6 +31,10 @@ func DataSourceSecurityPolicy() *schema.Resource {
2731
"name": {
2832
Type: schema.TypeString,
2933
Required: true,
34+
ValidateFunc: validation.All(
35+
validation.StringLenBetween(3, 32),
36+
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 "-"`),
37+
),
3038
},
3139
"policy": {
3240
Type: schema.TypeString,
@@ -37,8 +45,9 @@ func DataSourceSecurityPolicy() *schema.Resource {
3745
Computed: true,
3846
},
3947
"type": {
40-
Type: schema.TypeString,
41-
Required: true,
48+
Type: schema.TypeString,
49+
Required: true,
50+
ValidateDiagFunc: enum.Validate[types.SecurityPolicyType](),
4251
},
4352
},
4453
}

internal/service/opensearchserverless/security_policy_data_source_test.go

+16-14
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func TestAccOpenSearchServerlessSecurityPolicyDataSource_basic(t *testing.T) {
2323
},
2424
ErrorCheck: acctest.ErrorCheck(t, names.OpenSearchServerlessEndpointID),
2525
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
26+
CheckDestroy: testAccCheckSecurityPolicyDestroy(ctx),
2627
Steps: []resource.TestStep{
2728
{
2829
Config: testAccSecurityPolicyDataSourceConfig_basic(rName),
@@ -31,6 +32,7 @@ func TestAccOpenSearchServerlessSecurityPolicyDataSource_basic(t *testing.T) {
3132
resource.TestCheckResourceAttrPair(dataSourceName, "type", resourceName, "type"),
3233
resource.TestCheckResourceAttrPair(dataSourceName, "description", resourceName, "description"),
3334
resource.TestCheckResourceAttrPair(dataSourceName, "policy", resourceName, "policy"),
35+
resource.TestCheckResourceAttrPair(dataSourceName, "policy_version", resourceName, "policy_version"),
3436
),
3537
},
3638
},
@@ -41,20 +43,20 @@ func testAccSecurityPolicyDataSourceConfig_basic(rName string) string {
4143
collection := fmt.Sprintf("collection/%s", rName)
4244
return fmt.Sprintf(`
4345
resource "aws_opensearchserverless_security_policy" "test" {
44-
name = %[1]q
45-
type = "encryption"
46-
description = %[1]q
47-
policy = jsonencode({
48-
"Rules" = [
49-
{
50-
"Resource" = [
51-
%[2]q
52-
],
53-
"ResourceType" = "collection"
54-
}
55-
],
56-
"AWSOwnedKey" = true
57-
})
46+
name = %[1]q
47+
type = "encryption"
48+
description = %[1]q
49+
policy = jsonencode({
50+
"Rules" = [
51+
{
52+
"Resource" = [
53+
%[2]q
54+
],
55+
"ResourceType" = "collection"
56+
}
57+
],
58+
"AWSOwnedKey" = true
59+
})
5860
}
5961
6062
data "aws_opensearchserverless_security_policy" "test" {

0 commit comments

Comments
 (0)