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 tag support for aws_codestarconnections_connection resource #16835

Merged
merged 4 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions aws/internal/keyvaluetags/generators/listtags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var serviceNames = []string{
"codecommit",
"codedeploy",
"codepipeline",
"codestarconnections",
"codestarnotifications",
"cognitoidentity",
"cognitoidentityprovider",
Expand Down
1 change: 1 addition & 0 deletions aws/internal/keyvaluetags/generators/servicetags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var sliceServiceNames = []string{
"codebuild",
"codedeploy",
"codepipeline",
"codestarconnections",
"configservice",
"databasemigrationservice",
"datapipeline",
Expand Down
1 change: 1 addition & 0 deletions aws/internal/keyvaluetags/generators/updatetags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var serviceNames = []string{
"codecommit",
"codedeploy",
"codepipeline",
"codestarconnections",
"codestarnotifications",
"cognitoidentity",
"cognitoidentityprovider",
Expand Down
18 changes: 18 additions & 0 deletions aws/internal/keyvaluetags/list_tags_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/aws/aws-sdk-go/service/codecommit"
"github.com/aws/aws-sdk-go/service/codedeploy"
"github.com/aws/aws-sdk-go/service/codepipeline"
"github.com/aws/aws-sdk-go/service/codestarconnections"
"github.com/aws/aws-sdk-go/service/codestarnotifications"
"github.com/aws/aws-sdk-go/service/cognitoidentity"
"github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
Expand Down Expand Up @@ -175,6 +176,8 @@ func ServiceClientType(serviceName string) string {
funcType = reflect.TypeOf(codedeploy.New)
case "codepipeline":
funcType = reflect.TypeOf(codepipeline.New)
case "codestarconnections":
funcType = reflect.TypeOf(codestarconnections.New)
case "codestarnotifications":
funcType = reflect.TypeOf(codestarnotifications.New)
case "cognitoidentity":
Expand Down
28 changes: 28 additions & 0 deletions aws/internal/keyvaluetags/service_tags_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions aws/internal/keyvaluetags/update_tags_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 24 additions & 1 deletion aws/resource_aws_codestarconnections_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func resourceAwsCodeStarConnectionsConnection() *schema.Resource {
Expand Down Expand Up @@ -43,6 +44,13 @@ func resourceAwsCodeStarConnectionsConnection() *schema.Resource {
ForceNew: true,
ValidateFunc: validation.StringInSlice(codestarconnections.ProviderType_Values(), false),
},

"tags": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updating resource tags should not require resource recreation -- we can add an Update function that only handles tags. 👍

Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
Expand All @@ -55,6 +63,10 @@ func resourceAwsCodeStarConnectionsConnectionCreate(d *schema.ResourceData, meta
ProviderType: aws.String(d.Get("provider_type").(string)),
}

if v, ok := d.GetOk("tags"); ok {
params.Tags = keyvaluetags.New(v.(map[string]interface{})).IgnoreAws().CodestarconnectionsTags()
}

resp, err := conn.CreateConnection(params)
if err != nil {
return fmt.Errorf("error creating CodeStar connection: %w", err)
Expand All @@ -67,6 +79,7 @@ func resourceAwsCodeStarConnectionsConnectionCreate(d *schema.ResourceData, meta

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

resp, err := conn.GetConnection(&codestarconnections.GetConnectionInput{
ConnectionArn: aws.String(d.Id()),
Expand All @@ -84,12 +97,22 @@ func resourceAwsCodeStarConnectionsConnectionRead(d *schema.ResourceData, meta i
return fmt.Errorf("error reading CodeStar connection (%s): empty response", d.Id())
}

d.SetId(aws.StringValue(resp.Connection.ConnectionArn))
arn := aws.StringValue(resp.Connection.ConnectionArn)
d.SetId(arn)
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)

tags, err := keyvaluetags.CodestarconnectionsListTags(conn, arn)
if err != nil {
return fmt.Errorf("error listing tags for CodeStar connection (%s): %w", arn, err)
}

if err := d.Set("tags", tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags for CodeStar connection (%s): %w", arn, err)
}

return nil
}

Expand Down
41 changes: 41 additions & 0 deletions aws/resource_aws_codestarconnections_connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,33 @@ func TestAccAWSCodeStarConnectionsConnection_disappears(t *testing.T) {
})
}

func TestAccAWSCodeStarConnectionsConnection_tags(t *testing.T) {
var v codestarconnections.Connection
resourceName := "aws_codestarconnections_connection.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPartitionHasServicePreCheck(codestarconnections.EndpointsID, t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCodeStarConnectionsConnectionDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCodeStarConnectionsConnectionConfigTags(rName),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Will parameterize this to match the Contributing Guide so it is easier to add the update step.

Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSCodeStarConnectionsConnectionExists(resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "tags.Name", rName),
resource.TestCheckResourceAttr(resourceName, "tags.Environment", "production"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckAWSCodeStarConnectionsConnectionExists(n string, v *codestarconnections.Connection) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -118,3 +145,17 @@ resource "aws_codestarconnections_connection" "test" {
}
`, rName)
}

func testAccAWSCodeStarConnectionsConnectionConfigTags(rName string) string {
return fmt.Sprintf(`
resource "aws_codestarconnections_connection" "test" {
name = %[1]q
provider_type = "Bitbucket"

tags = {
Name = %[1]q
Environment = "production"
}
}
`, rName)
}
1 change: 1 addition & 0 deletions website/docs/r/codestarconnections_connection.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ The following arguments are supported:

* `name` - (Required) The name of the connection to be created. The name must be unique in the calling AWS account. Changing `name` will create a new resource.
* `provider_type` - (Required) The name of the external provider where your third-party code repository is configured. Valid values are `Bitbucket`, `GitHub`, or `GitHubEnterpriseServer`. Changing `provider_type` will create a new resource.
* `tags` - (Optional) An array of key:value pairs to associate with the resource.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Terraform the type is map

Suggested change
* `tags` - (Optional) An array of key:value pairs to associate with the resource.
* `tags` - (Optional) Map of key-value resource tags to associate with the resource.


## Attributes Reference

Expand Down