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

[New Resource]: aws_servicequotas_template_association #33725

Merged
merged 2 commits into from
Oct 10, 2023
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/33725.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_servicequotas_template_association
```
3 changes: 2 additions & 1 deletion internal/service/servicequotas/exports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ package servicequotas

// Exports for use in tests only.
var (
ResourceTemplate = newResourceTemplate
ResourceTemplate = newResourceTemplate
ResourceTemplateAssociation = newResourceTemplateAssociation
)
4 changes: 4 additions & 0 deletions internal/service/servicequotas/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions internal/service/servicequotas/servicequotas_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package servicequotas_test

import (
"testing"

"github.com/hashicorp/terraform-provider-aws/internal/acctest"
)

func TestAccServiceQuotas_serial(t *testing.T) {
t.Parallel()

testCases := map[string]map[string]func(t *testing.T){
"Template": {
"basic": testAccTemplate_basic,
"disappears": testAccTemplate_disappears,
"value": testAccTemplate_value,
},
"TemplateAssociation": {
"basic": testAccTemplateAssociation_basic,
"disappears": testAccTemplateAssociation_disappears,
"skipDestroy": testAccTemplateAssociation_skipDestroy,
},
}

acctest.RunSerialTests2Levels(t, testCases, 0)
}
149 changes: 149 additions & 0 deletions internal/service/servicequotas/template_association.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package servicequotas

import (
"context"

"github.com/aws/aws-sdk-go-v2/service/servicequotas"
awstypes "github.com/aws/aws-sdk-go-v2/service/servicequotas/types"
"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/types"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/framework"
"github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @FrameworkResource(name="Template Association")
func newResourceTemplateAssociation(_ context.Context) (resource.ResourceWithConfigure, error) {
return &resourceTemplateAssociation{}, nil
}

const (
ResNameTemplateAssociation = "Template Association"
)

type resourceTemplateAssociation struct {
framework.ResourceWithConfigure
}

func (r *resourceTemplateAssociation) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = "aws_servicequotas_template_association"
}

func (r *resourceTemplateAssociation) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": framework.IDAttribute(),
"skip_destroy": schema.BoolAttribute{
Optional: true,
},
"status": schema.StringAttribute{
Computed: true,
},
},
}
}

func (r *resourceTemplateAssociation) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
conn := r.Meta().ServiceQuotasClient(ctx)

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

plan.ID = types.StringValue(r.Meta().AccountID)

_, err := conn.AssociateServiceQuotaTemplate(ctx, &servicequotas.AssociateServiceQuotaTemplateInput{})
if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.ServiceQuotas, create.ErrActionCreating, ResNameTemplateAssociation, plan.ID.String(), err),
err.Error(),
)
return
}

// Status is not returned from Associate API, so call Get to get computed value
out, err := conn.GetAssociationForServiceQuotaTemplate(ctx, &servicequotas.GetAssociationForServiceQuotaTemplateInput{})
if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.ServiceQuotas, create.ErrActionCreating, ResNameTemplateAssociation, plan.ID.String(), err),
err.Error(),
)
return
}

plan.Status = flex.StringValueToFramework(ctx, string(out.ServiceQuotaTemplateAssociationStatus))

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

func (r *resourceTemplateAssociation) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
conn := r.Meta().ServiceQuotasClient(ctx)

var state resourceTemplateAssociationData
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}

out, err := conn.GetAssociationForServiceQuotaTemplate(ctx, &servicequotas.GetAssociationForServiceQuotaTemplateInput{})
if out == nil || out.ServiceQuotaTemplateAssociationStatus == awstypes.ServiceQuotaTemplateAssociationStatusDisassociated {
resp.State.RemoveResource(ctx)
return
}
if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.ServiceQuotas, create.ErrActionSetting, ResNameTemplateAssociation, state.ID.String(), err),
err.Error(),
)
return
}

state.Status = flex.StringValueToFramework(ctx, string(out.ServiceQuotaTemplateAssociationStatus))

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

// Update is a no-op
func (r *resourceTemplateAssociation) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
}

func (r *resourceTemplateAssociation) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
conn := r.Meta().ServiceQuotasClient(ctx)

var state resourceTemplateAssociationData
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}

if state.SkipDestroy.ValueBool() {
return
}

_, err := conn.DisassociateServiceQuotaTemplate(ctx, &servicequotas.DisassociateServiceQuotaTemplateInput{})
if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.ServiceQuotas, create.ErrActionDeleting, ResNameTemplateAssociation, state.ID.String(), err),
err.Error(),
)
return
}
}

func (r *resourceTemplateAssociation) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

type resourceTemplateAssociationData struct {
ID types.String `tfsdk:"id"`
SkipDestroy types.Bool `tfsdk:"skip_destroy"`
Status types.String `tfsdk:"status"`
}
Loading
Loading