diff --git a/.changelog/18882.txt b/.changelog/18882.txt new file mode 100644 index 00000000000..373fcfa7a7f --- /dev/null +++ b/.changelog/18882.txt @@ -0,0 +1,3 @@ +```release-note:bug +resource/aws_ecr_replication_configuration: Remove relication rules on resource deletion +``` diff --git a/aws/resource_aws_ecr_replication_configuration.go b/aws/resource_aws_ecr_replication_configuration.go index 625cb2a9ca2..fd02fa20002 100644 --- a/aws/resource_aws_ecr_replication_configuration.go +++ b/aws/resource_aws_ecr_replication_configuration.go @@ -14,7 +14,7 @@ func resourceAwsEcrReplicationConfiguration() *schema.Resource { Create: resourceAwsEcrReplicationConfigurationPut, Read: resourceAwsEcrReplicationConfigurationRead, Update: resourceAwsEcrReplicationConfigurationPut, - Delete: schema.Noop, + Delete: resourceAwsEcrReplicationConfigurationDelete, Importer: &schema.ResourceImporter{ State: schema.ImportStatePassthrough, }, @@ -98,6 +98,23 @@ func resourceAwsEcrReplicationConfigurationRead(d *schema.ResourceData, meta int return nil } +func resourceAwsEcrReplicationConfigurationDelete(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).ecrconn + + input := ecr.PutReplicationConfigurationInput{ + ReplicationConfiguration: &ecr.ReplicationConfiguration{ + Rules: []*ecr.ReplicationRule{}, + }, + } + + _, err := conn.PutReplicationConfiguration(&input) + if err != nil { + return fmt.Errorf("error deleting ECR Replication Configuration: %w", err) + } + + return nil +} + func expandEcrReplicationConfigurationReplicationConfiguration(data []interface{}) *ecr.ReplicationConfiguration { if len(data) == 0 || data[0] == nil { return nil diff --git a/aws/resource_aws_ecr_replication_configuration_test.go b/aws/resource_aws_ecr_replication_configuration_test.go index 37ba7f1a524..133be0214c6 100644 --- a/aws/resource_aws_ecr_replication_configuration_test.go +++ b/aws/resource_aws_ecr_replication_configuration_test.go @@ -16,7 +16,7 @@ func TestAccAWSEcrReplicationConfiguration_basic(t *testing.T) { PreCheck: func() { testAccPreCheck(t) }, ErrorCheck: testAccErrorCheck(t, ecr.EndpointsID), Providers: testAccProviders, - CheckDestroy: nil, + CheckDestroy: testAccCheckAWSEcrReplicationConfigurationDestroy, Steps: []resource.TestStep{ { Config: testAccAWSEcrReplicationConfiguration(testAccGetAlternateRegion()), @@ -46,10 +46,41 @@ func testAccCheckAWSEcrReplicationConfigurationExists(name string) resource.Test return fmt.Errorf("Not found: %s", name) } + conn := testAccProvider.Meta().(*AWSClient).ecrconn + out, err := conn.DescribeRegistry(&ecr.DescribeRegistryInput{}) + if err != nil { + return fmt.Errorf("ECR replication rules not found: %w", err) + } + + if len(out.ReplicationConfiguration.Rules) == 0 { + return fmt.Errorf("ECR replication rules not found") + } + return nil } } +func testAccCheckAWSEcrReplicationConfigurationDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).ecrconn + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_ecr_replication_configuration" { + continue + } + + out, err := conn.DescribeRegistry(&ecr.DescribeRegistryInput{}) + if err != nil { + return err + } + + if len(out.ReplicationConfiguration.Rules) != 0 { + return fmt.Errorf("ECR replication rules found") + } + } + + return nil +} + func testAccAWSEcrReplicationConfiguration(region string) string { return fmt.Sprintf(` data "aws_caller_identity" "current" {}