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

r/codebuild_project - add secondary_source_version argument #22345

Merged
merged 4 commits into from
Jan 3, 2022
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
3 changes: 3 additions & 0 deletions .changelog/22345.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_codebuild_project: Add `secondary_source_version` argument
```
86 changes: 86 additions & 0 deletions internal/service/codebuild/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,23 @@ func ResourceProject() *schema.Resource {
},
},
},
"secondary_source_version": {
Type: schema.TypeSet,
Optional: true,
MaxItems: 12,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"source_identifier": {
Type: schema.TypeString,
Required: true,
},
"source_version": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"service_role": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -788,6 +805,10 @@ func resourceProjectCreate(d *schema.ResourceData, meta interface{}) error {
params.BadgeEnabled = aws.Bool(v.(bool))
}

if v, ok := d.GetOk("secondary_source_version"); ok && v.(*schema.Set).Len() > 0 {
params.SecondarySourceVersions = expandProjectSecondarySourceVersions(v.(*schema.Set))
}

var resp *codebuild.CreateProjectOutput
// Handle IAM eventual consistency
err := resource.Retry(5*time.Minute, func() *resource.RetryError {
Expand Down Expand Up @@ -835,6 +856,32 @@ func resourceProjectCreate(d *schema.ResourceData, meta interface{}) error {
return resourceProjectRead(d, meta)
}

func expandProjectSecondarySourceVersions(ssv *schema.Set) []*codebuild.ProjectSourceVersion {
sourceVersions := make([]*codebuild.ProjectSourceVersion, 0)

rawSourceVersions := ssv.List()
if len(rawSourceVersions) == 0 {
return nil
}

for _, config := range rawSourceVersions {
sourceVersion := expandProjectSourceVersion(config.(map[string]interface{}))
sourceVersions = append(sourceVersions, &sourceVersion)
}

return sourceVersions
}

func expandProjectSourceVersion(data map[string]interface{}) codebuild.ProjectSourceVersion {

sourceVersion := codebuild.ProjectSourceVersion{
SourceIdentifier: aws.String(data["source_identifier"].(string)),
SourceVersion: aws.String(data["source_version"].(string)),
}

return sourceVersion
}

func expandProjectFileSystemLocations(d *schema.ResourceData) []*codebuild.ProjectFileSystemLocation {
fileSystemLocations := make([]*codebuild.ProjectFileSystemLocation, 0)

Expand Down Expand Up @@ -1332,6 +1379,10 @@ func resourceProjectRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("error setting secondary_sources: %w", err)
}

if err := d.Set("secondary_source_version", flattenAwsCodeBuildProjectSecondarySourceVersions(project.SecondarySourceVersions)); err != nil {
return fmt.Errorf("error setting secondary_source_version: %w", err)
}

if err := d.Set("source", flattenProjectSource(project.Source)); err != nil {
return fmt.Errorf("error setting source: %w", err)
}
Expand Down Expand Up @@ -1437,6 +1488,18 @@ func resourceProjectUpdate(d *schema.ResourceData, meta interface{}) error {
}
}

if d.HasChange("secondary_source_version") {
_, n := d.GetChange("secondary_source_version")

psv := d.Get("secondary_source_version").(*schema.Set)

if n.(*schema.Set).Len() > 0 {
params.SecondarySourceVersions = expandProjectSecondarySourceVersions(psv)
} else {
params.SecondarySourceVersions = []*codebuild.ProjectSourceVersion{}
}
}

if d.HasChange("secondary_artifacts") {
_, n := d.GetChange("secondary_artifacts")

Expand Down Expand Up @@ -1784,6 +1847,29 @@ func flattenProjectSourceData(source *codebuild.ProjectSource) interface{} {
return m
}

func flattenAwsCodeBuildProjectSecondarySourceVersions(sourceVersions []*codebuild.ProjectSourceVersion) []interface{} {
l := make([]interface{}, 0)

for _, sourceVersion := range sourceVersions {
l = append(l, flattenAwsCodeBuildProjectsourceVersionsData(sourceVersion))
}
return l
}

func flattenAwsCodeBuildProjectsourceVersionsData(sourceVersion *codebuild.ProjectSourceVersion) map[string]interface{} {
values := map[string]interface{}{}

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

if sourceVersion.SourceVersion != nil {
values["source_version"] = aws.StringValue(sourceVersion.SourceVersion)
}

return values
}

func flattenProjectGitSubmodulesConfig(config *codebuild.GitSubmodulesConfig) []interface{} {
if config == nil {
return []interface{}{}
Expand Down
183 changes: 183 additions & 0 deletions internal/service/codebuild/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ func TestAccCodeBuildProject_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "source.0.location", "https://github.com/hashibot-test/aws-test.git"),
resource.TestCheckResourceAttr(resourceName, "source.0.report_build_status", "false"),
resource.TestCheckResourceAttr(resourceName, "source.0.type", "GITHUB"),
resource.TestCheckResourceAttr(resourceName, "secondary_source_version.#", "0"),
resource.TestCheckResourceAttr(resourceName, "build_batch_config.#", "0"),
resource.TestCheckResourceAttr(resourceName, "secondary_artifacts.#", "0"),
resource.TestCheckResourceAttr(resourceName, "secondary_sources.#", "0"),
resource.TestCheckResourceAttr(resourceName, "vpc_config.#", "0"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
Expand Down Expand Up @@ -1025,6 +1029,98 @@ func TestAccCodeBuildProject_SecondarySourcesGitSubmodules_gitHubEnterprise(t *t
})
}

func TestAccCodeBuildProject_SecondarySourcesVersions(t *testing.T) {
var project codebuild.Project
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_codebuild_project.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t); testAccPreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, codebuild.EndpointsID),
Providers: acctest.Providers,
CheckDestroy: testAccCheckProjectDestroy,
Steps: []resource.TestStep{
{
Config: testAccProjectConfig_SecondarySourceVersions_CodeCommit(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckProjectExists(resourceName, &project),
resource.TestCheckResourceAttr(resourceName, "secondary_sources.#", "2"),
resource.TestCheckResourceAttr(resourceName, "secondary_source_version.#", "1"),
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "secondary_sources.*", map[string]string{
"source_identifier": "secondarySource1",
}),
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "secondary_sources.*", map[string]string{
"source_identifier": "secondarySource2",
}),
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "secondary_source_version.*", map[string]string{
"source_identifier": "secondarySource1",
"source_version": "master",
}),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccProjectConfig_SecondarySourceVersions_CodeCommitUpdated(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckProjectExists(resourceName, &project),
resource.TestCheckResourceAttr(resourceName, "secondary_sources.#", "2"),
resource.TestCheckResourceAttr(resourceName, "secondary_source_version.#", "2"),
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "secondary_sources.*", map[string]string{
"source_identifier": "secondarySource1",
}),
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "secondary_sources.*", map[string]string{
"source_identifier": "secondarySource2",
}),
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "secondary_source_version.*", map[string]string{
"source_identifier": "secondarySource1",
"source_version": "master",
}),
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "secondary_source_version.*", map[string]string{
"source_identifier": "secondarySource2",
"source_version": "master",
}),
),
},
{
Config: testAccProjectConfig_SecondarySourceVersions_CodeCommit(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckProjectExists(resourceName, &project),
resource.TestCheckResourceAttr(resourceName, "secondary_sources.#", "2"),
resource.TestCheckResourceAttr(resourceName, "secondary_source_version.#", "1"),
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "secondary_sources.*", map[string]string{
"source_identifier": "secondarySource1",
}),
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "secondary_sources.*", map[string]string{
"source_identifier": "secondarySource2",
}),
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "secondary_source_version.*", map[string]string{
"source_identifier": "secondarySource1",
"source_version": "master",
}),
),
},
{
Config: testAccProjectConfig_SecondarySources_GitSubmodulesConfig_CodeCommit(rName, false),
Check: resource.ComposeTestCheckFunc(
testAccCheckProjectExists(resourceName, &project),
resource.TestCheckResourceAttr(resourceName, "secondary_sources.#", "2"),
resource.TestCheckResourceAttr(resourceName, "secondary_source_version.#", "0"),
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "secondary_sources.*", map[string]string{
"source_identifier": "secondarySource1",
}),
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "secondary_sources.*", map[string]string{
"source_identifier": "secondarySource2",
}),
),
},
},
})
}

func TestAccCodeBuildProject_SourceBuildStatus_gitHubEnterprise(t *testing.T) {
var project codebuild.Project
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
Expand Down Expand Up @@ -3531,6 +3627,93 @@ resource "aws_codebuild_project" "test" {
`, rName, fetchSubmodules))
}

