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: Switch artifacts from TypeSet to TypeList and verify updates #9559

Merged
merged 2 commits into from
Jul 31, 2019
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
39 changes: 25 additions & 14 deletions aws/resource_aws_codebuild_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,20 @@ func resourceAwsCodeBuildProject() *schema.Resource {
Computed: true,
},
"artifacts": {
Type: schema.TypeSet,
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if old == d.Get("name") && new == "" {
return true
}
return false
},
},
"encryption_disabled": {
Type: schema.TypeBool,
Expand All @@ -54,6 +60,12 @@ func resourceAwsCodeBuildProject() *schema.Resource {
"namespace_type": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
bflad marked this conversation as resolved.
Show resolved Hide resolved
if d.Get("artifacts.0.type") == codebuild.ArtifactsTypeS3 && old == codebuild.ArtifactNamespaceNone && new == "" {
return true
}
return false
},
ValidateFunc: validation.StringInSlice([]string{
codebuild.ArtifactNamespaceNone,
codebuild.ArtifactNamespaceBuildId,
Expand All @@ -62,6 +74,15 @@ func resourceAwsCodeBuildProject() *schema.Resource {
"packaging": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
bflad marked this conversation as resolved.
Show resolved Hide resolved
if d.Get("artifacts.0.type") == codebuild.ArtifactsTypeCodepipeline && new == "" {
return true
}
if d.Get("artifacts.0.type") == codebuild.ArtifactsTypeS3 && old == codebuild.ArtifactPackagingNone && new == "" {
return true
}
return false
},
},
"path": {
Type: schema.TypeString,
Expand All @@ -83,7 +104,6 @@ func resourceAwsCodeBuildProject() *schema.Resource {
},
},
},
Set: resourceAwsCodeBuildProjectArtifactsHash,
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see it in the diff, but we can probably get rid of the actual function definition as it is no longer being used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's still used by secondary_artifacts on line 324 and cannot be removed from there. 😉 golangci-lint is configured to automatically report unused functions.

Copy link
Contributor

Choose a reason for hiding this comment

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

Lesson learned don't use ack when you have a linter. 🙉 🙈 🙊

},
"cache": {
Type: schema.TypeList,
Expand Down Expand Up @@ -659,7 +679,7 @@ func expandProjectSecondaryArtifacts(d *schema.ResourceData) []*codebuild.Projec
}

func expandProjectArtifacts(d *schema.ResourceData) codebuild.ProjectArtifacts {
configs := d.Get("artifacts").(*schema.Set).List()
configs := d.Get("artifacts").([]interface{})
data := configs[0].(map[string]interface{})

return expandProjectArtifactData(data)
Expand Down Expand Up @@ -1215,17 +1235,8 @@ func flattenAwsCodeBuildProjectSecondaryArtifacts(artifactsList []*codebuild.Pro
return &artifactSet
}

func flattenAwsCodeBuildProjectArtifacts(artifacts *codebuild.ProjectArtifacts) *schema.Set {

artifactSet := schema.Set{
F: resourceAwsCodeBuildProjectArtifactsHash,
}

values := flattenAwsCodeBuildProjectArtifactsData(*artifacts)

artifactSet.Add(values)

return &artifactSet
func flattenAwsCodeBuildProjectArtifacts(artifacts *codebuild.ProjectArtifacts) []interface{} {
return []interface{}{flattenAwsCodeBuildProjectArtifactsData(*artifacts)}
}

func flattenAwsCodeBuildProjectArtifactsData(artifacts codebuild.ProjectArtifacts) map[string]interface{} {
Expand Down
192 changes: 192 additions & 0 deletions aws/resource_aws_codebuild_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -910,13 +910,57 @@ func TestAccAWSCodeBuildProject_Artifacts_EncryptionDisabled(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeBuildProjectExists(resourceName, &project),
resource.TestCheckResourceAttr(resourceName, "artifacts.#", "1"),
resource.TestCheckResourceAttr(resourceName, "artifacts.0.encryption_disabled", "true"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSCodebuildProjectConfig_Artifacts_EncryptionDisabled(rName, bName, false),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeBuildProjectExists(resourceName, &project),
resource.TestCheckResourceAttr(resourceName, "artifacts.#", "1"),
resource.TestCheckResourceAttr(resourceName, "artifacts.0.encryption_disabled", "false"),
),
},
},
})
}

func TestAccAWSCodeBuildProject_Artifacts_NamespaceType(t *testing.T) {
var project codebuild.Project
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_codebuild_project.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSCodeBuild(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCodeBuildProjectDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCodebuildProjectConfig_Artifacts_NamespaceType(rName, codebuild.ArtifactNamespaceBuildId),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeBuildProjectExists(resourceName, &project),
resource.TestCheckResourceAttr(resourceName, "artifacts.#", "1"),
resource.TestCheckResourceAttr(resourceName, "artifacts.0.namespace_type", codebuild.ArtifactNamespaceBuildId),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSCodebuildProjectConfig_Artifacts_NamespaceType(rName, codebuild.ArtifactNamespaceNone),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeBuildProjectExists(resourceName, &project),
resource.TestCheckResourceAttr(resourceName, "artifacts.#", "1"),
resource.TestCheckResourceAttr(resourceName, "artifacts.0.namespace_type", codebuild.ArtifactNamespaceNone),
),
},
},
})
}
Expand Down Expand Up @@ -955,6 +999,76 @@ func TestAccAWSCodeBuildProject_Artifacts_OverrideArtifactName(t *testing.T) {
})
}

