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: Implements git_submodules_config block #10952

Merged
merged 3 commits into from
Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
63 changes: 62 additions & 1 deletion aws/resource_aws_codebuild_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,19 @@ func resourceAwsCodeBuildProject() *schema.Resource {
Optional: true,
ValidateFunc: validation.IntAtLeast(0),
},
"git_submodules_config": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"fetch_submodules": {
Type: schema.TypeBool,
Required: true,
},
},
},
},
"insecure_ssl": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -508,6 +521,19 @@ func resourceAwsCodeBuildProject() *schema.Resource {
Optional: true,
ValidateFunc: validation.IntAtLeast(0),
},
"git_submodules_config": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"fetch_submodules": {
Type: schema.TypeBool,
Required: true,
},
},
},
},
"insecure_ssl": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -1000,6 +1026,21 @@ func expandProjectSourceData(data map[string]interface{}) codebuild.ProjectSourc
}
}

// Only valid for CODECOMMIT source types.
if sourceType == codebuild.SourceTypeCodecommit {

Choose a reason for hiding this comment

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

Why is it valid only for Codecommit and not for other providers? It does work for us with GitHub when we set it manually (not through Terraform).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No specific reason except that for some reason, I thought it was only valid for CodeCommit. Not sure where I thought I had seen that, but clearly should work with everything. AWS docs doesn't seem to have anything mentioning that it should be CodeCommit only.

Seeing as this has been merged, and released, I'm not sure what is the best way forward to resolve this.

if v, ok := data["git_submodules_config"]; ok && len(v.([]interface{})) > 0 {
config := v.([]interface{})[0].(map[string]interface{})

gitSubmodulesConfig := &codebuild.GitSubmodulesConfig{}

if v, ok := config["fetch_submodules"]; ok {
gitSubmodulesConfig.FetchSubmodules = aws.Bool(v.(bool))
}

projectSource.GitSubmodulesConfig = gitSubmodulesConfig
}
}

return projectSource
}

Expand Down Expand Up @@ -1366,17 +1407,30 @@ func flattenAwsCodeBuildProjectSourceData(source *codebuild.ProjectSource) inter
"type": aws.StringValue(source.Type),
}

m["git_submodules_config"] = flattenAwsCodebuildProjectGitSubmodulesConfig(source.GitSubmodulesConfig)

if source.Auth != nil {
m["auth"] = schema.NewSet(resourceAwsCodeBuildProjectSourceAuthHash, []interface{}{sourceAuthToMap(source.Auth)})
}

if source.SourceIdentifier != nil {
m["source_identifier"] = aws.StringValue(source.SourceIdentifier)
}

return m
}

func flattenAwsCodebuildProjectGitSubmodulesConfig(config *codebuild.GitSubmodulesConfig) []interface{} {
if config == nil {
return []interface{}{}
}

values := map[string]interface{}{
"fetch_submodules": aws.BoolValue(config.FetchSubmodules),
}

return []interface{}{values}
}

func flattenAwsCodeBuildVpcConfig(vpcConfig *codebuild.VpcConfig) []interface{} {
if vpcConfig != nil {
values := map[string]interface{}{}
Expand Down Expand Up @@ -1490,6 +1544,13 @@ func resourceAwsCodeBuildProjectSourceHash(v interface{}) int {
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))))
jValdron marked this conversation as resolved.
Show resolved Hide resolved
}
}
if v, ok := m["insecure_ssl"]; ok {
buf.WriteString(fmt.Sprintf("%s-", strconv.FormatBool(v.(bool))))
}
Expand Down
158 changes: 154 additions & 4 deletions aws/resource_aws_codebuild_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,80 @@ func TestAccAWSCodeBuildProject_Source_GitCloneDepth(t *testing.T) {
})
}

