Skip to content

Commit

Permalink
Merge pull request #16006 from DrFaust92/r/codeartifact_tags
Browse files Browse the repository at this point in the history
codeartifact resources tagging support
  • Loading branch information
breathingdust authored Nov 4, 2020
2 parents a52ae00 + 23e2d14 commit 485f878
Show file tree
Hide file tree
Showing 13 changed files with 303 additions and 5 deletions.
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 @@ -36,6 +36,7 @@ var serviceNames = []string{
"cloudwatch",
"cloudwatchevents",
"cloudwatchlogs",
"codeartifact",
"codecommit",
"codedeploy",
"codepipeline",
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 @@ -30,6 +30,7 @@ var sliceServiceNames = []string{
"cloudtrail",
"cloudwatch",
"cloudwatchevents",
"codeartifact",
"codebuild",
"codedeploy",
"codepipeline",
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 @@ -37,6 +37,7 @@ var serviceNames = []string{
"cloudwatch",
"cloudwatchevents",
"cloudwatchlogs",
"codeartifact",
"codecommit",
"codedeploy",
"codepipeline",
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 @@ -26,6 +26,7 @@ import (
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/cloudwatchevents"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go/service/codeartifact"
"github.com/aws/aws-sdk-go/service/codecommit"
"github.com/aws/aws-sdk-go/service/codedeploy"
"github.com/aws/aws-sdk-go/service/codepipeline"
Expand Down Expand Up @@ -164,6 +165,8 @@ func ServiceClientType(serviceName string) string {
funcType = reflect.TypeOf(cloudwatchevents.New)
case "cloudwatchlogs":
funcType = reflect.TypeOf(cloudwatchlogs.New)
case "codeartifact":
funcType = reflect.TypeOf(codeartifact.New)
case "codecommit":
funcType = reflect.TypeOf(codecommit.New)
case "codedeploy":
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.

31 changes: 30 additions & 1 deletion aws/resource_aws_codeartifact_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import (
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/codeartifact"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func resourceAwsCodeArtifactDomain() *schema.Resource {
return &schema.Resource{
Create: resourceAwsCodeArtifactDomainCreate,
Read: resourceAwsCodeArtifactDomainRead,
Delete: resourceAwsCodeArtifactDomainDelete,
Update: resourceAwsCodeArtifactDomainUpdate,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Expand Down Expand Up @@ -53,6 +55,7 @@ func resourceAwsCodeArtifactDomain() *schema.Resource {
Type: schema.TypeInt,
Computed: true,
},
"tags": tagsSchema(),
},
}
}
Expand All @@ -64,6 +67,7 @@ func resourceAwsCodeArtifactDomainCreate(d *schema.ResourceData, meta interface{
params := &codeartifact.CreateDomainInput{
Domain: aws.String(d.Get("domain").(string)),
EncryptionKey: aws.String(d.Get("encryption_key").(string)),
Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().CodeartifactTags(),
}

domain, err := conn.CreateDomain(params)
Expand All @@ -78,6 +82,7 @@ func resourceAwsCodeArtifactDomainCreate(d *schema.ResourceData, meta interface{

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

log.Printf("[DEBUG] Reading CodeArtifact Domain: %s", d.Id())

Expand All @@ -99,17 +104,41 @@ func resourceAwsCodeArtifactDomainRead(d *schema.ResourceData, meta interface{})
return fmt.Errorf("error reading CodeArtifact Domain (%s): %w", d.Id(), err)
}

arn := aws.StringValue(sm.Domain.Arn)
d.Set("domain", sm.Domain.Name)
d.Set("arn", sm.Domain.Arn)
d.Set("arn", arn)
d.Set("encryption_key", sm.Domain.EncryptionKey)
d.Set("owner", sm.Domain.Owner)
d.Set("asset_size_bytes", sm.Domain.AssetSizeBytes)
d.Set("repository_count", sm.Domain.RepositoryCount)
d.Set("created_time", sm.Domain.CreatedTime.Format(time.RFC3339))

tags, err := keyvaluetags.CodeartifactListTags(conn, arn)

if err != nil {
return fmt.Errorf("error listing tags for CodeArtifact Domain (%s): %w", arn, err)
}

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

return nil
}

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

if d.HasChange("tags") {
o, n := d.GetChange("tags")
if err := keyvaluetags.CodeartifactUpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return fmt.Errorf("error updating CodeArtifact Domain (%s) tags: %w", d.Id(), err)
}
}

return resourceAwsCodeArtifactDomainRead(d, meta)
}

func resourceAwsCodeArtifactDomainDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).codeartifactconn
log.Printf("[DEBUG] Deleting CodeArtifact Domain: %s", d.Id())
Expand Down
80 changes: 80 additions & 0 deletions aws/resource_aws_codeartifact_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func TestAccAWSCodeArtifactDomain_basic(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "created_time"),
resource.TestCheckResourceAttrPair(resourceName, "encryption_key", "aws_kms_key.test", "arn"),
testAccCheckResourceAttrAccountID(resourceName, "owner"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
{
Expand All @@ -97,6 +98,48 @@ func TestAccAWSCodeArtifactDomain_basic(t *testing.T) {
})
}

func TestAccAWSCodeArtifactDomain_tags(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_codeartifact_domain.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPartitionHasServicePreCheck("codeartifact", t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCodeArtifactDomainDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCodeArtifactDomainConfigTags1(rName, "key1", "value1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeArtifactDomainExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSCodeArtifactDomainConfigTags2(rName, "key1", "value1updated", "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeArtifactDomainExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
{
Config: testAccAWSCodeArtifactDomainConfigTags1(rName, "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeArtifactDomainExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2")),
},
},
})
}

func TestAccAWSCodeArtifactDomain_disappears(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_codeartifact_domain.test"
Expand Down Expand Up @@ -192,3 +235,40 @@ resource "aws_codeartifact_domain" "test" {
}
`, rName)
}

func testAccAWSCodeArtifactDomainConfigTags1(rName, tagKey1, tagValue1 string) string {
return fmt.Sprintf(`
resource "aws_kms_key" "test" {
description = %[1]q
deletion_window_in_days = 7
}
resource "aws_codeartifact_domain" "test" {
domain = %[1]q
encryption_key = aws_kms_key.test.arn
tags = {
%[2]q = %[3]q
}
}
`, rName, tagKey1, tagValue1)
}

func testAccAWSCodeArtifactDomainConfigTags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string {
return fmt.Sprintf(`
resource "aws_kms_key" "test" {
description = %[1]q
deletion_window_in_days = 7
}
resource "aws_codeartifact_domain" "test" {
domain = %[1]q
encryption_key = aws_kms_key.test.arn
tags = {
%[2]q = %[3]q
%[4]q = %[5]q
}
}
`, rName, tagKey1, tagValue1, tagKey2, tagValue2)
}
Loading

0 comments on commit 485f878

Please sign in to comment.