-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Changes from 1 commit
222c479
4855e64
2ae27e7
220e9e3
56b5336
5cc7321
c691946
91c5f2d
8a963ff
51e997d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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": { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be renamed to Optionally, we can also use the name generation documented at https://github.com/hashicorp/terraform-provider-aws/blob/master/docs/contributing/contribution-checklists.md#adding-resource-name-generation-support |
||||||
Type: schema.TypeString, | ||||||
Required: true, | ||||||
ForceNew: true, | ||||||
}, | ||||||
|
||||||
"provider_type": { | ||||||
Type: schema.TypeString, | ||||||
Required: true, | ||||||
ForceNew: true, | ||||||
ValidateFunc: validation.StringInSlice([]string{ | ||||||
codestarconnections.ProviderTypeBitbucket, | ||||||
}, false), | ||||||
shuheiktgw marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
}, | ||||||
}, | ||||||
} | ||||||
} | ||||||
|
||||||
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) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We prefer error messages to use the Go 1.13 error wrapping verb
Suggested change
|
||||||
} | ||||||
|
||||||
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{ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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
|
||||||
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 { | ||||||
gdavison marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
return fmt.Errorf("error deleting codestar connection: %s", err) | ||||||
} | ||||||
|
||||||
return nil | ||||||
} |
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, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
gdavison marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) | ||
} |
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 | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Normally, we prefer to follow the API, but in the case of "standard" outputs such as
arn
, it's ok to just have thearn
and removeconnection_arn