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 support resource tags for aws codepipeline resources #8993

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
11 changes: 11 additions & 0 deletions aws/resource_aws_codepipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ func resourceAwsCodePipeline() *schema.Resource {
},
},
},
"tags": tagsSchema(),
},
}
}
Expand All @@ -169,6 +170,7 @@ func resourceAwsCodePipelineCreate(d *schema.ResourceData, meta interface{}) err
conn := meta.(*AWSClient).codepipelineconn
params := &codepipeline.CreatePipelineInput{
Pipeline: expandAwsCodePipeline(d),
Tags: tagsFromMapCodePipeline(d.Get("tags").(map[string]interface{})),
}

var resp *codepipeline.CreatePipelineOutput
Expand Down Expand Up @@ -447,6 +449,11 @@ func resourceAwsCodePipelineRead(d *schema.ResourceData, meta interface{}) error
d.Set("arn", metadata.PipelineArn)
d.Set("name", pipeline.Name)
d.Set("role_arn", pipeline.RoleArn)

if err := saveTagsCodePipeline(conn, d); err != nil {
return err
}

return nil
}

Expand All @@ -465,6 +472,10 @@ func resourceAwsCodePipelineUpdate(d *schema.ResourceData, meta interface{}) err
d.Id(), err)
}

if err := setTagsCodePipeline(conn, d); err != nil {
return fmt.Errorf("Error updating CodePipeline tags: %s", d.Id())
}

return resourceAwsCodePipelineRead(d, meta)
}

Expand Down
167 changes: 167 additions & 0 deletions aws/resource_aws_codepipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,55 @@ func TestAccAWSCodePipeline_deployWithServiceRole(t *testing.T) {
})
}

func TestAccAWSCodePipeline_tags(t *testing.T) {
if os.Getenv("GITHUB_TOKEN") == "" {
t.Skip("Environment variable GITHUB_TOKEN is not set")
}

name := acctest.RandString(10)
resourceName := "aws_codepipeline.bar"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCodePipelineDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCodePipelineConfigWithTags(name, "tag1value", "tag2value"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodePipelineExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.Name", fmt.Sprintf("test-pipeline-%s", name)),
resource.TestCheckResourceAttr(resourceName, "tags.tag1", "tag1value"),
resource.TestCheckResourceAttr(resourceName, "tags.tag2", "tag2value"),
),
},
{
Config: testAccAWSCodePipelineConfigWithTags(name, "tag1valueUpdate", "tag2valueUpdate"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodePipelineExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.Name", fmt.Sprintf("test-pipeline-%s", name)),
resource.TestCheckResourceAttr(resourceName, "tags.tag1", "tag1valueUpdate"),
resource.TestCheckResourceAttr(resourceName, "tags.tag2", "tag2valueUpdate"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSCodePipelineConfig_basic(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodePipelineExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
},
})
}

func testAccCheckAWSCodePipelineExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -695,3 +744,121 @@ resource "aws_codepipeline" "bar" {
}
`, rName, rName, rName, rName)
}

func testAccAWSCodePipelineConfigWithTags(rName, tag1, tag2 string) string {
return fmt.Sprintf(`
resource "aws_s3_bucket" "foo" {
bucket = "tf-test-pipeline-%[1]s"
acl = "private"
}

resource "aws_iam_role" "codepipeline_role" {
name = "codepipeline-role-%[1]s"

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": [
"s3:GetObject",
"s3:GetObjectVersion",
"s3:GetBucketVersioning"
],
"Resource": [
"${aws_s3_bucket.foo.arn}",
"${aws_s3_bucket.foo.arn}/*"
]
},
{
"Effect": "Allow",
"Action": [
"codebuild:BatchGetBuilds",
"codebuild:StartBuild"
],
"Resource": "*"
}
]
}
EOF
}