func testAccProjectConfig_SecondarySourceVersions_CodeCommit(rName string) string {
return acctest.ConfigCompose(testAccProjectConfig_Base_ServiceRole(rName), fmt.Sprintf(`
resource "aws_codebuild_project" "test" {
name = %[1]q
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"
}

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

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

secondary_source_version {
source_version = "master"
source_identifier = "secondarySource1"
}
}
`, rName))
}

func testAccProjectConfig_SecondarySourceVersions_CodeCommitUpdated(rName string) string {
return acctest.ConfigCompose(testAccProjectConfig_Base_ServiceRole(rName), fmt.Sprintf(`
resource "aws_codebuild_project" "test" {
name = %[1]q
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"
}

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

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

secondary_source_version {
source_version = "master"
source_identifier = "secondarySource1"
}

secondary_source_version {
source_version = "master"
source_identifier = "secondarySource2"
}
}
`, rName))
}

func testAccProjectConfig_Source_InsecureSSL(rName string, insecureSSL bool) string {
return acctest.ConfigCompose(testAccProjectConfig_Base_ServiceRole(rName), fmt.Sprintf(`
resource "aws_codebuild_project" "test" {
Expand Down
6 changes: 6 additions & 0 deletions website/docs/r/codebuild_project.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ The following arguments are optional:
* `queued_timeout` - (Optional) Number of minutes, from 5 to 480 (8 hours), a build is allowed to be queued before it times out. The default is 8 hours.
* `secondary_artifacts` - (Optional) Configuration block. Detailed below.
* `secondary_sources` - (Optional) Configuration block. Detailed below.
* `secondary_source_version` - (Optional) Configuration block. Detailed below.
* `source_version` - (Optional) Version of the build input to be built for this project. If not specified, the latest version is used.
* `tags` - (Optional) Map of tags to assign to the resource. If configured with a provider [`default_tags` configuration block](/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.
* `vpc_config` - (Optional) Configuration block. Detailed below.
Expand Down Expand Up @@ -363,6 +364,11 @@ This block is only valid when the `type` is `CODECOMMIT`, `GITHUB` or `GITHUB_EN
* `context` - (Optional) Specifies the context of the build status CodeBuild sends to the source provider. The usage of this parameter depends on the source provider.
* `target_url` - (Optional) Specifies the target url of the build status CodeBuild sends to the source provider. The usage of this parameter depends on the source provider.

### secondary_source_version

* `source_identifier` - (Required) An identifier for a source in the build project.
* `source_version` - (Required) The source version for the corresponding source identifier. See [AWS docs](https://docs.aws.amazon.com/codebuild/latest/APIReference/API_ProjectSourceVersion.html#CodeBuild-Type-ProjectSourceVersion-sourceVersion) for more details.

### source

* `auth` - (Optional, **Deprecated**) Configuration block with the authorization settings for AWS CodeBuild to access the source code to be built. This information is for the AWS CodeBuild console's use only. Use the [`aws_codebuild_source_credential` resource](codebuild_source_credential.html) instead. Auth blocks are documented below.
Expand Down