Skip to content

Commit

Permalink
Merge pull request #4134 from terraform-providers/b-aws_codebuild_pro…
Browse files Browse the repository at this point in the history
…ject-NO_CACHE

resource/aws_codebuild_project: Properly handle setting cache type NO_CACHE
  • Loading branch information
bflad authored Apr 11, 2018
2 parents 62c4c61 + 7664eef commit b811b46
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 22 deletions.
49 changes: 34 additions & 15 deletions aws/resource_aws_codebuild_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/codebuild"
"github.com/hashicorp/terraform/helper/customdiff"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -69,21 +70,27 @@ func resourceAwsCodeBuildProject() *schema.Resource {
"cache": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if old == "1" && new == "0" {
return true
}
return false
},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
Optional: true,
Default: codebuild.CacheTypeNoCache,
ValidateFunc: validation.StringInSlice([]string{
codebuild.CacheTypeNoCache,
codebuild.CacheTypeS3,
}, false),
},
"location": {
Type: schema.TypeString,
Required: true,
Optional: true,
},
},
},
Expand Down Expand Up @@ -253,6 +260,20 @@ func resourceAwsCodeBuildProject() *schema.Resource {
},
},
},

CustomizeDiff: customdiff.Sequence(
func(diff *schema.ResourceDiff, v interface{}) error {
// Plan time validation for cache location
cacheType, cacheTypeOk := diff.GetOk("cache.0.type")
if !cacheTypeOk || cacheType.(string) == codebuild.CacheTypeNoCache {
return nil
}
if v, ok := diff.GetOk("cache.0.location"); ok && v.(string) != "" {
return nil
}
return fmt.Errorf(`cache location is required when cache type is %q`, cacheType.(string))
},
),
}
}

Expand Down Expand Up @@ -366,8 +387,11 @@ func expandProjectCache(s []interface{}) *codebuild.ProjectCache {
data := s[0].(map[string]interface{})

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

if v, ok := data["location"]; ok {
projectCache.Location = aws.String(v.(string))
}

return projectCache
Expand Down Expand Up @@ -643,18 +667,13 @@ func flattenAwsCodeBuildProjectArtifacts(artifacts *codebuild.ProjectArtifacts)
}

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 == nil {
return []interface{}{}
}

if cache.Location != nil {
values["location"] = *cache.Location
values := map[string]interface{}{
"location": aws.StringValue(cache.Location),
"type": aws.StringValue(cache.Type),
}

return []interface{}{values}
Expand Down
25 changes: 20 additions & 5 deletions aws/resource_aws_codebuild_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,35 +86,50 @@ func TestAccAWSCodeBuildProject_cache(t *testing.T) {
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCodeBuildProjectDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCodeBuildProjectConfig_cache(name, testAccAWSCodeBuildProjectConfig_cacheConfig("S3", "")),
ExpectError: regexp.MustCompile(`cache location is required when cache type is "S3"`),
},
{
Config: testAccAWSCodeBuildProjectConfig_cache(name, testAccAWSCodeBuildProjectConfig_cacheConfig("NO_CACHE", "")),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeBuildProjectExists("aws_codebuild_project.foo"),
resource.TestCheckResourceAttr("aws_codebuild_project.foo", "cache.#", "1"),
resource.TestCheckResourceAttr("aws_codebuild_project.foo", "cache.0.type", "NO_CACHE"),
),
},
{
Config: testAccAWSCodeBuildProjectConfig_cache(name, ""),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeBuildProjectExists("aws_codebuild_project.foo"),
resource.TestCheckNoResourceAttr("aws_codebuild_project.foo", "cache"),
resource.TestCheckResourceAttr("aws_codebuild_project.foo", "cache.#", "1"),
resource.TestCheckResourceAttr("aws_codebuild_project.foo", "cache.0.type", "NO_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.#", "1"),
resource.TestCheckResourceAttr("aws_codebuild_project.foo", "cache.0.type", "S3"),
resource.TestCheckResourceAttrSet("aws_codebuild_project.foo", "cache.0.location"),
resource.TestCheckResourceAttr("aws_codebuild_project.foo", "cache.0.location", "some-bucket"),
),
},
{
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.#", "1"),
resource.TestCheckResourceAttr("aws_codebuild_project.foo", "cache.0.type", "S3"),
resource.TestCheckResourceAttrSet("aws_codebuild_project.foo", "cache.0.location"),
resource.TestCheckResourceAttr("aws_codebuild_project.foo", "cache.0.location", "some-new-bucket"),
),
},
{
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"),
resource.TestCheckResourceAttr("aws_codebuild_project.foo", "cache.#", "1"),
resource.TestCheckResourceAttr("aws_codebuild_project.foo", "cache.0.type", "NO_CACHE"),
),
},
},
Expand Down
4 changes: 2 additions & 2 deletions website/docs/r/codebuild_project.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ The following arguments are supported:

`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. This value must be a valid S3 bucket name/prefix.
* `type` - (Optional) The type of storage that will be used for the AWS CodeBuild project cache. Valid values: `NO_CACHE` and `S3`. Defaults to `NO_CACHE`.
* `location` - (Required when cache type is `S3`) The location where the AWS CodeBuild project stores cached resources. For type `S3` the value must be a valid S3 bucket name/prefix.

`environment` supports the following:

Expand Down

0 comments on commit b811b46

Please sign in to comment.