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

add aws_appfabric_app_bundle resource #37542

Merged
merged 31 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
8eee0d4
adding all files from gitlab
kaitheal May 14, 2024
1097acf
AppBundle Tests Pass
walkoaw May 15, 2024
a44bd4c
Final
walkoaw May 15, 2024
233815f
All comments removed
walkoaw May 15, 2024
d9c19d0
added framework resource and no op
kaitheal May 29, 2024
7ffcb49
added framework resource commented out
kaitheal May 29, 2024
c4a4f55
adding endpointID
kaitheal May 29, 2024
cdd2ebf
added precheck to other functions
kaitheal May 29, 2024
05ed84d
delete basic
kaitheal May 29, 2024
f89fe1f
delete precheck function
kaitheal May 29, 2024
7dc7a30
added full test
kaitheal May 29, 2024
9ab3ede
adding changelog
kaitheal May 29, 2024
9ac84ee
Update Tests, Added Documentation
walkoaw May 29, 2024
43d8992
Merge branch 'main' of https:/kaitheal/terraform-provider…
walkoaw May 29, 2024
804a8a2
All comments addressed
walkoaw May 29, 2024
7197927
Added 3 tests: 1 for customer_managed_key and 2 for tags
walkoaw May 31, 2024
33e8c56
Merge branch 'main' into HEAD
ewbankkit Jun 10, 2024
dbf75db
r/aws_appfabric_app_bundle: Tidy up.
ewbankkit Jun 11, 2024
7d46e8f
r/aws_appfabric_app_bundle: Additional acceptance tests.
ewbankkit Jun 11, 2024
720565e
Fix markdown-lint 'MD022/blanks-around-headings/blanks-around-headers…
ewbankkit Jun 11, 2024
8e1a95a
Fix markdown-lint 'MD047/single-trailing-newline Files should end wit…
ewbankkit Jun 11, 2024
9cc2cd5
Fix documentation terrafmt errors.
ewbankkit Jun 11, 2024
e00c8a5
Add 'TestAccAppFabricAppBundle_cmk'.
ewbankkit Jun 11, 2024
100c6e6
appfabric: Remove partition-has-service checks.
ewbankkit Jun 11, 2024
4124326
appfabric: Correct generation.
ewbankkit Jun 11, 2024
62a7959
Use 'fwtypes.ARN' for 'customer_managed_key_arn'.
ewbankkit Jun 11, 2024
7246d46
appfabric: Region check for acceptance tests.
ewbankkit Jun 11, 2024
c84d24c
ClientToken should be a UUID.
ewbankkit Jun 11, 2024
3f4d3c1
r/aws_appfabric_app_bundle: Add PlanModifier for 'tags_all'.
ewbankkit Jun 11, 2024
bf2bc22
appfabric: Serialize acceptance tests.
ewbankkit Jun 11, 2024
b5f59f5
Run 'make gen'.
ewbankkit Jun 11, 2024
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
200 changes: 200 additions & 0 deletions internal/service/appfabric/app_bundle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package appfabric

import (
"context"
"errors"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/appfabric"
awstypes "github.com/aws/aws-sdk-go-v2/service/appfabric/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-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/framework"
"github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)

func newResourceAppBundle(_ context.Context) (resource.ResourceWithConfigure, error) {
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved
r := &resourceAppBundle{}
r.SetDefaultCreateTimeout(5 * time.Minute)
r.SetDefaultUpdateTimeout(5 * time.Minute)
r.SetDefaultDeleteTimeout(5 * time.Minute)
return r, nil
}

const (
ResNameAppBundle = "AppBundle"
)

type resourceAppBundle struct {
framework.ResourceWithConfigure
framework.WithTimeouts
}

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

func (r *resourceAppBundle) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"arn": framework.ARNAttributeComputedOnly(),
"customer_managed_key_identifier": schema.StringAttribute{
Optional: true,
},
names.AttrID: framework.IDAttribute(),
names.AttrTags: tftags.TagsAttribute(),
names.AttrTagsAll: tftags.TagsAttributeComputedOnly(),
},
}
}

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

