Skip to content

Commit

Permalink
Merge pull request #2860 from kaofelix/master
Browse files Browse the repository at this point in the history
Add cache option to AWS CodeBuild projects
  • Loading branch information
bflad authored Apr 6, 2018
2 parents 2e696c3 + 1516c4e commit d284993
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 0 deletions.
71 changes: 71 additions & 0 deletions aws/resource_aws_codebuild_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,28 @@ func resourceAwsCodeBuildProject() *schema.Resource {
},
Set: resourceAwsCodeBuildProjectArtifactsHash,
},
"cache": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
codebuild.CacheTypeNoCache,
codebuild.CacheTypeS3,
}, false),
},
"location": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"description": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -248,6 +270,10 @@ func resourceAwsCodeBuildProjectCreate(d *schema.ResourceData, meta interface{})
Artifacts: &projectArtifacts,
}

if v, ok := d.GetOk("cache"); ok {
params.Cache = expandProjectCache(v.([]interface{}))
}

if v, ok := d.GetOk("description"); ok {
params.Description = aws.String(v.(string))
}
Expand Down Expand Up @@ -334,6 +360,19 @@ func expandProjectArtifacts(d *schema.ResourceData) codebuild.ProjectArtifacts {
return projectArtifacts
}

func expandProjectCache(s []interface{}) *codebuild.ProjectCache {
var projectCache *codebuild.ProjectCache

data := s[0].(map[string]interface{})

projectCache = &codebuild.ProjectCache{
Type: aws.String(data["type"].(string)),
Location: aws.String(data["location"].(string)),
}

return projectCache
}

func expandProjectEnvironment(d *schema.ResourceData) *codebuild.ProjectEnvironment {
configs := d.Get("environment").(*schema.Set).List()

Expand Down Expand Up @@ -460,6 +499,10 @@ func resourceAwsCodeBuildProjectRead(d *schema.ResourceData, meta interface{}) e
return err
}

if err := d.Set("cache", flattenAwsCodebuildProjectCache(project.Cache)); err != nil {
return err
}

if err := d.Set("source", flattenAwsCodeBuildProjectSource(project.Source)); err != nil {
return err
}
Expand Down Expand Up @@ -507,6 +550,16 @@ func resourceAwsCodeBuildProjectUpdate(d *schema.ResourceData, meta interface{})
params.VpcConfig = expandCodeBuildVpcConfig(d.Get("vpc_config").([]interface{}))
}

if d.HasChange("cache") {
if v, ok := d.GetOk("cache"); ok {
params.Cache = expandProjectCache(v.([]interface{}))
} else {
params.Cache = &codebuild.ProjectCache{
Type: aws.String("NO_CACHE"),
}
}
}

if d.HasChange("description") {
params.Description = aws.String(d.Get("description").(string))
}
Expand Down Expand Up @@ -589,6 +642,24 @@ func flattenAwsCodeBuildProjectArtifacts(artifacts *codebuild.ProjectArtifacts)
return &artifactSet
}

func flattenAwsCodebuildProjectCache(cache *codebuild.ProjectCache) []interface{} {
values := map[string]interface{}{}

if cache.Type != nil {
if *cache.Type == "NO_CACHE" {
values["type"] = ""
} else {
values["type"] = *cache.Type
}
}

if cache.Location != nil {
values["location"] = *cache.Location
}

return []interface{}{values}
}

func flattenAwsCodeBuildProjectEnvironment(environment *codebuild.ProjectEnvironment) []interface{} {
envConfig := map[string]interface{}{}

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

func TestAccAWSCodeBuildProject_cache(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_cache(name, ""),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeBuildProjectExists("aws_codebuild_project.foo"),
resource.TestCheckNoResourceAttr("aws_codebuild_project.foo", "cache"),
),
},
{
Config: testAccAWSCodeBuildProjectConfig_cache(name, testAccAWSCodeBuildProjectConfig_cacheConfig("S3", "some-bucket")),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeBuildProjectExists("aws_codebuild_project.foo"),
resource.TestCheckResourceAttr("aws_codebuild_project.foo", "cache.0.type", "S3"),
resource.TestCheckResourceAttrSet("aws_codebuild_project.foo", "cache.0.location"),
),
},
{
Config: testAccAWSCodeBuildProjectConfig_cache(name, testAccAWSCodeBuildProjectConfig_cacheConfig("S3", "some-new-bucket")),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeBuildProjectExists("aws_codebuild_project.foo"),
resource.TestCheckResourceAttr("aws_codebuild_project.foo", "cache.0.type", "S3"),
resource.TestCheckResourceAttrSet("aws_codebuild_project.foo", "cache.0.location"),
),
},
{
Config: testAccAWSCodeBuildProjectConfig_cache(name, ""),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeBuildProjectExists("aws_codebuild_project.foo"),
resource.TestCheckResourceAttr("aws_codebuild_project.foo", "cache.0.type", "S3"),
resource.TestCheckResourceAttrSet("aws_codebuild_project.foo", "cache.0.location"),
),
},
},
})
}

