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

Added badge_enabled argument, exported badge_url parameter #3504

Merged
merged 5 commits into from
May 24, 2018
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
19 changes: 19 additions & 0 deletions aws/resource_aws_codebuild_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,16 @@ func resourceAwsCodeBuildProject() *schema.Resource {
Default: "60",
ValidateFunc: validateAwsCodeBuildTimeout,
},
"badge_enabled": {
Copy link
Contributor

Choose a reason for hiding this comment

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

In order for Terraform to determine that this parameter has changed from the API, we need to call d.Set("badge_enabled", project.Badge.BadgeEnabled) in the read function.

Type: schema.TypeBool,
Optional: true,
Default: false,
},
"badge_url": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we cannot set the badge URL via the API, Optional: true should be removed from this attribute. More information: https://www.terraform.io/docs/extend/schemas/schema-behaviors.html#optional

},
"tags": tagsSchema(),
"vpc_config": {
Type: schema.TypeList,
Expand Down Expand Up @@ -246,6 +256,10 @@ func resourceAwsCodeBuildProjectCreate(d *schema.ResourceData, meta interface{})
params.VpcConfig = expandCodeBuildVpcConfig(v.([]interface{}))
}

if v, ok := d.GetOk("badge_enabled"); ok {
params.BadgeEnabled = aws.Bool(v.(bool))
}

if v, ok := d.GetOk("tags"); ok {
params.Tags = tagsFromMapCodeBuild(v.(map[string]interface{}))
}
Expand Down Expand Up @@ -451,6 +465,7 @@ func resourceAwsCodeBuildProjectRead(d *schema.ResourceData, meta interface{}) e
d.Set("name", project.Name)
d.Set("service_role", project.ServiceRole)
d.Set("build_timeout", project.TimeoutInMinutes)
d.Set("badge_url", project.Badge.BadgeRequestUrl)
Copy link
Contributor

Choose a reason for hiding this comment

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

In order to prevent potential nil dereference panic, we need to wrap these in a nil check:

if project.Badge != nil {
  d.Set("badge_enabled", project.Badge.BadgeEnabled)
  d.Set("badge_url", project.Badge.BadgeRequestUrl)
} else {
  d.Set("badge_enabled", false)
  d.Set("badge_url", "")
}


if err := d.Set("tags", tagsToMapCodeBuild(project.Tags)); err != nil {
return err
Expand Down Expand Up @@ -501,6 +516,10 @@ func resourceAwsCodeBuildProjectUpdate(d *schema.ResourceData, meta interface{})
params.TimeoutInMinutes = aws.Int64(int64(d.Get("build_timeout").(int)))
}

if d.HasChange("badge_enabled") {
params.BadgeEnabled = aws.Bool(d.Get("badge_enabled").(bool))
}

// The documentation clearly says "The replacement set of tags for this build project."
// But its a slice of pointers so if not set for every update, they get removed.
params.Tags = tagsFromMapCodeBuild(d.Get("tags").(map[string]interface{}))
Expand Down
103 changes: 103 additions & 0 deletions aws/resource_aws_codebuild_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,25 @@ func testAccCheckAWSCodeBuildProjectDestroy(s *terraform.State) error {
return fmt.Errorf("Default error in CodeBuild Test")
}

func TestAccAWSCodeBuildProject_buildBadgeUrlValidation(t *testing.T) {
name := acctest.RandString(10)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCodeBuildProjectDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSCodebuildProjectConfig_buildBadgeUrlValidation(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeBuildProjectExists("aws_codebuild_project.vanilla_codebuild_project"),
resource.TestMatchResourceAttr("aws_codebuild_project.vanilla_codebuild_project", "badge_url", regexp.MustCompile(`\b(https?).*\b`)),
),
},
},
})
}

func testAccAWSCodeBuildProjectConfig_basic(rName, vpcConfig, vpcResources string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "codebuild_role" {
Expand Down Expand Up @@ -774,3 +793,87 @@ func testAccAWSCodeBuildProjectConfig_vpcConfig(subnets string) string {
}
`, subnets)
}

func testAccAWSCodebuildProjectConfig_buildBadgeUrlValidation(rName string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "codebuild_role" {
name = "codebuild-role-%s"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "codebuild.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}

resource "aws_iam_policy" "codebuild_policy" {
name = "codebuild-policy-%s"
path = "/service-role/"
description = "Policy used in trust relationship with CodeBuild"
policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Resource": [
"*"
],
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
}
]
}
POLICY
}

resource "aws_iam_policy_attachment" "codebuild_policy_attachment" {
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor nitpick: We generally discourage the usage of aws_iam_policy_attachment in preference of aws_iam_role_policy_attachment in this case as the former resource often provides confusing/unexpected behavior for operators if they are unfamiliar with all the caveats of using that resource.

name = "codebuild-policy-attachment-%s"
policy_arn = "${aws_iam_policy.codebuild_policy.arn}"
roles = ["${aws_iam_role.codebuild_role.id}"]
}

resource "aws_codebuild_project" "vanilla_codebuild_project" {
name = "test-project-%s"
badge_enabled = true
description = "test_codebuild_project"

service_role = "${aws_iam_role.codebuild_role.arn}"

artifacts {
type = "NO_ARTIFACTS"
}

environment {
compute_type = "BUILD_GENERAL1_SMALL"
image = "2"
type = "LINUX_CONTAINER"

environment_variable = {
"name" = "SOME_OTHERKEY"
"value" = "SOME_OTHERVALUE"
}
}

source {
type = "GITHUB"
location = "https://github.com/hashicorp/packer.git"
}

tags {
"Environment" = "Test"
}
}
`, rName, rName, rName, rName)
}
2 changes: 2 additions & 0 deletions website/docs/r/codebuild_project.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ The following arguments are supported:
* `environment` - (Required) Information about the project's build environment. Environment blocks are documented below.
* `source` - (Required) Information about the project's input source code. Source blocks are documented below.
* `vpc_config` - (Optional) Configuration for the builds to run inside a VPC. VPC config blocks are documented below.
* `badge_enabled` - (Optional) Generates a publicly-accessible URL for the projects build badge.

`artifacts` supports the following:

Expand Down Expand Up @@ -178,3 +179,4 @@ The following attributes are exported:
* `encryption_key` - The AWS Key Management Service (AWS KMS) customer master key (CMK) that was used for encrypting the build project's build output artifacts.
* `name` - The projects name.
* `service_role` - The ARN of the IAM service role.
* `badge_url` - The URL of the build badge