var plan resourceAppBundleData
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
in := &appfabric.CreateAppBundleInput{
Tags: getTagsIn(ctx),
}

out, err := conn.CreateAppBundle(ctx, in)
if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.AppFabric, create.ErrActionCreating, ResNameAppBundle, plan.ARN.String(), err),
err.Error(),
)
return
}
if out == nil || out.AppBundle == nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.AppFabric, create.ErrActionCreating, ResNameAppBundle, plan.ARN.String(), nil),
errors.New("empty output").Error(),
)
return
}

plan.ARN = flex.StringToFramework(ctx, out.AppBundle.Arn)
plan.CustomerManagedKeyArn = flex.StringToFramework(ctx, out.AppBundle.CustomerManagedKeyArn)
plan.ID = types.StringValue(string(*out.AppBundle.Arn))

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

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

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

out, err := findAppBundleByID(ctx, conn, state.ARN.ValueString())

if tfresource.NotFound(err) {
resp.State.RemoveResource(ctx)
return
}
if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.AppFabric, create.ErrActionSetting, ResNameAppBundle, state.ARN.String(), err),
err.Error(),
)
return
}

state.ARN = flex.StringToFramework(ctx, out.Arn)
state.CustomerManagedKeyArn = flex.StringToFramework(ctx, out.CustomerManagedKeyArn)
state.ID = types.StringValue(string(*out.Arn))

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

func (r *resourceAppBundle) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved
}

func (r *resourceAppBundle) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {

conn := r.Meta().AppFabricClient(ctx)

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

in := &appfabric.DeleteAppBundleInput{
AppBundleIdentifier: aws.String(state.ARN.ValueString()),
}

_, err := conn.DeleteAppBundle(ctx, in)

if err != nil {
if errs.IsA[*awstypes.ResourceNotFoundException](err) {
return
}
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.AppFabric, create.ErrActionDeleting, ResNameAppBundle, state.ARN.String(), err),
err.Error(),
)
return
}
}

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

const (
statusChangePending = "Pending"
statusDeleting = "Deleting"
statusNormal = "Normal"
statusUpdated = "Updated"
)

func findAppBundleByID(ctx context.Context, conn *appfabric.Client, arn string) (*awstypes.AppBundle, error) {
in := &appfabric.GetAppBundleInput{
AppBundleIdentifier: aws.String(arn),
}

out, err := conn.GetAppBundle(ctx, in)
if err != nil {
if errs.IsA[*awstypes.ResourceNotFoundException](err) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: in,
}
}

return nil, err
}

if out == nil || out.AppBundle == nil {
return nil, tfresource.NewEmptyResultError(in)
}

return out.AppBundle, nil
}

type resourceAppBundleData struct {
ARN types.String `tfsdk:"arn"`
CustomerManagedKeyArn types.String `tfsdk:"customer_managed_key_identifier"`
ID types.String `tfsdk:"id"`
Tags types.Map `tfsdk:"tags"`
TagsAll types.Map `tfsdk:"tags_all"`
}
194 changes: 194 additions & 0 deletions internal/service/appfabric/app_bundle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package appfabric_test

import (
"context"
"errors"
"fmt"
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/appfabric"
"github.com/aws/aws-sdk-go-v2/service/appfabric/types"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/names"

tfappfabric "github.com/hashicorp/terraform-provider-aws/internal/service/appfabric"
)

func TestAccAppFabricAppBundle_basic(t *testing.T) {
ctx := acctest.Context(t)
if testing.Short() {
t.Skip("skipping long-running test in short mode")
}

var appbundle appfabric.GetAppBundleOutput
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_appfabric_app_bundle.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
testAccPreCheck(ctx, t)
},
ErrorCheck: acctest.ErrorCheck(t, names.AppFabricServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckAppBundleDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccAppBundleConfig_basic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAppBundleExists(ctx, resourceName, &appbundle),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateIdFunc: testAccAppBundleImportStateIDFunc(ctx, resourceName),
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved
ImportStateVerify: true,
},
},
})
}

