Skip to content

Commit 3fcffa7

Browse files
authored
Merge pull request #42663 from stefanfreitag/f-aws_verifiedpermissions_policy_store-add-tag-support
feat: add tags implementation
2 parents 31d0265 + a80101b commit 3fcffa7

10 files changed

+351
-72
lines changed

.changelog/42663.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:enhancement
2+
resource/aws_verifiedpermissions_policy_store: Add `tags` argument and `tags_all` attribute. This functionality requires the `verifiedpermissions:ListTagsForResource`, `verifiedpermissions:TagResource`, and `verifiedpermissions:UntagResource` IAM permissions
3+
```
4+
5+
```release-note:enhancement
6+
data-source/aws_verifiedpermissions_policy_store: Add `tags` attribute. This functionality requires the `verifiedpermissions:ListTagsForResource` IAM permission
7+
```

internal/service/verifiedpermissions/generate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) HashiCorp, Inc.
22
// SPDX-License-Identifier: MPL-2.0
33

4+
//go:generate go run ../../generate/tags/main.go -ListTags -KVTValues=true -ServiceTagsMap -UpdateTags
45
//go:generate go run ../../generate/servicepackage/main.go
56
// ONLY generate directives and package declaration! Do not add anything else to this file.
67

internal/service/verifiedpermissions/policy_store.go

Lines changed: 54 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,29 @@ import (
1010
"github.com/aws/aws-sdk-go-v2/service/verifiedpermissions"
1111
awstypes "github.com/aws/aws-sdk-go-v2/service/verifiedpermissions/types"
1212
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
13-
"github.com/hashicorp/terraform-plugin-framework/path"
1413
"github.com/hashicorp/terraform-plugin-framework/resource"
1514
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1615
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1716
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1817
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1918
"github.com/hashicorp/terraform-plugin-framework/types"
2019
"github.com/hashicorp/terraform-plugin-log/tflog"
21-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
20+
sdkid "github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
2221
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
2322
"github.com/hashicorp/terraform-provider-aws/internal/create"
2423
"github.com/hashicorp/terraform-provider-aws/internal/errs"
24+
"github.com/hashicorp/terraform-provider-aws/internal/errs/fwdiag"
2525
"github.com/hashicorp/terraform-provider-aws/internal/framework"
26-
"github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
26+
fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
2727
fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types"
28+
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
2829
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
2930
"github.com/hashicorp/terraform-provider-aws/names"
3031
)
3132

3233
// @FrameworkResource("aws_verifiedpermissions_policy_store", name="Policy Store")
34+
// @Tags(identifierAttribute="arn")
35+
// @Testing(tagsTest=false)
3336
func newResourcePolicyStore(context.Context) (resource.ResourceWithConfigure, error) {
3437
r := &resourcePolicyStore{}
3538

@@ -42,6 +45,7 @@ const (
4245

4346
type resourcePolicyStore struct {
4447
framework.ResourceWithConfigure
48+
framework.WithImportByID
4549
}
4650

4751
func (r *resourcePolicyStore) Schema(ctx context.Context, request resource.SchemaRequest, response *resource.SchemaResponse) {
@@ -58,6 +62,8 @@ func (r *resourcePolicyStore) Schema(ctx context.Context, request resource.Schem
5862
stringplanmodifier.UseStateForUnknown(),
5963
},
6064
},
65+
names.AttrTags: tftags.TagsAttribute(),
66+
names.AttrTagsAll: tftags.TagsAttributeComputedOnly(),
6167
},
6268
Blocks: map[string]schema.Block{
6369
"validation_settings": schema.ListNestedBlock{
@@ -82,26 +88,26 @@ func (r *resourcePolicyStore) Schema(ctx context.Context, request resource.Schem
8288
}
8389

8490
func (r *resourcePolicyStore) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) {
85-
conn := r.Meta().VerifiedPermissionsClient(ctx)
86-
var plan resourcePolicyStoreData
87-
88-
response.Diagnostics.Append(request.Plan.Get(ctx, &plan)...)
89-
91+
var data resourcePolicyStoreData
92+
response.Diagnostics.Append(request.Plan.Get(ctx, &data)...)
9093
if response.Diagnostics.HasError() {
9194
return
9295
}
9396

94-
input := &verifiedpermissions.CreatePolicyStoreInput{}
95-
response.Diagnostics.Append(flex.Expand(ctx, plan, input)...)
97+
conn := r.Meta().VerifiedPermissionsClient(ctx)
9698

99+
var input verifiedpermissions.CreatePolicyStoreInput
100+
response.Diagnostics.Append(fwflex.Expand(ctx, data, &input)...)
97101
if response.Diagnostics.HasError() {
98102
return
99103
}
100104

101-
clientToken := id.UniqueId()
105+
// Additional fields.
106+
clientToken := sdkid.UniqueId()
102107
input.ClientToken = aws.String(clientToken)
108+
input.Tags = getTagsIn(ctx)
103109

104-
output, err := conn.CreatePolicyStore(ctx, input)
110+
output, err := conn.CreatePolicyStore(ctx, &input)
105111

106112
if err != nil {
107113
response.Diagnostics.AddError(
@@ -111,134 +117,123 @@ func (r *resourcePolicyStore) Create(ctx context.Context, request resource.Creat
111117
return
112118
}
113119

114-
state := plan
115-
state.ID = flex.StringToFramework(ctx, output.PolicyStoreId)
116-
117-
response.Diagnostics.Append(flex.Flatten(ctx, output, &state)...)
118-
120+
// Set values for unknowns.
121+
response.Diagnostics.Append(fwflex.Flatten(ctx, output, &data)...)
119122
if response.Diagnostics.HasError() {
120123
return
121124
}
125+
data.ID = fwflex.StringToFramework(ctx, output.PolicyStoreId)
122126

123-
response.Diagnostics.Append(response.State.Set(ctx, &state)...)
127+
response.Diagnostics.Append(response.State.Set(ctx, &data)...)
124128
}
125129

126130
func (r *resourcePolicyStore) Read(ctx context.Context, request resource.ReadRequest, response *resource.ReadResponse) {
127-
conn := r.Meta().VerifiedPermissionsClient(ctx)
128-
var state resourcePolicyStoreData
129-
130-
response.Diagnostics.Append(request.State.Get(ctx, &state)...)
131-
131+
var data resourcePolicyStoreData
132+
response.Diagnostics.Append(request.State.Get(ctx, &data)...)
132133
if response.Diagnostics.HasError() {
133134
return
134135
}
135136

136-
output, err := findPolicyStoreByID(ctx, conn, state.ID.ValueString())
137+
conn := r.Meta().VerifiedPermissionsClient(ctx)
138+
139+
output, err := findPolicyStoreByID(ctx, conn, data.ID.ValueString())
137140

138141
if tfresource.NotFound(err) {
142+
response.Diagnostics.Append(fwdiag.NewResourceNotFoundWarningDiagnostic(err))
139143
response.State.RemoveResource(ctx)
144+
140145
return
141146
}
142147

143148
if err != nil {
144149
response.Diagnostics.AddError(
145-
create.ProblemStandardMessage(names.VerifiedPermissions, create.ErrActionReading, ResNamePolicyStore, state.PolicyStoreID.ValueString(), err),
150+
create.ProblemStandardMessage(names.VerifiedPermissions, create.ErrActionReading, ResNamePolicyStore, data.PolicyStoreID.ValueString(), err),
146151
err.Error(),
147152
)
148153
return
149154
}
150155

151-
response.Diagnostics.Append(flex.Flatten(ctx, output, &state)...)
152-
156+
// Set attributes for import.
157+
response.Diagnostics.Append(fwflex.Flatten(ctx, output, &data)...)
153158
if response.Diagnostics.HasError() {
154159
return
155160
}
156161

157-
response.Diagnostics.Append(response.State.Set(ctx, &state)...)
162+
response.Diagnostics.Append(response.State.Set(ctx, &data)...)
158163
}
159164

160165
func (r *resourcePolicyStore) Update(ctx context.Context, request resource.UpdateRequest, response *resource.UpdateResponse) {
161-
conn := r.Meta().VerifiedPermissionsClient(ctx)
162-
var state, plan resourcePolicyStoreData
163-
164-
response.Diagnostics.Append(request.State.Get(ctx, &state)...)
165-
166+
var old, new resourcePolicyStoreData
167+
response.Diagnostics.Append(request.State.Get(ctx, &old)...)
166168
if response.Diagnostics.HasError() {
167169
return
168170
}
169-
170-
response.Diagnostics.Append(request.Plan.Get(ctx, &plan)...)
171-
171+
response.Diagnostics.Append(request.Plan.Get(ctx, &new)...)
172172
if response.Diagnostics.HasError() {
173173
return
174174
}
175175

176-
if !plan.Description.Equal(state.Description) || !plan.ValidationSettings.Equal(state.ValidationSettings) {
177-
input := &verifiedpermissions.UpdatePolicyStoreInput{}
178-
response.Diagnostics.Append(flex.Expand(ctx, plan, input)...)
176+
conn := r.Meta().VerifiedPermissionsClient(ctx)
179177

178+
if !new.Description.Equal(old.Description) || !new.ValidationSettings.Equal(old.ValidationSettings) {
179+
var input verifiedpermissions.UpdatePolicyStoreInput
180+
response.Diagnostics.Append(fwflex.Expand(ctx, new, &input)...)
180181
if response.Diagnostics.HasError() {
181182
return
182183
}
183184

184-
output, err := conn.UpdatePolicyStore(ctx, input)
185+
_, err := conn.UpdatePolicyStore(ctx, &input)
185186

186187
if err != nil {
187188
response.Diagnostics.AddError(
188-
create.ProblemStandardMessage(names.VerifiedPermissions, create.ErrActionUpdating, ResNamePolicyStore, state.PolicyStoreID.ValueString(), err),
189+
create.ProblemStandardMessage(names.VerifiedPermissions, create.ErrActionUpdating, ResNamePolicyStore, old.PolicyStoreID.ValueString(), err),
189190
err.Error(),
190191
)
191192
return
192193
}
193-
194-
response.Diagnostics.Append(flex.Flatten(ctx, output, &plan)...)
195194
}
196195

197-
response.Diagnostics.Append(response.State.Set(ctx, &plan)...)
196+
response.Diagnostics.Append(response.State.Set(ctx, &new)...)
198197
}
199198

200199
func (r *resourcePolicyStore) Delete(ctx context.Context, request resource.DeleteRequest, response *resource.DeleteResponse) {
201-
conn := r.Meta().VerifiedPermissionsClient(ctx)
202-
var state resourcePolicyStoreData
203-
204-
response.Diagnostics.Append(request.State.Get(ctx, &state)...)
205-
200+
var data resourcePolicyStoreData
201+
response.Diagnostics.Append(request.State.Get(ctx, &data)...)
206202
if response.Diagnostics.HasError() {
207203
return
208204
}
209205

206+
conn := r.Meta().VerifiedPermissionsClient(ctx)
207+
210208
tflog.Debug(ctx, "deleting Verified Permissions Policy Store", map[string]any{
211-
names.AttrID: state.ID.ValueString(),
209+
names.AttrID: data.ID.ValueString(),
212210
})
213211

214-
input := &verifiedpermissions.DeletePolicyStoreInput{
215-
PolicyStoreId: flex.StringFromFramework(ctx, state.ID),
212+
input := verifiedpermissions.DeletePolicyStoreInput{
213+
PolicyStoreId: fwflex.StringFromFramework(ctx, data.ID),
216214
}
217-
218-
_, err := conn.DeletePolicyStore(ctx, input)
215+
_, err := conn.DeletePolicyStore(ctx, &input)
219216

220217
if errs.IsA[*awstypes.ResourceNotFoundException](err) {
221218
return
222219
}
223220

224221
if err != nil {
225222
response.Diagnostics.AddError(
226-
create.ProblemStandardMessage(names.VerifiedPermissions, create.ErrActionDeleting, ResNamePolicyStore, state.PolicyStoreID.ValueString(), err),
223+
create.ProblemStandardMessage(names.VerifiedPermissions, create.ErrActionDeleting, ResNamePolicyStore, data.PolicyStoreID.ValueString(), err),
227224
err.Error(),
228225
)
229226
return
230227
}
231228
}
232229

233-
func (r *resourcePolicyStore) ImportState(ctx context.Context, request resource.ImportStateRequest, response *resource.ImportStateResponse) {
234-
resource.ImportStatePassthroughID(ctx, path.Root(names.AttrID), request, response)
235-
}
236-
237230
type resourcePolicyStoreData struct {
238231
ARN types.String `tfsdk:"arn"`
239232
Description types.String `tfsdk:"description"`
240233
ID types.String `tfsdk:"id"`
241234
PolicyStoreID types.String `tfsdk:"policy_store_id"`
235+
Tags tftags.Map `tfsdk:"tags"`
236+
TagsAll tftags.Map `tfsdk:"tags_all"`
242237
ValidationSettings fwtypes.ListNestedObjectValueOf[validationSettings] `tfsdk:"validation_settings"`
243238
}
244239

internal/service/verifiedpermissions/policy_store_data_source.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ import (
1515
"github.com/hashicorp/terraform-provider-aws/internal/framework"
1616
fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
1717
fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types"
18+
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
1819
"github.com/hashicorp/terraform-provider-aws/names"
1920
)
2021

2122
// @FrameworkDataSource("aws_verifiedpermissions_policy_store", name="Policy Store")
23+
// @Tags(identifierAttribute="arn")
2224
func newDataSourcePolicyStore(context.Context) (datasource.DataSourceWithConfigure, error) {
2325
return &dataSourcePolicyStore{}, nil
2426
}
@@ -31,8 +33,8 @@ type dataSourcePolicyStore struct {
3133
framework.DataSourceWithConfigure
3234
}
3335

34-
func (d *dataSourcePolicyStore) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
35-
resp.Schema = schema.Schema{
36+
func (d *dataSourcePolicyStore) Schema(ctx context.Context, request datasource.SchemaRequest, response *datasource.SchemaResponse) {
37+
response.Schema = schema.Schema{
3638
Attributes: map[string]schema.Attribute{
3739
names.AttrARN: framework.ARNAttributeComputedOnly(),
3840
names.AttrCreatedDate: schema.StringAttribute{
@@ -49,6 +51,7 @@ func (d *dataSourcePolicyStore) Schema(ctx context.Context, req datasource.Schem
4951
CustomType: timetypes.RFC3339Type{},
5052
Computed: true,
5153
},
54+
names.AttrTags: tftags.TagsAttributeComputedOnly(),
5255
"validation_settings": schema.ListAttribute{
5356
CustomType: fwtypes.NewListNestedObjectTypeOf[validationSettingsDataSource](ctx),
5457
ElementType: fwtypes.NewObjectTypeOf[validationSettingsDataSource](ctx),
@@ -57,32 +60,31 @@ func (d *dataSourcePolicyStore) Schema(ctx context.Context, req datasource.Schem
5760
},
5861
}
5962
}
60-
func (d *dataSourcePolicyStore) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
61-
conn := d.Meta().VerifiedPermissionsClient(ctx)
62-
63+
func (d *dataSourcePolicyStore) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) {
6364
var data dataSourcePolicyStoreData
64-
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
65-
if resp.Diagnostics.HasError() {
65+
response.Diagnostics.Append(request.Config.Get(ctx, &data)...)
66+
if response.Diagnostics.HasError() {
6667
return
6768
}
6869

69-
out, err := findPolicyStoreByID(ctx, conn, data.ID.ValueString())
70+
conn := d.Meta().VerifiedPermissionsClient(ctx)
71+
72+
output, err := findPolicyStoreByID(ctx, conn, data.ID.ValueString())
7073

7174
if err != nil {
72-
resp.Diagnostics.AddError(
75+
response.Diagnostics.AddError(
7376
create.ProblemStandardMessage(names.VerifiedPermissions, create.ErrActionReading, DSNamePolicyStore, data.ID.ValueString(), err),
7477
err.Error(),
7578
)
7679
return
7780
}
7881

79-
resp.Diagnostics.Append(fwflex.Flatten(ctx, out, &data)...)
80-
81-
if resp.Diagnostics.HasError() {
82+
response.Diagnostics.Append(fwflex.Flatten(ctx, output, &data)...)
83+
if response.Diagnostics.HasError() {
8284
return
8385
}
8486

85-
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
87+
response.Diagnostics.Append(response.State.Set(ctx, &data)...)
8688
}
8789

8890
type dataSourcePolicyStoreData struct {
@@ -91,6 +93,7 @@ type dataSourcePolicyStoreData struct {
9193
Description types.String `tfsdk:"description"`
9294
ID types.String `tfsdk:"id"`
9395
LastUpdatedDate timetypes.RFC3339 `tfsdk:"last_updated_date"`
96+
Tags tftags.Map `tfsdk:"tags"`
9497
ValidationSettings fwtypes.ListNestedObjectValueOf[validationSettingsDataSource] `tfsdk:"validation_settings"`
9598
}
9699

0 commit comments

Comments
 (0)