Skip to content

Commit

Permalink
fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
aristosvo committed Aug 23, 2024
1 parent 6f9a4b2 commit e197acd
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 47 deletions.
28 changes: 14 additions & 14 deletions internal/service/route53profiles/association.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,49 +101,49 @@ 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 plan associationResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
var state associationResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}

input := &route53profiles.AssociateProfileInput{}
resp.Diagnostics.Append(flex.Expand(ctx, plan, input)...)
resp.Diagnostics.Append(flex.Expand(ctx, state, input)...)

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

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

createTimeout := r.CreateTimeout(ctx, plan.Timeouts)
profileAssociation, err := waitAssociationCreated(ctx, conn, plan.ID.ValueString(), createTimeout)
createTimeout := r.CreateTimeout(ctx, state.Timeouts)
profileAssociation, err := waitAssociationCreated(ctx, conn, state.ID.ValueString(), createTimeout)
if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.Route53Profiles, create.ErrActionWaitingForCreation, ResNameAssociation, plan.Name.String(), err),
create.ProblemStandardMessage(names.Route53Profiles, create.ErrActionWaitingForCreation, ResNameAssociation, state.Name.String(), err),
err.Error(),
)
return
}
if profileAssociation != nil {
plan.OwnerId = flex.StringToFramework(ctx, profileAssociation.OwnerId)
plan.Status = fwtypes.StringEnumValue[awstypes.ProfileStatus](profileAssociation.Status)
plan.StatusMessage = flex.StringToFramework(ctx, profileAssociation.StatusMessage)

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

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

func (r *resourceAssociation) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
Expand Down
14 changes: 7 additions & 7 deletions internal/service/route53profiles/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,18 @@ func (r *resourceProfile) Create(ctx context.Context, req resource.CreateRequest

data.ID = fwflex.StringToFramework(ctx, output.Profile.Id)

profileOutput, err := waitProfileCreated(ctx, conn, data.ID.ValueString(), r.CreateTimeout(ctx, data.Timeouts))
profile, err := waitProfileCreated(ctx, conn, data.ID.ValueString(), r.CreateTimeout(ctx, data.Timeouts))
if err != nil {
resp.Diagnostics.AddError(fmt.Sprintf("waiting for route53 profile (%s) created", name), err.Error())
return
}

if profileOutput != nil {
data.ARN = fwflex.StringToFramework(ctx, profileOutput.Arn)
data.OwnerId = fwflex.StringToFramework(ctx, profileOutput.OwnerId)
data.ShareStatus = fwtypes.StringEnumValue(profileOutput.ShareStatus)
data.Status = fwtypes.StringEnumValue(profileOutput.Status)
data.StatusMessage = fwflex.StringToFramework(ctx, profileOutput.StatusMessage)
if profile != nil {
data.ARN = fwflex.StringToFramework(ctx, profile.Arn)
data.OwnerId = fwflex.StringToFramework(ctx, profile.OwnerId)
data.ShareStatus = fwtypes.StringEnumValue(profile.ShareStatus)
data.Status = fwtypes.StringEnumValue(profile.Status)
data.StatusMessage = fwflex.StringToFramework(ctx, profile.StatusMessage)
}

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
Expand Down
2 changes: 1 addition & 1 deletion internal/service/route53profiles/profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func TestAccRoute53ProfilesProfile_tags(t *testing.T) {
),
},
{
Config: testAccProfileConfig_tags1(rName, acctest.CtTagsKey2, acctest.CtValue2),
Config: testAccProfileConfig_tags1(rName, acctest.CtKey2, acctest.CtValue2),
Check: resource.ComposeTestCheckFunc(
testAccCheckProfileExists(ctx, resourceName, &v),
resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, acctest.Ct1),
Expand Down
41 changes: 18 additions & 23 deletions internal/service/route53profiles/resource_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/route53profiles"
awstypes "github.com/aws/aws-sdk-go-v2/service/route53profiles/types"
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-provider-aws/internal/create"
Expand All @@ -26,7 +26,6 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/framework"
"github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types"
fwvalidators "github.com/hashicorp/terraform-provider-aws/internal/framework/validators"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)
Expand Down Expand Up @@ -83,10 +82,8 @@ func (r *resourceResourceAssociation) Schema(ctx context.Context, req resource.S
},
},
"resource_properties": schema.StringAttribute{
Optional: true,
Validators: []validator.String{
fwvalidators.JSON(),
},
CustomType: jsontypes.NormalizedType{},
Optional: true,
},
names.AttrResourceType: schema.StringAttribute{
Computed: true,
Expand All @@ -112,51 +109,49 @@ func (r *resourceResourceAssociation) Schema(ctx context.Context, req resource.S
func (r *resourceResourceAssociation) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
conn := r.Meta().Route53ProfilesClient(ctx)

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

input := &route53profiles.AssociateResourceToProfileInput{}
resp.Diagnostics.Append(flex.Expand(ctx, plan, input)...)
resp.Diagnostics.Append(flex.Expand(ctx, state, input)...)

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

plan.ID = flex.StringToFramework(ctx, out.ProfileResourceAssociation.Id)
state.ID = flex.StringToFramework(ctx, out.ProfileResourceAssociation.Id)

createTimeout := r.CreateTimeout(ctx, plan.Timeouts)
resourceAssociation, err := waitResourceAssociationCreated(ctx, conn, plan.ID.ValueString(), createTimeout)
createTimeout := r.CreateTimeout(ctx, state.Timeouts)
resourceAssociation, err := waitResourceAssociationCreated(ctx, conn, state.ID.ValueString(), createTimeout)
if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.Route53Profiles, create.ErrActionWaitingForCreation, ResNameResourceAssociation, plan.Name.String(), err),
create.ProblemStandardMessage(names.Route53Profiles, create.ErrActionWaitingForCreation, ResNameResourceAssociation, state.Name.String(), err),
err.Error(),
)
return
}
if resourceAssociation != nil {
plan.OwnerId = flex.StringToFramework(ctx, resourceAssociation.OwnerId)
plan.ResourceProperties = flex.StringToFramework(ctx, resourceAssociation.ResourceProperties)
plan.ResourceType = flex.StringToFramework(ctx, resourceAssociation.ResourceType)
plan.Status = fwtypes.StringEnumValue[awstypes.ProfileStatus](resourceAssociation.Status)
plan.StatusMessage = flex.StringToFramework(ctx, resourceAssociation.StatusMessage)

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

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

func (r *resourceResourceAssociation) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
Expand Down Expand Up @@ -309,7 +304,7 @@ type resourceAssociationResourceModel struct {
OwnerId types.String `tfsdk:"owner_id"`
ProfileID types.String `tfsdk:"profile_id"`
ResourceArn types.String `tfsdk:"resource_arn"`
ResourceProperties types.String `tfsdk:"resource_properties"`
ResourceProperties jsontypes.Normalized `tfsdk:"resource_properties"`
ResourceType types.String `tfsdk:"resource_type"`
Status fwtypes.StringEnum[awstypes.ProfileStatus] `tfsdk:"status"`
StatusMessage types.String `tfsdk:"status_message"`
Expand Down
3 changes: 1 addition & 2 deletions website/docs/d/route53profiles_profiles.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ There are no arguments available for this data source.

This data source exports the following attributes:


* `profiles` - List of Profiles.
* `arn` - The ARN of the Profile.
* `id` - The ID of the Profile.
* `name` - The name of the Profile.
* `share_status` - The share status of the Profile. Valid values [AWS docs](https://docs.aws.amazon.com/Route53/latest/APIReference/API_route53profiles_Profile.html)
* `share_status` - The share status of the Profile. Valid values [AWS docs](https://docs.aws.amazon.com/Route53/latest/APIReference/API_route53profiles_Profile.html)

0 comments on commit e197acd

Please sign in to comment.