Skip to content

Commit

Permalink
Fix struct, tests, and schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharon Nam authored and Sharon Nam committed Oct 10, 2024
1 parent bb5ec21 commit abc1f24
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions internal/service/route53profiles/association.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"context"
"errors"
"fmt"
"log"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
Expand Down Expand Up @@ -51,7 +50,7 @@ const (

type resourceAssociation struct {
framework.ResourceWithConfigure
framework.WithNoOpUpdate[resourceProfileData]
framework.WithNoOpUpdate[associationResourceModel]
framework.WithTimeouts
framework.WithImportByID
}
Expand All @@ -73,6 +72,9 @@ func (r *resourceAssociation) Schema(ctx context.Context, req resource.SchemaReq
},
names.AttrOwnerID: schema.StringAttribute{
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"profile_id": schema.StringAttribute{
Required: true,
Expand All @@ -89,9 +91,15 @@ func (r *resourceAssociation) Schema(ctx context.Context, req resource.SchemaReq
names.AttrStatus: schema.StringAttribute{
CustomType: fwtypes.StringEnumType[awstypes.ProfileStatus](),
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
names.AttrStatusMessage: schema.StringAttribute{
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
names.AttrTags: tftags.TagsAttribute(),
names.AttrTagsAll: tftags.TagsAttributeComputedOnly(),
Expand All @@ -109,35 +117,35 @@ func (r *resourceAssociation) Schema(ctx context.Context, req resource.SchemaReq
func (r *resourceAssociation) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
conn := r.Meta().Route53ProfilesClient(ctx)

var state associationResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &state)...)
var data associationResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

input := &route53profiles.AssociateProfileInput{

Name: data.Name.ValueStringPointer(),
Tags: getTagsInSlice(ctx),
}
resp.Diagnostics.Append(flex.Expand(ctx, state, input)...)
resp.Diagnostics.Append(flex.Expand(ctx, data, input)...)

out, err := conn.AssociateProfile(ctx, input)
if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.Route53Profiles, create.ErrActionCreating, ResNameAssociation, state.Name.String(), err),
create.ProblemStandardMessage(names.Route53Profiles, create.ErrActionCreating, ResNameAssociation, data.Name.String(), err),
err.Error(),
)
return
}
if out == nil || out.ProfileAssociation == nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.Route53Profiles, create.ErrActionCreating, ResNameAssociation, state.Name.String(), nil),
create.ProblemStandardMessage(names.Route53Profiles, create.ErrActionCreating, ResNameAssociation, data.Name.String(), nil),
errors.New("empty output").Error(),
)
return
}

state.ID = flex.StringToFramework(ctx, out.ProfileAssociation.Id)
data.ID = flex.StringToFramework(ctx, out.ProfileAssociation.Id)

associationArn := arn.ARN{
Partition: r.Meta().Partition,
Expand All @@ -147,26 +155,24 @@ func (r *resourceAssociation) Create(ctx context.Context, req resource.CreateReq
Resource: fmt.Sprintf("profile-association/%s", aws.ToString(out.ProfileAssociation.Id)),
}.String()

log.Println("WARN", associationArn)

state.ARN = flex.StringValueToFramework(ctx, associationArn)
data.ARN = flex.StringValueToFramework(ctx, associationArn)

createTimeout := r.CreateTimeout(ctx, state.Timeouts)
profileAssociation, err := waitAssociationCreated(ctx, conn, state.ID.ValueString(), createTimeout)
createTimeout := r.CreateTimeout(ctx, data.Timeouts)
profileAssociation, err := waitAssociationCreated(ctx, conn, data.ID.ValueString(), createTimeout)
if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.Route53Profiles, create.ErrActionWaitingForCreation, ResNameAssociation, state.Name.String(), err),
create.ProblemStandardMessage(names.Route53Profiles, create.ErrActionWaitingForCreation, ResNameAssociation, data.Name.String(), err),
err.Error(),
)
return
}

resp.Diagnostics.Append(flex.Flatten(ctx, profileAssociation, &state)...)
resp.Diagnostics.Append(flex.Flatten(ctx, profileAssociation, &data)...)
if resp.Diagnostics.HasError() {
return
}

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

func (r *resourceAssociation) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
Expand Down

0 comments on commit abc1f24

Please sign in to comment.