Skip to content

Commit

Permalink
Merge pull request #3666 from loivis/validatefunc/resource_aws_codebu…
Browse files Browse the repository at this point in the history
…ild_project

resourc/codebuild_project: drop custom ValidateFunc
  • Loading branch information
bflad authored Mar 16, 2018
2 parents b1a2c5c + e69f975 commit e9de732
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 282 deletions.
170 changes: 44 additions & 126 deletions aws/resource_aws_codebuild_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"fmt"
"log"
"regexp"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/codebuild"
"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 {
Expand All @@ -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,
Expand All @@ -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),
},
},
},
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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),
},
},
},
Expand All @@ -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),
},
},
},
Expand All @@ -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": {
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}
Loading

0 comments on commit e9de732

Please sign in to comment.