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

removing unfound ecs service from state file #6039

Merged
merged 3 commits into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion aws/resource_aws_ecs_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,9 @@ func resourceAwsEcsServiceRead(d *schema.ResourceData, meta interface{}) error {
if d.IsNewResource() {
return resource.RetryableError(fmt.Errorf("ECS service not created yet: %q", d.Id()))
}
return resource.NonRetryableError(fmt.Errorf("No ECS service found: %q", d.Id()))
log.Printf("[WARN] ECS Service %s not found, removing from state.", d.Id())
d.SetId("")
return nil
}

service := out.Services[0]
Expand Down
45 changes: 39 additions & 6 deletions aws/resource_aws_ecs_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,30 @@ func TestAccAWSEcsService_basicImport(t *testing.T) {
ImportState: true,
ImportStateVerify: true,
},
// Test non-existent resource import
},
})
}

func TestAccAWSEcsService_disappears(t *testing.T) {
var service ecs.Service
rString := acctest.RandString(8)

clusterName := fmt.Sprintf("tf-acc-cluster-svc-w-arn-%s", rString)
tdName := fmt.Sprintf("tf-acc-td-svc-w-arn-%s", rString)
svcName := fmt.Sprintf("tf-acc-svc-w-arn-%s", rString)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEcsServiceDestroy,
Steps: []resource.TestStep{
{
ResourceName: resourceName,
ImportStateId: fmt.Sprintf("%s/nonexistent", clusterName),
ImportState: true,
ImportStateVerify: false,
ExpectError: regexp.MustCompile(`No ECS service found`),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We actually want to keep this test as its currently the only way we can verify the condition you were originally seeing. We just needed to grab something out of the new error message from import failing (properly!):

--- FAIL: TestAccAWSEcsService_basicImport (43.46s)
    testing.go:520: Step 2, expected error:

        1 error occurred:
        	* aws_ecs_service.jenkins (import id: tf-acc-cluster-svc-ye9z6h7j/nonexistent): 1 error occurred:
        	* import aws_ecs_service.jenkins result: nonexistent: import aws_ecs_service.jenkins (id: nonexistent): Terraform detected a resource with this ID doesn't
        exist. Please verify the ID is correct. You cannot import non-existent
        resources using Terraform import.





        To match:

        No ECS service found

I went with:

ExpectError:       regexp.MustCompile(`Please verify the ID is correct`),

Config: testAccAWSEcsService(clusterName, tdName, svcName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEcsServiceExists("aws_ecs_service.mongo", &service),
testAccCheckAWSEcsServiceDisappears(&service),
),
ExpectNonEmptyPlan: true,
},
},
})
Expand Down Expand Up @@ -803,6 +820,22 @@ func testAccCheckAWSEcsServiceExists(name string, service *ecs.Service) resource
}
}

func testAccCheckAWSEcsServiceDisappears(service *ecs.Service) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ecsconn

input := &ecs.DeleteServiceInput{
Cluster: aws.String(*service.ClusterArn),
Service: aws.String(*service.ServiceName),
Force: aws.Bool(true),
}

_, err := conn.DeleteService(input)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It turns out the ECS service needs some additional checking to ensure its gone INACTIVE looking at its Delete function:

func testAccCheckAWSEcsServiceDisappears(service *ecs.Service) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		conn := testAccProvider.Meta().(*AWSClient).ecsconn

		input := &ecs.DeleteServiceInput{
			Cluster: service.ClusterArn,
			Service: service.ServiceName,
			Force:   aws.Bool(true),
		}

		_, err := conn.DeleteService(input)

		if err != nil {
			return err
		}

		// Wait until it's deleted
		wait := resource.StateChangeConf{
			Pending:    []string{"ACTIVE", "DRAINING"},
			Target:     []string{"INACTIVE"},
			Timeout:    10 * time.Minute,
			MinTimeout: 1 * time.Second,
			Refresh: func() (interface{}, string, error) {
				resp, err := conn.DescribeServices(&ecs.DescribeServicesInput{
					Cluster:  service.ClusterArn,
					Services: []*string{service.ServiceName},
				})
				if err != nil {
					return resp, "FAILED", err
				}

				return resp, aws.StringValue(resp.Services[0].Status), nil
			},
		}

		_, err = wait.WaitForState()

		return err
	}
}


return err
}
}

func testAccAWSEcsService(clusterName, tdName, svcName string) string {
return fmt.Sprintf(`
resource "aws_ecs_cluster" "default" {
Expand Down