func TestAccAWSCodeBuildProject_sourceAuth(t *testing.T) {
authResource := "FAKERESOURCE1"
authType := "OAUTH"
Expand Down Expand Up @@ -412,6 +455,104 @@ resource "aws_codebuild_project" "foo" {
`, rName, rName, rName, rName)
}

func testAccAWSCodeBuildProjectConfig_cache(rName, cacheConfig 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"
]
},
{
"Effect": "Allow",
"Action": [
"ec2:CreateNetworkInterface",
"ec2:DescribeDhcpOptions",
"ec2:DescribeNetworkInterfaces",
"ec2:DeleteNetworkInterface",
"ec2:DescribeSubnets",
"ec2:DescribeSecurityGroups",
"ec2:DescribeVpcs"
],
"Resource": "*"
}
]
}
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"
}
%s
environment {
compute_type = "BUILD_GENERAL1_SMALL"
image = "2"
type = "LINUX_CONTAINER"
environment_variable = {
"name" = "SOME_KEY"
"value" = "SOME_VALUE"
}
}
source {
type = "GITHUB"
location = "https://github.com/hashicorp/packer.git"
}
tags {
"Environment" = "Test"
}
}
`, rName, rName, rName, rName, cacheConfig)
}

func testAccAWSCodeBuildProjectConfig_default_timeout(rName string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "codebuild_role" {
Expand Down Expand Up @@ -625,3 +766,12 @@ func testAccAWSCodeBuildProjectConfig_vpcConfig(subnets string) string {
}
`, subnets)
}

func testAccAWSCodeBuildProjectConfig_cacheConfig(cacheType, cacheLocation string) string {
return fmt.Sprintf(`
cache {
type = "%s"
location = "%s"
}
`, cacheType, cacheLocation)
}
16 changes: 16 additions & 0 deletions website/docs/r/codebuild_project.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ Provides a CodeBuild Project resource.
## Example Usage

```hcl
resource "aws_s3_bucket" "foo" {
bucket = "test-bucket"
acl = "private"
}
resource "aws_iam_role" "codebuild_role" {
name = "codebuild-role-"
Expand Down Expand Up @@ -73,6 +78,11 @@ resource "aws_codebuild_project" "foo" {
type = "NO_ARTIFACTS"
}
cache {
type = "S3"
location = "${aws_s3_bucket.foo.bucket}"
}
environment {
compute_type = "BUILD_GENERAL1_SMALL"
image = "aws/codebuild/nodejs:6.3.1"
Expand Down Expand Up @@ -125,6 +135,7 @@ The following arguments are supported:
* `build_timeout` - (Optional) How long in minutes, from 5 to 480 (8 hours), for AWS CodeBuild to wait until timing out any related build that does not get marked as completed. The default is 60 minutes.
* `tags` - (Optional) A mapping of tags to assign to the resource.
* `artifacts` - (Required) Information about the project's build output artifacts. Artifact blocks are documented below.
* `cache` - (Optional) Information about the cache storage for the project. Cache blocks are documented below.
* `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.
Expand All @@ -138,6 +149,11 @@ The following arguments are supported:
* `packaging` - (Optional) The type of build output artifact to create. If `type` is set to `S3`, valid values for this parameter are: `NONE` or `ZIP`
* `path` - (Optional) If `type` is set to `S3`, this is the path to the output artifact

`cache` supports the following:

* `type` - (Required) The type of storage that will be used for the AWS CodeBuild project cache. The only valid value is `S3`.
* `location` - (Required) The location where the AWS CodeBuild project stores cached resources. Has to be an S3 bucket.

`environment` supports the following:

* `compute_type` - (Required) Information about the compute resources the build project will use. Available values for this parameter are: `BUILD_GENERAL1_SMALL`, `BUILD_GENERAL1_MEDIUM` or `BUILD_GENERAL1_LARGE`
Expand Down

0 comments on commit d284993

Please sign in to comment.