-
Notifications
You must be signed in to change notification settings - Fork 9.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add aws_opensearchserverless_security_policy data source #32226
Changes from 1 commit
322977a
304e3fb
c39a199
3f05e64
146a72d
eaa8ea9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,72 @@ | ||||
package opensearchserverless | ||||
|
||||
import ( | ||||
"context" | ||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws" | ||||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||||
"github.com/hashicorp/terraform-provider-aws/internal/conns" | ||||
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" | ||||
) | ||||
|
||||
// @SDKDataSource("aws_opensearchserverless_security_policy") | ||||
func DataSourceSecurityPolicy() *schema.Resource { | ||||
return &schema.Resource{ | ||||
ReadWithoutTimeout: dataSourceSecurityPolicyRead, | ||||
|
||||
Schema: map[string]*schema.Schema{ | ||||
"description": { | ||||
Type: schema.TypeString, | ||||
Computed: true, | ||||
}, | ||||
"id": { | ||||
Type: schema.TypeString, | ||||
Computed: true, | ||||
}, | ||||
"name": { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The API documentation has stated You can see an example here: https://github.com/hashicorp/terraform-provider-aws/blob/8f461fec79149dc1031ee843250ead0da9030483/internal/service/opensearchserverless/collection.go#L104C8-L104C8 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated to provide validation on both |
||||
Type: schema.TypeString, | ||||
Required: true, | ||||
}, | ||||
"policy": { | ||||
Type: schema.TypeString, | ||||
Computed: true, | ||||
}, | ||||
"policy_version": { | ||||
Type: schema.TypeString, | ||||
Computed: true, | ||||
}, | ||||
"type": { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The API has two allowed values ( You can see an example here: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added validation for the |
||||
Type: schema.TypeString, | ||||
Required: true, | ||||
}, | ||||
}, | ||||
} | ||||
} | ||||
|
||||
func dataSourceSecurityPolicyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||||
var diags diag.Diagnostics | ||||
conn := meta.(*conns.AWSClient).OpenSearchServerlessClient(ctx) | ||||
|
||||
securityPolicyName := d.Get("name").(string) | ||||
securityPolicyType := d.Get("type").(string) | ||||
securityPolicy, err := FindSecurityPolicyByNameAndType(ctx, conn, securityPolicyName, securityPolicyType) | ||||
|
||||
if err != nil { | ||||
return sdkdiag.AppendErrorf(diags, "reading SecurityPolicy with name (%s) and type (%s): %s", securityPolicyName, securityPolicyType, err) | ||||
} | ||||
|
||||
policyBytes, err := securityPolicy.Policy.MarshalSmithyDocument() | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jar-b have you seen this method before? I have not seen Smith document marshaling prior, to want to verify. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the OpenSearch Serverless Access Policy was just merged today and uses a similar pattern: terraform-provider-aws/internal/service/opensearchserverless/access_policy_data_source.go Line 88 in 2402e64
|
||||
if err != nil { | ||||
return sdkdiag.AppendErrorf(diags, "reading JSON policy document for SecurityPolicy with name %s and type %s: %s", securityPolicyName, securityPolicyType, err) | ||||
} | ||||
|
||||
d.SetId(aws.ToString(securityPolicy.Name)) | ||||
d.Set("description", securityPolicy.Description) | ||||
d.Set("name", securityPolicy.Name) | ||||
d.Set("policy", string(policyBytes)) | ||||
d.Set("policy_version", securityPolicy.PolicyVersion) | ||||
d.Set("type", securityPolicy.Type) | ||||
|
||||
return diags | ||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package opensearchserverless_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-provider-aws/internal/acctest" | ||
"github.com/hashicorp/terraform-provider-aws/names" | ||
) | ||
|
||
func TestAccOpenSearchServerlessSecurityPolicyDataSource_basic(t *testing.T) { | ||
ctx := acctest.Context(t) | ||
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) | ||
resourceName := "aws_opensearchserverless_security_policy.test" | ||
dataSourceName := "data.aws_opensearchserverless_security_policy.test" | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
acctest.PreCheck(ctx, t) | ||
acctest.PreCheckPartitionHasService(t, names.OpenSearchServerlessEndpointID) | ||
}, | ||
ErrorCheck: acctest.ErrorCheck(t, names.OpenSearchServerlessEndpointID), | ||
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccSecurityPolicyDataSourceConfig_basic(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrPair(dataSourceName, "name", resourceName, "name"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "type", resourceName, "type"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "description", resourceName, "description"), | ||
resource.TestCheckResourceAttrPair(dataSourceName, "policy", resourceName, "policy"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccSecurityPolicyDataSourceConfig_basic(rName string) string { | ||
collection := fmt.Sprintf("collection/%s", rName) | ||
return fmt.Sprintf(` | ||
resource "aws_opensearchserverless_security_policy" "test" { | ||
name = %[1]q | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use spaces (not tabs) in the embedded Terraform/HCL string. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated to use spaces |
||
type = "encryption" | ||
description = %[1]q | ||
policy = jsonencode({ | ||
"Rules" = [ | ||
{ | ||
"Resource" = [ | ||
%[2]q | ||
], | ||
"ResourceType" = "collection" | ||
} | ||
], | ||
"AWSOwnedKey" = true | ||
}) | ||
} | ||
|
||
data "aws_opensearchserverless_security_policy" "test" { | ||
name = aws_opensearchserverless_security_policy.test.name | ||
type = "encryption" | ||
} | ||
`, rName, collection) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
subcategory: "OpenSearch Serverless" | ||
layout: "aws" | ||
page_title: "AWS: aws_opensearchserverless_security_policy" | ||
description: |- | ||
Get information on an OpenSearch Serverless Security Policy. | ||
--- | ||
|
||
# Data Source: aws_opensearchserverless_security_policy | ||
|
||
Use this data source to get information about an AWS OpenSearch Serverless Security Policy. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
data "aws_opensearchserverless_security_policy" "example" { | ||
name = "example-security-policy" | ||
type = "encryption" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `name` - (Required) Name of the policy | ||
* `type` - (Required) Type of security policy. One of `encryption` or `network`. | ||
|
||
## Attributes Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `description` - Description of the security policy. | ||
* `policy` - The JSON policy document without any whitespaces. | ||
* `policy_version` - Version of the policy. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The API has
createdDate
andlastModifiedDate
in the response elements. What were your thoughts on omitting these from the DS?Cite: https://docs.aws.amazon.com/opensearch-service/latest/ServerlessAPIReference/API_GetSecurityPolicy.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Resource Contribution Guidelines suggested that timestamps should be skipped. Do you think they should still be included here?
Cite: https://hashicorp.github.io/terraform-provider-aws/raising-a-pull-request/#resource-contribution-guidelines
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joshjluo we do avoid timestamps in resources because of the high potential to cause continuous drift, but they can be included in data sources