Skip to content

Commit

Permalink
Merge pull request #13417 from terraform-providers/ap_typelist_codebu…
Browse files Browse the repository at this point in the history
…ild_project

service/codebuild: use typelist where maxitems are 1
  • Loading branch information
anGie44 authored May 28, 2020
2 parents cf7b81f + 76c5578 commit 62fd223
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 252 deletions.
173 changes: 39 additions & 134 deletions aws/resource_aws_codebuild_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"log"
"regexp"
"strconv"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -160,7 +159,7 @@ func resourceAwsCodeBuildProject() *schema.Resource {
Computed: true,
},
"environment": {
Type: schema.TypeSet,
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Expand All @@ -178,7 +177,6 @@ func resourceAwsCodeBuildProject() *schema.Resource {
"environment_variable": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Expand Down Expand Up @@ -257,7 +255,6 @@ func resourceAwsCodeBuildProject() *schema.Resource {
},
},
},
Set: resourceAwsCodeBuildProjectEnvironmentHash,
},
"logs_config": {
Type: schema.TypeList,
Expand Down Expand Up @@ -397,7 +394,9 @@ func resourceAwsCodeBuildProject() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"auth": {
Type: schema.TypeSet,
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"resource": {
Expand All @@ -414,8 +413,6 @@ func resourceAwsCodeBuildProject() *schema.Resource {
},
},
},
Optional: true,
Set: resourceAwsCodeBuildProjectSourceAuthHash,
},
"buildspec": {
Type: schema.TypeString,
Expand Down Expand Up @@ -475,11 +472,15 @@ func resourceAwsCodeBuildProject() *schema.Resource {
Required: true,
},
"source": {
Type: schema.TypeSet,
Type: schema.TypeList,
MaxItems: 1,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"auth": {
Type: schema.TypeSet,
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"resource": {
Expand All @@ -496,8 +497,6 @@ func resourceAwsCodeBuildProject() *schema.Resource {
},
},
},
Optional: true,
Set: resourceAwsCodeBuildProjectSourceAuthHash,
},
"buildspec": {
Type: schema.TypeString,
Expand Down Expand Up @@ -548,9 +547,6 @@ func resourceAwsCodeBuildProject() *schema.Resource {
},
},
},
Required: true,
MaxItems: 1,
Set: resourceAwsCodeBuildProjectSourceHash,
},
"source_version": {
Type: schema.TypeString,
Expand Down Expand Up @@ -716,7 +712,7 @@ func resourceAwsCodeBuildProjectCreate(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error creating CodeBuild project: %s", err)
}

d.SetId(*resp.Project.Arn)
d.SetId(aws.StringValue(resp.Project.Arn))

return resourceAwsCodeBuildProjectRead(d, meta)
}
Expand Down Expand Up @@ -813,7 +809,7 @@ func expandProjectCache(s []interface{}) *codebuild.ProjectCache {
}

func expandProjectEnvironment(d *schema.ResourceData) *codebuild.ProjectEnvironment {
configs := d.Get("environment").(*schema.Set).List()
configs := d.Get("environment").([]interface{})

envConfig := configs[0].(map[string]interface{})

Expand Down Expand Up @@ -1007,7 +1003,7 @@ func expandProjectSecondarySources(d *schema.ResourceData) []*codebuild.ProjectS
}

func expandProjectSource(d *schema.ResourceData) codebuild.ProjectSource {
configs := d.Get("source").(*schema.Set).List()
configs := d.Get("source").([]interface{})

data := configs[0].(map[string]interface{})
return expandProjectSourceData(data)
Expand Down Expand Up @@ -1037,10 +1033,10 @@ func expandProjectSourceData(data map[string]interface{}) codebuild.ProjectSourc
projectSource.ReportBuildStatus = aws.Bool(data["report_build_status"].(bool))
}

