Skip to content

Commit

Permalink
Merging PR hashicorp#2860 - Add cache option to AWS CodeBuild projects
Browse files Browse the repository at this point in the history
r/aws_codebuild_project: Add cache options

r/aws_codebuild_project: Add documentation cache option

r/aws_codebuild_project: Do not display NO_CACHE value in the plan

r/aws_codebuild_project: Fix acceptance test

r/aws_codebuild_project: Fix indentation in code samples

r/aws_codebuild_project: Move cache type validator to validators.go

r/aws_codebuild_project: Make cache option into a *schema.List instead of *schema.Set
  • Loading branch information
kaofelix authored and opetch committed Jan 29, 2018
1 parent d3bab25 commit 7394e01
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 2 deletions.
67 changes: 67 additions & 0 deletions aws/resource_aws_codebuild_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ func resourceAwsCodeBuildProject() *schema.Resource {
},
Set: resourceAwsCodeBuildProjectArtifactsHash,
},
"cache": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateAwsCodeBuildCacheType,
},
"location": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"description": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -199,6 +217,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 @@ -277,6 +299,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 @@ -384,6 +419,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("environment", schema.NewSet(resourceAwsCodeBuildProjectEnvironmentHash, flattenAwsCodebuildProjectEnvironment(project.Environment))); err != nil {
return err
}
Expand Down Expand Up @@ -427,6 +466,16 @@ func resourceAwsCodeBuildProjectUpdate(d *schema.ResourceData, meta interface{})
params.Artifacts = &projectArtifacts
}

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 @@ -509,6 +558,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
24 changes: 22 additions & 2 deletions aws/resource_aws_codebuild_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ func testAccCheckAWSCodeBuildProjectDestroy(s *terraform.State) error {

func testAccAWSCodeBuildProjectConfig_basic(rName string) string {
return fmt.Sprintf(`
resource "aws_s3_bucket" "foo" {
bucket = "tf-test-codebuild-%s"
acl = "private"
}
resource "aws_iam_role" "codebuild_role" {
name = "codebuild-role-%s"
assume_role_policy = <<EOF
Expand Down Expand Up @@ -382,6 +387,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 = "2"
Expand All @@ -402,11 +412,16 @@ resource "aws_codebuild_project" "foo" {
"Environment" = "Test"
}
}
`, rName, rName, rName, rName)
`, rName, rName, rName, rName, rName)
}

func testAccAWSCodeBuildProjectConfig_basicUpdated(rName string) string {
return fmt.Sprintf(`
resource "aws_s3_bucket" "foo" {
bucket = "tf-test-codebuild-%s"
acl = "private"
}
resource "aws_iam_role" "codebuild_role" {
name = "codebuild-role-%s"
assume_role_policy = <<EOF
Expand Down Expand Up @@ -465,6 +480,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 = "2"
Expand All @@ -485,7 +505,7 @@ resource "aws_codebuild_project" "foo" {
"Environment" = "Test"
}
}
`, rName, rName, rName, rName)
`, rName, rName, rName, rName, rName)
}

func testAccAWSCodeBuildProjectConfig_default_timeout(rName string) string {
Expand Down
12 changes: 12 additions & 0 deletions aws/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -2097,6 +2097,18 @@ func validateAwsElastiCacheReplicationGroupAuthToken(v interface{}, k string) (w
return
}

func validateAwsCodeBuildCacheType(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
types := map[string]bool{
"S3": true,
}

if !types[value] {
errors = append(errors, fmt.Errorf("CodeBuild: Cache Type can only be S3"))
}
return
}

func validateServiceDiscoveryServiceDnsRecordsType(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
validType := []string{"SRV", "A", "AAAA"}
Expand Down
18 changes: 18 additions & 0 deletions aws/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2974,6 +2974,24 @@ func TestValidateCognitoUserPoolDomain(t *testing.T) {
}
}

func TestValidateAwsCodeBuildCacheType(t *testing.T) {
cases := []struct {
Value string
ErrCount int
}{
{Value: "S3", ErrCount: 0},
{Value: "XYZ", ErrCount: 1},
}

for _, tc := range cases {
_, errors := validateAwsCodeBuildCacheType(tc.Value, "aws_codebuild_project")

if len(errors) != tc.ErrCount {
t.Fatalf("Expected the AWS CodeBuild project artifacts type to trigger a validation error")
}
}
}

func TestValidateServiceDiscoveryServiceDnsRecordsType(t *testing.T) {
validTypes := []string{
"SRV",
Expand Down
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 = "2"
Expand Down Expand Up @@ -111,6 +121,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.

Expand All @@ -123,6 +134,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 7394e01

Please sign in to comment.