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
perform lint fixs and removed skaff tips
  • Loading branch information
rubenandre committed Apr 26, 2024
commit aa530c96f6bd1c4044e599828b77e3964963d12f
158 changes: 2 additions & 156 deletions internal/service/route53profiles/profile.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
// 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

import (
// TIP: ==== IMPORTS ====
// This is a common set of imports but not customized to your code since
// your code hasn't been written yet. Make sure you, your IDE, or
// goimports -w <file> fixes these imports.
//
// The provider linter wants your imports to be in two groups: first,
// standard library (i.e., "fmt" or "strings"), second, everything else.
//
// Also, AWS Go SDK v2 may handle nested structures differently than v1,
// using the services/route53profiles/types package. If so, you'll
// need to import types and reference the nested types, e.g., as
// awstypes.<Type Name>.
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
Expand Down Expand Up @@ -78,50 +66,6 @@
resp.TypeName = "aws_route53profiles_profile"
}

// TIP: ==== SCHEMA ====
// In the schema, add each of the attributes in snake case (e.g.,
// delete_automated_backups).
//
// Formatting rules:
// * Alphabetize attributes to make them easier to find.
// * Do not add a blank line between attributes.
//
// Attribute basics:
// - If a user can provide a value ("configure a value") for an
// attribute (e.g., instances = 5), we call the attribute an
// "argument."
// - You change the way users interact with attributes using:
// - Required
// - Optional
// - Computed
// - There are only four valid combinations:
//
// 1. Required only - the user must provide a value
// Required: true,
//
// 2. Optional only - the user can configure or omit a value; do not
// use Default or DefaultFunc
//
// Optional: true,
//
// 3. Computed only - the provider can provide a value but the user
// cannot, i.e., read-only
//
// Computed: true,
//
// 4. Optional AND Computed - the provider or user can provide a value;
// use this combination if you are using Default
//
// Optional: true,
// Computed: true,
//
// You will typically find arguments in the input struct
// (e.g., CreateDBInstanceInput) for the create operation. Sometimes
// they are only in the input struct (e.g., ModifyDBInstanceInput) for
// the modify operation.
//
// For more about schema options, visit
// https://developer.hashicorp.com/terraform/plugin/framework/handling-data/schemas?page=schemas
func (r *resourceProfile) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
Expand All @@ -145,24 +89,8 @@
}

func (r *resourceProfile) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
// TIP: ==== RESOURCE CREATE ====
// Generally, the Create function should do the following things. Make
// sure there is a good reason if you don't do one of these.
//
// 1. Get a client connection to the relevant service
// 2. Fetch the plan
// 3. Populate a create input structure
// 4. Call the AWS create/put function
// 5. Using the output from the create function, set the minimum arguments
// and attributes for the Read function to work, as well as any computed
// only attributes.
// 6. Use a waiter to wait for create to complete
// 7. Save the request plan to response state

// TIP: -- 1. Get a client connection to the relevant service
conn := r.Meta().Route53ProfilesClient(ctx)

// TIP: -- 2. Fetch the plan
var data resourceProfileData
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
Expand Down Expand Up @@ -195,31 +123,16 @@
}

func (r *resourceProfile) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// TIP: ==== RESOURCE READ ====
// Generally, the Read function should do the following things. Make
// sure there is a good reason if you don't do one of these.
//
// 1. Get a client connection to the relevant service
// 2. Fetch the state
// 3. Get the resource from AWS
// 4. Remove resource from state if it is not found
// 5. Set the arguments and attributes
// 6. Set the state

// TIP: -- 1. Get a client connection to the relevant service
conn := r.Meta().Route53ProfilesClient(ctx)

// TIP: -- 2. Fetch the state
var state resourceProfileData
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}

// TIP: -- 3. Get the resource from AWS using an API Get, List, or Describe-
// type function, or, better yet, using a finder.
out, err := FindProfileByID(ctx, conn, state.ID.ValueString())
// TIP: -- 4. Remove resource from state if it is not found

if tfresource.NotFound(err) {
resp.State.RemoveResource(ctx)
return
Expand All @@ -241,40 +154,20 @@
}

func (r *resourceProfile) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
// TIP: ==== RESOURCE DELETE ====
// Most resources have Delete functions. There are rare situations
// where you might not need a delete:
// a. The AWS API does not provide a way to delete the resource
// b. The point of your resource is to perform an action (e.g., reboot a
// server) and deleting serves no purpose.
//
// The Delete function should do the following things. Make sure there
// is a good reason if you don't do one of these.
//
// 1. Get a client connection to the relevant service
// 2. Fetch the state
// 3. Populate a delete input structure
// 4. Call the AWS delete function
// 5. Use a waiter to wait for delete to complete
// TIP: -- 1. Get a client connection to the relevant service
conn := r.Meta().Route53ProfilesClient(ctx)

// TIP: -- 2. Fetch the state
var state resourceProfileData
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}

// TIP: -- 3. Populate a delete input structure
in := &route53profiles.DeleteProfileInput{
ProfileId: aws.String(state.ID.ValueString()),
}

// TIP: -- 4. Call the AWS delete function
_, err := conn.DeleteProfile(ctx, in)
// TIP: On rare occassions, the API returns a not found error after deleting a
// resource. If that happens, we don't want it to show up as an error.

if err != nil {
if errs.IsA[*awstypes.ResourceNotFoundException](err) {
return
Expand All @@ -286,7 +179,6 @@
return
}

// TIP: -- 5. Use a waiter to wait for delete to complete
deleteTimeout := r.DeleteTimeout(ctx, state.Timeouts)
_, err = waitProfileDeleted(ctx, conn, state.ID.ValueString(), deleteTimeout)
if err != nil {
Expand All @@ -298,30 +190,10 @@
}
}