if v, ok := data["auth"]; ok {
if len(v.(*schema.Set).List()) > 0 {
auth := v.(*schema.Set).List()[0].(map[string]interface{})

// Probe data for auth details (max of 1 auth per ProjectSource object)
if v, ok := data["auth"]; ok && len(v.([]interface{})) > 0 {
if auths := v.([]interface{}); auths[0] != nil {
auth := auths[0].(map[string]interface{})
projectSource.Auth = &codebuild.SourceAuth{
Type: aws.String(auth["type"].(string)),
Resource: aws.String(auth["resource"].(string)),
Expand Down Expand Up @@ -1093,7 +1089,7 @@ func resourceAwsCodeBuildProjectRead(d *schema.ResourceData, meta interface{}) e
return fmt.Errorf("error setting artifacts: %s", err)
}

if err := d.Set("environment", schema.NewSet(resourceAwsCodeBuildProjectEnvironmentHash, flattenAwsCodeBuildProjectEnvironment(project.Environment))); err != nil {
if err := d.Set("environment", flattenAwsCodeBuildProjectEnvironment(project.Environment)); err != nil {
return fmt.Errorf("error setting environment: %s", err)
}

Expand Down Expand Up @@ -1330,38 +1326,38 @@ func flattenAwsCodeBuildProjectArtifacts(artifacts *codebuild.ProjectArtifacts)
func flattenAwsCodeBuildProjectArtifactsData(artifacts codebuild.ProjectArtifacts) map[string]interface{} {
values := map[string]interface{}{}

values["type"] = *artifacts.Type
values["type"] = aws.StringValue(artifacts.Type)

if artifacts.ArtifactIdentifier != nil {
values["artifact_identifier"] = *artifacts.ArtifactIdentifier
values["artifact_identifier"] = aws.StringValue(artifacts.ArtifactIdentifier)
}

if artifacts.EncryptionDisabled != nil {
values["encryption_disabled"] = *artifacts.EncryptionDisabled
values["encryption_disabled"] = aws.BoolValue(artifacts.EncryptionDisabled)
}

if artifacts.OverrideArtifactName != nil {
values["override_artifact_name"] = *artifacts.OverrideArtifactName
values["override_artifact_name"] = aws.BoolValue(artifacts.OverrideArtifactName)
}

if artifacts.Location != nil {
values["location"] = *artifacts.Location
values["location"] = aws.StringValue(artifacts.Location)
}

if artifacts.Name != nil {
values["name"] = *artifacts.Name
values["name"] = aws.StringValue(artifacts.Name)
}

if artifacts.NamespaceType != nil {
values["namespace_type"] = *artifacts.NamespaceType
values["namespace_type"] = aws.StringValue(artifacts.NamespaceType)
}

if artifacts.Packaging != nil {
values["packaging"] = *artifacts.Packaging
values["packaging"] = aws.StringValue(artifacts.Packaging)
}

if artifacts.Path != nil {
values["path"] = *artifacts.Path
values["path"] = aws.StringValue(artifacts.Path)
}
return values
}
Expand All @@ -1383,12 +1379,12 @@ func flattenAwsCodebuildProjectCache(cache *codebuild.ProjectCache) []interface{
func flattenAwsCodeBuildProjectEnvironment(environment *codebuild.ProjectEnvironment) []interface{} {
envConfig := map[string]interface{}{}

envConfig["type"] = *environment.Type
envConfig["compute_type"] = *environment.ComputeType
envConfig["image"] = *environment.Image
envConfig["type"] = aws.StringValue(environment.Type)
envConfig["compute_type"] = aws.StringValue(environment.ComputeType)
envConfig["image"] = aws.StringValue(environment.Image)
envConfig["certificate"] = aws.StringValue(environment.Certificate)
envConfig["privileged_mode"] = *environment.PrivilegedMode
envConfig["image_pull_credentials_type"] = *environment.ImagePullCredentialsType
envConfig["privileged_mode"] = aws.BoolValue(environment.PrivilegedMode)
envConfig["image_pull_credentials_type"] = aws.StringValue(environment.ImagePullCredentialsType)

envConfig["registry_credential"] = flattenAwsCodebuildRegistryCredential(environment.RegistryCredential)

Expand Down Expand Up @@ -1443,7 +1439,7 @@ func flattenAwsCodeBuildProjectSourceData(source *codebuild.ProjectSource) inter
m["git_submodules_config"] = flattenAwsCodebuildProjectGitSubmodulesConfig(source.GitSubmodulesConfig)

if source.Auth != nil {
m["auth"] = schema.NewSet(resourceAwsCodeBuildProjectSourceAuthHash, []interface{}{sourceAuthToMap(source.Auth)})
m["auth"] = []interface{}{sourceAuthToMap(source.Auth)}
}
if source.SourceIdentifier != nil {
m["source_identifier"] = aws.StringValue(source.SourceIdentifier)
Expand All @@ -1468,7 +1464,7 @@ func flattenAwsCodeBuildVpcConfig(vpcConfig *codebuild.VpcConfig) []interface{}
if vpcConfig != nil {
values := map[string]interface{}{}

values["vpc_id"] = *vpcConfig.VpcId
values["vpc_id"] = aws.StringValue(vpcConfig.VpcId)
values["subnets"] = schema.NewSet(schema.HashString, flattenStringList(vpcConfig.Subnets))
values["security_group_ids"] = schema.NewSet(schema.HashString, flattenStringList(vpcConfig.SecurityGroupIds))

Expand Down Expand Up @@ -1516,107 +1512,16 @@ func resourceAwsCodeBuildProjectArtifactsHash(v interface{}) int {
return hashcode.String(buf.String())
}

func resourceAwsCodeBuildProjectEnvironmentHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})

environmentType := m["type"].(string)
computeType := m["compute_type"].(string)
image := m["image"].(string)
privilegedMode := m["privileged_mode"].(bool)
imagePullCredentialsType := m["image_pull_credentials_type"].(string)
environmentVariables := m["environment_variable"].([]interface{})
buf.WriteString(fmt.Sprintf("%s-", environmentType))
buf.WriteString(fmt.Sprintf("%s-", computeType))
buf.WriteString(fmt.Sprintf("%s-", image))
buf.WriteString(fmt.Sprintf("%t-", privilegedMode))
buf.WriteString(fmt.Sprintf("%s-", imagePullCredentialsType))
if v, ok := m["certificate"]; ok && v.(string) != "" {
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
}
if v, ok := m["registry_credential"]; ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
m := v.([]interface{})[0].(map[string]interface{})

if v, ok := m["credential"]; ok && v.(string) != "" {
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
}

if v, ok := m["credential_provider"]; ok && v.(string) != "" {
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
}
}
for _, e := range environmentVariables {
if e != nil { // Old statefiles might have nil values in them
ev := e.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%s:", ev["name"].(string)))
// type is sometimes not returned by the API
if v, ok := ev["type"]; ok {
buf.WriteString(fmt.Sprintf("%s:", v.(string)))
}
buf.WriteString(fmt.Sprintf("%s-", ev["value"].(string)))
}
}

return hashcode.String(buf.String())
}

func resourceAwsCodeBuildProjectSourceHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})

buf.WriteString(fmt.Sprintf("%s-", m["type"].(string)))
if v, ok := m["source_identifier"]; ok {
buf.WriteString(fmt.Sprintf("%s-", strconv.Itoa(v.(int))))
}
if v, ok := m["buildspec"]; ok {
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
}
if v, ok := m["location"]; ok {
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
}
if v, ok := m["git_clone_depth"]; ok {
buf.WriteString(fmt.Sprintf("%s-", strconv.Itoa(v.(int))))
}
if v, ok := m["git_submodules_config"]; ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
m := v.([]interface{})[0].(map[string]interface{})

if v, ok := m["fetch_submodules"]; ok {
buf.WriteString(fmt.Sprintf("%s-", strconv.FormatBool(v.(bool))))
}
}
if v, ok := m["insecure_ssl"]; ok {
buf.WriteString(fmt.Sprintf("%s-", strconv.FormatBool(v.(bool))))
}
if v, ok := m["report_build_status"]; ok {
buf.WriteString(fmt.Sprintf("%s-", strconv.FormatBool(v.(bool))))
}

return hashcode.String(buf.String())
}

func resourceAwsCodeBuildProjectSourceAuthHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})

