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

r/aws_route53profiles_profile #37124

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
add tags generation; update only for tags and acceptance tests
  • Loading branch information
rubenandre committed Apr 26, 2024
commit 90eb2df57a0b72b2988a1b494dbc97170cb37489
6 changes: 6 additions & 0 deletions internal/service/route53profiles/exports_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package route53profiles

var (
Route53ProfilesProfile = newResourceProfile
FindProfileByID = findProfileByID
)
1 change: 1 addition & 0 deletions internal/service/route53profiles/generate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

//go:generate go run ../../generate/tags/main.go -AWSSDKVersion=2 -ServiceTagsMap -ListTags -ListTagsInIDElem=ResourceArn -UpdateTags -TagInIDElem=ResourceArn -KVTValues -SkipTypesImp
//go:generate go run ../../generate/servicepackage/main.go
// ONLY generate directives and package declaration! Do not add anything else to this file.

Expand Down
61 changes: 43 additions & 18 deletions internal/service/route53profiles/profile.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) HashiCorp, Inc.

Check failure on line 1 in internal/service/route53profiles/profile.go

View workflow job for this annotation

GitHub Actions / importlint

Imports of different types are not allowed in the same group (0): "context" != "github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
// SPDX-License-Identifier: MPL-2.0

package route53profiles
Expand All @@ -9,13 +9,13 @@
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-provider-aws/internal/enum"
fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"time"

"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-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"
Expand All @@ -30,18 +30,9 @@
"github.com/hashicorp/terraform-provider-aws/names"
)

// TIP: ==== FILE STRUCTURE ====
// All resources should follow this basic outline. Improve this resource's
// maintainability by sticking to it.
//
// 1. Package declaration
// 2. Imports
// 3. Main resource struct with schema method
// 4. Create, read, update, delete methods (in that order)
// 5. Other functions (flatteners, expanders, waiters, finders, etc.)

// Function annotations are used for resource registration to the Provider. DO NOT EDIT.
// @FrameworkResource("aws_route53profiles_profile", name="Profile")
// @Tags("identifierAttribute=arn")
func newResourceProfile(_ context.Context) (resource.ResourceWithConfigure, error) {
r := &resourceProfile{}

Expand Down Expand Up @@ -92,6 +83,8 @@
"status_message": schema.StringAttribute{
Computed: true,
},
names.AttrTags: tftags.TagsAttribute(),
names.AttrTagsAll: tftags.TagsAttributeComputedOnly(),
},
Blocks: map[string]schema.Block{
"timeouts": timeouts.Block(ctx, timeouts.Opts{
Expand All @@ -115,8 +108,16 @@
input := &route53profiles.CreateProfileInput{}
resp.Diagnostics.Append(fwflex.Expand(ctx, data, input)...)

var tags []awstypes.Tag

// Even tough the tags are Map based, CreateProfile expects a slice of tags
for k, v := range getTagsIn(ctx) {
tags = append(tags, awstypes.Tag{Key: &k, Value: &v})

Check failure on line 115 in internal/service/route53profiles/profile.go

View workflow job for this annotation

GitHub Actions / 2 of 2

exporting a pointer for the loop variable k (exportloopref)
}

// Additional fields
input.ClientToken = aws.String(id.UniqueId())
input.Tags = tags

output, err := conn.CreateProfile(ctx, input)
name := data.Name
Expand Down Expand Up @@ -150,7 +151,7 @@
return
}

out, err := FindProfileByID(ctx, conn, state.ID.ValueString())
out, err := findProfileByID(ctx, conn, state.ID.ValueString())

if tfresource.NotFound(err) {
resp.State.RemoveResource(ctx)
Expand All @@ -172,6 +173,28 @@
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}

func (r *resourceProfile) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var oldProfile, newProfile resourceProfileData
resp.Diagnostics.Append(req.State.Get(ctx, &oldProfile)...)
if resp.Diagnostics.HasError() {
return
}

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

// Update is only called when `tags` are updated
// Set unknown values on newProfile to oldProfile
newProfile.OwnerId = oldProfile.OwnerId
newProfile.ShareStatus = oldProfile.ShareStatus
newProfile.Status = oldProfile.Status
newProfile.StatusMessage = oldProfile.StatusMessage

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

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

Expand Down Expand Up @@ -209,10 +232,6 @@
}
}

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

