From 75db1c485d77eea0e464bd6e0a0d721f5807b11e Mon Sep 17 00:00:00 2001 From: kaofelix Date: Wed, 3 Jan 2018 23:21:37 +0100 Subject: [PATCH 01/10] r/aws_codebuild_project: Add cache options --- aws/resource_aws_codebuild_project.go | 91 ++++++++++++++++++++++ aws/resource_aws_codebuild_project_test.go | 30 ++++++- 2 files changed, 120 insertions(+), 1 deletion(-) diff --git a/aws/resource_aws_codebuild_project.go b/aws/resource_aws_codebuild_project.go index eb6afb70b6d9..cd432d6c1fae 100644 --- a/aws/resource_aws_codebuild_project.go +++ b/aws/resource_aws_codebuild_project.go @@ -59,6 +59,25 @@ func resourceAwsCodeBuildProject() *schema.Resource { }, Set: resourceAwsCodeBuildProjectArtifactsHash, }, + "cache": { + Type: schema.TypeSet, + 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, + }, + }, + }, + Set: resourceAwsCodeBuildProjectCacheHash, + }, "description": { Type: schema.TypeString, Optional: true, @@ -226,6 +245,10 @@ func resourceAwsCodeBuildProjectCreate(d *schema.ResourceData, meta interface{}) Artifacts: &projectArtifacts, } + if v, ok := d.GetOk("cache"); ok { + params.Cache = expandProjectCache(v.(*schema.Set)) + } + if v, ok := d.GetOk("description"); ok { params.Description = aws.String(v.(string)) } @@ -312,6 +335,19 @@ func expandProjectArtifacts(d *schema.ResourceData) codebuild.ProjectArtifacts { return projectArtifacts } +func expandProjectCache(s *schema.Set) *codebuild.ProjectCache { + var projectCache *codebuild.ProjectCache + + data := s.List()[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() @@ -438,6 +474,10 @@ func resourceAwsCodeBuildProjectRead(d *schema.ResourceData, meta interface{}) e return err } + if err := d.Set("cache", schema.NewSet(resourceAwsCodeBuildProjectCacheHash, flattenAwsCodebuildProjectCache(project.Cache))); err != nil { + return err + } + if err := d.Set("source", flattenAwsCodeBuildProjectSource(project.Source)); err != nil { return err } @@ -485,6 +525,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.(*schema.Set)) + } else { + params.Cache = &codebuild.ProjectCache{ + Type: aws.String("NO_CACHE"), + } + } + } + if d.HasChange("description") { params.Description = aws.String(d.Get("description").(string)) } @@ -567,6 +617,20 @@ func flattenAwsCodeBuildProjectArtifacts(artifacts *codebuild.ProjectArtifacts) return &artifactSet } +func flattenAwsCodebuildProjectCache(cache *codebuild.ProjectCache) []interface{} { + values := map[string]interface{}{} + + if cache.Type != nil { + 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{}{} @@ -630,6 +694,21 @@ func resourceAwsCodeBuildProjectArtifactsHash(v interface{}) int { return hashcode.String(buf.String()) } +func resourceAwsCodeBuildProjectCacheHash(v interface{}) int { + var buf bytes.Buffer + m := v.(map[string]interface{}) + + if m["type"] != nil { + buf.WriteString(fmt.Sprintf("%s-", m["type"].(string))) + } + + if m["location"] != nil { + buf.WriteString(fmt.Sprintf("%s-", m["location"].(string))) + } + + return hashcode.String(buf.String()) +} + func resourceAwsCodeBuildProjectEnvironmentHash(v interface{}) int { var buf bytes.Buffer m := v.(map[string]interface{}) @@ -789,6 +868,18 @@ func validateAwsCodeBuildEnvironmentType(v interface{}, k string) (ws []string, 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 validateAwsCodeBuildSourceType(v interface{}, k string) (ws []string, errors []error) { value := v.(string) types := map[string]bool{ diff --git a/aws/resource_aws_codebuild_project_test.go b/aws/resource_aws_codebuild_project_test.go index 1dd088595a2b..80097d4c636e 100644 --- a/aws/resource_aws_codebuild_project_test.go +++ b/aws/resource_aws_codebuild_project_test.go @@ -172,6 +172,24 @@ func TestAWSCodeBuildProject_artifactsNamespaceTypeValidation(t *testing.T) { } } +func TestAWSCodeBuildProject_cacheTypeValidation(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 longTestData() string { data := ` test-test-test-test-test-test-test-test-test-test- @@ -388,6 +406,11 @@ func testAccCheckAWSCodeBuildProjectDestroy(s *terraform.State) error { func testAccAWSCodeBuildProjectConfig_basic(rName, vpcConfig, vpcResources 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 = < Date: Thu, 4 Jan 2018 09:47:10 +0100 Subject: [PATCH 02/10] r/aws_codebuild_project: Add documentation cache option --- website/docs/r/codebuild_project.html.markdown | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/website/docs/r/codebuild_project.html.markdown b/website/docs/r/codebuild_project.html.markdown index d9679b5d7964..476e28128de1 100644 --- a/website/docs/r/codebuild_project.html.markdown +++ b/website/docs/r/codebuild_project.html.markdown @@ -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-" @@ -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" @@ -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. @@ -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` From 8246aff516758b2ab59aa27818425b3978b6bbd3 Mon Sep 17 00:00:00 2001 From: kaofelix Date: Thu, 4 Jan 2018 10:47:14 +0100 Subject: [PATCH 03/10] r/aws_codebuild_project: Do not display NO_CACHE value in the plan --- aws/resource_aws_codebuild_project.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/aws/resource_aws_codebuild_project.go b/aws/resource_aws_codebuild_project.go index cd432d6c1fae..605ede509a7a 100644 --- a/aws/resource_aws_codebuild_project.go +++ b/aws/resource_aws_codebuild_project.go @@ -621,7 +621,11 @@ func flattenAwsCodebuildProjectCache(cache *codebuild.ProjectCache) []interface{ values := map[string]interface{}{} if cache.Type != nil { - values["type"] = *cache.Type + if *cache.Type == "NO_CACHE" { + values["type"] = "" + } else { + values["type"] = *cache.Type + } } if cache.Location != nil { From b3a3bf4dbcac5d34bf9250a735b55ebeeeb32526 Mon Sep 17 00:00:00 2001 From: kaofelix Date: Thu, 4 Jan 2018 10:48:29 +0100 Subject: [PATCH 04/10] r/aws_codebuild_project: Fix acceptance test --- aws/resource_aws_codebuild_project_test.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/aws/resource_aws_codebuild_project_test.go b/aws/resource_aws_codebuild_project_test.go index 80097d4c636e..1fd7d3dfd6e5 100644 --- a/aws/resource_aws_codebuild_project_test.go +++ b/aws/resource_aws_codebuild_project_test.go @@ -514,6 +514,11 @@ resource "aws_codebuild_project" "foo" { 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 = < Date: Thu, 4 Jan 2018 10:48:57 +0100 Subject: [PATCH 05/10] r/aws_codebuild_project: Fix indentation in code samples --- aws/resource_aws_codebuild_project_test.go | 4 ++-- website/docs/r/codebuild_project.html.markdown | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/aws/resource_aws_codebuild_project_test.go b/aws/resource_aws_codebuild_project_test.go index 1fd7d3dfd6e5..daaa2f60b031 100644 --- a/aws/resource_aws_codebuild_project_test.go +++ b/aws/resource_aws_codebuild_project_test.go @@ -483,7 +483,7 @@ resource "aws_codebuild_project" "foo" { } cache { - type = "S3" + type = "S3" location = "${aws_s3_bucket.foo.bucket}" } @@ -578,7 +578,7 @@ resource "aws_codebuild_project" "foo" { } cache { - type = "S3" + type = "S3" location = "${aws_s3_bucket.foo.bucket}" } diff --git a/website/docs/r/codebuild_project.html.markdown b/website/docs/r/codebuild_project.html.markdown index 476e28128de1..b536e622f761 100644 --- a/website/docs/r/codebuild_project.html.markdown +++ b/website/docs/r/codebuild_project.html.markdown @@ -79,7 +79,7 @@ resource "aws_codebuild_project" "foo" { } cache { - type = "S3" + type = "S3" location = "${aws_s3_bucket.foo.bucket}" } From 536dea9dc3b8d86c4c4d30072dcafcd3a6f091d6 Mon Sep 17 00:00:00 2001 From: kaofelix Date: Mon, 8 Jan 2018 10:14:52 +0100 Subject: [PATCH 06/10] r/aws_codebuild_project: Move cache type validator to validators.go --- aws/resource_aws_codebuild_project.go | 12 ---- aws/resource_aws_codebuild_project_test.go | 18 ------ aws/validators.go | 65 ++++++++++++++++++++++ aws/validators_test.go | 18 ++++++ 4 files changed, 83 insertions(+), 30 deletions(-) diff --git a/aws/resource_aws_codebuild_project.go b/aws/resource_aws_codebuild_project.go index 605ede509a7a..61532b648ba6 100644 --- a/aws/resource_aws_codebuild_project.go +++ b/aws/resource_aws_codebuild_project.go @@ -872,18 +872,6 @@ func validateAwsCodeBuildEnvironmentType(v interface{}, k string) (ws []string, 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 validateAwsCodeBuildSourceType(v interface{}, k string) (ws []string, errors []error) { value := v.(string) types := map[string]bool{ diff --git a/aws/resource_aws_codebuild_project_test.go b/aws/resource_aws_codebuild_project_test.go index daaa2f60b031..7694e48c7484 100644 --- a/aws/resource_aws_codebuild_project_test.go +++ b/aws/resource_aws_codebuild_project_test.go @@ -172,24 +172,6 @@ func TestAWSCodeBuildProject_artifactsNamespaceTypeValidation(t *testing.T) { } } -func TestAWSCodeBuildProject_cacheTypeValidation(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 longTestData() string { data := ` test-test-test-test-test-test-test-test-test-test- diff --git a/aws/validators.go b/aws/validators.go index 79fc3c2f165d..3e314d18e0ff 100644 --- a/aws/validators.go +++ b/aws/validators.go @@ -20,6 +20,8 @@ import ( "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/structure" "github.com/hashicorp/terraform/helper/validation" + "github.com/aws/aws-sdk-go/service/gamelift" + "github.com/aws/aws-sdk-go/service/guardduty" ) // When released, replace all usage with upstream validation function: @@ -1656,6 +1658,69 @@ 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 validateGameliftOperatingSystem(v interface{}, k string) (ws []string, errors []error) { + value := v.(string) + operatingSystems := map[string]bool{ + gamelift.OperatingSystemAmazonLinux: true, + gamelift.OperatingSystemWindows2012: true, + } + + if !operatingSystems[value] { + errors = append(errors, fmt.Errorf("%q must be a valid operating system value: %q", k, value)) + } + return +} + +func validateGuardDutyIpsetFormat(v interface{}, k string) (ws []string, errors []error) { + value := v.(string) + validType := []string{ + guardduty.IpSetFormatTxt, + guardduty.IpSetFormatStix, + guardduty.IpSetFormatOtxCsv, + guardduty.IpSetFormatAlienVault, + guardduty.IpSetFormatProofPoint, + guardduty.IpSetFormatFireEye, + } + for _, str := range validType { + if value == str { + return + } + } + errors = append(errors, fmt.Errorf("expected %s to be one of %v, got %s", k, validType, value)) + return +} + +func validateGuardDutyThreatIntelSetFormat(v interface{}, k string) (ws []string, errors []error) { + value := v.(string) + validType := []string{ + guardduty.ThreatIntelSetFormatTxt, + guardduty.ThreatIntelSetFormatStix, + guardduty.ThreatIntelSetFormatOtxCsv, + guardduty.ThreatIntelSetFormatAlienVault, + guardduty.ThreatIntelSetFormatProofPoint, + guardduty.ThreatIntelSetFormatFireEye, + } + for _, str := range validType { + if value == str { + return + } + } + errors = append(errors, fmt.Errorf("expected %s to be one of %v, got %s", k, validType, value)) + return +} + func validateDynamoDbStreamSpec(d *schema.ResourceDiff) error { enabled := d.Get("stream_enabled").(bool) if enabled { diff --git a/aws/validators_test.go b/aws/validators_test.go index f241c0e20e91..8b6635edf511 100644 --- a/aws/validators_test.go +++ b/aws/validators_test.go @@ -2503,6 +2503,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 TestValidateCognitoUserGroupName(t *testing.T) { validValues := []string{ "foo", From 0090b9fc79028712396adae438eb5086b8cafc4e Mon Sep 17 00:00:00 2001 From: kaofelix Date: Mon, 8 Jan 2018 14:42:25 +0100 Subject: [PATCH 07/10] r/aws_codebuild_project: Make cache option into a *schema.List instead of *schema.Set --- aws/resource_aws_codebuild_project.go | 28 ++++++--------------------- aws/validators.go | 4 ++-- 2 files changed, 8 insertions(+), 24 deletions(-) diff --git a/aws/resource_aws_codebuild_project.go b/aws/resource_aws_codebuild_project.go index 61532b648ba6..0ea733a898be 100644 --- a/aws/resource_aws_codebuild_project.go +++ b/aws/resource_aws_codebuild_project.go @@ -60,7 +60,7 @@ func resourceAwsCodeBuildProject() *schema.Resource { Set: resourceAwsCodeBuildProjectArtifactsHash, }, "cache": { - Type: schema.TypeSet, + Type: schema.TypeList, Optional: true, MaxItems: 1, Elem: &schema.Resource{ @@ -76,7 +76,6 @@ func resourceAwsCodeBuildProject() *schema.Resource { }, }, }, - Set: resourceAwsCodeBuildProjectCacheHash, }, "description": { Type: schema.TypeString, @@ -246,7 +245,7 @@ func resourceAwsCodeBuildProjectCreate(d *schema.ResourceData, meta interface{}) } if v, ok := d.GetOk("cache"); ok { - params.Cache = expandProjectCache(v.(*schema.Set)) + params.Cache = expandProjectCache(v.([]interface{})) } if v, ok := d.GetOk("description"); ok { @@ -335,10 +334,10 @@ func expandProjectArtifacts(d *schema.ResourceData) codebuild.ProjectArtifacts { return projectArtifacts } -func expandProjectCache(s *schema.Set) *codebuild.ProjectCache { +func expandProjectCache(s []interface{}) *codebuild.ProjectCache { var projectCache *codebuild.ProjectCache - data := s.List()[0].(map[string]interface{}) + data := s[0].(map[string]interface{}) projectCache = &codebuild.ProjectCache{ Type: aws.String(data["type"].(string)), @@ -474,7 +473,7 @@ func resourceAwsCodeBuildProjectRead(d *schema.ResourceData, meta interface{}) e return err } - if err := d.Set("cache", schema.NewSet(resourceAwsCodeBuildProjectCacheHash, flattenAwsCodebuildProjectCache(project.Cache))); err != nil { + if err := d.Set("cache", flattenAwsCodebuildProjectCache(project.Cache)); err != nil { return err } @@ -527,7 +526,7 @@ func resourceAwsCodeBuildProjectUpdate(d *schema.ResourceData, meta interface{}) if d.HasChange("cache") { if v, ok := d.GetOk("cache"); ok { - params.Cache = expandProjectCache(v.(*schema.Set)) + params.Cache = expandProjectCache(v.([]interface{})) } else { params.Cache = &codebuild.ProjectCache{ Type: aws.String("NO_CACHE"), @@ -698,21 +697,6 @@ func resourceAwsCodeBuildProjectArtifactsHash(v interface{}) int { return hashcode.String(buf.String()) } -func resourceAwsCodeBuildProjectCacheHash(v interface{}) int { - var buf bytes.Buffer - m := v.(map[string]interface{}) - - if m["type"] != nil { - buf.WriteString(fmt.Sprintf("%s-", m["type"].(string))) - } - - if m["location"] != nil { - buf.WriteString(fmt.Sprintf("%s-", m["location"].(string))) - } - - return hashcode.String(buf.String()) -} - func resourceAwsCodeBuildProjectEnvironmentHash(v interface{}) int { var buf bytes.Buffer m := v.(map[string]interface{}) diff --git a/aws/validators.go b/aws/validators.go index 3e314d18e0ff..1cc0aeb3ab38 100644 --- a/aws/validators.go +++ b/aws/validators.go @@ -15,13 +15,13 @@ import ( "github.com/aws/aws-sdk-go/service/cognitoidentityprovider" "github.com/aws/aws-sdk-go/service/configservice" "github.com/aws/aws-sdk-go/service/ec2" + "github.com/aws/aws-sdk-go/service/gamelift" + "github.com/aws/aws-sdk-go/service/guardduty" "github.com/aws/aws-sdk-go/service/s3" "github.com/aws/aws-sdk-go/service/waf" "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/structure" "github.com/hashicorp/terraform/helper/validation" - "github.com/aws/aws-sdk-go/service/gamelift" - "github.com/aws/aws-sdk-go/service/guardduty" ) // When released, replace all usage with upstream validation function: From 28a14043399abffca6fba0c8ffe93ff5b8aa8043 Mon Sep 17 00:00:00 2001 From: kaofelix Date: Fri, 6 Apr 2018 13:24:45 +0200 Subject: [PATCH 08/10] validators.go: remove gamelift / guardduty functions added by accident by bad rebase --- aws/validators.go | 53 ----------------------------------------------- 1 file changed, 53 deletions(-) diff --git a/aws/validators.go b/aws/validators.go index ae6143a42c71..cdf057b2b4a5 100644 --- a/aws/validators.go +++ b/aws/validators.go @@ -15,8 +15,6 @@ import ( "github.com/aws/aws-sdk-go/service/cognitoidentityprovider" "github.com/aws/aws-sdk-go/service/configservice" "github.com/aws/aws-sdk-go/service/ec2" - "github.com/aws/aws-sdk-go/service/gamelift" - "github.com/aws/aws-sdk-go/service/guardduty" "github.com/aws/aws-sdk-go/service/s3" "github.com/aws/aws-sdk-go/service/waf" "github.com/hashicorp/terraform/helper/schema" @@ -1620,57 +1618,6 @@ func validateAwsCodeBuildCacheType(v interface{}, k string) (ws []string, errors return } -func validateGameliftOperatingSystem(v interface{}, k string) (ws []string, errors []error) { - value := v.(string) - operatingSystems := map[string]bool{ - gamelift.OperatingSystemAmazonLinux: true, - gamelift.OperatingSystemWindows2012: true, - } - - if !operatingSystems[value] { - errors = append(errors, fmt.Errorf("%q must be a valid operating system value: %q", k, value)) - } - return -} - -func validateGuardDutyIpsetFormat(v interface{}, k string) (ws []string, errors []error) { - value := v.(string) - validType := []string{ - guardduty.IpSetFormatTxt, - guardduty.IpSetFormatStix, - guardduty.IpSetFormatOtxCsv, - guardduty.IpSetFormatAlienVault, - guardduty.IpSetFormatProofPoint, - guardduty.IpSetFormatFireEye, - } - for _, str := range validType { - if value == str { - return - } - } - errors = append(errors, fmt.Errorf("expected %s to be one of %v, got %s", k, validType, value)) - return -} - -func validateGuardDutyThreatIntelSetFormat(v interface{}, k string) (ws []string, errors []error) { - value := v.(string) - validType := []string{ - guardduty.ThreatIntelSetFormatTxt, - guardduty.ThreatIntelSetFormatStix, - guardduty.ThreatIntelSetFormatOtxCsv, - guardduty.ThreatIntelSetFormatAlienVault, - guardduty.ThreatIntelSetFormatProofPoint, - guardduty.ThreatIntelSetFormatFireEye, - } - for _, str := range validType { - if value == str { - return - } - } - errors = append(errors, fmt.Errorf("expected %s to be one of %v, got %s", k, validType, value)) - return -} - func validateDynamoDbStreamSpec(d *schema.ResourceDiff) error { enabled := d.Get("stream_enabled").(bool) if enabled { From 69f1a23e55ef2725f87bd212b44b8b55754731f7 Mon Sep 17 00:00:00 2001 From: kaofelix Date: Fri, 6 Apr 2018 13:26:46 +0200 Subject: [PATCH 09/10] resource/aws_codebuild_project: Use validation.StringInSlice to validate cache type --- aws/resource_aws_codebuild_project.go | 9 ++++++--- aws/validators.go | 12 ------------ aws/validators_test.go | 18 ------------------ 3 files changed, 6 insertions(+), 33 deletions(-) diff --git a/aws/resource_aws_codebuild_project.go b/aws/resource_aws_codebuild_project.go index 06540a5b13a7..fe21d1994585 100644 --- a/aws/resource_aws_codebuild_project.go +++ b/aws/resource_aws_codebuild_project.go @@ -73,9 +73,12 @@ func resourceAwsCodeBuildProject() *schema.Resource { Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "type": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validateAwsCodeBuildCacheType, + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringInSlice([]string{ + codebuild.CacheTypeNoCache, + codebuild.CacheTypeS3, + }, false), }, "location": { Type: schema.TypeString, diff --git a/aws/validators.go b/aws/validators.go index cdf057b2b4a5..3fcbdb1b444d 100644 --- a/aws/validators.go +++ b/aws/validators.go @@ -1606,18 +1606,6 @@ 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 validateDynamoDbStreamSpec(d *schema.ResourceDiff) error { enabled := d.Get("stream_enabled").(bool) if enabled { diff --git a/aws/validators_test.go b/aws/validators_test.go index 9519464eaa08..e32679e35cc2 100644 --- a/aws/validators_test.go +++ b/aws/validators_test.go @@ -2431,24 +2431,6 @@ 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 TestValidateCognitoUserGroupName(t *testing.T) { validValues := []string{ "foo", From 1516c4e1895863c705126287a07999cd13e6477d Mon Sep 17 00:00:00 2001 From: kaofelix Date: Fri, 6 Apr 2018 16:39:26 +0200 Subject: [PATCH 10/10] resource/aws_codebuild_project: acceptance tests for codebuild project cache --- aws/resource_aws_codebuild_project.go | 1 + aws/resource_aws_codebuild_project_test.go | 174 ++++++++++++++++++--- 2 files changed, 153 insertions(+), 22 deletions(-) diff --git a/aws/resource_aws_codebuild_project.go b/aws/resource_aws_codebuild_project.go index fe21d1994585..e6d92150aee2 100644 --- a/aws/resource_aws_codebuild_project.go +++ b/aws/resource_aws_codebuild_project.go @@ -69,6 +69,7 @@ func resourceAwsCodeBuildProject() *schema.Resource { "cache": { Type: schema.TypeList, Optional: true, + Computed: true, MaxItems: 1, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ diff --git a/aws/resource_aws_codebuild_project_test.go b/aws/resource_aws_codebuild_project_test.go index f103f70d1ec0..2851b4964bc6 100644 --- a/aws/resource_aws_codebuild_project_test.go +++ b/aws/resource_aws_codebuild_project_test.go @@ -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" @@ -233,11 +276,6 @@ func testAccCheckAWSCodeBuildProjectDestroy(s *terraform.State) error { func testAccAWSCodeBuildProjectConfig_basic(rName, vpcConfig, vpcResources 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 = <