From 222c479d4d4120974d98230d8c1b800c0f7ddb64 Mon Sep 17 00:00:00 2001 From: shuheiktgw Date: Tue, 3 Nov 2020 09:07:45 +0900 Subject: [PATCH 01/10] Add aws_codestarconnections_connection resource MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Kévin Sénéchal --- aws/provider.go | 1 + ...urce_aws_codestarconnections_connection.go | 112 ++++++++++++ ...aws_codestarconnections_connection_test.go | 70 +++++++ .../r/codestarconnections_connection.markdown | 171 ++++++++++++++++++ 4 files changed, 354 insertions(+) create mode 100644 aws/resource_aws_codestarconnections_connection.go create mode 100644 aws/resource_aws_codestarconnections_connection_test.go create mode 100644 website/docs/r/codestarconnections_connection.markdown diff --git a/aws/provider.go b/aws/provider.go index 8075ec7d6a6..fe199021a6a 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -512,6 +512,7 @@ func Provider() *schema.Provider { "aws_codebuild_webhook": resourceAwsCodeBuildWebhook(), "aws_codepipeline": resourceAwsCodePipeline(), "aws_codepipeline_webhook": resourceAwsCodePipelineWebhook(), + "aws_codestarconnections_connection": resourceAwsCodeStarConnectionsConnection(), "aws_codestarnotifications_notification_rule": resourceAwsCodeStarNotificationsNotificationRule(), "aws_cur_report_definition": resourceAwsCurReportDefinition(), "aws_customer_gateway": resourceAwsCustomerGateway(), diff --git a/aws/resource_aws_codestarconnections_connection.go b/aws/resource_aws_codestarconnections_connection.go new file mode 100644 index 00000000000..a96f3b581e7 --- /dev/null +++ b/aws/resource_aws_codestarconnections_connection.go @@ -0,0 +1,112 @@ +package aws + +import ( + "fmt" + "log" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/codestarconnections" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" +) + +func resourceAwsCodeStarConnectionsConnection() *schema.Resource { + return &schema.Resource{ + Create: resourceAwsCodeStarConnectionsConnectionCreate, + Read: resourceAwsCodeStarConnectionsConnectionRead, + Delete: resourceAwsCodeStarConnectionsConnectionDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Schema: map[string]*schema.Schema{ + "arn": { + Type: schema.TypeString, + Computed: true, + }, + + "connection_arn": { + Type: schema.TypeString, + Computed: true, + }, + + "connection_status": { + Type: schema.TypeString, + Computed: true, + }, + + "connection_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "provider_type": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{ + codestarconnections.ProviderTypeBitbucket, + }, false), + }, + }, + } +} + +func resourceAwsCodeStarConnectionsConnectionCreate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).codestarconnectionsconn + + params := &codestarconnections.CreateConnectionInput{ + ConnectionName: aws.String(d.Get("connection_name").(string)), + ProviderType: aws.String(d.Get("provider_type").(string)), + } + + res, err := conn.CreateConnection(params) + if err != nil { + return fmt.Errorf("error creating codestar connection: %s", err) + } + + d.SetId(aws.StringValue(res.ConnectionArn)) + + return resourceAwsCodeStarConnectionsConnectionRead(d, meta) +} + +func resourceAwsCodeStarConnectionsConnectionRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).codestarconnectionsconn + + rule, err := conn.GetConnection(&codestarconnections.GetConnectionInput{ + ConnectionArn: aws.String(d.Id()), + }) + + if err != nil { + if isAWSErr(err, codestarconnections.ErrCodeResourceNotFoundException, "") { + log.Printf("[WARN] codestar connection (%s) not found, removing from state", d.Id()) + d.SetId("") + return nil + } + return fmt.Errorf("error reading codestar connection: %s", err) + } + + d.SetId(aws.StringValue(rule.Connection.ConnectionArn)) + d.Set("arn", rule.Connection.ConnectionArn) + d.Set("connection_arn", rule.Connection.ConnectionArn) + d.Set("connection_name", rule.Connection.ConnectionName) + d.Set("connection_status", rule.Connection.ConnectionStatus) + d.Set("provider_type", rule.Connection.ProviderType) + + return nil +} + +func resourceAwsCodeStarConnectionsConnectionDelete(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).codestarconnectionsconn + + _, err := conn.DeleteConnection(&codestarconnections.DeleteConnectionInput{ + ConnectionArn: aws.String(d.Id()), + }) + + if err != nil { + return fmt.Errorf("error deleting codestar connection: %s", err) + } + + return nil +} diff --git a/aws/resource_aws_codestarconnections_connection_test.go b/aws/resource_aws_codestarconnections_connection_test.go new file mode 100644 index 00000000000..6a090ac8fc4 --- /dev/null +++ b/aws/resource_aws_codestarconnections_connection_test.go @@ -0,0 +1,70 @@ +package aws + +import ( + "fmt" + "regexp" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/codestarconnections" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" +) + +func TestAccAWSCodeStarConnectionsConnection_Basic(t *testing.T) { + resourceName := "aws_codestarconnections_connection.test" + rName := acctest.RandomWithPrefix("tf-acc-test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSCodeStarConnectionsConnectionDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSCodeStarConnectionsConnectionConfigBasic(rName), + Check: resource.ComposeAggregateTestCheckFunc( + testAccMatchResourceAttrRegionalARN(resourceName, "id", "codestar-connections", regexp.MustCompile("connection/.+")), + testAccMatchResourceAttrRegionalARN(resourceName, "arn", "codestar-connections", regexp.MustCompile("connection/.+")), + testAccMatchResourceAttrRegionalARN(resourceName, "connection_arn", "codestar-connections", regexp.MustCompile("connection/.+")), + resource.TestCheckResourceAttr(resourceName, "provider_type", codestarconnections.ProviderTypeBitbucket), + resource.TestCheckResourceAttr(resourceName, "connection_name", rName), + resource.TestCheckResourceAttr(resourceName, "connection_status", codestarconnections.ConnectionStatusPending), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccCheckAWSCodeStarConnectionsConnectionDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).codestarconnectionsconn + + for _, rs := range s.RootModule().Resources { + switch rs.Type { + case "aws_codestarconnections_connection": + _, err := conn.GetConnection(&codestarconnections.GetConnectionInput{ + ConnectionArn: aws.String(rs.Primary.ID), + }) + + if err != nil && !isAWSErr(err, codestarconnections.ErrCodeResourceNotFoundException, "") { + return err + } + } + } + + return nil +} + +func testAccAWSCodeStarConnectionsConnectionConfigBasic(rName string) string { + return fmt.Sprintf(` +resource "aws_codestarconnections_connection" "test" { + connection_name = %[1]q + provider_type = "Bitbucket" +} +`, rName) +} diff --git a/website/docs/r/codestarconnections_connection.markdown b/website/docs/r/codestarconnections_connection.markdown new file mode 100644 index 00000000000..3d01a0acb9b --- /dev/null +++ b/website/docs/r/codestarconnections_connection.markdown @@ -0,0 +1,171 @@ +--- +subcategory: "CodeStar Connections" +layout: "aws" +page_title: "AWS: aws_codestarconnections_connection" +description: |- + Provides a CodeStar Connection +--- + +# Resource: aws_codestarconnections_connection + +Provides a CodeStar Connection. + +## Example Usage + +```hcl +resource "aws_s3_bucket" "codepipeline_bucket" { + bucket = "tf-codestarconnections-codepipeline-bucket" + acl = "private" +} + +resource "aws_codestarconnections_connection" "example" { + connection_name = "example-connection" + provider_type = "Bitbucket" +} + +resource "aws_iam_role" "codepipeline_role" { + name = "test-role" + assume_role_policy = < Date: Tue, 15 Dec 2020 08:32:34 +0900 Subject: [PATCH 02/10] Use codestarconnections.ProviderType_Values() instead Co-authored-by: Graham Davison --- aws/resource_aws_codestarconnections_connection.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/aws/resource_aws_codestarconnections_connection.go b/aws/resource_aws_codestarconnections_connection.go index a96f3b581e7..a0b8d1cb9e6 100644 --- a/aws/resource_aws_codestarconnections_connection.go +++ b/aws/resource_aws_codestarconnections_connection.go @@ -45,9 +45,7 @@ func resourceAwsCodeStarConnectionsConnection() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - ValidateFunc: validation.StringInSlice([]string{ - codestarconnections.ProviderTypeBitbucket, - }, false), + ValidateFunc: validation.StringInSlice(codestarconnections.ProviderType_Values(), false), }, }, } From 2ae27e71da89b29e158c4e5fed1d94e87d8c288a Mon Sep 17 00:00:00 2001 From: shuheiktgw Date: Tue, 15 Dec 2020 18:34:31 +0900 Subject: [PATCH 03/10] Remove connection_arn --- aws/resource_aws_codestarconnections_connection.go | 6 ------ aws/resource_aws_codestarconnections_connection_test.go | 1 - 2 files changed, 7 deletions(-) diff --git a/aws/resource_aws_codestarconnections_connection.go b/aws/resource_aws_codestarconnections_connection.go index a0b8d1cb9e6..28551696c69 100644 --- a/aws/resource_aws_codestarconnections_connection.go +++ b/aws/resource_aws_codestarconnections_connection.go @@ -25,11 +25,6 @@ func resourceAwsCodeStarConnectionsConnection() *schema.Resource { Computed: true, }, - "connection_arn": { - Type: schema.TypeString, - Computed: true, - }, - "connection_status": { Type: schema.TypeString, Computed: true, @@ -87,7 +82,6 @@ func resourceAwsCodeStarConnectionsConnectionRead(d *schema.ResourceData, meta i d.SetId(aws.StringValue(rule.Connection.ConnectionArn)) d.Set("arn", rule.Connection.ConnectionArn) - d.Set("connection_arn", rule.Connection.ConnectionArn) d.Set("connection_name", rule.Connection.ConnectionName) d.Set("connection_status", rule.Connection.ConnectionStatus) d.Set("provider_type", rule.Connection.ProviderType) diff --git a/aws/resource_aws_codestarconnections_connection_test.go b/aws/resource_aws_codestarconnections_connection_test.go index 6a090ac8fc4..7df409d4eb6 100644 --- a/aws/resource_aws_codestarconnections_connection_test.go +++ b/aws/resource_aws_codestarconnections_connection_test.go @@ -26,7 +26,6 @@ func TestAccAWSCodeStarConnectionsConnection_Basic(t *testing.T) { Check: resource.ComposeAggregateTestCheckFunc( testAccMatchResourceAttrRegionalARN(resourceName, "id", "codestar-connections", regexp.MustCompile("connection/.+")), testAccMatchResourceAttrRegionalARN(resourceName, "arn", "codestar-connections", regexp.MustCompile("connection/.+")), - testAccMatchResourceAttrRegionalARN(resourceName, "connection_arn", "codestar-connections", regexp.MustCompile("connection/.+")), resource.TestCheckResourceAttr(resourceName, "provider_type", codestarconnections.ProviderTypeBitbucket), resource.TestCheckResourceAttr(resourceName, "connection_name", rName), resource.TestCheckResourceAttr(resourceName, "connection_status", codestarconnections.ConnectionStatusPending), From 220e9e39d5f0843c343cb5e35ee0e428e47243e2 Mon Sep 17 00:00:00 2001 From: shuheiktgw Date: Tue, 15 Dec 2020 18:36:01 +0900 Subject: [PATCH 04/10] Rename connection_name to name --- aws/resource_aws_codestarconnections_connection.go | 6 +++--- aws/resource_aws_codestarconnections_connection_test.go | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/aws/resource_aws_codestarconnections_connection.go b/aws/resource_aws_codestarconnections_connection.go index 28551696c69..40fcc3fb477 100644 --- a/aws/resource_aws_codestarconnections_connection.go +++ b/aws/resource_aws_codestarconnections_connection.go @@ -30,7 +30,7 @@ func resourceAwsCodeStarConnectionsConnection() *schema.Resource { Computed: true, }, - "connection_name": { + "name": { Type: schema.TypeString, Required: true, ForceNew: true, @@ -50,7 +50,7 @@ func resourceAwsCodeStarConnectionsConnectionCreate(d *schema.ResourceData, meta conn := meta.(*AWSClient).codestarconnectionsconn params := &codestarconnections.CreateConnectionInput{ - ConnectionName: aws.String(d.Get("connection_name").(string)), + ConnectionName: aws.String(d.Get("name").(string)), ProviderType: aws.String(d.Get("provider_type").(string)), } @@ -82,7 +82,7 @@ func resourceAwsCodeStarConnectionsConnectionRead(d *schema.ResourceData, meta i d.SetId(aws.StringValue(rule.Connection.ConnectionArn)) d.Set("arn", rule.Connection.ConnectionArn) - d.Set("connection_name", rule.Connection.ConnectionName) + d.Set("name", rule.Connection.ConnectionName) d.Set("connection_status", rule.Connection.ConnectionStatus) d.Set("provider_type", rule.Connection.ProviderType) diff --git a/aws/resource_aws_codestarconnections_connection_test.go b/aws/resource_aws_codestarconnections_connection_test.go index 7df409d4eb6..5ad64f8de71 100644 --- a/aws/resource_aws_codestarconnections_connection_test.go +++ b/aws/resource_aws_codestarconnections_connection_test.go @@ -27,7 +27,7 @@ func TestAccAWSCodeStarConnectionsConnection_Basic(t *testing.T) { testAccMatchResourceAttrRegionalARN(resourceName, "id", "codestar-connections", regexp.MustCompile("connection/.+")), testAccMatchResourceAttrRegionalARN(resourceName, "arn", "codestar-connections", regexp.MustCompile("connection/.+")), resource.TestCheckResourceAttr(resourceName, "provider_type", codestarconnections.ProviderTypeBitbucket), - resource.TestCheckResourceAttr(resourceName, "connection_name", rName), + resource.TestCheckResourceAttr(resourceName, "name", rName), resource.TestCheckResourceAttr(resourceName, "connection_status", codestarconnections.ConnectionStatusPending), ), }, @@ -62,7 +62,7 @@ func testAccCheckAWSCodeStarConnectionsConnectionDestroy(s *terraform.State) err func testAccAWSCodeStarConnectionsConnectionConfigBasic(rName string) string { return fmt.Sprintf(` resource "aws_codestarconnections_connection" "test" { - connection_name = %[1]q + name = %[1]q provider_type = "Bitbucket" } `, rName) From 56b53369cef374fd712a6c1bc6c203139c6e99f3 Mon Sep 17 00:00:00 2001 From: shuheiktgw Date: Tue, 15 Dec 2020 18:44:17 +0900 Subject: [PATCH 05/10] Modify error messages --- aws/resource_aws_codestarconnections_connection.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/aws/resource_aws_codestarconnections_connection.go b/aws/resource_aws_codestarconnections_connection.go index 40fcc3fb477..cf237bcf42b 100644 --- a/aws/resource_aws_codestarconnections_connection.go +++ b/aws/resource_aws_codestarconnections_connection.go @@ -56,7 +56,7 @@ func resourceAwsCodeStarConnectionsConnectionCreate(d *schema.ResourceData, meta res, err := conn.CreateConnection(params) if err != nil { - return fmt.Errorf("error creating codestar connection: %s", err) + return fmt.Errorf("error creating CodeStar connection: %w", err) } d.SetId(aws.StringValue(res.ConnectionArn)) @@ -73,11 +73,11 @@ func resourceAwsCodeStarConnectionsConnectionRead(d *schema.ResourceData, meta i if err != nil { if isAWSErr(err, codestarconnections.ErrCodeResourceNotFoundException, "") { - log.Printf("[WARN] codestar connection (%s) not found, removing from state", d.Id()) + log.Printf("[WARN] CodeStar connection (%s) not found, removing from state", d.Id()) d.SetId("") return nil } - return fmt.Errorf("error reading codestar connection: %s", err) + return fmt.Errorf("error reading CodeStar connection: %s", err) } d.SetId(aws.StringValue(rule.Connection.ConnectionArn)) @@ -97,7 +97,7 @@ func resourceAwsCodeStarConnectionsConnectionDelete(d *schema.ResourceData, meta }) if err != nil { - return fmt.Errorf("error deleting codestar connection: %s", err) + return fmt.Errorf("error deleting CodeStar connection: %w", err) } return nil From 5cc7321d836485371be9931681e34ca0472eee38 Mon Sep 17 00:00:00 2001 From: shuheiktgw Date: Tue, 15 Dec 2020 19:31:45 +0900 Subject: [PATCH 06/10] Add a check if rule and rule.Connections is nil --- aws/resource_aws_codestarconnections_connection.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/aws/resource_aws_codestarconnections_connection.go b/aws/resource_aws_codestarconnections_connection.go index cf237bcf42b..2e2b23167c8 100644 --- a/aws/resource_aws_codestarconnections_connection.go +++ b/aws/resource_aws_codestarconnections_connection.go @@ -80,6 +80,10 @@ func resourceAwsCodeStarConnectionsConnectionRead(d *schema.ResourceData, meta i return fmt.Errorf("error reading CodeStar connection: %s", err) } + if rule == nil || rule.Connection == nil { + return fmt.Errorf("error reading CodeStar connection (%s): empty response", d.Id()) + } + d.SetId(aws.StringValue(rule.Connection.ConnectionArn)) d.Set("arn", rule.Connection.ConnectionArn) d.Set("name", rule.Connection.ConnectionName) From c6919468b80f7c622b3d371b902204a4a9be694b Mon Sep 17 00:00:00 2001 From: shuheiktgw Date: Tue, 15 Dec 2020 19:33:21 +0900 Subject: [PATCH 07/10] Rename rule to resp --- ...ource_aws_codestarconnections_connection.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/aws/resource_aws_codestarconnections_connection.go b/aws/resource_aws_codestarconnections_connection.go index 2e2b23167c8..cf372fbb839 100644 --- a/aws/resource_aws_codestarconnections_connection.go +++ b/aws/resource_aws_codestarconnections_connection.go @@ -54,12 +54,12 @@ func resourceAwsCodeStarConnectionsConnectionCreate(d *schema.ResourceData, meta ProviderType: aws.String(d.Get("provider_type").(string)), } - res, err := conn.CreateConnection(params) + resp, err := conn.CreateConnection(params) if err != nil { return fmt.Errorf("error creating CodeStar connection: %w", err) } - d.SetId(aws.StringValue(res.ConnectionArn)) + d.SetId(aws.StringValue(resp.ConnectionArn)) return resourceAwsCodeStarConnectionsConnectionRead(d, meta) } @@ -67,7 +67,7 @@ func resourceAwsCodeStarConnectionsConnectionCreate(d *schema.ResourceData, meta func resourceAwsCodeStarConnectionsConnectionRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).codestarconnectionsconn - rule, err := conn.GetConnection(&codestarconnections.GetConnectionInput{ + resp, err := conn.GetConnection(&codestarconnections.GetConnectionInput{ ConnectionArn: aws.String(d.Id()), }) @@ -80,15 +80,15 @@ func resourceAwsCodeStarConnectionsConnectionRead(d *schema.ResourceData, meta i return fmt.Errorf("error reading CodeStar connection: %s", err) } - if rule == nil || rule.Connection == nil { + if resp == nil || resp.Connection == nil { return fmt.Errorf("error reading CodeStar connection (%s): empty response", d.Id()) } - d.SetId(aws.StringValue(rule.Connection.ConnectionArn)) - d.Set("arn", rule.Connection.ConnectionArn) - d.Set("name", rule.Connection.ConnectionName) - d.Set("connection_status", rule.Connection.ConnectionStatus) - d.Set("provider_type", rule.Connection.ProviderType) + d.SetId(aws.StringValue(resp.Connection.ConnectionArn)) + d.Set("arn", resp.Connection.ConnectionArn) + d.Set("name", resp.Connection.ConnectionName) + d.Set("connection_status", resp.Connection.ConnectionStatus) + d.Set("provider_type", resp.Connection.ProviderType) return nil } From 91c5f2d9fb8ccaf474f6ccbb80d3c52991d79c41 Mon Sep 17 00:00:00 2001 From: shuheiktgw Date: Tue, 15 Dec 2020 19:37:31 +0900 Subject: [PATCH 08/10] Handle a case when a connection is not found --- aws/resource_aws_codestarconnections_connection.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/aws/resource_aws_codestarconnections_connection.go b/aws/resource_aws_codestarconnections_connection.go index cf372fbb839..80e0cea08de 100644 --- a/aws/resource_aws_codestarconnections_connection.go +++ b/aws/resource_aws_codestarconnections_connection.go @@ -101,6 +101,10 @@ func resourceAwsCodeStarConnectionsConnectionDelete(d *schema.ResourceData, meta }) if err != nil { + if isAWSErr(err, codestarconnections.ErrCodeResourceNotFoundException, "") { + return nil + } + return fmt.Errorf("error deleting CodeStar connection: %w", err) } From 8a963ff50d68e8ad276530b7178726673349504c Mon Sep 17 00:00:00 2001 From: shuheiktgw Date: Tue, 15 Dec 2020 19:52:07 +0900 Subject: [PATCH 09/10] Add TestAccAWSCodeStarConnectionsConnection_disappears --- ...urce_aws_codestarconnections_connection.go | 6 +-- ...aws_codestarconnections_connection_test.go | 44 +++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/aws/resource_aws_codestarconnections_connection.go b/aws/resource_aws_codestarconnections_connection.go index 80e0cea08de..16ffe5d4e86 100644 --- a/aws/resource_aws_codestarconnections_connection.go +++ b/aws/resource_aws_codestarconnections_connection.go @@ -37,9 +37,9 @@ func resourceAwsCodeStarConnectionsConnection() *schema.Resource { }, "provider_type": { - Type: schema.TypeString, - Required: true, - ForceNew: true, + Type: schema.TypeString, + Required: true, + ForceNew: true, ValidateFunc: validation.StringInSlice(codestarconnections.ProviderType_Values(), false), }, }, diff --git a/aws/resource_aws_codestarconnections_connection_test.go b/aws/resource_aws_codestarconnections_connection_test.go index 5ad64f8de71..a0d4ae99a78 100644 --- a/aws/resource_aws_codestarconnections_connection_test.go +++ b/aws/resource_aws_codestarconnections_connection_test.go @@ -1,6 +1,7 @@ package aws import ( + "errors" "fmt" "regexp" "testing" @@ -24,6 +25,7 @@ func TestAccAWSCodeStarConnectionsConnection_Basic(t *testing.T) { { Config: testAccAWSCodeStarConnectionsConnectionConfigBasic(rName), Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSCodeStarConnectionsConnectionExists(resourceName), testAccMatchResourceAttrRegionalARN(resourceName, "id", "codestar-connections", regexp.MustCompile("connection/.+")), testAccMatchResourceAttrRegionalARN(resourceName, "arn", "codestar-connections", regexp.MustCompile("connection/.+")), resource.TestCheckResourceAttr(resourceName, "provider_type", codestarconnections.ProviderTypeBitbucket), @@ -40,6 +42,48 @@ func TestAccAWSCodeStarConnectionsConnection_Basic(t *testing.T) { }) } +func TestAccAWSCodeStarConnectionsConnection_disappears(t *testing.T) { + resourceName := "aws_codestarconnections_connection.test" + rName := acctest.RandomWithPrefix("tf-acc-test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSCodeStarConnectionsConnectionDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSCodeStarConnectionsConnectionConfigBasic(rName), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSCodeStarConnectionsConnectionExists(resourceName), + testAccCheckResourceDisappears(testAccProvider, resourceAwsCodeStarConnectionsConnection(), resourceName), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + +func testAccCheckAWSCodeStarConnectionsConnectionExists(n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return errors.New("No CodeStar connection ID is set") + } + + conn := testAccProvider.Meta().(*AWSClient).codestarconnectionsconn + + _, err := conn.GetConnection(&codestarconnections.GetConnectionInput{ + ConnectionArn: aws.String(rs.Primary.ID), + }) + + return err + } +} + func testAccCheckAWSCodeStarConnectionsConnectionDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).codestarconnectionsconn From 51e997dcf0c6c9595b2f495c14b7b931dc18a1f5 Mon Sep 17 00:00:00 2001 From: shuheiktgw Date: Tue, 15 Dec 2020 20:28:59 +0900 Subject: [PATCH 10/10] Fix a lint error --- aws/resource_aws_codestarconnections_connection_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aws/resource_aws_codestarconnections_connection_test.go b/aws/resource_aws_codestarconnections_connection_test.go index a0d4ae99a78..40753285254 100644 --- a/aws/resource_aws_codestarconnections_connection_test.go +++ b/aws/resource_aws_codestarconnections_connection_test.go @@ -106,8 +106,8 @@ func testAccCheckAWSCodeStarConnectionsConnectionDestroy(s *terraform.State) err func testAccAWSCodeStarConnectionsConnectionConfigBasic(rName string) string { return fmt.Sprintf(` resource "aws_codestarconnections_connection" "test" { - name = %[1]q - provider_type = "Bitbucket" + name = %[1]q + provider_type = "Bitbucket" } `, rName) }