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

Add aws_codestarconnections_connection resource #15990

1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
112 changes: 112 additions & 0 deletions aws/resource_aws_codestarconnections_connection.go
Original file line number Diff line number Diff line change
@@ -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_status": {
Type: schema.TypeString,
Computed: true,
},

"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"provider_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(codestarconnections.ProviderType_Values(), false),
},
},
}
}

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

params := &codestarconnections.CreateConnectionInput{
ConnectionName: aws.String(d.Get("name").(string)),
ProviderType: aws.String(d.Get("provider_type").(string)),
}

resp, err := conn.CreateConnection(params)
if err != nil {
return fmt.Errorf("error creating CodeStar connection: %w", err)
}

d.SetId(aws.StringValue(resp.ConnectionArn))

return resourceAwsCodeStarConnectionsConnectionRead(d, meta)
}

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

resp, 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)
}

gdavison marked this conversation as resolved.
Show resolved Hide resolved
if resp == nil || resp.Connection == nil {
return fmt.Errorf("error reading CodeStar connection (%s): empty response", d.Id())
}

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
}

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 {
gdavison marked this conversation as resolved.
Show resolved Hide resolved
if isAWSErr(err, codestarconnections.ErrCodeResourceNotFoundException, "") {
return nil
}

return fmt.Errorf("error deleting CodeStar connection: %w", err)
}

return nil
}
113 changes: 113 additions & 0 deletions aws/resource_aws_codestarconnections_connection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package aws

import (
"errors"
"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(
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),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "connection_status", codestarconnections.ConnectionStatusPending),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

gdavison marked this conversation as resolved.
Show resolved Hide resolved
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

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" {
name = %[1]q
provider_type = "Bitbucket"
}
`, rName)
}
Loading