Skip to content

Commit

Permalink
r/aws_emrcontainers_virtual_cluster: Reduce visibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed Jul 19, 2024
1 parent cfb6dd8 commit a78723d
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 87 deletions.
6 changes: 4 additions & 2 deletions internal/service/emrcontainers/exports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ package emrcontainers

// Exports for use in tests only.
var (
ResourceJobTemplate = resourceJobTemplate
ResourceJobTemplate = resourceJobTemplate
ResourceVirtualCluster = resourceVirtualCluster

FindJobTemplateByID = findJobTemplateByID
FindJobTemplateByID = findJobTemplateByID
FindVirtualClusterByID = findVirtualClusterByID
)
2 changes: 1 addition & 1 deletion internal/service/emrcontainers/service_package_gen.go

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

2 changes: 1 addition & 1 deletion internal/service/emrcontainers/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func sweepVirtualClusters(region string) error {
continue
}

r := ResourceVirtualCluster()
r := resourceVirtualCluster()
d := r.Data(nil)
d.SetId(aws.ToString(v.Id))

Expand Down
161 changes: 79 additions & 82 deletions internal/service/emrcontainers/virtual_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

// @SDKResource("aws_emrcontainers_virtual_cluster", name="Virtual Cluster")
// @Tags(identifierAttribute="arn")
func ResourceVirtualCluster() *schema.Resource {
func resourceVirtualCluster() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceVirtualClusterCreate,
ReadWithoutTimeout: resourceVirtualClusterRead,
Expand Down Expand Up @@ -115,7 +115,6 @@ func ResourceVirtualCluster() *schema.Resource {

func resourceVirtualClusterCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).EMRContainersClient(ctx)

name := d.Get(names.AttrName).(string)
Expand All @@ -141,10 +140,9 @@ func resourceVirtualClusterCreate(ctx context.Context, d *schema.ResourceData, m

func resourceVirtualClusterRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).EMRContainersClient(ctx)

vc, err := FindVirtualClusterByID(ctx, conn, d.Id())
vc, err := findVirtualClusterByID(ctx, conn, d.Id())

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] EMR Containers Virtual Cluster %s not found, removing from state", d.Id())
Expand Down Expand Up @@ -178,7 +176,6 @@ func resourceVirtualClusterUpdate(ctx context.Context, d *schema.ResourceData, m

func resourceVirtualClusterDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).EMRContainersClient(ctx)

log.Printf("[INFO] Deleting EMR Containers Virtual Cluster: %s", d.Id())
Expand All @@ -204,13 +201,89 @@ func resourceVirtualClusterDelete(ctx context.Context, d *schema.ResourceData, m
return sdkdiag.AppendErrorf(diags, "deleting EMR Containers Virtual Cluster (%s): %s", d.Id(), err)
}

if _, err = waitVirtualClusterDeleted(ctx, conn, d.Id(), d.Timeout(schema.TimeoutDelete)); err != nil {
if _, err := waitVirtualClusterDeleted(ctx, conn, d.Id(), d.Timeout(schema.TimeoutDelete)); err != nil {
return sdkdiag.AppendErrorf(diags, "waiting for EMR Containers Virtual Cluster (%s) delete: %s", d.Id(), err)
}

return diags
}

func findVirtualCluster(ctx context.Context, conn *emrcontainers.Client, input *emrcontainers.DescribeVirtualClusterInput) (*awstypes.VirtualCluster, error) {
output, err := conn.DescribeVirtualCluster(ctx, input)

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

if err != nil {
return nil, err
}

if output == nil || output.VirtualCluster == nil {
return nil, tfresource.NewEmptyResultError(input)
}

return output.VirtualCluster, nil
}

func findVirtualClusterByID(ctx context.Context, conn *emrcontainers.Client, id string) (*awstypes.VirtualCluster, error) {
input := &emrcontainers.DescribeVirtualClusterInput{
Id: aws.String(id),
}

output, err := findVirtualCluster(ctx, conn, input)

if err != nil {
return nil, err
}

if output.State == awstypes.VirtualClusterStateTerminated {
return nil, &retry.NotFoundError{
Message: string(output.State),
LastRequest: input,
}
}

return output, nil
}

