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

[New Data Source] Glue registry #37953

Merged
merged 20 commits into from
Sep 4, 2024
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/37953.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_glue_registry
```
32 changes: 32 additions & 0 deletions internal/service/glue/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,38 @@ func FindRegistryByID(ctx context.Context, conn *glue.Client, id string) (*glue.
}

output, err := conn.GetRegistry(ctx, input)

if errs.IsA[*awstypes.EntityNotFoundException](err) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

return output, nil
}

// FindRegistryByName returns the Registry corresponding to the specified name.
func FindRegistryByName(ctx context.Context, conn *glue.Client, name string) (*glue.GetRegistryOutput, error) {
input := &glue.GetRegistryInput{
RegistryId: &awstypes.RegistryId{
RegistryName: aws.String(name),
},
}

output, err := conn.GetRegistry(ctx, input)

if errs.IsA[*awstypes.EntityNotFoundException](err) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}
Expand Down
13 changes: 8 additions & 5 deletions internal/service/glue/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)
Expand Down Expand Up @@ -89,12 +90,14 @@ func resourceRegistryRead(ctx context.Context, d *schema.ResourceData, meta inte
conn := meta.(*conns.AWSClient).GlueClient(ctx)

output, err := FindRegistryByID(ctx, conn, d.Id())

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] Glue Registry (%s) not found, removing from state", d.Id())
d.SetId("")
return diags
}

if err != nil {
if errs.IsA[*awstypes.EntityNotFoundException](err) {
log.Printf("[WARN] Glue Registry (%s) not found, removing from state", d.Id())
d.SetId("")
return diags
}
return sdkdiag.AppendErrorf(diags, "reading Glue Registry (%s): %s", d.Id(), err)
}

Expand Down
80 changes: 80 additions & 0 deletions internal/service/glue/registry_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package glue

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/framework"
"github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @FrameworkDataSource(name="Registry")
func newDataSourceRegistry(context.Context) (datasource.DataSourceWithConfigure, error) {
return &dataSourceRegistry{}, nil
}

const (
DSNameRegistry = "Registry Data Source"
)

type dataSourceRegistry struct {
framework.DataSourceWithConfigure
}

func (d *dataSourceRegistry) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = "aws_glue_registry"
}

func (d *dataSourceRegistry) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
names.AttrARN: framework.ARNAttributeComputedOnly(),
names.AttrDescription: schema.StringAttribute{
Computed: true,
},
names.AttrName: schema.StringAttribute{
Required: true,
},
},
}
}

func (d *dataSourceRegistry) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
conn := d.Meta().GlueClient(ctx)

var data dataSourceRegistryData
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

out, err := FindRegistryByName(ctx, conn, data.Name.ValueString())

if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.Glue, create.ErrActionReading, DSNameRegistry, data.Name.String(), err),
err.Error(),
)
return
}

resp.Diagnostics.Append(flex.Flatten(ctx, out, &data, flex.WithFieldNamePrefix("Registry"))...)
if resp.Diagnostics.HasError() {
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

type dataSourceRegistryData struct {
ARN types.String `tfsdk:"arn"`
Description types.String `tfsdk:"description"`
Name types.String `tfsdk:"name"`
}
60 changes: 60 additions & 0 deletions internal/service/glue/registry_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package glue_test

import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go-v2/service/glue"
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 TestAccGlueRegistryDataSource_basic(t *testing.T) {
ctx := acctest.Context(t)
if testing.Short() {
t.Skip("skipping long-running test in short mode")
}

var registry glue.GetRegistryOutput
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
dataSourceName := "data.aws_glue_registry.test"
resourceName := "aws_glue_registry.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
testAccPreCheckRegistry(ctx, t)
},
ErrorCheck: acctest.ErrorCheck(t, names.GlueServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccRegistryDataSourceConfig_basic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckRegistryExists(ctx, resourceName, &registry),
resource.TestCheckResourceAttrPair(dataSourceName, names.AttrARN, resourceName, names.AttrARN),
resource.TestCheckResourceAttrPair(dataSourceName, names.AttrDescription, resourceName, names.AttrDescription),
resource.TestCheckResourceAttrPair(dataSourceName, names.AttrName, resourceName, "registry_name"),
),
},
},
})
}

func testAccRegistryDataSourceConfig_basic(rName string) string {
return fmt.Sprintf(`
resource "aws_glue_registry" "test" {
registry_name = %[1]q
description = %[1]q
}

data "aws_glue_registry" "test" {
name = aws_glue_registry.test.registry_name
}
`, rName)
}
12 changes: 7 additions & 5 deletions internal/service/glue/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import (

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/glue"
awstypes "github.com/aws/aws-sdk-go-v2/service/glue/types"
"github.com/hashicorp/aws-sdk-go-base/v2/tfawserr"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
tfglue "github.com/hashicorp/terraform-provider-aws/internal/service/glue"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)

Expand Down Expand Up @@ -215,10 +214,13 @@ func testAccCheckRegistryDestroy(ctx context.Context) resource.TestCheckFunc {

conn := acctest.Provider.Meta().(*conns.AWSClient).GlueClient(ctx)
output, err := tfglue.FindRegistryByID(ctx, conn, rs.Primary.ID)

if tfresource.NotFound(err) {
continue
}

if err != nil {
if errs.IsA[*awstypes.EntityNotFoundException](err) {
return nil
}
return err
}

if output != nil && aws.ToString(output.RegistryArn) == rs.Primary.ID {
Expand Down
7 changes: 6 additions & 1 deletion internal/service/glue/service_package_gen.go

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

34 changes: 34 additions & 0 deletions website/docs/d/glue_registry.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
subcategory: "Glue"
layout: "aws"
page_title: "AWS: aws_glue_registry"
description: |-
Terraform data source for managing an AWS Glue Registry.
---

# Data Source: aws_glue_registry

Terraform data source for managing an AWS Glue Registry.

## Example Usage

### Basic Usage

```terraform
data "aws_glue_registry" "example" {
name = "example"
}
```

## Argument Reference

The following arguments are required:

* `name` - (Required) Name of the Glue Registry.

## Attribute Reference

This data source exports the following attributes in addition to the arguments above:

* `arn` - Amazon Resource Name (ARN) of Glue Registry.
* `description` - A description of the registry.
Loading