buf.WriteString(fmt.Sprintf("%s-", m["type"].(string)))

if m["resource"] != nil {
buf.WriteString(fmt.Sprintf("%s-", m["resource"].(string)))
}

return hashcode.String(buf.String())
}

func environmentVariablesToMap(environmentVariables []*codebuild.EnvironmentVariable) []interface{} {

envVariables := []interface{}{}
if len(environmentVariables) > 0 {
for _, env := range environmentVariables {
item := map[string]interface{}{}
item["name"] = *env.Name
item["value"] = *env.Value
item["name"] = aws.StringValue(env.Name)
item["value"] = aws.StringValue(env.Value)
if env.Type != nil {
item["type"] = *env.Type
item["type"] = aws.StringValue(env.Type)
}
envVariables = append(envVariables, item)
}
Expand All @@ -1628,10 +1533,10 @@ func environmentVariablesToMap(environmentVariables []*codebuild.EnvironmentVari
func sourceAuthToMap(sourceAuth *codebuild.SourceAuth) map[string]interface{} {

auth := map[string]interface{}{}
auth["type"] = *sourceAuth.Type
auth["type"] = aws.StringValue(sourceAuth.Type)

if sourceAuth.Resource != nil {
auth["resource"] = *sourceAuth.Resource
auth["resource"] = aws.StringValue(sourceAuth.Resource)
}

return auth
Expand Down
Loading

0 comments on commit 62fd223

Please sign in to comment.