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_autoscalingplans_scaling_plan #8965

Merged
merged 20 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
d3eb4da
Add 'aws_autoscalingplans_scaling_plan' resource.
Jun 12, 2019
ce4c98e
Terraform Plugin SDK migration.
ewbankkit Oct 5, 2019
df98e92
Add 'subcategory'.
ewbankkit Nov 22, 2019
b254cec
Replace 'validateStringDoesNotContainAny' with 'validation.StringDoes…
Nov 27, 2019
e88b0dc
Remove 'sidebar_current'.
ewbankkit Mar 17, 2020
6fa9aa6
Use 'go-multierror' in test sweeper.
ewbankkit May 11, 2020
1c979ff
Add 'internal/service/autoscalingplans/waiter' package.
ewbankkit May 11, 2020
2bddadc
Simplify acceptance tests a bit.
ewbankkit May 12, 2020
dadedfe
r/aws_autoscalingplans_scaling_plan: Remove custom set hash functions.
ewbankkit May 21, 2020
22726cf
Use 'testAccAvailableEc2InstanceTypeForRegion', not 'testAccAvailable…
ewbankkit Jul 6, 2020
c21359e
Use 'tfawsresource' acceptance test checkers.
ewbankkit Jul 6, 2020
8304317
Upgrade to Plugin SDK v2 and use Terraform 0.12 syntax in documentation.
ewbankkit Aug 16, 2020
0662a1b
r/aws_autoscalingplans_scaling_plan: Use '_Values()' (#14601).
ewbankkit Oct 16, 2020
3883d3d
r/aws_autoscalingplans_scaling_plan: Better handling of eventual cons…
ewbankkit Oct 16, 2020
bb43021
Add documentation note about service-linked role.
ewbankkit Oct 19, 2020
a2ab9dc
Update website/docs/r/autoscalingplans_scaling_plan.html.markdown
ewbankkit Oct 19, 2020
1b8260e
Add 'testAccPreCheckIamServiceLinkedRole' and use for scaling plans w…
ewbankkit Oct 19, 2020
a9751f7
Merge branch 'issue-6562' of https://github.com/ewbankkit/terraform-p…
ewbankkit Oct 19, 2020
4e83eb6
Use 'StateChangeConf.Delay' to better handle API eventual consistency…
ewbankkit Oct 20, 2020
543e5d3
Update aws/internal/service/autoscalingplans/finder/finder.go
ewbankkit Oct 21, 2020
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
26 changes: 26 additions & 0 deletions aws/internal/service/autoscalingplans/finder/finder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package finder

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscalingplans"
)

// ScalingPlan returns the scaling plan corresponding to the specified name and version.
// Returns nil and potentially an API error if no scaling plan is found.
func ScalingPlan(conn *autoscalingplans.AutoScalingPlans, scalingPlanName string, scalingPlanVersion int) (*autoscalingplans.ScalingPlan, error) {
input := &autoscalingplans.DescribeScalingPlansInput{
ScalingPlanNames: aws.StringSlice([]string{scalingPlanName}),
ScalingPlanVersion: aws.Int64(int64(scalingPlanVersion)),
}

output, err := conn.DescribeScalingPlans(input)
if err != nil {
return nil, err
}

if output == nil || len(output.ScalingPlans) == 0 {
return nil, nil
}

return output.ScalingPlans[0], nil
}
41 changes: 41 additions & 0 deletions aws/internal/service/autoscalingplans/waiter/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package waiter

import (
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscalingplans"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/autoscalingplans/finder"
)

const (
scalingPlanStatusNotFound = "NotFound"
scalingPlanStatusUnknown = "Unknown"
)

// ScalingPlanStatus fetches the ScalingPlan and its Status
func ScalingPlanStatus(conn *autoscalingplans.AutoScalingPlans, scalingPlanName string, scalingPlanVersion int) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
scalingPlan, err := finder.ScalingPlan(conn, scalingPlanName, scalingPlanVersion)

if tfawserr.ErrCodeEquals(err, autoscalingplans.ErrCodeObjectNotFoundException) {
return nil, scalingPlanStatusNotFound, nil
}

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

if scalingPlan == nil {
return nil, scalingPlanStatusNotFound, nil
}

if statusMessage := aws.StringValue(scalingPlan.StatusMessage); statusMessage != "" {
log.Printf("[INFO] Auto Scaling Scaling Plan (%s/%d) status message: %s", scalingPlanName, scalingPlanVersion, statusMessage)
}

return scalingPlan, aws.StringValue(scalingPlan.StatusCode), nil
}
}
76 changes: 76 additions & 0 deletions aws/internal/service/autoscalingplans/waiter/waiter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package waiter

