Skip to content

Commit 7316bc0

Browse files
authored
Merge pull request #45400 from hashicorp/add-codebuild-project-list-resource
New list resource: `aws_codebuild_project`
2 parents 724f799 + bb447a3 commit 7316bc0

File tree

7 files changed

+260
-0
lines changed

7 files changed

+260
-0
lines changed

.changelog/45400.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-list-resource
2+
aws_codebuild_project
3+
```

internal/service/codebuild/project.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,24 @@ import (
1313
"github.com/aws/aws-sdk-go-v2/aws"
1414
"github.com/aws/aws-sdk-go-v2/service/codebuild"
1515
"github.com/aws/aws-sdk-go-v2/service/codebuild/types"
16+
"github.com/hashicorp/terraform-plugin-framework/list"
17+
listschema "github.com/hashicorp/terraform-plugin-framework/list/schema"
18+
"github.com/hashicorp/terraform-plugin-log/tflog"
1619
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1720
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
1821
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1922
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
2023
"github.com/hashicorp/terraform-provider-aws/internal/conns"
2124
"github.com/hashicorp/terraform-provider-aws/internal/create"
2225
"github.com/hashicorp/terraform-provider-aws/internal/enum"
26+
"github.com/hashicorp/terraform-provider-aws/internal/errs/fwdiag"
2327
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
2428
"github.com/hashicorp/terraform-provider-aws/internal/flex"
29+
"github.com/hashicorp/terraform-provider-aws/internal/framework"
30+
"github.com/hashicorp/terraform-provider-aws/internal/logging"
2531
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
2632
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
33+
inttypes "github.com/hashicorp/terraform-provider-aws/internal/types"
2734
"github.com/hashicorp/terraform-provider-aws/internal/verify"
2835
"github.com/hashicorp/terraform-provider-aws/names"
2936
)
@@ -2194,3 +2201,92 @@ func validProjectS3LogsLocation(v any, k string) (ws []string, errors []error) {
21942201

21952202
return
21962203
}
2204+
2205+
// @SDKListResource("aws_codebuild_project")
2206+
func projectResourceAsListResource() inttypes.ListResourceForSDK {
2207+
l := projectListResource{}
2208+
l.SetResourceSchema(resourceProject())
2209+
return &l
2210+
}
2211+
2212+
type projectListResource struct {
2213+
framework.ResourceWithConfigure
2214+
framework.ListResourceWithSDKv2Resource
2215+
framework.ListResourceWithSDKv2Tags
2216+
}
2217+
2218+
type projectListResourceModel struct {
2219+
framework.WithRegionModel
2220+
}
2221+
2222+
func (l *projectListResource) ListResourceConfigSchema(ctx context.Context, request list.ListResourceSchemaRequest, response *list.ListResourceSchemaResponse) {
2223+
response.Schema = listschema.Schema{
2224+
Attributes: map[string]listschema.Attribute{},
2225+
Blocks: map[string]listschema.Block{},
2226+
}
2227+
}
2228+
2229+
func (l *projectListResource) List(ctx context.Context, request list.ListRequest, stream *list.ListResultsStream) {
2230+
awsClient := l.Meta()
2231+
conn := awsClient.CodeBuildClient(ctx)
2232+
2233+
var query projectListResourceModel
2234+
if request.Config.Raw.IsKnown() && !request.Config.Raw.IsNull() {
2235+
if diags := request.Config.Get(ctx, &query); diags.HasError() {
2236+
stream.Results = list.ListResultsStreamDiagnostics(diags)
2237+
return
2238+
}
2239+
}
2240+
2241+
var input codebuild.ListProjectsInput
2242+
2243+
tflog.Info(ctx, "Listing CodeBuild projects")
2244+
2245+
stream.Results = func(yield func(list.ListResult) bool) {
2246+
pages := codebuild.NewListProjectsPaginator(conn, &input)
2247+
for pages.HasMorePages() {
2248+
page, err := pages.NextPage(ctx)
2249+
if err != nil {
2250+
result := fwdiag.NewListResultErrorDiagnostic(err)
2251+
yield(result)
2252+
return
2253+
}
2254+
2255+
for _, projectName := range page.Projects {
2256+
ctx := tflog.SetField(ctx, logging.ResourceAttributeKey(names.AttrID), projectName)
2257+
2258+
result := request.NewListResult(ctx)
2259+
2260+
rd := l.ResourceData()
2261+
rd.SetId(projectName)
2262+
2263+
tflog.Info(ctx, "Reading CodeBuild project")
2264+
diags := resourceProjectRead(ctx, rd, awsClient)
2265+
if diags.HasError() {
2266+
result = fwdiag.NewListResultErrorDiagnostic(fmt.Errorf("reading CodeBuild project %s", projectName))
2267+
yield(result)
2268+
return
2269+
}
2270+
2271+
err = l.SetTags(ctx, awsClient, rd)
2272+
if err != nil {
2273+
result = fwdiag.NewListResultErrorDiagnostic(err)
2274+
yield(result)
2275+
return
2276+
}
2277+
2278+
result.DisplayName = projectName
2279+
2280+
l.SetResult(ctx, awsClient, request.IncludeResource, &result, rd)
2281+
if result.Diagnostics.HasError() {
2282+
yield(result)
2283+
return
2284+
}
2285+
2286+
if !yield(result) {
2287+
return
2288+
}
2289+
}
2290+
}
2291+
}
2292+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package codebuild_test
5+
6+
import (
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-testing/config"
10+
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
11+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
12+
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
13+
"github.com/hashicorp/terraform-plugin-testing/querycheck"
14+
"github.com/hashicorp/terraform-plugin-testing/statecheck"
15+
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
16+
"github.com/hashicorp/terraform-plugin-testing/tfversion"
17+
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
18+
tfknownvalue "github.com/hashicorp/terraform-provider-aws/internal/acctest/knownvalue"
19+
"github.com/hashicorp/terraform-provider-aws/names"
20+
)
21+
22+
func TestAccCodeBuildProject_List_basic(t *testing.T) {
23+
ctx := acctest.Context(t)
24+
25+
resourceName1 := "aws_codebuild_project.test[0]"
26+
resourceName2 := "aws_codebuild_project.test[1]"
27+
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
28+
29+
acctest.ParallelTest(ctx, t, resource.TestCase{
30+
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
31+
tfversion.SkipBelow(tfversion.Version1_14_0),
32+
},
33+
PreCheck: func() { acctest.PreCheck(ctx, t) },
34+
ErrorCheck: acctest.ErrorCheck(t, names.CodeBuildServiceID),
35+
CheckDestroy: testAccCheckProjectDestroy(ctx),
36+
Steps: []resource.TestStep{
37+
// Step 1: Setup
38+
{
39+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
40+
ConfigDirectory: config.StaticDirectory("testdata/Project/list_basic/"),
41+
ConfigVariables: config.Variables{
42+
acctest.CtRName: config.StringVariable(rName),
43+
},
44+
ConfigStateChecks: []statecheck.StateCheck{
45+
statecheck.ExpectKnownValue(resourceName1, tfjsonpath.New(names.AttrARN), tfknownvalue.RegionalARNExact("codebuild", "project/"+rName+"-0")),
46+
statecheck.ExpectKnownValue(resourceName2, tfjsonpath.New(names.AttrARN), tfknownvalue.RegionalARNExact("codebuild", "project/"+rName+"-1")),
47+
},
48+
},
49+
50+
// Step 2: Query
51+
{
52+
Query: true,
53+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
54+
ConfigDirectory: config.StaticDirectory("testdata/Project/list_basic/"),
55+
ConfigVariables: config.Variables{
56+
acctest.CtRName: config.StringVariable(rName),
57+
},
58+
QueryResultChecks: []querycheck.QueryResultCheck{
59+
querycheck.ExpectIdentity("aws_codebuild_project.test", map[string]knownvalue.Check{
60+
names.AttrARN: knownvalue.NotNull(),
61+
}),
62+
63+
querycheck.ExpectIdentity("aws_codebuild_project.test", map[string]knownvalue.Check{
64+
names.AttrARN: knownvalue.NotNull(),
65+
}),
66+
},
67+
},
68+
},
69+
})
70+
}

internal/service/codebuild/service_package_gen.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright (c) HashiCorp, Inc.
2+
# SPDX-License-Identifier: MPL-2.0
3+
4+
list "aws_codebuild_project" "test" {
5+
provider = aws
6+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright (c) HashiCorp, Inc.
2+
# SPDX-License-Identifier: MPL-2.0
3+
4+
resource "aws_codebuild_project" "test" {
5+
count = 2
6+
7+
name = "${var.rName}-${count.index}"
8+
service_role = aws_iam_role.test.arn
9+
10+
artifacts {
11+
type = "NO_ARTIFACTS"
12+
}
13+
14+
environment {
15+
compute_type = "BUILD_GENERAL1_SMALL"
16+
image = "aws/codebuild/amazonlinux2-x86_64-standard:2.0"
17+
type = "LINUX_CONTAINER"
18+
}
19+
20+
source {
21+
type = "NO_SOURCE"
22+
buildspec = "version: 0.2\nphases:\n build:\n commands:\n - echo hello"
23+
}
24+
}
25+
26+
resource "aws_iam_role" "test" {
27+
name = var.rName
28+
29+
assume_role_policy = jsonencode({
30+
Version = "2012-10-17"
31+
Statement = [{
32+
Action = "sts:AssumeRole"
33+
Effect = "Allow"
34+
Principal = {
35+
Service = "codebuild.amazonaws.com"
36+
}
37+
}]
38+
})
39+
}
40+
41+
variable "rName" {
42+
description = "Name for resource"
43+
type = string
44+
nullable = false
45+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
subcategory: "CodeBuild"
3+
layout: "aws"
4+
page_title: "AWS: aws_codebuild_project"
5+
description: |-
6+
Lists CodeBuild Project resources.
7+
---
8+
9+
# List Resource: aws_codebuild_project
10+
11+
Lists CodeBuild Project resources.
12+
13+
## Example Usage
14+
15+
```terraform
16+
list "aws_codebuild_project" "example" {
17+
provider = aws
18+
}
19+
```
20+
21+
## Argument Reference
22+
23+
This list resource supports the following arguments:
24+
25+
* `region` - (Optional) Region to query. Defaults to provider region.

0 commit comments

Comments
 (0)