Skip to content

Commit

Permalink
Merge pull request hashicorp#3862 from terraform-providers/t-aws_batc…
Browse files Browse the repository at this point in the history
…h_compute_environment-sweeper

resource/aws_batch_*: Add test sweepers
  • Loading branch information
bflad authored Mar 26, 2018
2 parents 26083f4 + 4a06602 commit 680afc8
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 88 deletions.
103 changes: 52 additions & 51 deletions aws/resource_aws_batch_compute_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func resourceAwsBatchComputeEnvironmentCreate(d *schema.ResourceData, meta inter
stateConf := &resource.StateChangeConf{
Pending: []string{batch.CEStatusCreating},
Target: []string{batch.CEStatusValid},
Refresh: resourceAwsBatchComputeEnvironmentStatusRefreshFunc(d, meta),
Refresh: resourceAwsBatchComputeEnvironmentStatusRefreshFunc(computeEnvironmentName, conn),
Timeout: d.Timeout(schema.TimeoutCreate),
MinTimeout: 5 * time.Second,
}
Expand Down Expand Up @@ -305,52 +305,20 @@ func flattenBatchComputeResources(computeResource *batch.ComputeResource) []map[

func resourceAwsBatchComputeEnvironmentDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).batchconn

computeEnvironmentName := d.Get("compute_environment_name").(string)

updateInput := &batch.UpdateComputeEnvironmentInput{
ComputeEnvironment: aws.String(computeEnvironmentName),
State: aws.String(batch.CEStateDisabled),
}

log.Printf("[DEBUG] Delete compute environment %s.\n", updateInput)

if _, err := conn.UpdateComputeEnvironment(updateInput); err != nil {
return err
}

stateConf := &resource.StateChangeConf{
Pending: []string{batch.CEStatusUpdating},
Target: []string{batch.CEStatusValid},
Refresh: resourceAwsBatchComputeEnvironmentStatusRefreshFunc(d, meta),
Timeout: d.Timeout(schema.TimeoutDelete),
MinTimeout: 5 * time.Second,
}
if _, err := stateConf.WaitForState(); err != nil {
return err
}

input := &batch.DeleteComputeEnvironmentInput{
ComputeEnvironment: aws.String(computeEnvironmentName),
}

if _, err := conn.DeleteComputeEnvironment(input); err != nil {
return err
log.Printf("[DEBUG] Disabling Batch Compute Environment: %s", computeEnvironmentName)
err := disableBatchComputeEnvironment(computeEnvironmentName, d.Timeout(schema.TimeoutDelete), conn)
if err != nil {
return fmt.Errorf("error disabling Batch Compute Environment (%s): %s", computeEnvironmentName, err)
}

stateConfForDelete := &resource.StateChangeConf{
Pending: []string{batch.CEStatusDeleting},
Target: []string{batch.CEStatusDeleted},
Refresh: resourceAwsBatchComputeEnvironmentDeleteRefreshFunc(d, meta),
Timeout: d.Timeout(schema.TimeoutDelete),
MinTimeout: 5 * time.Second,
}
if _, err := stateConfForDelete.WaitForState(); err != nil {
return err
log.Printf("[DEBUG] Deleting Batch Compute Environment: %s", computeEnvironmentName)
err = deleteBatchComputeEnvironment(computeEnvironmentName, d.Timeout(schema.TimeoutDelete), conn)
if err != nil {
return fmt.Errorf("error deleting Batch Compute Environment (%s): %s", computeEnvironmentName, err)
}

d.SetId("")

return nil
}

Expand Down Expand Up @@ -392,12 +360,8 @@ func resourceAwsBatchComputeEnvironmentUpdate(d *schema.ResourceData, meta inter
return resourceAwsBatchComputeEnvironmentRead(d, meta)
}

func resourceAwsBatchComputeEnvironmentStatusRefreshFunc(d *schema.ResourceData, meta interface{}) resource.StateRefreshFunc {
func resourceAwsBatchComputeEnvironmentStatusRefreshFunc(computeEnvironmentName string, conn *batch.Batch) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
conn := meta.(*AWSClient).batchconn

computeEnvironmentName := d.Get("compute_environment_name").(string)

result, err := conn.DescribeComputeEnvironments(&batch.DescribeComputeEnvironmentsInput{
ComputeEnvironments: []*string{
aws.String(computeEnvironmentName),
Expand All @@ -416,12 +380,8 @@ func resourceAwsBatchComputeEnvironmentStatusRefreshFunc(d *schema.ResourceData,
}
}

func resourceAwsBatchComputeEnvironmentDeleteRefreshFunc(d *schema.ResourceData, meta interface{}) resource.StateRefreshFunc {
func resourceAwsBatchComputeEnvironmentDeleteRefreshFunc(computeEnvironmentName string, conn *batch.Batch) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
conn := meta.(*AWSClient).batchconn

computeEnvironmentName := d.Get("compute_environment_name").(string)

result, err := conn.DescribeComputeEnvironments(&batch.DescribeComputeEnvironmentsInput{
ComputeEnvironments: []*string{
aws.String(computeEnvironmentName),
Expand All @@ -439,3 +399,44 @@ func resourceAwsBatchComputeEnvironmentDeleteRefreshFunc(d *schema.ResourceData,
return result, *(computeEnvironment.Status), nil
}
}

func deleteBatchComputeEnvironment(computeEnvironment string, timeout time.Duration, conn *batch.Batch) error {
input := &batch.DeleteComputeEnvironmentInput{
ComputeEnvironment: aws.String(computeEnvironment),
}

if _, err := conn.DeleteComputeEnvironment(input); err != nil {
return err
}

stateChangeConf := &resource.StateChangeConf{
Pending: []string{batch.CEStatusDeleting},
Target: []string{batch.CEStatusDeleted},
Refresh: resourceAwsBatchComputeEnvironmentDeleteRefreshFunc(computeEnvironment, conn),
Timeout: timeout,
MinTimeout: 5 * time.Second,
}
_, err := stateChangeConf.WaitForState()
return err
}

func disableBatchComputeEnvironment(computeEnvironment string, timeout time.Duration, conn *batch.Batch) error {
input := &batch.UpdateComputeEnvironmentInput{
ComputeEnvironment: aws.String(computeEnvironment),
State: aws.String(batch.CEStateDisabled),
}

if _, err := conn.UpdateComputeEnvironment(input); err != nil {
return err
}

stateChangeConf := &resource.StateChangeConf{
Pending: []string{batch.CEStatusUpdating},
Target: []string{batch.CEStatusValid},
Refresh: resourceAwsBatchComputeEnvironmentStatusRefreshFunc(computeEnvironment, conn),
Timeout: timeout,
MinTimeout: 5 * time.Second,
}
_, err := stateChangeConf.WaitForState()
return err
}
59 changes: 59 additions & 0 deletions aws/resource_aws_batch_compute_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package aws

import (
"fmt"
"log"
"regexp"
"strings"
"testing"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/batch"
Expand All @@ -12,6 +15,62 @@ import (
"github.com/hashicorp/terraform/terraform"
)

func init() {
resource.AddTestSweepers("aws_batch_compute_environment", &resource.Sweeper{
Name: "aws_batch_compute_environment",
Dependencies: []string{
"aws_batch_job_queue",
},
F: testSweepBatchComputeEnvironments,
})
}

func testSweepBatchComputeEnvironments(region string) error {
client, err := sharedClientForRegion(region)
if err != nil {
return fmt.Errorf("error getting client: %s", err)
}
conn := client.(*AWSClient).batchconn

prefixes := []string{
"tf_acc",
}

out, err := conn.DescribeComputeEnvironments(&batch.DescribeComputeEnvironmentsInput{})
if err != nil {
return fmt.Errorf("Error retrieving Batch Compute Environments: %s", err)
}
for _, computeEnvironment := range out.ComputeEnvironments {
name := computeEnvironment.ComputeEnvironmentName
skip := true
for _, prefix := range prefixes {
if strings.HasPrefix(*name, prefix) {
skip = false
break
}
}
if skip {
log.Printf("[INFO] Skipping Batch Compute Environment: %s", *name)
continue
}

log.Printf("[INFO] Disabling Batch Compute Environment: %s", *name)
err := disableBatchComputeEnvironment(*name, 20*time.Minute, conn)
if err != nil {
log.Printf("[ERROR] Failed to disable Batch Compute Environment %s: %s", *name, err)
continue
}

log.Printf("[INFO] Deleting Batch Compute Environment: %s", *name)
err = deleteBatchComputeEnvironment(*name, 20*time.Minute, conn)
if err != nil {
log.Printf("[ERROR] Failed to delete Batch Compute Environment %s: %s", *name, err)
}
}

return nil
}

func TestAccAWSBatchComputeEnvironment_createEc2(t *testing.T) {
rInt := acctest.RandInt()

Expand Down
83 changes: 46 additions & 37 deletions aws/resource_aws_batch_job_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,64 +133,73 @@ func resourceAwsBatchJobQueueUpdate(d *schema.ResourceData, meta interface{}) er

func resourceAwsBatchJobQueueDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).batchconn
sn := d.Get("name").(string)
_, err := conn.UpdateJobQueue(&batch.UpdateJobQueueInput{
JobQueue: aws.String(sn),
State: aws.String(batch.JQStateDisabled),
})
name := d.Get("name").(string)

log.Printf("[DEBUG] Disabling Batch Job Queue %s", name)
err := disableBatchJobQueue(name, 10*time.Minute, conn)
if err != nil {
return err
return fmt.Errorf("error disabling Batch Job Queue (%s): %s", name, err)
}

// Wait until the Job Queue is disabled before deleting
stateConf := &resource.StateChangeConf{
Pending: []string{batch.JQStatusUpdating},
Target: []string{batch.JQStatusValid},
Refresh: batchJobQueueRefreshStatusFunc(conn, sn),
Timeout: 10 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
log.Printf("[DEBUG] Deleting Batch Job Queue %s", name)
err = deleteBatchJobQueue(name, 10*time.Minute, conn)
if err != nil {
return err
return fmt.Errorf("error deleting Batch Job Queue (%s): %s", name, err)
}

return nil
}

func createComputeEnvironmentOrder(order []interface{}) (envs []*batch.ComputeEnvironmentOrder) {
for i, env := range order {
envs = append(envs, &batch.ComputeEnvironmentOrder{
Order: aws.Int64(int64(i)),
ComputeEnvironment: aws.String(env.(string)),
})
}
return
}

log.Printf("[DEBUG] Trying to delete Job Queue %s", sn)
_, err = conn.DeleteJobQueue(&batch.DeleteJobQueueInput{
JobQueue: aws.String(sn),
func deleteBatchJobQueue(jobQueue string, timeout time.Duration, conn *batch.Batch) error {
_, err := conn.DeleteJobQueue(&batch.DeleteJobQueueInput{
JobQueue: aws.String(jobQueue),
})
if err != nil {
return err
}

deleteStateConf := &resource.StateChangeConf{
stateChangeConf := &resource.StateChangeConf{
Pending: []string{batch.JQStateDisabled, batch.JQStatusDeleting},
Target: []string{batch.JQStatusDeleted},
Refresh: batchJobQueueRefreshStatusFunc(conn, sn),
Timeout: 10 * time.Minute,
Refresh: batchJobQueueRefreshStatusFunc(conn, jobQueue),
Timeout: timeout,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}

_, err = deleteStateConf.WaitForState()
_, err = stateChangeConf.WaitForState()
return err
}

func disableBatchJobQueue(jobQueue string, timeout time.Duration, conn *batch.Batch) error {
_, err := conn.UpdateJobQueue(&batch.UpdateJobQueueInput{
JobQueue: aws.String(jobQueue),
State: aws.String(batch.JQStateDisabled),
})
if err != nil {
return fmt.Errorf(
"Error waiting for Job Queue (%s) to be deleted: %s",
sn, err)
return err
}
d.SetId("")
return nil
}

func createComputeEnvironmentOrder(order []interface{}) (envs []*batch.ComputeEnvironmentOrder) {
for i, env := range order {
envs = append(envs, &batch.ComputeEnvironmentOrder{
Order: aws.Int64(int64(i)),
ComputeEnvironment: aws.String(env.(string)),
})
stateChangeConf := &resource.StateChangeConf{
Pending: []string{batch.JQStatusUpdating},
Target: []string{batch.JQStatusValid},
Refresh: batchJobQueueRefreshStatusFunc(conn, jobQueue),
Timeout: timeout,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
return
_, err = stateChangeConf.WaitForState()
return err
}

func getJobQueue(conn *batch.Batch, sn string) (*batch.JobQueueDetail, error) {
Expand Down
54 changes: 54 additions & 0 deletions aws/resource_aws_batch_job_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,67 @@ import (
"strconv"
"strings"
"testing"
"time"

"github.com/aws/aws-sdk-go/service/batch"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func init() {
resource.AddTestSweepers("aws_batch_job_queue", &resource.Sweeper{
Name: "aws_batch_job_queue",
F: testSweepBatchJobQueues,
})
}

func testSweepBatchJobQueues(region string) error {
client, err := sharedClientForRegion(region)
if err != nil {
return fmt.Errorf("error getting client: %s", err)
}
conn := client.(*AWSClient).batchconn

prefixes := []string{
"tf_acc",
}

out, err := conn.DescribeJobQueues(&batch.DescribeJobQueuesInput{})
if err != nil {
return fmt.Errorf("Error retrieving Batch Job Queues: %s", err)
}
for _, jobQueue := range out.JobQueues {
name := jobQueue.JobQueueName
skip := true
for _, prefix := range prefixes {
if strings.HasPrefix(*name, prefix) {
skip = false
break
}
}
if skip {
log.Printf("[INFO] Skipping Batch Job Queue: %s", *name)
continue
}

log.Printf("[INFO] Disabling Batch Job Queue: %s", *name)
err := disableBatchJobQueue(*name, 10*time.Minute, conn)
if err != nil {
log.Printf("[ERROR] Failed to disable Batch Job Queue %s: %s", *name, err)
continue
}

log.Printf("[INFO] Deleting Batch Job Queue: %s", *name)
err = deleteBatchJobQueue(*name, 10*time.Minute, conn)
if err != nil {
log.Printf("[ERROR] Failed to delete Batch Job Queue %s: %s", *name, err)
}
}

return nil
}

func TestAccAWSBatchJobQueue_basic(t *testing.T) {
var jq batch.JobQueueDetail
ri := acctest.RandInt()
Expand Down
Loading

0 comments on commit 680afc8

Please sign in to comment.