Skip to content

Commit

Permalink
r/ecs_service: Migrate to AWS SDK v2
Browse files Browse the repository at this point in the history
  • Loading branch information
mattburgess committed Jun 17, 2024
1 parent be79828 commit 0706afe
Show file tree
Hide file tree
Showing 13 changed files with 431 additions and 432 deletions.
2 changes: 1 addition & 1 deletion internal/service/ecs/account_setting_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func resourceAccountSettingDefaultRead(ctx context.Context, d *schema.ResourceDa
EffectiveSettings: true,
}

log.Printf("[DEBUG] Reading Default Account Settings: %s", input)
log.Printf("[DEBUG] Reading Default Account Settings: %+v", input)
resp, err := conn.ListAccountSettings(ctx, input)

if err != nil {
Expand Down
11 changes: 7 additions & 4 deletions internal/service/ecs/capacity_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,9 @@ func resourceCapacityProviderCreate(ctx context.Context, d *schema.ResourceData,
func resourceCapacityProviderRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ECSClient(ctx)
partition := meta.(*conns.AWSClient).Partition

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

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] ECS Capacity Provider (%s) not found, removing from state", d.Id())
Expand Down Expand Up @@ -200,14 +201,15 @@ func resourceCapacityProviderRead(ctx context.Context, d *schema.ResourceData, m
func resourceCapacityProviderUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ECSClient(ctx)
partition := meta.(*conns.AWSClient).Partition

if d.HasChangesExcept(names.AttrTags, names.AttrTagsAll) {
input := &ecs.UpdateCapacityProviderInput{
AutoScalingGroupProvider: expandAutoScalingGroupProviderUpdate(d.Get("auto_scaling_group_provider")),
Name: aws.String(d.Get(names.AttrName).(string)),
}

log.Printf("[DEBUG] Updating ECS Capacity Provider: %s", input)
log.Printf("[DEBUG] Updating ECS Capacity Provider: %+v", input)
err := retry.RetryContext(ctx, capacityProviderUpdateTimeout, func() *retry.RetryError {
_, err := conn.UpdateCapacityProvider(ctx, input)

Expand All @@ -230,7 +232,7 @@ func resourceCapacityProviderUpdate(ctx context.Context, d *schema.ResourceData,
return sdkdiag.AppendErrorf(diags, "updating ECS Capacity Provider (%s): %s", d.Id(), err)
}

if _, err = waitCapacityProviderUpdated(ctx, conn, d.Id()); err != nil {
if _, err = waitCapacityProviderUpdated(ctx, conn, partition, d.Id()); err != nil {
return sdkdiag.AppendErrorf(diags, "waiting for ECS Capacity Provider (%s) to update: %s", d.Id(), err)
}
}
Expand All @@ -241,6 +243,7 @@ func resourceCapacityProviderUpdate(ctx context.Context, d *schema.ResourceData,
func resourceCapacityProviderDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ECSClient(ctx)
partition := meta.(*conns.AWSClient).Partition

log.Printf("[DEBUG] Deleting ECS Capacity Provider (%s)", d.Id())
_, err := conn.DeleteCapacityProvider(ctx, &ecs.DeleteCapacityProviderInput{
Expand All @@ -256,7 +259,7 @@ func resourceCapacityProviderDelete(ctx context.Context, d *schema.ResourceData,
return sdkdiag.AppendErrorf(diags, "deleting ECS Capacity Provider (%s): %s", d.Id(), err)
}

if _, err := waitCapacityProviderDeleted(ctx, conn, d.Id()); err != nil {
if _, err := waitCapacityProviderDeleted(ctx, conn, partition, d.Id()); err != nil {
return sdkdiag.AppendErrorf(diags, "waiting for ECS Capacity Provider (%s) to delete: %s", d.Id(), err)
}

Expand Down
7 changes: 4 additions & 3 deletions internal/service/ecs/capacity_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,18 +236,18 @@ func TestAccECSCapacityProvider_tags(t *testing.T) {
func testAccCheckCapacityProviderDestroy(ctx context.Context) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).ECSClient(ctx)
partition := acctest.Provider.Meta().(*conns.AWSClient).Partition

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_ecs_capacity_provider" {
continue
}

_, err := tfecs.FindCapacityProviderByARN(ctx, conn, rs.Primary.ID)
_, err := tfecs.FindCapacityProviderByARN(ctx, conn, partition, rs.Primary.ID)

if tfresource.NotFound(err) {
continue
}

if err != nil {
return err
}
Expand All @@ -271,8 +271,9 @@ func testAccCheckCapacityProviderExists(ctx context.Context, resourceName string
}

conn := acctest.Provider.Meta().(*conns.AWSClient).ECSClient(ctx)
partition := acctest.Provider.Meta().(*conns.AWSClient).Partition

output, err := tfecs.FindCapacityProviderByARN(ctx, conn, rs.Primary.ID)
output, err := tfecs.FindCapacityProviderByARN(ctx, conn, partition, rs.Primary.ID)

if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion internal/service/ecs/container_definition_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func dataSourceContainerDefinitionRead(ctx context.Context, d *schema.ResourceDa
params := &ecs.DescribeTaskDefinitionInput{
TaskDefinition: aws.String(d.Get("task_definition").(string)),
}
log.Printf("[DEBUG] Reading ECS Container Definition: %s", params)
log.Printf("[DEBUG] Reading ECS Container Definition: %+v", params)
desc, err := conn.DescribeTaskDefinition(ctx, params)

if err != nil {
Expand Down
66 changes: 33 additions & 33 deletions internal/service/ecs/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,35 @@ import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ecs"
awstypes "github.com/aws/aws-sdk-go-v2/service/ecs/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

func FindCapacityProviderByARN(ctx context.Context, conn *ecs.ECS, arn string) (*ecs.CapacityProvider, error) {
func FindCapacityProviderByARN(ctx context.Context, conn *ecs.Client, partition string, arn string) (*awstypes.CapacityProvider, error) {
input := &ecs.DescribeCapacityProvidersInput{
CapacityProviders: aws.StringSlice([]string{arn}),
Include: aws.StringSlice([]string{ecs.CapacityProviderFieldTags}),
CapacityProviders: []string{arn},
Include: []awstypes.CapacityProviderField{awstypes.CapacityProviderFieldTags},
}

output, err := conn.DescribeCapacityProvidersWithContext(ctx, input)
output, err := conn.DescribeCapacityProviders(ctx, input)

// Some partitions (i.e., ISO) may not support tagging, giving error
if errs.IsUnsupportedOperationInPartitionError(conn.PartitionID, err) {
if errs.IsUnsupportedOperationInPartitionError(partition, err) {
log.Printf("[WARN] ECS tagging failed describing Capacity Provider (%s) with tags: %s; retrying without tags", arn, err)

input.Include = nil
output, err = conn.DescribeCapacityProvidersWithContext(ctx, input)
output, err = conn.DescribeCapacityProviders(ctx, input)
}

if err != nil {
return nil, err
}

if output == nil || len(output.CapacityProviders) == 0 || output.CapacityProviders[0] == nil {
if output == nil || len(output.CapacityProviders) == 0 {
return nil, &retry.NotFoundError{
Message: "Empty result",
LastRequest: input,
Expand All @@ -45,35 +45,35 @@ func FindCapacityProviderByARN(ctx context.Context, conn *ecs.ECS, arn string) (

capacityProvider := output.CapacityProviders[0]

if status := aws.StringValue(capacityProvider.Status); status == ecs.CapacityProviderStatusInactive {
if capacityProvider.Status == awstypes.CapacityProviderStatusInactive {
return nil, &retry.NotFoundError{
Message: status,
Message: string(capacityProvider.Status),
LastRequest: input,
}
}

return capacityProvider, nil
return &capacityProvider, nil
}

func FindServiceByID(ctx context.Context, conn *ecs.ECS, id, cluster string) (*ecs.Service, error) {
func FindServiceByID(ctx context.Context, conn *ecs.Client, partition, id, cluster string) (*awstypes.Service, error) {
input := &ecs.DescribeServicesInput{
Cluster: aws.String(cluster),
Include: aws.StringSlice([]string{ecs.ServiceFieldTags}),
Services: aws.StringSlice([]string{id}),
Include: []awstypes.ServiceField{awstypes.ServiceFieldTags},
Services: []string{id},
}

return FindService(ctx, conn, input)
return FindService(ctx, conn, partition, input)
}

func FindServiceNoTagsByID(ctx context.Context, conn *ecs.ECS, id, cluster string) (*ecs.Service, error) {
func FindServiceNoTagsByID(ctx context.Context, conn *ecs.Client, partition, id, cluster string) (*awstypes.Service, error) {
input := &ecs.DescribeServicesInput{
Services: aws.StringSlice([]string{id}),
Services: []string{id},
}
if cluster != "" {
input.Cluster = aws.String(cluster)
}

return FindService(ctx, conn, input)
return FindService(ctx, conn, partition, input)
}

type expectActiveError struct {
Expand All @@ -90,46 +90,46 @@ func (e *expectActiveError) Error() string {
return fmt.Sprintf("expected status %[1]q, was %[2]q", serviceStatusActive, e.status)
}

func FindServiceByIDWaitForActive(ctx context.Context, conn *ecs.ECS, id, cluster string) (*ecs.Service, error) {
var service *ecs.Service
func FindServiceByIDWaitForActive(ctx context.Context, conn *ecs.Client, partition, id, cluster string) (*awstypes.Service, error) {
var service *awstypes.Service
// Use the retry.RetryContext function instead of WaitForState() because we don't want the timeout error, if any
err := retry.RetryContext(ctx, serviceDescribeTimeout, func() *retry.RetryError {
var err error
service, err = FindServiceByID(ctx, conn, id, cluster)
service, err = FindServiceByID(ctx, conn, id, partition, cluster)
if tfresource.NotFound(err) {
return retry.RetryableError(err)
}
if err != nil {
return retry.NonRetryableError(err)
}

if status := aws.StringValue(service.Status); status != serviceStatusActive {
if status := aws.ToString(service.Status); status != serviceStatusActive {
return retry.RetryableError(newExpectActiveError(status))
}

return nil
})
if tfresource.TimedOut(err) {
service, err = FindServiceByID(ctx, conn, id, cluster)
service, err = FindServiceByID(ctx, conn, id, partition, cluster)
}

return service, err
}

func FindService(ctx context.Context, conn *ecs.ECS, input *ecs.DescribeServicesInput) (*ecs.Service, error) {
output, err := conn.DescribeServicesWithContext(ctx, input)
func FindService(ctx context.Context, conn *ecs.Client, partition string, input *ecs.DescribeServicesInput) (*awstypes.Service, error) {
output, err := conn.DescribeServices(ctx, input)

if errs.IsUnsupportedOperationInPartitionError(conn.PartitionID, err) && input.Include != nil {
id := aws.StringValueSlice(input.Services)[0]
if errs.IsUnsupportedOperationInPartitionError(partition, err) && input.Include != nil {
id := input.Services[0]
log.Printf("[WARN] failed describing ECS Service (%s) with tags: %s; retrying without tags", id, err)

input.Include = nil
output, err = conn.DescribeServicesWithContext(ctx, input)
output, err = conn.DescribeServices(ctx, input)
}

// As of AWS SDK for Go v1.44.42, DescribeServices does not return the error code ecs.ErrCodeServiceNotFoundException
// Keep this here in case it ever does
if tfawserr.ErrCodeEquals(err, ecs.ErrCodeServiceNotFoundException) {
if errs.IsA[*awstypes.ServiceNotFoundException](err) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: input,
Expand All @@ -141,7 +141,7 @@ func FindService(ctx context.Context, conn *ecs.ECS, input *ecs.DescribeServices

// When an ECS Service is not found by DescribeServices(), it will return a Failure struct with Reason = "MISSING"
for _, v := range output.Failures {
if aws.StringValue(v.Reason) == "MISSING" {
if aws.ToString(v.Reason) == "MISSING" {
return nil, &retry.NotFoundError{
LastRequest: input,
}
Expand All @@ -155,5 +155,5 @@ func FindService(ctx context.Context, conn *ecs.ECS, input *ecs.DescribeServices
return nil, tfresource.NewTooManyResultsError(n, input)
}

return output.Services[0], nil
return &output.Services[0], nil
}
14 changes: 7 additions & 7 deletions internal/service/ecs/flex.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func expandLoadBalancers(configured []interface{}) []awstypes.LoadBalancer {
}

// Flattens an array of ECS LoadBalancers into a []map[string]interface{}
func flattenLoadBalancers(list []*awstypes.LoadBalancer) []map[string]interface{} {
func flattenLoadBalancers(list []awstypes.LoadBalancer) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(list))
for _, loadBalancer := range list {
l := map[string]interface{}{
Expand Down Expand Up @@ -110,7 +110,7 @@ func expandTaskSetLoadBalancers(l []interface{}) []awstypes.LoadBalancer {
for _, lRaw := range l {
data := lRaw.(map[string]interface{})

l := &awstypes.LoadBalancer{}
l := awstypes.LoadBalancer{}

if v, ok := data["container_name"].(string); ok && v != "" {
l.ContainerName = aws.String(v)
Expand Down Expand Up @@ -162,7 +162,7 @@ func expandServiceRegistries(l []interface{}) []awstypes.ServiceRegistry {

for _, v := range l {
m := v.(map[string]interface{})
sr := &awstypes.ServiceRegistry{
sr := awstypes.ServiceRegistry{
RegistryArn: aws.String(m["registry_arn"].(string)),
}
if raw, ok := m["container_name"].(string); ok && raw != "" {
Expand Down Expand Up @@ -195,11 +195,11 @@ func expandScale(l []interface{}) *awstypes.Scale {
result := &awstypes.Scale{}

if v, ok := tfMap[names.AttrUnit].(string); ok && v != "" {
result.Unit = aws.String(v)
result.Unit = awstypes.ScaleUnit(v)
}

if v, ok := tfMap[names.AttrValue].(float64); ok {
result.Value = aws.Float64(v)
result.Value = v
}

return result
Expand All @@ -212,8 +212,8 @@ func flattenScale(scale *awstypes.Scale) []map[string]interface{} {
}

m := make(map[string]interface{})
m[names.AttrUnit] = aws.ToString(scale.Unit)
m[names.AttrValue] = aws.ToFloat64(scale.Value)
m[names.AttrUnit] = string(scale.Unit)
m[names.AttrValue] = scale.Value

return []map[string]interface{}{m}
}
Loading

0 comments on commit 0706afe

Please sign in to comment.