func statusVirtualCluster(ctx context.Context, conn *emrcontainers.Client, id string) retry.StateRefreshFunc {
return func() (interface{}, string, error) {
output, err := findVirtualClusterByID(ctx, conn, id)

if tfresource.NotFound(err) {
return nil, "", nil
}

if err != nil {
return nil, "", err
}

return output, string(output.State), nil
}
}

func waitVirtualClusterDeleted(ctx context.Context, conn *emrcontainers.Client, id string, timeout time.Duration) (*awstypes.VirtualCluster, error) {
stateConf := &retry.StateChangeConf{
Pending: enum.Slice(awstypes.VirtualClusterStateTerminating),
Target: []string{},
Refresh: statusVirtualCluster(ctx, conn, id),
Timeout: timeout,
Delay: 1 * time.Minute,
}

outputRaw, err := stateConf.WaitForStateContext(ctx)

if v, ok := outputRaw.(*awstypes.VirtualCluster); ok {
return v, err
}

return nil, err
}

func expandContainerProvider(tfMap map[string]interface{}) *awstypes.ContainerProvider {
if tfMap == nil {
return nil
Expand Down Expand Up @@ -305,79 +378,3 @@ func flattenEKSInfo(apiObject *awstypes.EksInfo) map[string]interface{} {

return tfMap
}

func findVirtualCluster(ctx context.Context, conn *emrcontainers.Client, input *emrcontainers.DescribeVirtualClusterInput) (*awstypes.VirtualCluster, error) {
output, err := conn.DescribeVirtualCluster(ctx, input)

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

if err != nil {
return nil, err
}

if output == nil || output.VirtualCluster == nil {
return nil, tfresource.NewEmptyResultError(input)
}

return output.VirtualCluster, nil
}

func FindVirtualClusterByID(ctx context.Context, conn *emrcontainers.Client, id string) (*awstypes.VirtualCluster, error) {
input := &emrcontainers.DescribeVirtualClusterInput{
Id: aws.String(id),
}

output, err := findVirtualCluster(ctx, conn, input)

if err != nil {
return nil, err
}

if output.State == awstypes.VirtualClusterStateTerminated {
return nil, &retry.NotFoundError{
Message: string(output.State),
LastRequest: input,
}
}

return output, nil
}

func statusVirtualCluster(ctx context.Context, conn *emrcontainers.Client, id string) retry.StateRefreshFunc {
return func() (interface{}, string, error) {
output, err := FindVirtualClusterByID(ctx, conn, id)

if tfresource.NotFound(err) {
return nil, "", nil
}

if err != nil {
return nil, "", err
}

return output, string(output.State), nil
}
}

func waitVirtualClusterDeleted(ctx context.Context, conn *emrcontainers.Client, id string, timeout time.Duration) (*awstypes.VirtualCluster, error) {
stateConf := &retry.StateChangeConf{
Pending: enum.Slice(awstypes.VirtualClusterStateTerminating),
Target: []string{},
Refresh: statusVirtualCluster(ctx, conn, id),
Timeout: timeout,
Delay: 1 * time.Minute,
}

outputRaw, err := stateConf.WaitForStateContext(ctx)

if v, ok := outputRaw.(*awstypes.VirtualCluster); ok {
return v, err
}

return nil, err
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func dataSourceVirtualClusterRead(ctx context.Context, d *schema.ResourceData, m
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

id := d.Get("virtual_cluster_id").(string)
vc, err := FindVirtualClusterByID(ctx, conn, id)
vc, err := findVirtualClusterByID(ctx, conn, id)

if err != nil {
return sdkdiag.AppendErrorf(diags, "reading EMR Containers Virtual Cluster (%s): %s", id, err)
Expand Down

0 comments on commit a78723d

Please sign in to comment.