Skip to content

Commit

Permalink
Merge pull request #34446 from brittandeyoung/f-aws_ecr_repositories
Browse files Browse the repository at this point in the history
New DataSource: `aws_ecr_repositories`
  • Loading branch information
ewbankkit authored Dec 5, 2023
2 parents c13f7b1 + c55235b commit 54f9d4d
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/34446.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_ecr_repositories
``
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/directoryservice v1.22.2
github.com/aws/aws-sdk-go-v2/service/docdbelastic v1.6.2
github.com/aws/aws-sdk-go-v2/service/ec2 v1.138.2
github.com/aws/aws-sdk-go-v2/service/ecr v1.24.2
github.com/aws/aws-sdk-go-v2/service/eks v1.35.2
github.com/aws/aws-sdk-go-v2/service/emr v1.35.2
github.com/aws/aws-sdk-go-v2/service/emrserverless v1.14.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ github.com/aws/aws-sdk-go-v2/service/dynamodb v1.26.2 h1:IPMh5Selz3UKr1rY8FaNTv4
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.26.2/go.mod h1:Mj372IvfZ9ftME7Kdo74stz3KAjMA+WC7Fzzry9uCDI=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.138.2 h1:e3Imv1oXz+W3Tfclflkh72t5TUPUwWdkHP7ctQGk8Dc=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.138.2/go.mod h1:d1hAqgLDOPaSO1Piy/0bBmj6oAplFwv6p0cquHntNHM=
github.com/aws/aws-sdk-go-v2/service/ecr v1.24.2 h1:ETo/9bTM+fp1Gf6l04jhoA8emIjZNDRu1qoNRBd419g=
github.com/aws/aws-sdk-go-v2/service/ecr v1.24.2/go.mod h1:/m9MiYl5Ds0cZqy/bbeSUWxKLwTarGugjXxSgiXNQFc=
github.com/aws/aws-sdk-go-v2/service/eks v1.35.2 h1:G1gPnbhGBEKedQLoSI71u3+IMJ+uIPxCNB0ZPmv6dpk=
github.com/aws/aws-sdk-go-v2/service/eks v1.35.2/go.mod h1:HZ5xGhvvZEs7iBPbwhLWZWvAxWezzOFn/HrepDivCgQ=
github.com/aws/aws-sdk-go-v2/service/emr v1.35.2 h1:Q4nxzsfd61L/d83LDDD/UhDk1Tktd4+8QPhRLyRUVIs=
Expand Down
5 changes: 5 additions & 0 deletions internal/conns/awsclient_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 76 additions & 0 deletions internal/service/ecr/repositories_data_source.go
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"`
}
67 changes: 67 additions & 0 deletions internal/service/ecr/repositories_data_source_test.go
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)
}
20 changes: 19 additions & 1 deletion internal/service/ecr/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions names/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const (
DocDBElasticEndpointID = "docdb-elastic"
ControlTowerEndpointID = "controltower"
DSEndpointID = "ds"
ECREndpointID = "api.ecr"
EKSEndpointID = "eks"
EMREndpointID = "elasticmapreduce"
EMRServerlessEndpointID = "emrserverless"
Expand Down
2 changes: 1 addition & 1 deletion names/names_data.csv
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ ebs,ebs,ebs,ebs,,ebs,,,EBS,EBS,,1,,,aws_ebs_,,changewhenimplemented,EBS (Elastic
ec2,ec2,ec2,ec2,,ec2,ec2,,EC2,EC2,,1,2,aws_(ami|availability_zone|ec2_(availability|capacity|fleet|host|instance|public_ipv4_pool|serial|spot|tag)|eip|instance|key_pair|launch_template|placement_group|spot),aws_ec2_,ec2_,ami;availability_zone;ec2_availability_;ec2_capacity_;ec2_fleet;ec2_host;ec2_image_;ec2_instance_;ec2_public_ipv4_pool;ec2_serial_;ec2_spot_;ec2_tag;eip;instance;key_pair;launch_template;placement_group;spot_,EC2 (Elastic Compute Cloud),Amazon,,,,,,,
imagebuilder,imagebuilder,imagebuilder,imagebuilder,,imagebuilder,,,ImageBuilder,Imagebuilder,,1,,,aws_imagebuilder_,,imagebuilder_,EC2 Image Builder,Amazon,,,,,,,
ec2-instance-connect,ec2instanceconnect,ec2instanceconnect,ec2instanceconnect,,ec2instanceconnect,,,EC2InstanceConnect,EC2InstanceConnect,,1,,,aws_ec2instanceconnect_,,ec2instanceconnect_,EC2 Instance Connect,AWS,,x,,,,,
ecr,ecr,ecr,ecr,,ecr,,,ECR,ECR,,1,,,aws_ecr_,,ecr_,ECR (Elastic Container Registry),Amazon,,,,,,,
ecr,ecr,ecr,ecr,,ecr,,,ECR,ECR,,1,2,,aws_ecr_,,ecr_,ECR (Elastic Container Registry),Amazon,,,,,,,
ecr-public,ecrpublic,ecrpublic,ecrpublic,,ecrpublic,,,ECRPublic,ECRPublic,,1,,,aws_ecrpublic_,,ecrpublic_,ECR Public,Amazon,,,,,,,
ecs,ecs,ecs,ecs,,ecs,,,ECS,ECS,,1,,,aws_ecs_,,ecs_,ECS (Elastic Container),Amazon,,,,,,,
efs,efs,efs,efs,,efs,,,EFS,EFS,,1,,,aws_efs_,,efs_,EFS (Elastic File System),Amazon,,,,,,,
Expand Down
30 changes: 30 additions & 0 deletions website/docs/d/ecr_repositories.html.markdown
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.

0 comments on commit 54f9d4d

Please sign in to comment.