import (
"time"

"github.com/aws/aws-sdk-go/service/autoscalingplans"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

const (
// Maximum amount of time to wait for a ScalingPlan to return Created
ScalingPlanCreatedTimeout = 5 * time.Minute

// Maximum amount of time to wait for a ScalingPlan to return Deleted
ScalingPlanDeletedTimeout = 5 * time.Minute

// Maximum amount of time to wait for a ScalingPlan to return Updated
ScalingPlanUpdatedTimeout = 5 * time.Minute
)

// ScalingPlanCreated waits for a ScalingPlan to return Created
func ScalingPlanCreated(conn *autoscalingplans.AutoScalingPlans, scalingPlanName string, scalingPlanVersion int) (*autoscalingplans.ScalingPlan, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{autoscalingplans.ScalingPlanStatusCodeCreationInProgress},
Target: []string{autoscalingplans.ScalingPlanStatusCodeActive, autoscalingplans.ScalingPlanStatusCodeActiveWithProblems},
Refresh: ScalingPlanStatus(conn, scalingPlanName, scalingPlanVersion),
Timeout: ScalingPlanCreatedTimeout,
Delay: 10 * time.Second,
}

outputRaw, err := stateConf.WaitForState()

if v, ok := outputRaw.(*autoscalingplans.ScalingPlan); ok {
return v, err
}

return nil, err
}

// ScalingPlanDeleted waits for a ScalingPlan to return Deleted
func ScalingPlanDeleted(conn *autoscalingplans.AutoScalingPlans, scalingPlanName string, scalingPlanVersion int) (*autoscalingplans.ScalingPlan, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{autoscalingplans.ScalingPlanStatusCodeDeletionInProgress},
Target: []string{},
Refresh: ScalingPlanStatus(conn, scalingPlanName, scalingPlanVersion),
Timeout: ScalingPlanDeletedTimeout,
Delay: 10 * time.Second,
}

outputRaw, err := stateConf.WaitForState()

if v, ok := outputRaw.(*autoscalingplans.ScalingPlan); ok {
return v, err
}

return nil, err
}

// ScalingPlanUpdated waits for a ScalingPlan to return Updated
func ScalingPlanUpdated(conn *autoscalingplans.AutoScalingPlans, scalingPlanName string, scalingPlanVersion int) (*autoscalingplans.ScalingPlan, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{autoscalingplans.ScalingPlanStatusCodeUpdateInProgress},
Target: []string{autoscalingplans.ScalingPlanStatusCodeActive, autoscalingplans.ScalingPlanStatusCodeActiveWithProblems},
Refresh: ScalingPlanStatus(conn, scalingPlanName, scalingPlanVersion),
Timeout: ScalingPlanUpdatedTimeout,
Delay: 10 * time.Second,
}

outputRaw, err := stateConf.WaitForState()

if v, ok := outputRaw.(*autoscalingplans.ScalingPlan); ok {
return v, err
}

return nil, err
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ func Provider() *schema.Provider {
"aws_autoscaling_notification": resourceAwsAutoscalingNotification(),
"aws_autoscaling_policy": resourceAwsAutoscalingPolicy(),
"aws_autoscaling_schedule": resourceAwsAutoscalingSchedule(),
"aws_autoscalingplans_scaling_plan": resourceAwsAutoScalingPlansScalingPlan(),
"aws_backup_plan": resourceAwsBackupPlan(),
"aws_backup_selection": resourceAwsBackupSelection(),
"aws_backup_vault": resourceAwsBackupVault(),
Expand Down
31 changes: 31 additions & 0 deletions aws/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/organizations"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -571,6 +572,36 @@ func testAccOrganizationsEnabledPreCheck(t *testing.T) {
}
}

func testAccPreCheckIamServiceLinkedRole(t *testing.T, pathPrefix string) {
conn := testAccProvider.Meta().(*AWSClient).iamconn

input := &iam.ListRolesInput{
PathPrefix: aws.String(pathPrefix),
}

var role *iam.Role
err := conn.ListRolesPages(input, func(page *iam.ListRolesOutput, lastPage bool) bool {
for _, r := range page.Roles {
role = r
break
}

return !lastPage
})

if testAccPreCheckSkipError(err) {
t.Skipf("skipping tests: %s", err)
}

if err != nil {
t.Fatalf("error listing IAM roles: %s", err)
}

if role == nil {
t.Skipf("skipping tests; missing IAM service-linked role %s. Please create the role and retry", pathPrefix)
}
}

func testAccAlternateAccountProviderConfig() string {
//lintignore:AT004
return fmt.Sprintf(`
Expand Down
Loading