func TestAccAWSCodeBuildProject_Source_GitSubmodulesConfig(t *testing.T) {
jValdron marked this conversation as resolved.
Show resolved Hide resolved
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_Source_GitSubmodulesConfig(rName, true),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeBuildProjectExists(resourceName, &project),
resource.TestCheckResourceAttr(resourceName, "source.3389748318.git_submodules_config.#", "1"),
resource.TestCheckResourceAttr(resourceName, "source.3389748318.git_submodules_config.0.fetch_submodules", "true"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSCodeBuildProjectConfig_Source_GitSubmodulesConfig(rName, false),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeBuildProjectExists(resourceName, &project),
resource.TestCheckResourceAttr(resourceName, "source.3338377709.git_submodules_config.#", "1"),
resource.TestCheckResourceAttr(resourceName, "source.3338377709.git_submodules_config.0.fetch_submodules", "false"),
),
},
},
})
}

func TestAccAWSCodeBuildProject_SecondarySources_GitSubmodulesConfig(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_SecondarySources_GitSubmodulesConfig(rName, true),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeBuildProjectExists(resourceName, &project),
resource.TestCheckResourceAttr(resourceName, "secondary_sources.2336845252.git_submodules_config.#", "1"),
resource.TestCheckResourceAttr(resourceName, "secondary_sources.2336845252.git_submodules_config.0.fetch_submodules", "true"),
resource.TestCheckResourceAttr(resourceName, "secondary_sources.2080741754.git_submodules_config.#", "1"),
resource.TestCheckResourceAttr(resourceName, "secondary_sources.2080741754.git_submodules_config.0.fetch_submodules", "true"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSCodeBuildProjectConfig_SecondarySources_GitSubmodulesConfig(rName, false),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeBuildProjectExists(resourceName, &project),
resource.TestCheckResourceAttr(resourceName, "secondary_sources.3511868825.git_submodules_config.#", "1"),
resource.TestCheckResourceAttr(resourceName, "secondary_sources.3511868825.git_submodules_config.0.fetch_submodules", "false"),
resource.TestCheckResourceAttr(resourceName, "secondary_sources.1651171204.git_submodules_config.#", "1"),
resource.TestCheckResourceAttr(resourceName, "secondary_sources.1651171204.git_submodules_config.0.fetch_submodules", "false"),
),
},
},
})
}

