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

r/codebuild_project: Support type for environment_variable #4021

Merged
merged 9 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
15 changes: 15 additions & 0 deletions aws/resource_aws_codebuild_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ func resourceAwsCodeBuildProject() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
codebuild.EnvironmentVariableTypeParameterStore,
codebuild.EnvironmentVariableTypePlaintext,
}, false),
},
},
},
},
Expand Down Expand Up @@ -412,6 +420,10 @@ func expandProjectEnvironment(d *schema.ResourceData) *codebuild.ProjectEnvironm
projectEnvironmentVar.Value = &v
}

if v := config["type"].(string); v != "" {
projectEnvironmentVar.Type = &v
}

projectEnvironmentVariables = append(projectEnvironmentVariables, projectEnvironmentVar)
}

Expand Down Expand Up @@ -782,6 +794,9 @@ func environmentVariablesToMap(environmentVariables []*codebuild.EnvironmentVari
item := map[string]interface{}{}
item["name"] = *env.Name
item["value"] = *env.Value
if env.Type != nil {
item["type"] = *env.Type
}
envVariables = append(envVariables, item)
}
}
Expand Down
198 changes: 197 additions & 1 deletion aws/resource_aws_codebuild_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,34 @@ func TestAccAWSCodeBuildProject_sourceAuth(t *testing.T) {
})
}

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

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCodeBuildProjectDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCodeBuildProjectConfig_environment_variable_plaintext_type(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeBuildProjectExists("aws_codebuild_project.foo"),
resource.TestCheckResourceAttr(
"aws_codebuild_project.foo", "environment.0.environment_variable.0.type", "PLAINTEXT"),
),
},
{
Config: testAccAWSCodeBuildProjectConfig_environment_variable_parameter_store_type(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeBuildProjectExists("aws_codebuild_project.foo"),
resource.TestCheckResourceAttr(
"aws_codebuild_project.foo", "environment.0.environment_variable.0.type", "PARAMETER_STORE"),
),
},
},
})
}

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

Expand Down Expand Up @@ -710,7 +738,7 @@ resource "aws_codebuild_project" "foo" {
source {
type = "GITHUB"
location = "https://github.com/hashicorp/packer.git"

auth {
resource = "%[2]s"
type = "%[3]s"
Expand Down Expand Up @@ -767,6 +795,174 @@ func testAccAWSCodeBuildProjectConfig_vpcConfig(subnets string) string {
`, subnets)
}

func testAccAWSCodeBuildProjectConfig_environment_variable_plaintext_type(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" {
name = "codebuild-policy-attachment-%s"
policy_arn = "${aws_iam_policy.codebuild_policy.arn}"
roles = ["${aws_iam_role.codebuild_role.id}"]
}

resource "aws_codebuild_project" "foo" {
name = "test-project-%s"
description = "test_codebuild_project"
build_timeout = "5"
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_KEY"
"value" = "SOME_VALUE"
"type" = "PLAINTEXT"
}
}

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

tags {
"Environment" = "Test"
}
}
`, rName, rName, rName, rName)
}

func testAccAWSCodeBuildProjectConfig_environment_variable_parameter_store_type(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" {
name = "codebuild-policy-attachment-%s"
policy_arn = "${aws_iam_policy.codebuild_policy.arn}"
roles = ["${aws_iam_role.codebuild_role.id}"]
}

resource "aws_codebuild_project" "foo" {
name = "test-project-%s"
description = "test_codebuild_project"
build_timeout = "50"
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"
"type" = "PARAMETER_STORE"
}
}

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

tags {
"Environment" = "Test"
}
}
`, rName, rName, rName, rName)
}

func testAccAWSCodeBuildProjectConfig_cacheConfig(cacheType, cacheLocation string) string {
return fmt.Sprintf(`
cache {
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/codebuild_project.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ The following arguments are supported:

* `name` - (Required) The environment variable's name or key.
* `value` - (Required) The environment variable's value.
* `type` - (Optional) The type of environment variable. Valid values: `PARAMETER_STORE`, `PLAINTEXT`.

`source` supports the following:

Expand Down