Skip to content

Commit 64d339a

Browse files
authored
Merge pull request #34672 from hashicorp/f-codeguruprofiler_profiling_group
[new resource/datasource]: aws_codeguruprofiler_profiling_group
2 parents ea8d41c + 02ca822 commit 64d339a

10 files changed

+931
-2
lines changed

.changelog/34672.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:new-resource
2+
aws_codeguruprofiler_profiling_group
3+
```
4+
5+
```release-note:new-data-source
6+
aws_codeguruprofiler_profiling_group
7+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package codeguruprofiler
5+
6+
// Exports for use in tests only.
7+
var (
8+
ResourceProfilingGroup = newResourceProfilingGroup
9+
10+
FindProfilingGroupByName = findProfilingGroupByName
11+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,288 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package codeguruprofiler
5+
6+
import (
7+
"context"
8+
"errors"
9+
10+
"github.com/aws/aws-sdk-go-v2/aws"
11+
"github.com/aws/aws-sdk-go-v2/service/codeguruprofiler"
12+
awstypes "github.com/aws/aws-sdk-go-v2/service/codeguruprofiler/types"
13+
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
14+
"github.com/hashicorp/terraform-plugin-framework/path"
15+
"github.com/hashicorp/terraform-plugin-framework/resource"
16+
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
17+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
18+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
19+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
20+
"github.com/hashicorp/terraform-plugin-framework/types"
21+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
22+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
23+
"github.com/hashicorp/terraform-provider-aws/internal/create"
24+
"github.com/hashicorp/terraform-provider-aws/internal/errs"
25+
"github.com/hashicorp/terraform-provider-aws/internal/framework"
26+
"github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
27+
fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types"
28+
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
29+
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
30+
"github.com/hashicorp/terraform-provider-aws/names"
31+
)
32+
33+
// @FrameworkResource(name="Profiling Group")
34+
// @Tags(identifierAttribute="arn")
35+
func newResourceProfilingGroup(_ context.Context) (resource.ResourceWithConfigure, error) {
36+
r := &resourceProfilingGroup{}
37+
38+
return r, nil
39+
}
40+
41+
const (
42+
ResNameProfilingGroup = "Profiling Group"
43+
)
44+
45+
type resourceProfilingGroup struct {
46+
framework.ResourceWithConfigure
47+
}
48+
49+
func (r *resourceProfilingGroup) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
50+
resp.TypeName = "aws_codeguruprofiler_profiling_group"
51+
}
52+
53+
func (r *resourceProfilingGroup) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
54+
computePlatform := fwtypes.StringEnumType[awstypes.ComputePlatform]()
55+
56+
resp.Schema = schema.Schema{
57+
Attributes: map[string]schema.Attribute{
58+
"arn": framework.ARNAttributeComputedOnly(),
59+
"compute_platform": schema.StringAttribute{
60+
CustomType: computePlatform,
61+
Optional: true,
62+
Computed: true,
63+
PlanModifiers: []planmodifier.String{
64+
stringplanmodifier.RequiresReplace(),
65+
stringplanmodifier.UseStateForUnknown(),
66+
},
67+
},
68+
"id": framework.IDAttribute(),
69+
"name": schema.StringAttribute{
70+
Required: true,
71+
PlanModifiers: []planmodifier.String{
72+
stringplanmodifier.RequiresReplace(),
73+
},
74+
},
75+
names.AttrTags: tftags.TagsAttribute(),
76+
names.AttrTagsAll: tftags.TagsAttributeComputedOnly(),
77+
},
78+
Blocks: map[string]schema.Block{
79+
"agent_orchestration_config": schema.ListNestedBlock{
80+
CustomType: fwtypes.NewListNestedObjectTypeOf[agentOrchestrationConfig](ctx),
81+
Validators: []validator.List{
82+
listvalidator.SizeAtMost(1),
83+
listvalidator.IsRequired(),
84+
},
85+
NestedObject: schema.NestedBlockObject{
86+
Attributes: map[string]schema.Attribute{
87+
"profiling_enabled": schema.BoolAttribute{
88+
Required: true,
89+
},
90+
},
91+
},
92+
},
93+
},
94+
}
95+
}
96+
97+
func (r *resourceProfilingGroup) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
98+
conn := r.Meta().CodeGuruProfilerClient(ctx)
99+
100+
var plan resourceProfilingGroupData
101+
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
102+
if resp.Diagnostics.HasError() {
103+
return
104+
}
105+
106+
in := &codeguruprofiler.CreateProfilingGroupInput{}
107+
resp.Diagnostics.Append(flex.Expand(ctx, plan, in)...)
108+
109+
if resp.Diagnostics.HasError() {
110+
return
111+
}
112+
113+
in.ProfilingGroupName = flex.StringFromFramework(ctx, plan.Name)
114+
in.ClientToken = aws.String(id.UniqueId())
115+
in.Tags = getTagsIn(ctx)
116+
117+
out, err := conn.CreateProfilingGroup(ctx, in)
118+
if err != nil {
119+
resp.Diagnostics.AddError(
120+
create.ProblemStandardMessage(names.CodeGuruProfiler, create.ErrActionCreating, ResNameProfilingGroup, plan.Name.ValueString(), err),
121+
err.Error(),
122+
)
123+
return
124+
}
125+
if out == nil || out.ProfilingGroup == nil {
126+
resp.Diagnostics.AddError(
127+
create.ProblemStandardMessage(names.CodeGuruProfiler, create.ErrActionCreating, ResNameProfilingGroup, plan.Name.ValueString(), nil),
128+
errors.New("empty output").Error(),
129+
)
130+
return
131+
}
132+
133+
state := plan
134+
135+
resp.Diagnostics.Append(flex.Flatten(ctx, out.ProfilingGroup, &state)...)
136+
137+
state.ID = flex.StringToFramework(ctx, out.ProfilingGroup.Name)
138+
139+
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
140+
}
141+
142+
func (r *resourceProfilingGroup) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
143+
conn := r.Meta().CodeGuruProfilerClient(ctx)
144+
145+
var state resourceProfilingGroupData
146+
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
147+
if resp.Diagnostics.HasError() {
148+
return
149+
}
150+
151+
out, err := findProfilingGroupByName(ctx, conn, state.ID.ValueString())
152+
if tfresource.NotFound(err) {
153+
resp.State.RemoveResource(ctx)
154+
return
155+
}
156+
if err != nil {
157+
resp.Diagnostics.AddError(
158+
create.ProblemStandardMessage(names.CodeGuruProfiler, create.ErrActionSetting, ResNameProfilingGroup, state.ID.ValueString(), err),
159+
err.Error(),
160+
)
161+
return
162+
}
163+
164+
resp.Diagnostics.Append(flex.Flatten(ctx, out, &state)...)
165+
166+
if resp.Diagnostics.HasError() {
167+
return
168+
}
169+
170+
setTagsOut(ctx, out.Tags)
171+
172+
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
173+
}
174+
175+
func (r *resourceProfilingGroup) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
176+
conn := r.Meta().CodeGuruProfilerClient(ctx)
177+
178+
var plan, state resourceProfilingGroupData
179+
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
180+
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
181+
182+
if resp.Diagnostics.HasError() {
183+
return
184+
}
185+
186+
if !plan.AgentOrchestrationConfig.Equal(state.AgentOrchestrationConfig) {
187+
in := &codeguruprofiler.UpdateProfilingGroupInput{}
188+
resp.Diagnostics.Append(flex.Expand(ctx, plan, in)...)
189+
190+
if resp.Diagnostics.HasError() {
191+
return
192+
}
193+
194+
in.ProfilingGroupName = flex.StringFromFramework(ctx, state.ID)
195+
out, err := conn.UpdateProfilingGroup(ctx, in)
196+
if err != nil {
197+
resp.Diagnostics.AddError(
198+
create.ProblemStandardMessage(names.CodeGuruProfiler, create.ErrActionUpdating, ResNameProfilingGroup, plan.ID.String(), err),
199+
err.Error(),
200+
)
201+
return
202+
}
203+
204+
if out == nil || out.ProfilingGroup == nil {
205+
resp.Diagnostics.AddError(
206+
create.ProblemStandardMessage(names.CodeGuruProfiler, create.ErrActionUpdating, ResNameProfilingGroup, plan.ID.String(), nil),
207+
errors.New("empty output").Error(),
208+
)
209+
return
210+
}
211+
}
212+
213+
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
214+
}
215+
216+
func (r *resourceProfilingGroup) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
217+
conn := r.Meta().CodeGuruProfilerClient(ctx)
218+
219+
var state resourceProfilingGroupData
220+
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
221+
if resp.Diagnostics.HasError() {
222+
return
223+
}
224+
225+
in := &codeguruprofiler.DeleteProfilingGroupInput{
226+
ProfilingGroupName: aws.String(state.ID.ValueString()),
227+
}
228+
229+
_, err := conn.DeleteProfilingGroup(ctx, in)
230+
231+
if errs.IsA[*awstypes.ResourceNotFoundException](err) {
232+
return
233+
}
234+
235+
if err != nil {
236+
resp.Diagnostics.AddError(
237+
create.ProblemStandardMessage(names.CodeGuruProfiler, create.ErrActionDeleting, ResNameProfilingGroup, state.ID.String(), err),
238+
err.Error(),
239+
)
240+
return
241+
}
242+
}
243+
244+
func (r *resourceProfilingGroup) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
245+
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
246+
}
247+
248+
func (r *resourceProfilingGroup) ModifyPlan(ctx context.Context, request resource.ModifyPlanRequest, response *resource.ModifyPlanResponse) {
249+
r.SetTagsAll(ctx, request, response)
250+
}
251+
252+
func findProfilingGroupByName(ctx context.Context, conn *codeguruprofiler.Client, name string) (*awstypes.ProfilingGroupDescription, error) {
253+
in := &codeguruprofiler.DescribeProfilingGroupInput{
254+
ProfilingGroupName: aws.String(name),
255+
}
256+
257+
out, err := conn.DescribeProfilingGroup(ctx, in)
258+
if errs.IsA[*awstypes.ResourceNotFoundException](err) {
259+
return nil, &retry.NotFoundError{
260+
LastError: err,
261+
LastRequest: in,
262+
}
263+
}
264+
265+
if err != nil {
266+
return nil, err
267+
}
268+
269+
if out == nil || out.ProfilingGroup == nil {
270+
return nil, tfresource.NewEmptyResultError(in)
271+
}
272+
273+
return out.ProfilingGroup, nil
274+
}
275+
276+
type resourceProfilingGroupData struct {
277+
ARN types.String `tfsdk:"arn"`
278+
AgentOrchestrationConfig fwtypes.ListNestedObjectValueOf[agentOrchestrationConfig] `tfsdk:"agent_orchestration_config"`
279+
ComputePlatform fwtypes.StringEnum[awstypes.ComputePlatform] `tfsdk:"compute_platform"`
280+
ID types.String `tfsdk:"id"`
281+
Name types.String `tfsdk:"name"`
282+
Tags types.Map `tfsdk:"tags"`
283+
TagsAll types.Map `tfsdk:"tags_all"`
284+
}
285+
286+
type agentOrchestrationConfig struct {
287+
ProfilingEnabled types.Bool `tfsdk:"profiling_enabled"`
288+
}

0 commit comments

Comments
 (0)