func TestAccAWSCodeBuildProject_Source_InsecureSSL(t *testing.T) {
var project codebuild.Project
rName := acctest.RandomWithPrefix("tf-acc-test")
Expand Down Expand Up @@ -1711,8 +1785,8 @@ func TestAccAWSCodeBuildProject_SecondarySources_CodeCommit(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeBuildProjectExists(resourceName, &project),
resource.TestCheckResourceAttr(resourceName, "source.3715340088.type", "CODECOMMIT"),
resource.TestCheckResourceAttr(resourceName, "secondary_sources.3525046785.source_identifier", "secondarySource1"),
resource.TestCheckResourceAttr(resourceName, "secondary_sources.2644986630.source_identifier", "secondarySource2"),
resource.TestCheckResourceAttr(resourceName, "secondary_sources.493771744.source_identifier", "secondarySource1"),
resource.TestCheckResourceAttr(resourceName, "secondary_sources.1385902896.source_identifier", "secondarySource2"),
),
},
{
Expand Down Expand Up @@ -2379,7 +2453,7 @@ resource "aws_codebuild_project" "test" {
location = "https://github.com/hashicorp/packer.git"
type = "GITHUB"
}

logs_config {
cloudwatch_logs {
status = %q
Expand Down Expand Up @@ -2411,7 +2485,7 @@ resource "aws_codebuild_project" "test" {
location = "https://github.com/hashicorp/packer.git"
type = "GITHUB"
}

logs_config {
s3_logs {
status = %q
Expand Down Expand Up @@ -2477,6 +2551,82 @@ resource "aws_codebuild_project" "test" {
`, rName, gitCloneDepth)
}

func testAccAWSCodeBuildProjectConfig_Source_GitSubmodulesConfig(rName string, fetchSubmodules bool) string {
return testAccAWSCodeBuildProjectConfig_Base_ServiceRole(rName) + fmt.Sprintf(`
resource "aws_codebuild_project" "test" {
name = "%s"
service_role = "${aws_iam_role.test.arn}"

artifacts {
type = "NO_ARTIFACTS"
}

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

source {
location = "https://git-codecommit.region-id.amazonaws.com/v1/repos/repo-name"
type = "CODECOMMIT"

git_submodules_config {
fetch_submodules = %t
}
}
}
`, rName, fetchSubmodules)
}

func testAccAWSCodeBuildProjectConfig_SecondarySources_GitSubmodulesConfig(rName string, fetchSubmodules bool) string {
return testAccAWSCodeBuildProjectConfig_Base_ServiceRole(rName) + fmt.Sprintf(`
resource "aws_codebuild_project" "test" {
name = "%[1]s"
service_role = "${aws_iam_role.test.arn}"

artifacts {
type = "NO_ARTIFACTS"
}

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

source {
location = "https://git-codecommit.region-id.amazonaws.com/v1/repos/repo-name"
type = "CODECOMMIT"

git_submodules_config {
fetch_submodules = %[2]t
}
}

secondary_sources {
location = "https://git-codecommit.region-id.amazonaws.com/v1/repos/second-repo-name"
type = "CODECOMMIT"
source_identifier = "secondarySource1"

git_submodules_config {
fetch_submodules = %[2]t
}
}

secondary_sources {
location = "https://git-codecommit.region-id.amazonaws.com/v1/repos/third-repo-name"
type = "CODECOMMIT"
source_identifier = "secondarySource2"

git_submodules_config {
fetch_submodules = %[2]t
}
}
}
`, rName, fetchSubmodules)
}

func testAccAWSCodeBuildProjectConfig_Source_InsecureSSL(rName string, insecureSSL bool) string {
return testAccAWSCodeBuildProjectConfig_Base_ServiceRole(rName) + fmt.Sprintf(`
resource "aws_codebuild_project" "test" {
Expand Down
10 changes: 10 additions & 0 deletions website/docs/r/codebuild_project.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ resource "aws_codebuild_project" "example" {
type = "GITHUB"
location = "https://github.com/mitchellh/packer.git"
git_clone_depth = 1

git_submodules_config {
fetch_submodules = true
}
}

vpc_config {
Expand Down Expand Up @@ -288,6 +292,7 @@ The following arguments are supported:
* `auth` - (Optional) Information about the authorization settings for AWS CodeBuild to access the source code to be built. Auth blocks are documented below.
* `buildspec` - (Optional) The build spec declaration to use for this build project's related builds. This must be set when `type` is `NO_SOURCE`.
* `git_clone_depth` - (Optional) Truncate git history to this many commits.
* `git_submodules_config` - (Optional) Information about the Git submodules configuration for an AWS CodeBuild build project. Git submodules config blocks are documented below. This option is only valid when the `type` is `CODECOMMIT`.
* `insecure_ssl` - (Optional) Ignore SSL warnings when connecting to source control.
* `location` - (Optional) The location of the source code from git or s3.
* `report_build_status` - (Optional) Set to `true` to report the status of a build's start and finish to your source provider. This option is only valid when the `type` is `BITBUCKET` or `GITHUB`.
Expand All @@ -297,6 +302,10 @@ The following arguments are supported:
* `type` - (Required) The authorization type to use. The only valid value is `OAUTH`
* `resource` - (Optional) The resource value that applies to the specified authorization type.

`git_submodules_config` supports the following:

* `fetch_submodules` - (Required) If set to true, fetches Git submodules for the AWS CodeBuild build project.

`vpc_config` supports the following:

* `security_group_ids` - (Required) The security group IDs to assign to running builds.
Expand Down Expand Up @@ -327,6 +336,7 @@ The following arguments are supported:
* `auth` - (Optional) Information about the authorization settings for AWS CodeBuild to access the source code to be built. Auth blocks are documented below.
* `buildspec` - (Optional) The build spec declaration to use for this build project's related builds.
* `git_clone_depth` - (Optional) Truncate git history to this many commits.
* `git_submodules_config` - (Optional) Information about the Git submodules configuration for an AWS CodeBuild build project. Git submodules config blocks are documented below. This option is only valid when the `type` is `CODECOMMIT`.
* `insecure_ssl` - (Optional) Ignore SSL warnings when connecting to source control.
* `location` - (Optional) The location of the source code from git or s3.
* `report_build_status` - (Optional) Set to `true` to report the status of a build's start and finish to your source provider. This option is only valid when your source provider is `GITHUB`, `BITBUCKET`, or `GITHUB_ENTERPRISE`.
Expand Down