Skip to content
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

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API has createdDate and lastModifiedDate 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

Copy link
Contributor Author

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?

Skips Timestamp Attributes: Generally, creation and modification dates from the API should be omitted from the schema.

Cite: https://hashicorp.github.io/terraform-provider-aws/raising-a-pull-request/#resource-contribution-guidelines

Copy link
Contributor

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

"description": {
Type: schema.TypeString,
Computed: true,
},
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to provide validation on both length and patterns.

Type: schema.TypeString,
Required: true,
},
"policy": {
Type: schema.TypeString,
Computed: true,
},
"policy_version": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API has two allowed values (encryption or network), and those values are defined in the AWS SDK. Can you please add validation as such?

You can see an example here:
https://github.com/hashicorp/terraform-provider-aws/blob/8f461fec79149dc1031ee843250ead0da9030483/internal/service/opensearchserverless/security_policy.go#L86C12-L86C12

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added validation for the type

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()
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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:

policyBytes, err := out.Policy.MarshalSmithyDocument()

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use spaces (not tabs) in the embedded Terraform/HCL string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.