resource "aws_codepipeline" "bar" {
name = "test-pipeline-%[1]s"
role_arn = "${aws_iam_role.codepipeline_role.arn}"

artifact_store {
location = "${aws_s3_bucket.foo.bucket}"
type = "S3"

encryption_key {
id = "1234"
type = "KMS"
}
}

stage {
name = "Source"

action {
name = "Source"
category = "Source"
owner = "ThirdParty"
provider = "GitHub"
version = "1"
output_artifacts = ["test"]

configuration = {
Owner = "lifesum-terraform"
Repo = "test"
Branch = "master"
}
}
}

stage {
name = "Build"

action {
name = "Build"
category = "Build"
owner = "AWS"
provider = "CodeBuild"
input_artifacts = ["test"]
version = "1"

configuration = {
ProjectName = "test"
}
}
}

tags = {
Name = "test-pipeline-%[1]s"
tag1 = %[2]q
tag2 = %[3]q
}
}
`, rName, tag1, tag2)
}
10 changes: 10 additions & 0 deletions aws/resource_aws_codepipeline_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ func resourceAwsCodePipelineWebhook() *schema.Resource {
ForceNew: true,
Required: true,
},
"tags": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
},
},
}
}
Expand Down Expand Up @@ -144,6 +149,7 @@ func resourceAwsCodePipelineWebhookCreate(d *schema.ResourceData, meta interface
TargetPipeline: aws.String(d.Get("target_pipeline").(string)),
AuthenticationConfiguration: extractCodePipelineWebhookAuthConfig(authType, authConfig),
},
Tags: tagsFromMapCodePipeline(d.Get("tags").(map[string]interface{})),
}

webhook, err := conn.PutWebhook(request)
Expand Down Expand Up @@ -265,6 +271,10 @@ func resourceAwsCodePipelineWebhookRead(d *schema.ResourceData, meta interface{}
return fmt.Errorf("error setting filter: %s", err)
}

if err := d.Set("tags", tagsToMapCodePipeline(webhook.Tags)); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

return nil
}

Expand Down
78 changes: 78 additions & 0 deletions aws/resource_aws_codepipeline_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,58 @@ func TestAccAWSCodePipelineWebhook_unauthenticated(t *testing.T) {
})
}

func TestAccAWSCodePipelineWebhook_tags(t *testing.T) {
if os.Getenv("GITHUB_TOKEN") == "" {
t.Skip("Environment variable GITHUB_TOKEN is not set")
}

resourceName := "aws_codepipeline_webhook.bar"
name := acctest.RandString(10)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCodePipelineDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCodePipelineWebhookConfigWithTags(name, "tag1value", "tag2value"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodePipelineExists("aws_codepipeline.bar"),
testAccCheckAWSCodePipelineWebhookExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.Name", fmt.Sprintf("test-webhook-%s", name)),
resource.TestCheckResourceAttr(resourceName, "tags.tag1", "tag1value"),
resource.TestCheckResourceAttr(resourceName, "tags.tag2", "tag2value"),
),
},
{
Config: testAccAWSCodePipelineWebhookConfigWithTags(name, "tag1valueUpdate", "tag2valueUpdate"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodePipelineExists("aws_codepipeline.bar"),
testAccCheckAWSCodePipelineWebhookExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.Name", fmt.Sprintf("test-webhook-%s", name)),
resource.TestCheckResourceAttr(resourceName, "tags.tag1", "tag1valueUpdate"),
resource.TestCheckResourceAttr(resourceName, "tags.tag2", "tag2valueUpdate"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSCodePipelineWebhookConfig_basic(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodePipelineExists("aws_codepipeline.bar"),
testAccCheckAWSCodePipelineWebhookExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
},
})
}

func testAccCheckAWSCodePipelineWebhookExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -181,6 +233,32 @@ resource "aws_codepipeline_webhook" "bar" {
`, rName))
}

func testAccAWSCodePipelineWebhookConfigWithTags(rName, tag1, tag2 string) string {
return testAccAWSCodePipelineWebhookConfig_codePipeline(rName, fmt.Sprintf(`
resource "aws_codepipeline_webhook" "bar" {
name = "test-webhook-%[1]s"
authentication = "GITHUB_HMAC"
target_action = "Source"
target_pipeline = "${aws_codepipeline.bar.name}"

authentication_configuration {
secret_token = "super-secret"
}

filter {
json_path = "$.ref"
match_equals = "refs/head/{Branch}"
}

tags = {
Name = "test-webhook-%[1]s"
tag1 = %[2]q
tag2 = %[3]q
}
}
`, rName, tag1, tag2))
}

func testAccAWSCodePipelineWebhookConfig_codePipeline(rName string, webhook string) string {
return fmt.Sprintf(`
resource "aws_s3_bucket" "foo" {
Expand Down
Loading