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

resource/aws_codebuild_project: Properly handle setting cache type NO_CACHE #4134

Merged
merged 2 commits into from
Apr 11, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 not `NO_CACHE`) The location where the AWS CodeBuild project stores cached resources. For type `S3` the value must be a valid S3 bucket name/prefix.
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be slightly clearer to state 'Required when cache type is S3'. On my first read through my eyes jumped over the not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great catch, updated to: Required when cache type is S3 👍


`environment` supports the following:

Expand Down