diff --git a/aws/provider.go b/aws/provider.go index 8075ec7d6a6c..fe199021a6a9 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 000000000000..a96f3b581e74 --- /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 000000000000..6a090ac8fc40 --- /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 000000000000..3d01a0acb9bf --- /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 = <