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

r/opensearchserverless_access_policy: policy is not set correctly #39322

Merged
merged 5 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions .changelog/39322.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_opensearchserverless_access_policy: Fix incompatible type error when setting `policy`
```
2 changes: 1 addition & 1 deletion internal/framework/flex/auto_flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ func (flattener autoFlattener) interface_(ctx context.Context, vFrom reflect.Val
//
// JSONStringer -> types.String-ish.
//
if vFrom.Type() == reflect.TypeFor[smithyjson.JSONStringer]() {
if vFrom.Type().Implements(reflect.TypeFor[smithyjson.JSONStringer]()) {
tflog.SubsystemInfo(ctx, subsystemName, "Source implements json.JSONStringer")

stringValue := types.StringNull()
Expand Down
2 changes: 1 addition & 1 deletion internal/framework/types/smithy_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (t SmithyJSONType[T]) ValueFromString(ctx context.Context, in basetypes.Str
return SmithyJSONUnknown[T](), diags
}

var data map[string]any
var data any
if err := json.Unmarshal([]byte(in.ValueString()), &data); err != nil {
return SmithyJSONUnknown[T](), diags
}
Expand Down
17 changes: 6 additions & 11 deletions internal/service/opensearchserverless/access_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/opensearchserverless"
"github.com/aws/aws-sdk-go-v2/service/opensearchserverless/document"
awstypes "github.com/aws/aws-sdk-go-v2/service/opensearchserverless/types"
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand Down Expand Up @@ -39,7 +39,7 @@ type resourceAccessPolicyData struct {
Description types.String `tfsdk:"description"`
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Policy jsontypes.Normalized `tfsdk:"policy"`
Policy fwtypes.SmithyJSON[document.Interface] `tfsdk:"policy"`
PolicyVersion types.String `tfsdk:"policy_version"`
Type fwtypes.StringEnum[awstypes.AccessPolicyType] `tfsdk:"type"`
}
Expand Down Expand Up @@ -76,17 +76,14 @@ func (r *resourceAccessPolicy) Schema(ctx context.Context, req resource.SchemaRe
},
},
names.AttrPolicy: schema.StringAttribute{
CustomType: jsontypes.NormalizedType{},
CustomType: fwtypes.NewSmithyJSONType(ctx, document.NewLazyDocument),
Required: true,
Validators: []validator.String{
stringvalidator.LengthBetween(1, 20480),
},
},
"policy_version": schema.StringAttribute{
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
names.AttrType: schema.StringAttribute{
CustomType: fwtypes.StringEnumType[awstypes.AccessPolicyType](),
Expand Down Expand Up @@ -166,7 +163,6 @@ func (r *resourceAccessPolicy) Read(ctx context.Context, req resource.ReadReques
}

resp.Diagnostics.Append(flex.Flatten(ctx, out, &state)...)

if resp.Diagnostics.HasError() {
return
}
Expand All @@ -189,27 +185,26 @@ func (r *resourceAccessPolicy) Update(ctx context.Context, req resource.UpdateRe
input := &opensearchserverless.UpdateAccessPolicyInput{}

resp.Diagnostics.Append(flex.Expand(ctx, plan, input)...)

if resp.Diagnostics.HasError() {
return
}

input.ClientToken = aws.String(id.UniqueId())
input.PolicyVersion = state.PolicyVersion.ValueStringPointer() // use policy version from state since it can be recalculated on update

out, err := conn.UpdateAccessPolicy(ctx, input)

if err != nil {
resp.Diagnostics.AddError(fmt.Sprintf("updating Security Policy (%s)", plan.Name.ValueString()), err.Error())
return
}
resp.Diagnostics.Append(flex.Flatten(ctx, out.AccessPolicyDetail, &state)...)

resp.Diagnostics.Append(flex.Flatten(ctx, out.AccessPolicyDetail, &plan)...)
if resp.Diagnostics.HasError() {
return
}
}

resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}

func (r *resourceAccessPolicy) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
Expand Down
51 changes: 51 additions & 0 deletions internal/service/opensearchserverless/access_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ func TestAccOpenSearchServerlessAccessPolicy_update(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, names.AttrDescription, "description updated"),
),
},
{
Config: testAccAccessPolicyConfig_updatePolicy(rName, "description updated"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAccessPolicyExists(ctx, resourceName, &accesspolicy),
resource.TestCheckResourceAttr(resourceName, names.AttrType, "data"),
resource.TestCheckResourceAttr(resourceName, names.AttrDescription, "description updated"),
resource.TestCheckResourceAttrSet(resourceName, names.AttrPolicy),
),
},
{
ResourceName: resourceName,
ImportStateIdFunc: testAccAccessPolicyImportStateIdFunc(resourceName),
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down Expand Up @@ -266,3 +281,39 @@ resource "aws_opensearchserverless_access_policy" "test" {
}
`, rName, description)
}

func testAccAccessPolicyConfig_updatePolicy(rName, description string) string {
return fmt.Sprintf(`
data "aws_caller_identity" "current" {}
data "aws_partition" "current" {}
resource "aws_opensearchserverless_access_policy" "test" {
name = %[1]q
type = "data"
description = %[2]q
policy = jsonencode([
{
"Rules" : [
{
"ResourceType" : "index",
"Resource" : [
"index/books/*"
],
"Permission" : [
"aoss:CreateIndex",
"aoss:ReadDocument",
"aoss:UpdateIndex",
"aoss:DeleteIndex",
"aoss:WriteDocument"
]
}
],
"Principal" : [
"arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:user/admin",
"arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:user/admin2"
]
}
])
}
`, rName, description)
}
Loading