-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34446 from brittandeyoung/f-aws_ecr_repositories
New DataSource: `aws_ecr_repositories`
- Loading branch information
Showing
10 changed files
with
205 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-data-source | ||
aws_ecr_repositories | ||
`` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package ecr | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/ecr" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
fwtypes "github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/hashicorp/terraform-provider-aws/internal/framework" | ||
"github.com/hashicorp/terraform-provider-aws/internal/framework/flex" | ||
) | ||
|
||
// @FrameworkDataSource(name="Repositories") | ||
func newRepositoriesDataSource(context.Context) (datasource.DataSourceWithConfigure, error) { | ||
return &repositoriesDataSource{}, nil | ||
} | ||
|
||
type repositoriesDataSource struct { | ||
framework.DataSourceWithConfigure | ||
} | ||
|
||
func (d *repositoriesDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name | ||
resp.TypeName = "aws_ecr_repositories" | ||
} | ||
|
||
func (d *repositoriesDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"id": framework.IDAttribute(), | ||
"names": schema.SetAttribute{ | ||
ElementType: fwtypes.StringType, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
func (d *repositoriesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
conn := d.Meta().ECRClient(ctx) | ||
|
||
var data repositoriesDataSourceModel | ||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
var names []string | ||
|
||
pages := ecr.NewDescribeRepositoriesPaginator(conn, &ecr.DescribeRepositoriesInput{}) | ||
for pages.HasMorePages() { | ||
output, err := pages.NextPage(ctx) | ||
|
||
if err != nil { | ||
resp.Diagnostics.AddError("reading ECR Repositories", err.Error()) | ||
return | ||
} | ||
|
||
for _, v := range output.Repositories { | ||
names = append(names, aws.ToString(v.RepositoryName)) | ||
} | ||
} | ||
|
||
data.ID = flex.StringValueToFramework(ctx, d.Meta().Region) | ||
data.Names = flex.FlattenFrameworkStringValueSet(ctx, names) | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} | ||
|
||
type repositoriesDataSourceModel struct { | ||
ID fwtypes.String `tfsdk:"id"` | ||
Names fwtypes.Set `tfsdk:"names"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package ecr_test | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/service/ecr" | ||
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-provider-aws/internal/acctest" | ||
"github.com/hashicorp/terraform-provider-aws/names" | ||
) | ||
|
||
func TestAccECRRepositoriesDataSource_basic(t *testing.T) { | ||
ctx := acctest.Context(t) | ||
var rNames []string | ||
for i := 1; i < 6; i++ { | ||
rNames = append(rNames, sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)) | ||
} | ||
dataSourceName := "data.aws_ecr_repositories.test" | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
acctest.PreCheck(ctx, t) | ||
acctest.PreCheckPartitionHasService(t, names.ECREndpointID) | ||
}, | ||
ErrorCheck: acctest.ErrorCheck(t, strings.ToLower(ecr.ServiceID)), | ||
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccRepositoriesDataSourceConfig_basic(rNames), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(dataSourceName, "names.#", "5"), | ||
resource.TestCheckTypeSetElemAttr(dataSourceName, "names.*", rNames[0]), | ||
resource.TestCheckTypeSetElemAttr(dataSourceName, "names.*", rNames[1]), | ||
resource.TestCheckTypeSetElemAttr(dataSourceName, "names.*", rNames[2]), | ||
resource.TestCheckTypeSetElemAttr(dataSourceName, "names.*", rNames[3]), | ||
resource.TestCheckTypeSetElemAttr(dataSourceName, "names.*", rNames[4]), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccRepositoriesDataSourceConfig_basic(rNames []string) string { | ||
rNameJson, _ := json.Marshal(rNames) | ||
rNameString := string(rNameJson) | ||
return fmt.Sprintf(` | ||
locals { | ||
repo_list = %[1]s | ||
} | ||
resource "aws_ecr_repository" "test" { | ||
count = length(local.repo_list) | ||
name = local.repo_list[count.index] | ||
} | ||
data "aws_ecr_repositories" "test" { | ||
depends_on = [aws_ecr_repository.test] | ||
} | ||
`, rNameString) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
subcategory: "ECR (Elastic Container Registry)" | ||
layout: "aws" | ||
page_title: "AWS: aws_ecr_repositories" | ||
description: |- | ||
Terraform data source for providing information on AWS ECR (Elastic Container Registry) Repositories. | ||
--- | ||
|
||
# Data Source: aws_ecr_repositories | ||
|
||
Terraform data source for providing information on AWS ECR (Elastic Container Registry) Repositories. | ||
|
||
## Example Usage | ||
|
||
### Basic Usage | ||
|
||
```terraform | ||
data "aws_ecr_repositories" "example" {} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
There are no arguments available for this data source. | ||
|
||
## Attributes Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `id` - AWS Region. | ||
* `names` - A list if AWS Elastic Container Registries for the region. |