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

r/aws_instance: launch_template Remove unnecessary api calls #20357

Merged
Changes from 1 commit
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
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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently HasChange with an empty state will always return true when resource filed has Default value, so I've added a check for top-level key.

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