Skip to content

Commit

Permalink
r/aws_instance: launch_template Remove unnecessary api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
gordonbondon committed Jul 29, 2021
1 parent 236e9e4 commit c19023f
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions aws/resource_aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,9 @@ func resourceAwsInstance() *schema.Resource {
CustomizeDiff: customdiff.All(
SetTagsDiff,
func(_ context.Context, diff *schema.ResourceDiff, meta interface{}) error {
if diff.HasChange("launch_template.0.version") {
_, ok := diff.GetOk("launch_template")

if diff.Id() != "" && diff.HasChange("launch_template.0.version") && ok {
conn := meta.(*AWSClient).ec2conn

stateVersion := diff.Get("launch_template.0.version")
Expand Down Expand Up @@ -3095,7 +3097,7 @@ func getAwsInstanceLaunchTemplate(conn *ec2.EC2, d *schema.ResourceData) ([]map[
name, defaultVersion, latestVersion, err := getAwsLaunchTemplateSpecification(conn, id)

if err != nil {
if isAWSErr(err, "InvalidLaunchTemplateId.Malformed", "") {
if isAWSErr(err, "InvalidLaunchTemplateId.Malformed", "") || isAWSErr(err, "InvalidLaunchTemplateId.NotFound", "") {
// Instance is tagged with non existent template just set it to nil
log.Printf("[WARN] Launch template %s not found, removing from state", id)
return nil, nil
Expand All @@ -3106,42 +3108,42 @@ func getAwsInstanceLaunchTemplate(conn *ec2.EC2, d *schema.ResourceData) ([]map[
attrs["id"] = id
attrs["name"] = name

version, err := getAwsInstanceLaunchTemplateVersion(conn, d.Id())
liveVersion, err := getAwsInstanceLaunchTemplateVersion(conn, d.Id())
if err != nil {
return nil, err
}

dltvi := &ec2.DescribeLaunchTemplateVersionsInput{
LaunchTemplateId: aws.String(id),
Versions: []*string{aws.String(version)},
Versions: []*string{aws.String(liveVersion)},
}

if _, err := conn.DescribeLaunchTemplateVersions(dltvi); err != nil {
if isAWSErr(err, "InvalidLaunchTemplateId.VersionNotFound", "") {
// Instance is tagged with non existent template version, just don't set it
log.Printf("[WARN] Launch template %s version %s not found, removing from state", id, version)
log.Printf("[WARN] Launch template %s version %s not found, removing from state", id, liveVersion)
result = append(result, attrs)
return result, nil
}
return nil, fmt.Errorf("error reading Launch Template Version: %s", err)
}

if v, ok := d.GetOk("launch_template.0.version"); ok {
switch v {
if stateVersion, ok := d.GetOk("launch_template.0.version"); ok {
switch stateVersion {
case "$Default":
if version == defaultVersion {
if liveVersion == defaultVersion {
attrs["version"] = "$Default"
} else {
attrs["version"] = version
attrs["version"] = liveVersion
}
case "$Latest":
if version == latestVersion {
if liveVersion == latestVersion {
attrs["version"] = "$Latest"
} else {
attrs["version"] = version
attrs["version"] = liveVersion
}
default:
attrs["version"] = version
attrs["version"] = liveVersion
}
}

Expand Down

0 comments on commit c19023f

Please sign in to comment.