-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add aws_codestarconnections_connection resource
Co-authored-by: Kévin Sénéchal <[email protected]>
- Loading branch information
1 parent
9834fca
commit 222c479
Showing
4 changed files
with
354 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = <<EOF | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "codepipeline.amazonaws.com" | ||
}, | ||
"Action": "sts:AssumeRole" | ||
} | ||
] | ||
} | ||
EOF | ||
} | ||
resource "aws_iam_role_policy" "codepipeline_policy" { | ||
name = "codepipeline_policy" | ||
role = aws_iam_role.codepipeline_role.id | ||
policy = <<EOF | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Action": "codestar-connections:UseConnection", | ||
"Resource": "${aws_codestarconnections_connection.example.arn}" | ||
}, | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"s3:GetObject*", | ||
"s3:PutObject", | ||
"s3:PutObjectAcl" | ||
], | ||
"Resource": [ | ||
"${aws_s3_bucket.codepipeline_bucket.arn}", | ||
"${aws_s3_bucket.codepipeline_bucket.arn}/*" | ||
] | ||
}, | ||
{ | ||
"Action": [ | ||
"codebuild:BatchGetBuilds", | ||
"codebuild:StartBuild" | ||
], | ||
"Resource": "*", | ||
"Effect": "Allow" | ||
} | ||
] | ||
} | ||
EOF | ||
} | ||
data "aws_kms_alias" "s3kmskey" { | ||
name = "alias/aws/s3" | ||
} | ||
resource "aws_codepipeline" "codepipeline" { | ||
name = "tf-test-pipeline" | ||
role_arn = aws_iam_role.codepipeline_role.arn | ||
artifact_store { | ||
location = aws_s3_bucket.codepipeline_bucket.bucket | ||
type = "S3" | ||
encryption_key { | ||
id = data.aws_kms_alias.s3kmskey.arn | ||
type = "KMS" | ||
} | ||
} | ||
stage { | ||
name = "Source" | ||
action { | ||
name = "Source" | ||
category = "Source" | ||
owner = "AWS" | ||
provider = "CodeStarSourceConnection" | ||
version = "1" | ||
output_artifacts = ["source_output"] | ||
configuration = { | ||
Owner = "my-organization" | ||
ConnectionArn = aws_codestarconnections_connection.example.arn | ||
Repo = "foo/test" | ||
Branch = "master" | ||
} | ||
} | ||
} | ||
stage { | ||
name = "Build" | ||
action { | ||
name = "Build" | ||
category = "Build" | ||
owner = "AWS" | ||
provider = "CodeBuild" | ||
input_artifacts = ["source_output"] | ||
output_artifacts = ["build_output"] | ||
version = "1" | ||
configuration = { | ||
ProjectName = "test" | ||
} | ||
} | ||
} | ||
stage { | ||
name = "Deploy" | ||
action { | ||
name = "Deploy" | ||
category = "Deploy" | ||
owner = "AWS" | ||
provider = "CloudFormation" | ||
input_artifacts = ["build_output"] | ||
version = "1" | ||
configuration = { | ||
ActionMode = "REPLACE_ON_FAILURE" | ||
Capabilities = "CAPABILITY_AUTO_EXPAND,CAPABILITY_IAM" | ||
OutputFileName = "CreateStackOutput.json" | ||
StackName = "MyStack" | ||
TemplatePath = "build_output::sam-templated.yaml" | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `connection_name` - (Required) The name of the connection to be created. The name must be unique in the calling AWS account. | ||
* `provider_type` - (Required) The name of the external provider where your third-party code repository is configured. Currently, the valid provider type is `Bitbucket`, `GitHub`, or `GitHubEnterpriseServer`. | ||
|
||
## Attributes Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `id` - The codestar connection ARN. | ||
* `arn` - The codestar connection ARN. | ||
* `connection_arn` - The codestar connection ARN. | ||
* `connection_status` - The codestar connection status. Possible values are `PENDING`, `AVAILABLE` and `ERROR`. | ||
|
||
## Import | ||
|
||
CodeStar connections can be imported using the ARN, e.g. | ||
|
||
``` | ||
$ terraform import aws_codestarconnections_connection.test-connection arn:aws:codestar-connections:us-west-1:0123456789:connection/79d4d357-a2ee-41e4-b350-2fe39ae59448 | ||
``` |