func waitProfileCreated(ctx context.Context, conn *route53profiles.Client, id string, timeout time.Duration) (*route53profiles.GetProfileOutput, error) {
stateConf := &retry.StateChangeConf{
Pending: enum.Slice(awstypes.ProfileStatusCreating),
Expand Down Expand Up @@ -249,7 +268,7 @@

func statusProfile(ctx context.Context, conn *route53profiles.Client, id string) retry.StateRefreshFunc {
return func() (interface{}, string, error) {
out, err := FindProfileByID(ctx, conn, id)
out, err := findProfileByID(ctx, conn, id)
if tfresource.NotFound(err) {
return nil, "", nil
}
Expand All @@ -262,7 +281,11 @@
}
}

func FindProfileByID(ctx context.Context, conn *route53profiles.Client, id string) (*awstypes.Profile, error) {
func (r *resourceProfile) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
r.SetTagsAll(ctx, req, resp)
}

func findProfileByID(ctx context.Context, conn *route53profiles.Client, id string) (*awstypes.Profile, error) {
in := &route53profiles.GetProfileInput{
ProfileId: aws.String(id),
}
Expand Down Expand Up @@ -294,5 +317,7 @@
ShareStatus fwtypes.StringEnum[awstypes.ShareStatus] `tfsdk:"share_status"`
Status fwtypes.StringEnum[awstypes.ProfileStatus] `tfsdk:"status"`
StatusMessage types.String `tfsdk:"status_message"`
Tags types.Map `tfsdk:"tags"`
TagsAll types.Map `tfsdk:"tags_all"`
Timeouts timeouts.Value `tfsdk:"timeouts"`
}
104 changes: 101 additions & 3 deletions internal/service/route53profiles/profile_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) HashiCorp, Inc.

Check failure on line 1 in internal/service/route53profiles/profile_test.go

View workflow job for this annotation

GitHub Actions / importlint

Imports of different types are not allowed in the same group (0): "context" != awstypes "github.com/aws/aws-sdk-go-v2/service/route53profiles/types"
// SPDX-License-Identifier: MPL-2.0

package route53profiles_test
Expand All @@ -7,15 +7,14 @@
"context"
"fmt"
awstypes "github.com/aws/aws-sdk-go-v2/service/route53profiles/types"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"testing"

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"
tfroute53profiles "github.com/hashicorp/terraform-provider-aws/internal/service/route53profiles"
"github.com/hashicorp/terraform-provider-aws/names"
"testing"
)

func TestAccRoute53ProfilesProfile_basic(t *testing.T) {
Expand Down Expand Up @@ -53,6 +52,80 @@
})
}

func TestAccRoute53ProfilesProfile_disappears(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_route53profiles_profile.test"
var v awstypes.Profile

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
},
ErrorCheck: acctest.ErrorCheck(t, names.Route53ProfilesServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckProfileDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccProfileConfig_basic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckProfileExists(ctx, resourceName, &v),
acctest.CheckFrameworkResourceDisappears(ctx, acctest.Provider, tfroute53profiles.Route53ProfilesProfile, resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccRoute53ProfilesProfile_tags(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_route53profiles_profile.test"
var v awstypes.Profile

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
},
ErrorCheck: acctest.ErrorCheck(t, names.Route53ProfilesServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckProfileDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccProfileConfig_tags1(rName, "key1", "value1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckProfileExists(ctx, resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccProfileConfig_tags2(rName, "key1", "value1", "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckProfileExists(ctx, resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
{
Config: testAccProfileConfig_tags1(rName, "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckProfileExists(ctx, resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
},
})
}

func testAccCheckProfileExists(ctx context.Context, name string, r *awstypes.Profile) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
Expand Down Expand Up @@ -100,3 +173,28 @@
}
`, rName)
}

func testAccProfileConfig_tags1(rName string, tagKey1 string, tagValue1 string) string {
return fmt.Sprintf(`
resource "aws_route53profiles_profile" "test" {
name = %[1]q

tags = {
%[2]q = %[3]q
}
}
`, rName, tagKey1, tagValue1)
}

func testAccProfileConfig_tags2(rName string, tagKey1 string, tagValue1 string, tagKey2 string, tagValue2 string) string {
return fmt.Sprintf(`
resource "aws_route53profiles_profile" "test" {
name = %[1]q

tags = {
%[2]q = %[3]q
%[4]q = %[5]q
}
}
`, rName, tagKey1, tagValue1, tagKey2, tagValue2)
}
3 changes: 3 additions & 0 deletions internal/service/route53profiles/service_package_gen.go

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

Loading
Loading