func TestAccAppFabricAppBundle_disappears(t *testing.T) {
ctx := acctest.Context(t)
if testing.Short() {
t.Skip("skipping long-running test in short mode")
}

var appbundle appfabric.GetAppBundleOutput
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_appfabric_app_bundle.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
testAccPreCheck(ctx, t)
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved
},
ErrorCheck: acctest.ErrorCheck(t, names.AppFabricServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckAppBundleDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccAppBundleConfig_basic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAppBundleExists(ctx, resourceName, &appbundle),
acctest.CheckFrameworkResourceDisappears(ctx, acctest.Provider, tfappfabric.ResourceAppBundle, resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testAccCheckAppBundleDestroy(ctx context.Context) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).AppFabricClient(ctx)

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_appfabric_app_bundle" {
continue
}

_, err := conn.GetAppBundle(ctx, &appfabric.GetAppBundleInput{
AppBundleIdentifier: aws.String(rs.Primary.Attributes["arn"]),
})
if errs.IsA[*types.ResourceNotFoundException](err) {
return nil
}
if err != nil {
return create.Error(names.AppFabric, create.ErrActionCheckingDestroyed, tfappfabric.ResNameAppBundle, rs.Primary.ID, err)
}

return create.Error(names.AppFabric, create.ErrActionCheckingDestroyed, tfappfabric.ResNameAppBundle, rs.Primary.ID, errors.New("not destroyed"))
}

return nil
}
}

func testAccCheckAppBundleExists(ctx context.Context, name string, appbundle *appfabric.GetAppBundleOutput) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return create.Error(names.AppFabric, create.ErrActionCheckingExistence, tfappfabric.ResNameAppBundle, name, errors.New("not found"))
}
if rs.Primary.ID == "" {
return create.Error(names.AppFabric, create.ErrActionCheckingExistence, tfappfabric.ResNameAppBundle, name, errors.New("not set"))
}
conn := acctest.Provider.Meta().(*conns.AWSClient).AppFabricClient(ctx)
resp, err := conn.GetAppBundle(ctx, &appfabric.GetAppBundleInput{
AppBundleIdentifier: aws.String(rs.Primary.Attributes["arn"]),
})
if err != nil {
return create.Error(names.AppFabric, create.ErrActionCheckingExistence, tfappfabric.ResNameAppBundle, rs.Primary.ID, err)
}
*appbundle = *resp
return nil
}
}

func testAccPreCheck(ctx context.Context, t *testing.T) {
conn := acctest.Provider.Meta().(*conns.AWSClient).AppFabricClient(ctx)
input := &appfabric.ListAppBundlesInput{}
_, err := conn.ListAppBundles(ctx, input)
if acctest.PreCheckSkipError(err) {
t.Skipf("skipping acceptance testing: %s", err)
}
if err != nil {
t.Fatalf("unexpected PreCheck error: %s", err)
}
}
func testAccCheckAppBundleNotRecreated(before, after *appfabric.GetAppBundleOutput) resource.TestCheckFunc {
return func(s *terraform.State) error {
if before, after := aws.ToString(before.AppBundle.Arn), aws.ToString(after.AppBundle.Arn); before != after {
return create.Error(names.AppFabric, create.ErrActionCheckingNotRecreated, tfappfabric.ResNameAppBundle, before, errors.New("recreated"))
}
return nil
}
}

func testAccAppBundleImportStateIDFunc(ctx context.Context, resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return "", fmt.Errorf("Not found: %s", resourceName)
}

if rs.Primary.ID == "" {
return "", errors.New("No AppBundle ID set")
}

conn := acctest.Provider.Meta().(*conns.AWSClient).AppFabricClient(ctx)
appBundleARN := rs.Primary.Attributes["arn"]

_, err := conn.GetAppBundle(ctx, &appfabric.GetAppBundleInput{
AppBundleIdentifier: aws.String(appBundleARN),
})

if err != nil {
return "", err
}

return appBundleARN, nil
}
}

func testAccAppBundleConfig_basic(rName string) string {
return fmt.Sprintf(`
resource "aws_appfabric_app_bundle" "test" {
tags = {
Name = "AppFabricTesting"
}
}
`)
}
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved
Loading