// TIP: ==== TERRAFORM IMPORTING ====
// If Read can get all the information it needs from the Identifier
// (i.e., path.Root("id")), you can use the PassthroughID importer. Otherwise,
// you'll need a custom import function.
//
// See more:
// https://developer.hashicorp.com/terraform/plugin/framework/resources/import
func (r *resourceProfile) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

// TIP: ==== WAITERS ====
// Some resources of some services have waiters provided by the AWS API.
// Unless they do not work properly, use them rather than defining new ones
// here.
//
// Sometimes we define the wait, status, and find functions in separate
// files, wait.go, status.go, and find.go. Follow the pattern set out in the
// service and define these where it makes the most sense.
//
// If these functions are used in the _test.go file, they will need to be
// exported (i.e., capitalized).
//
// You will need to adjust the parameters and names to fit the service.
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 All @@ -340,8 +212,6 @@
return nil, err
}

// TIP: A deleted waiter is almost like a backwards created waiter. There may
// be additional pending states, however.
func waitProfileDeleted(ctx context.Context, conn *route53profiles.Client, id string, timeout time.Duration) (*route53profiles.DeleteProfileOutput, error) {
stateConf := &retry.StateChangeConf{
Pending: enum.Slice(awstypes.ProfileStatusDeleting),
Expand All @@ -358,13 +228,6 @@
return nil, err
}

// TIP: ==== STATUS ====
// The status function can return an actual status when that field is
// available from the API (e.g., out.Status). Otherwise, you can use custom
// statuses to communicate the states of the resource.
//
// Waiters consume the values returned by status functions. Design status so
// that it can be reused by a create, update, and delete waiter, if possible.
func statusProfile(ctx context.Context, conn *route53profiles.Client, id string) retry.StateRefreshFunc {
return func() (interface{}, string, error) {
out, err := FindProfileByID(ctx, conn, id)
Expand All @@ -380,11 +243,6 @@
}
}

// TIP: ==== FINDERS ====
// The find function is not strictly necessary. You could do the API
// request from the status function. However, we have found that find often
// comes in handy in other places besides the status function. As a result, it
// is good practice to define it separately.
func FindProfileByID(ctx context.Context, conn *route53profiles.Client, id string) (*awstypes.Profile, error) {
in := &route53profiles.GetProfileInput{
ProfileId: aws.String(id),
Expand All @@ -409,18 +267,6 @@
return out.Profile, nil
}

// TIP: ==== DATA STRUCTURES ====
// With Terraform Plugin-Framework configurations are deserialized into
// Go types, providing type safety without the need for type assertions.
// These structs should match the schema definition exactly, and the `tfsdk`
// tag value should match the attribute name.
//
// Nested objects are represented in their own data struct. These will
// also have a corresponding attribute type mapping for use inside flex
// functions.
//
// See more:
// https://developer.hashicorp.com/terraform/plugin/framework/handling-data/accessing-values
type resourceProfileData struct {
ARN types.String `tfsdk:"arn"`
ID types.String `tfsdk:"id"`
Expand Down
41 changes: 6 additions & 35 deletions internal/service/route53profiles/profile_test.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,9 @@
// 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

// **PLEASE DELETE THIS AND ALL TIP COMMENTS BEFORE SUBMITTING A PR FOR REVIEW!**
//
// TIP: ==== INTRODUCTION ====
// Thank you for trying the skaff tool!
//
// You have opted to include these helpful comments. They all include "TIP:"
// to help you find and remove them when you're done with them.
//
// While some aspects of this file are customized to your input, the
// scaffold tool does *not* look at the AWS API and ensure it has correct
// function, structure, and variable names. It makes guesses based on
// commonalities. You will need to make significant adjustments.
//
// In other words, as generated, this is a rough outline of the work you will
// need to do. If something doesn't make sense for your situation, get rid of
// it.

import (
// TIP: ==== IMPORTS ====
// This is a common set of imports but not customized to your code since
// your code hasn't been written yet. Make sure you, your IDE, or
// goimports -w <file> fixes these imports.
//
// The provider linter wants your imports to be in two groups: first,
// standard library (i.e., "fmt" or "strings"), second, everything else.
//
// Also, AWS Go SDK v2 may handle nested structures differently than v1,
// using the services/route53profiles/types package. If so, you'll
// need to import types and reference the nested types, e.g., as
// types.<Type Name>.
"context"
"fmt"
awstypes "github.com/aws/aws-sdk-go-v2/service/route53profiles/types"
Expand All @@ -59,12 +30,12 @@
},
ErrorCheck: acctest.ErrorCheck(t, names.Route53ProfilesServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckRoute53ProfilesProfileDestroy(ctx),
CheckDestroy: testAccCheckProfileDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccRoute53ProfilesProfileConfig_basic(rName),
Config: testAccProfileConfig_basic(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckRoute53ProfilesProfileExists(ctx, resourceName, &v),
testAccCheckProfileExists(ctx, resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "name", rName),
),
},
Expand All @@ -77,7 +48,7 @@
})
}

func testAccCheckRoute53ProfilesProfileExists(ctx context.Context, name string, r *awstypes.Profile) resource.TestCheckFunc {
func testAccCheckProfileExists(ctx context.Context, name string, r *awstypes.Profile) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
Expand All @@ -98,7 +69,7 @@
}
}

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

Expand All @@ -117,7 +88,7 @@
}
}

func testAccRoute53ProfilesProfileConfig_basic(rName string) string {
func testAccProfileConfig_basic(rName string) string {
return fmt.Sprintf(`
resource "aws_route53profiles_profile" "test" {
name = %[1]q
Expand Down
Loading