Skip to content

Commit c4f5c17

Browse files
committed
r/aws_instance: Move finder functions.
1 parent 382c4a4 commit c4f5c17

File tree

2 files changed

+134
-135
lines changed

2 files changed

+134
-135
lines changed

internal/service/ec2/ec2_instance.go

Lines changed: 1 addition & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"encoding/hex"
1111
"errors"
1212
"fmt"
13-
"iter"
1413
"log"
1514
"maps"
1615
"slices"
@@ -33,7 +32,6 @@ import (
3332
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
3433
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
3534
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
36-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
3735
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
3836
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
3937
"github.com/hashicorp/terraform-provider-aws/internal/backoff"
@@ -2930,77 +2928,6 @@ func buildInstanceOpts(ctx context.Context, d *schema.ResourceData, meta any) (*
29302928
return opts, nil
29312929
}
29322930

2933-
func findInstanceByID(ctx context.Context, conn *ec2.Client, id string) (*awstypes.Instance, error) {
2934-
input := ec2.DescribeInstancesInput{
2935-
InstanceIds: []string{id},
2936-
}
2937-
2938-
output, err := findInstance(ctx, conn, &input)
2939-
2940-
if err != nil {
2941-
return nil, err
2942-
}
2943-
2944-
if state := output.State.Name; state == awstypes.InstanceStateNameTerminated {
2945-
return nil, &retry.NotFoundError{
2946-
Message: string(state),
2947-
LastRequest: &input,
2948-
}
2949-
}
2950-
2951-
// Eventual consistency check.
2952-
if aws.ToString(output.InstanceId) != id {
2953-
return nil, &retry.NotFoundError{
2954-
LastRequest: &input,
2955-
}
2956-
}
2957-
2958-
return output, nil
2959-
}
2960-
2961-
func findInstance(ctx context.Context, conn *ec2.Client, input *ec2.DescribeInstancesInput) (*awstypes.Instance, error) {
2962-
var output []awstypes.Instance
2963-
for v, err := range listInstances(ctx, conn, input) {
2964-
if err != nil {
2965-
return nil, err
2966-
}
2967-
output = append(output, v)
2968-
}
2969-
2970-
return tfresource.AssertSingleValueResult(output, func(v *awstypes.Instance) bool { return v.State != nil })
2971-
}
2972-
2973-
// DescribeInstances is an "All-Or-Some" call.
2974-
func listInstances(ctx context.Context, conn *ec2.Client, input *ec2.DescribeInstancesInput) iter.Seq2[awstypes.Instance, error] {
2975-
return func(yield func(awstypes.Instance, error) bool) {
2976-
pages := ec2.NewDescribeInstancesPaginator(conn, input)
2977-
for pages.HasMorePages() {
2978-
page, err := pages.NextPage(ctx)
2979-
2980-
if tfawserr.ErrCodeEquals(err, errCodeInvalidInstanceIDNotFound) {
2981-
yield(awstypes.Instance{}, &retry.NotFoundError{
2982-
LastError: err,
2983-
LastRequest: &input,
2984-
})
2985-
return
2986-
}
2987-
2988-
if err != nil {
2989-
yield(awstypes.Instance{}, err)
2990-
return
2991-
}
2992-
2993-
for _, v := range page.Reservations {
2994-
for _, instance := range v.Instances {
2995-
if !yield(instance, nil) {
2996-
return
2997-
}
2998-
}
2999-
}
3000-
}
3001-
}
3002-
}
3003-
30042931
// startInstance starts an EC2 instance and waits for the instance to start.
30052932
func startInstance(ctx context.Context, conn *ec2.Client, id string, retry bool, timeout time.Duration) error {
30062933
var err error
@@ -4036,43 +3963,6 @@ func findInstanceLaunchTemplateVersion(ctx context.Context, conn *ec2.Client, id
40363963
return launchTemplateVersion, nil
40373964
}
40383965

4039-
func findLaunchTemplateData(ctx context.Context, conn *ec2.Client, launchTemplateSpecification *awstypes.LaunchTemplateSpecification) (*awstypes.ResponseLaunchTemplateData, error) {
4040-
input := ec2.DescribeLaunchTemplateVersionsInput{}
4041-
4042-
if v := aws.ToString(launchTemplateSpecification.LaunchTemplateId); v != "" {
4043-
input.LaunchTemplateId = aws.String(v)
4044-
} else if v := aws.ToString(launchTemplateSpecification.LaunchTemplateName); v != "" {
4045-
input.LaunchTemplateName = aws.String(v)
4046-
}
4047-
4048-
var latestVersion bool
4049-
4050-
if v := aws.ToString(launchTemplateSpecification.Version); v != "" {
4051-
switch v {
4052-
case launchTemplateVersionDefault:
4053-
input.Filters = newAttributeFilterList(map[string]string{
4054-
"is-default-version": "true",
4055-
})
4056-
case launchTemplateVersionLatest:
4057-
latestVersion = true
4058-
default:
4059-
input.Versions = []string{v}
4060-
}
4061-
}
4062-
4063-
output, err := findLaunchTemplateVersions(ctx, conn, &input)
4064-
4065-
if err != nil {
4066-
return nil, fmt.Errorf("reading EC2 Launch Template versions: %w", err)
4067-
}
4068-
4069-
if latestVersion {
4070-
return output[len(output)-1].LaunchTemplateData, nil
4071-
}
4072-
4073-
return output[0].LaunchTemplateData, nil
4074-
}
4075-
40763966
// findLaunchTemplateNameAndVersions returns the specified launch template's name, default version and latest version.
40773967
func findLaunchTemplateNameAndVersions(ctx context.Context, conn *ec2.Client, id string) (string, string, string, error) {
40783968
lt, err := findLaunchTemplateByID(ctx, conn, id)
@@ -4081,31 +3971,7 @@ func findLaunchTemplateNameAndVersions(ctx context.Context, conn *ec2.Client, id
40813971
return "", "", "", err
40823972
}
40833973

4084-
return aws.ToString(lt.LaunchTemplateName), strconv.FormatInt(aws.ToInt64(lt.DefaultVersionNumber), 10), strconv.FormatInt(aws.ToInt64(lt.LatestVersionNumber), 10), nil
4085-
}
4086-
4087-
func findInstanceTagValue(ctx context.Context, conn *ec2.Client, instanceID, tagKey string) (string, error) {
4088-
input := ec2.DescribeTagsInput{
4089-
Filters: newAttributeFilterList(map[string]string{
4090-
"resource-id": instanceID,
4091-
names.AttrKey: tagKey,
4092-
}),
4093-
}
4094-
4095-
output, err := conn.DescribeTags(ctx, &input)
4096-
4097-
if err != nil {
4098-
return "", err
4099-
}
4100-
4101-
switch count := len(output.Tags); count {
4102-
case 0:
4103-
return "", nil
4104-
case 1:
4105-
return aws.ToString(output.Tags[0].Value), nil
4106-
default:
4107-
return "", tfresource.NewTooManyResultsError(count, input)
4108-
}
3974+
return aws.ToString(lt.LaunchTemplateName), flex.Int64ToStringValue(lt.DefaultVersionNumber), flex.Int64ToStringValue(lt.LatestVersionNumber), nil
41093975
}
41103976

41113977
// isSnowballEdgeInstance returns whether or not the specified instance ID indicates an SBE instance.

internal/service/ec2/find.go

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package ec2
66
import (
77
"context"
88
"fmt"
9+
"iter"
910
"slices"
1011
"strconv"
1112
"strings"
@@ -379,6 +380,77 @@ func findHost(ctx context.Context, conn *ec2.Client, input *ec2.DescribeHostsInp
379380
return tfresource.AssertSingleValueResult(output, func(v *awstypes.Host) bool { return v.HostProperties != nil })
380381
}
381382

383+
func findInstanceByID(ctx context.Context, conn *ec2.Client, id string) (*awstypes.Instance, error) {
384+
input := ec2.DescribeInstancesInput{
385+
InstanceIds: []string{id},
386+
}
387+
388+
output, err := findInstance(ctx, conn, &input)
389+
390+
if err != nil {
391+
return nil, err
392+
}
393+
394+
if state := output.State.Name; state == awstypes.InstanceStateNameTerminated {
395+
return nil, &retry.NotFoundError{
396+
Message: string(state),
397+
LastRequest: &input,
398+
}
399+
}
400+
401+
// Eventual consistency check.
402+
if aws.ToString(output.InstanceId) != id {
403+
return nil, &retry.NotFoundError{
404+
LastRequest: &input,
405+
}
406+
}
407+
408+
return output, nil
409+
}
410+
411+
func findInstance(ctx context.Context, conn *ec2.Client, input *ec2.DescribeInstancesInput) (*awstypes.Instance, error) {
412+
var output []awstypes.Instance
413+
for v, err := range listInstances(ctx, conn, input) {
414+
if err != nil {
415+
return nil, err
416+
}
417+
output = append(output, v)
418+
}
419+
420+
return tfresource.AssertSingleValueResult(output, func(v *awstypes.Instance) bool { return v.State != nil })
421+
}
422+
423+
// DescribeInstances is an "All-Or-Some" call.
424+
func listInstances(ctx context.Context, conn *ec2.Client, input *ec2.DescribeInstancesInput) iter.Seq2[awstypes.Instance, error] {
425+
return func(yield func(awstypes.Instance, error) bool) {
426+
pages := ec2.NewDescribeInstancesPaginator(conn, input)
427+
for pages.HasMorePages() {
428+
page, err := pages.NextPage(ctx)
429+
430+
if tfawserr.ErrCodeEquals(err, errCodeInvalidInstanceIDNotFound) {
431+
yield(awstypes.Instance{}, &retry.NotFoundError{
432+
LastError: err,
433+
LastRequest: &input,
434+
})
435+
return
436+
}
437+
438+
if err != nil {
439+
yield(awstypes.Instance{}, err)
440+
return
441+
}
442+
443+
for _, v := range page.Reservations {
444+
for _, instance := range v.Instances {
445+
if !yield(instance, nil) {
446+
return
447+
}
448+
}
449+
}
450+
}
451+
}
452+
}
453+
382454
func findInstanceCreditSpecifications(ctx context.Context, conn *ec2.Client, input *ec2.DescribeInstanceCreditSpecificationsInput) ([]awstypes.InstanceCreditSpecification, error) {
383455
var output []awstypes.InstanceCreditSpecification
384456

@@ -403,6 +475,30 @@ func findInstanceCreditSpecifications(ctx context.Context, conn *ec2.Client, inp
403475
return output, nil
404476
}
405477

478+
func findInstanceTagValue(ctx context.Context, conn *ec2.Client, instanceID, tagKey string) (string, error) {
479+
input := ec2.DescribeTagsInput{
480+
Filters: newAttributeFilterList(map[string]string{
481+
"resource-id": instanceID,
482+
names.AttrKey: tagKey,
483+
}),
484+
}
485+
486+
output, err := conn.DescribeTags(ctx, &input)
487+
488+
if err != nil {
489+
return "", err
490+
}
491+
492+
switch count := len(output.Tags); count {
493+
case 0:
494+
return "", nil
495+
case 1:
496+
return aws.ToString(output.Tags[0].Value), nil
497+
default:
498+
return "", tfresource.NewTooManyResultsError(count, input)
499+
}
500+
}
501+
406502
func findInstanceCreditSpecification(ctx context.Context, conn *ec2.Client, input *ec2.DescribeInstanceCreditSpecificationsInput) (*awstypes.InstanceCreditSpecification, error) {
407503
output, err := findInstanceCreditSpecifications(ctx, conn, input)
408504

@@ -785,6 +881,43 @@ func findLaunchTemplateVersionByTwoPartKey(ctx context.Context, conn *ec2.Client
785881
return output, nil
786882
}
787883

884+
func findLaunchTemplateData(ctx context.Context, conn *ec2.Client, launchTemplateSpecification *awstypes.LaunchTemplateSpecification) (*awstypes.ResponseLaunchTemplateData, error) {
885+
input := ec2.DescribeLaunchTemplateVersionsInput{}
886+
887+
if v := aws.ToString(launchTemplateSpecification.LaunchTemplateId); v != "" {
888+
input.LaunchTemplateId = aws.String(v)
889+
} else if v := aws.ToString(launchTemplateSpecification.LaunchTemplateName); v != "" {
890+
input.LaunchTemplateName = aws.String(v)
891+
}
892+
893+
var latestVersion bool
894+
895+
if v := aws.ToString(launchTemplateSpecification.Version); v != "" {
896+
switch v {
897+
case launchTemplateVersionDefault:
898+
input.Filters = newAttributeFilterList(map[string]string{
899+
"is-default-version": "true",
900+
})
901+
case launchTemplateVersionLatest:
902+
latestVersion = true
903+
default:
904+
input.Versions = []string{v}
905+
}
906+
}
907+
908+
output, err := findLaunchTemplateVersions(ctx, conn, &input)
909+
910+
if err != nil {
911+
return nil, fmt.Errorf("reading EC2 Launch Template versions: %w", err)
912+
}
913+
914+
if latestVersion {
915+
return output[len(output)-1].LaunchTemplateData, nil
916+
}
917+
918+
return output[0].LaunchTemplateData, nil
919+
}
920+
788921
func findLocalGatewayRouteTable(ctx context.Context, conn *ec2.Client, input *ec2.DescribeLocalGatewayRouteTablesInput) (*awstypes.LocalGatewayRouteTable, error) {
789922
output, err := findLocalGatewayRouteTables(ctx, conn, input)
790923

0 commit comments

Comments
 (0)