func TestAccAWSCodeBuildProject_Artifacts_Packaging(t *testing.T) {
var project codebuild.Project
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_codebuild_project.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSCodeBuild(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCodeBuildProjectDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCodebuildProjectConfig_Artifacts_Packaging(rName, codebuild.ArtifactPackagingZip),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeBuildProjectExists(resourceName, &project),
resource.TestCheckResourceAttr(resourceName, "artifacts.#", "1"),
resource.TestCheckResourceAttr(resourceName, "artifacts.0.packaging", codebuild.ArtifactPackagingZip),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSCodebuildProjectConfig_Artifacts_Packaging(rName, codebuild.ArtifactPackagingNone),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeBuildProjectExists(resourceName, &project),
resource.TestCheckResourceAttr(resourceName, "artifacts.#", "1"),
resource.TestCheckResourceAttr(resourceName, "artifacts.0.packaging", codebuild.ArtifactPackagingNone),
),
},
},
})
}

func TestAccAWSCodeBuildProject_Artifacts_Path(t *testing.T) {
var project codebuild.Project
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_codebuild_project.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSCodeBuild(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCodeBuildProjectDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCodebuildProjectConfig_Artifacts_Path(rName, "path1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeBuildProjectExists(resourceName, &project),
resource.TestCheckResourceAttr(resourceName, "artifacts.#", "1"),
resource.TestCheckResourceAttr(resourceName, "artifacts.0.path", "path1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSCodebuildProjectConfig_Artifacts_Path(rName, "path2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeBuildProjectExists(resourceName, &project),
resource.TestCheckResourceAttr(resourceName, "artifacts.#", "1"),
resource.TestCheckResourceAttr(resourceName, "artifacts.0.path", "path2"),
),
},
},
})
}

func TestAccAWSCodeBuildProject_SecondaryArtifacts(t *testing.T) {
var project codebuild.Project
rName := acctest.RandomWithPrefix("tf-acc-test")
Expand Down Expand Up @@ -2203,6 +2317,32 @@ resource "aws_codebuild_project" "test" {
`, rName, encryptionDisabled)
}

func testAccAWSCodebuildProjectConfig_Artifacts_NamespaceType(rName, namespaceType string) string {
return testAccAWSCodeBuildProjectConfig_Base_ServiceRole(rName) + testAccAWSCodeBuildProjectConfig_Base_Bucket(rName) + fmt.Sprintf(`
resource "aws_codebuild_project" "test" {
name = %[1]q
service_role = "${aws_iam_role.test.arn}"

artifacts {
location = "${aws_s3_bucket.test.bucket}"
namespace_type = %[2]q
type = "S3"
}

environment {
compute_type = "BUILD_GENERAL1_SMALL"
image = "2"
type = "LINUX_CONTAINER"
}

source {
type = "GITHUB"
location = "https://github.com/hashicorp/packer.git"
}
}
`, rName, namespaceType)
}

func testAccAWSCodebuildProjectConfig_Artifacts_OverrideArtifactName(rName string, bName string, overrideArtifactName bool) string {
return testAccAWSCodeBuildProjectConfig_Base_ServiceRole(rName) + testAccAWSCodeBuildProjectConfig_Base_Bucket(bName) + fmt.Sprintf(`
resource "aws_codebuild_project" "test" {
Expand All @@ -2229,6 +2369,58 @@ resource "aws_codebuild_project" "test" {
`, rName, overrideArtifactName)
}

func testAccAWSCodebuildProjectConfig_Artifacts_Packaging(rName, packaging string) string {
return testAccAWSCodeBuildProjectConfig_Base_ServiceRole(rName) + testAccAWSCodeBuildProjectConfig_Base_Bucket(rName) + fmt.Sprintf(`
resource "aws_codebuild_project" "test" {
name = %[1]q
service_role = "${aws_iam_role.test.arn}"

artifacts {
location = "${aws_s3_bucket.test.bucket}"
packaging = %[2]q
type = "S3"
}

environment {
compute_type = "BUILD_GENERAL1_SMALL"
image = "2"
type = "LINUX_CONTAINER"
}

source {
type = "GITHUB"
location = "https://github.com/hashicorp/packer.git"
}
}
`, rName, packaging)
}

func testAccAWSCodebuildProjectConfig_Artifacts_Path(rName, path string) string {
return testAccAWSCodeBuildProjectConfig_Base_ServiceRole(rName) + testAccAWSCodeBuildProjectConfig_Base_Bucket(rName) + fmt.Sprintf(`
resource "aws_codebuild_project" "test" {
name = %[1]q
service_role = "${aws_iam_role.test.arn}"

artifacts {
location = "${aws_s3_bucket.test.bucket}"
path = %[2]q
type = "S3"
}

environment {
compute_type = "BUILD_GENERAL1_SMALL"
image = "2"
type = "LINUX_CONTAINER"
}

source {
type = "GITHUB"
location = "https://github.com/hashicorp/packer.git"
}
}
`, rName, path)
}

func testAccAWSCodebuildProjectConfig_SecondaryArtifacts(rName string, bName string) string {
return testAccAWSCodeBuildProjectConfig_Base_ServiceRole(rName) + testAccAWSCodeBuildProjectConfig_Base_Bucket(bName) + fmt.Sprintf(`
resource "aws_codebuild_project" "test" {
Expand Down