From e69f97543e09ed42da63678d7516e175d32724cf Mon Sep 17 00:00:00 2001 From: "xiaowei.wang" Date: Tue, 6 Mar 2018 15:09:55 +0100 Subject: [PATCH] ValidateFunc: arguments for resource_aws_codebuild_project + artifacts.namespace_type + artifacts.type + environment.compute_type + environment.type + source.auth.type + source.type + build_timeout --- aws/resource_aws_codebuild_project.go | 170 ++++++--------------- aws/resource_aws_codebuild_project_test.go | 157 +------------------ 2 files changed, 45 insertions(+), 282 deletions(-) diff --git a/aws/resource_aws_codebuild_project.go b/aws/resource_aws_codebuild_project.go index eb6afb70b6d..d6025575bda 100644 --- a/aws/resource_aws_codebuild_project.go +++ b/aws/resource_aws_codebuild_project.go @@ -5,7 +5,6 @@ import ( "fmt" "log" "regexp" - "strings" "time" "github.com/aws/aws-sdk-go/aws" @@ -13,6 +12,7 @@ import ( "github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/helper/validation" ) func resourceAwsCodeBuildProject() *schema.Resource { @@ -38,9 +38,12 @@ func resourceAwsCodeBuildProject() *schema.Resource { Optional: true, }, "namespace_type": { - Type: schema.TypeString, - Optional: true, - ValidateFunc: validateAwsCodeBuildArifactsNamespaceType, + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice([]string{ + codebuild.ArtifactNamespaceNone, + codebuild.ArtifactNamespaceBuildId, + }, false), }, "packaging": { Type: schema.TypeString, @@ -51,9 +54,13 @@ func resourceAwsCodeBuildProject() *schema.Resource { Optional: true, }, "type": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validateAwsCodeBuildArifactsType, + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringInSlice([]string{ + codebuild.ArtifactsTypeCodepipeline, + codebuild.ArtifactsTypeS3, + codebuild.ArtifactsTypeNoArtifacts, + }, false), }, }, }, @@ -63,7 +70,7 @@ func resourceAwsCodeBuildProject() *schema.Resource { Type: schema.TypeString, Optional: true, Computed: true, - ValidateFunc: validateAwsCodeBuildProjectDescription, + ValidateFunc: validateMaxLength(255), }, "encryption_key": { Type: schema.TypeString, @@ -77,9 +84,13 @@ func resourceAwsCodeBuildProject() *schema.Resource { Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "compute_type": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validateAwsCodeBuildEnvironmentComputeType, + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringInSlice([]string{ + codebuild.ComputeTypeBuildGeneral1Small, + codebuild.ComputeTypeBuildGeneral1Medium, + codebuild.ComputeTypeBuildGeneral1Large, + }, false), }, "environment_variable": { Type: schema.TypeList, @@ -103,9 +114,11 @@ func resourceAwsCodeBuildProject() *schema.Resource { Required: true, }, "type": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validateAwsCodeBuildEnvironmentType, + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringInSlice([]string{ + codebuild.EnvironmentTypeLinuxContainer, + }, false), }, "privileged_mode": { Type: schema.TypeBool, @@ -140,9 +153,11 @@ func resourceAwsCodeBuildProject() *schema.Resource { Optional: true, }, "type": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validateAwsCodeBuildSourceAuthType, + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringInSlice([]string{ + codebuild.SourceAuthTypeOauth, + }, false), }, }, }, @@ -158,9 +173,16 @@ func resourceAwsCodeBuildProject() *schema.Resource { Optional: true, }, "type": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validateAwsCodeBuildSourceType, + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringInSlice([]string{ + codebuild.SourceTypeCodecommit, + codebuild.SourceTypeCodepipeline, + codebuild.SourceTypeGithub, + codebuild.SourceTypeS3, + codebuild.SourceTypeBitbucket, + codebuild.SourceTypeGithubEnterprise, + }, false), }, }, }, @@ -171,14 +193,14 @@ func resourceAwsCodeBuildProject() *schema.Resource { "timeout": { Type: schema.TypeInt, Optional: true, - ValidateFunc: validateAwsCodeBuildTimeout, + ValidateFunc: validation.IntBetween(5, 480), Removed: "This field has been removed. Please use build_timeout instead", }, "build_timeout": { Type: schema.TypeInt, Optional: true, Default: "60", - ValidateFunc: validateAwsCodeBuildTimeout, + ValidateFunc: validation.IntBetween(5, 480), }, "tags": tagsSchema(), "vpc_config": { @@ -708,33 +730,6 @@ func sourceAuthToMap(sourceAuth *codebuild.SourceAuth) map[string]interface{} { return auth } -func validateAwsCodeBuildArifactsType(v interface{}, k string) (ws []string, errors []error) { - value := v.(string) - types := map[string]bool{ - "CODEPIPELINE": true, - "NO_ARTIFACTS": true, - "S3": true, - } - - if !types[value] { - errors = append(errors, fmt.Errorf("CodeBuild: Arifacts Type can only be CODEPIPELINE / NO_ARTIFACTS / S3")) - } - return -} - -func validateAwsCodeBuildArifactsNamespaceType(v interface{}, k string) (ws []string, errors []error) { - value := v.(string) - types := map[string]bool{ - "NONE": true, - "BUILD_ID": true, - } - - if !types[value] { - errors = append(errors, fmt.Errorf("CodeBuild: Arifacts Namespace Type can only be NONE / BUILD_ID")) - } - return -} - func validateAwsCodeBuildProjectName(v interface{}, k string) (ws []string, errors []error) { value := v.(string) if !regexp.MustCompile(`^[A-Za-z0-9]`).MatchString(value) { @@ -754,80 +749,3 @@ func validateAwsCodeBuildProjectName(v interface{}, k string) (ws []string, erro return } - -func validateAwsCodeBuildProjectDescription(v interface{}, k string) (ws []string, errors []error) { - value := v.(string) - if len(value) > 255 { - errors = append(errors, fmt.Errorf("%q cannot be greater than 255 characters", value)) - } - return -} - -func validateAwsCodeBuildEnvironmentComputeType(v interface{}, k string) (ws []string, errors []error) { - value := v.(string) - types := map[string]bool{ - "BUILD_GENERAL1_SMALL": true, - "BUILD_GENERAL1_MEDIUM": true, - "BUILD_GENERAL1_LARGE": true, - } - - if !types[value] { - errors = append(errors, fmt.Errorf("CodeBuild: Environment Compute Type can only be BUILD_GENERAL1_SMALL / BUILD_GENERAL1_MEDIUM / BUILD_GENERAL1_LARGE")) - } - return -} - -func validateAwsCodeBuildEnvironmentType(v interface{}, k string) (ws []string, errors []error) { - value := v.(string) - types := map[string]bool{ - "LINUX_CONTAINER": true, - } - - if !types[value] { - errors = append(errors, fmt.Errorf("CodeBuild: Environment Type can only be LINUX_CONTAINER")) - } - return -} - -func validateAwsCodeBuildSourceType(v interface{}, k string) (ws []string, errors []error) { - value := v.(string) - types := map[string]bool{ - codebuild.SourceTypeBitbucket: true, - codebuild.SourceTypeCodecommit: true, - codebuild.SourceTypeCodepipeline: true, - codebuild.SourceTypeGithub: true, - codebuild.SourceTypeS3: true, - } - s := make([]string, 0, len(types)) - - for key, _ := range types { - s = append(s, key) - } - - if !types[value] { - strings.Join(s, ", ") - errors = append(errors, fmt.Errorf("CodeBuild: Source Type can only be one of: %s", strings.Join(s, ", "))) - } - return -} - -func validateAwsCodeBuildSourceAuthType(v interface{}, k string) (ws []string, errors []error) { - value := v.(string) - types := map[string]bool{ - "OAUTH": true, - } - - if !types[value] { - errors = append(errors, fmt.Errorf("CodeBuild: Source Auth Type can only be OAUTH")) - } - return -} - -func validateAwsCodeBuildTimeout(v interface{}, k string) (ws []string, errors []error) { - value := v.(int) - - if value < 5 || value > 480 { - errors = append(errors, fmt.Errorf("%q must be greater than 5 minutes and less than 480 minutes (8 hours)", value)) - } - return -} diff --git a/aws/resource_aws_codebuild_project_test.go b/aws/resource_aws_codebuild_project_test.go index 42282d0ab95..79d96495e8a 100644 --- a/aws/resource_aws_codebuild_project_test.go +++ b/aws/resource_aws_codebuild_project_test.go @@ -91,7 +91,7 @@ func TestAccAWSCodeBuildProject_sourceAuth(t *testing.T) { Steps: []resource.TestStep{ { Config: testAccAWSCodeBuildProjectConfig_sourceAuth(name, authResource, "INVALID"), - ExpectError: regexp.MustCompile(`Source Auth Type can only be`), + ExpectError: regexp.MustCompile(`expected source.0.auth.0.type to be one of`), }, { Config: testAccAWSCodeBuildProjectConfig_sourceAuth(name, authResource, authType), @@ -133,45 +133,6 @@ func TestAccAWSCodeBuildProject_default_build_timeout(t *testing.T) { }) } -func TestAWSCodeBuildProject_artifactsTypeValidation(t *testing.T) { - cases := []struct { - Value string - ErrCount int - }{ - {Value: "CODEPIPELINE", ErrCount: 0}, - {Value: "NO_ARTIFACTS", ErrCount: 0}, - {Value: "S3", ErrCount: 0}, - {Value: "XYZ", ErrCount: 1}, - } - - for _, tc := range cases { - _, errors := validateAwsCodeBuildArifactsType(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 TestAWSCodeBuildProject_artifactsNamespaceTypeValidation(t *testing.T) { - cases := []struct { - Value string - ErrCount int - }{ - {Value: "NONE", ErrCount: 0}, - {Value: "BUILD_ID", ErrCount: 0}, - {Value: "XYZ", ErrCount: 1}, - } - - for _, tc := range cases { - _, errors := validateAwsCodeBuildArifactsNamespaceType(tc.Value, "aws_codebuild_project") - - if len(errors) != tc.ErrCount { - t.Fatalf("Expected the AWS CodeBuild project artifacts namepsace_type to trigger a validation error") - } - } -} - func longTestData() string { data := ` test-test-test-test-test-test-test-test-test-test- @@ -211,122 +172,6 @@ func TestAWSCodeBuildProject_nameValidation(t *testing.T) { } } -func TestAWSCodeBuildProject_descriptionValidation(t *testing.T) { - cases := []struct { - Value string - ErrCount int - }{ - {Value: "test", ErrCount: 0}, - {Value: longTestData(), ErrCount: 1}, - } - - for _, tc := range cases { - _, errors := validateAwsCodeBuildProjectDescription(tc.Value, "aws_codebuild_project") - - if len(errors) != tc.ErrCount { - t.Fatalf("Expected the AWS CodeBuild project description to trigger a validation error") - } - } -} - -func TestAWSCodeBuildProject_environmentComputeTypeValidation(t *testing.T) { - cases := []struct { - Value string - ErrCount int - }{ - {Value: "BUILD_GENERAL1_SMALL", ErrCount: 0}, - {Value: "BUILD_GENERAL1_MEDIUM", ErrCount: 0}, - {Value: "BUILD_GENERAL1_LARGE", ErrCount: 0}, - {Value: "BUILD_GENERAL1_VERYLARGE", ErrCount: 1}, - } - - for _, tc := range cases { - _, errors := validateAwsCodeBuildEnvironmentComputeType(tc.Value, "aws_codebuild_project") - - if len(errors) != tc.ErrCount { - t.Fatalf("Expected the AWS CodeBuild project environment compute_type to trigger a validation error") - } - } -} - -func TestAWSCodeBuildProject_environmentTypeValidation(t *testing.T) { - cases := []struct { - Value string - ErrCount int - }{ - {Value: "LINUX_CONTAINER", ErrCount: 0}, - {Value: "WINDOWS_CONTAINER", ErrCount: 1}, - } - - for _, tc := range cases { - _, errors := validateAwsCodeBuildEnvironmentType(tc.Value, "aws_codebuild_project") - - if len(errors) != tc.ErrCount { - t.Fatalf("Expected the AWS CodeBuild project environment type to trigger a validation error") - } - } -} - -func TestAWSCodeBuildProject_sourceTypeValidation(t *testing.T) { - cases := []struct { - Value string - ErrCount int - }{ - {Value: "CODECOMMIT", ErrCount: 0}, - {Value: "CODEPIPELINE", ErrCount: 0}, - {Value: "GITHUB", ErrCount: 0}, - {Value: "S3", ErrCount: 0}, - {Value: "BITBUCKET", ErrCount: 0}, - {Value: "GITLAB", ErrCount: 1}, - } - - for _, tc := range cases { - _, errors := validateAwsCodeBuildSourceType(tc.Value, "aws_codebuild_project") - - if len(errors) != tc.ErrCount { - t.Fatalf("Expected the AWS CodeBuild project source type to trigger a validation error") - } - } -} - -func TestAWSCodeBuildProject_sourceAuthTypeValidation(t *testing.T) { - cases := []struct { - Value string - ErrCount int - }{ - {Value: "OAUTH", ErrCount: 0}, - {Value: "PASSWORD", ErrCount: 1}, - } - - for _, tc := range cases { - _, errors := validateAwsCodeBuildSourceAuthType(tc.Value, "aws_codebuild_project") - - if len(errors) != tc.ErrCount { - t.Fatalf("Expected the AWS CodeBuild project source auth to trigger a validation error") - } - } -} - -func TestAWSCodeBuildProject_timeoutValidation(t *testing.T) { - cases := []struct { - Value int - ErrCount int - }{ - {Value: 10, ErrCount: 0}, - {Value: 200, ErrCount: 0}, - {Value: 1, ErrCount: 1}, - {Value: 500, ErrCount: 1}, - } - - for _, tc := range cases { - _, errors := validateAwsCodeBuildTimeout(tc.Value, "aws_codebuild_project") - - if len(errors) != tc.ErrCount { - t.Fatalf("Expected the AWS CodeBuild project timeout to trigger a validation error") - } - } -} - func testAccCheckAWSCodeBuildProjectExists(n string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n]