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

Add resource sums to terraform plan #2458

Merged
merged 1 commit into from
Jun 25, 2015

Conversation

radeksimko
Copy link
Member

Closes #2355

Test plan

sample.tf

provider "aws" {
  access_key = ""
  secret_key = ""
  region = "${var.aws_region}"
}

resource "aws_vpc" "main" {
  cidr_block = "10.0.1.0/16"
}

resource "aws_instance" "web" {
    ami = "ami-1234"
    instance_type = "m1.small"
}
$ terraform plan # 2 to add, 0 to change, 0 to destroy
$ terraform plan -target=aws_vpc.main # 1 to add, 0 to change, 0 to destroy
$ terraform plan -destroy # 0 to add, 0 to change, 0 to destroy
$ terraform apply
$ # change aws_vpc.main.cidr_block
$ terraform plan # 0 to add, 1 to change, 0 to destroy
$ terraform plan -destroy # 0 to add, 0 to change, 2 to destroy
$ terraform plan -destroy -target=aws_instance.web # 0 to add, 0 to change, 1 to destroy

toChange := 0
toCreate := 0
toDestroy := 0
toDestroyAndCreate := 0
Copy link
Contributor

Choose a reason for hiding this comment

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

You can avoid the local vars by initializing the PlanSums struct empty here and incrementing its fields in the loop below, since they'll each start at zero.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, good point! 👍 Thanks! I will change that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

@phinze
Copy link
Contributor

phinze commented Jun 24, 2015

Ah yes I've been meaning to do this!

@mitchellh is there an alternative strategy for counting these fields using hooks - or is the loop over modules easiest here?

@radeksimko
Copy link
Member Author

@phinze I was first looking at hooks actually.

I thought the reason for using hooks was because both apply, destroy, refresh etc. are expensive operations and there's no such easy way of counting real resources unless we'd buffer the output somewhere - i.e. we have to wait for provider's API response and then decide what to do on per-resource-basis.

Although I can imagine that a solution using hooks might look slightly cleaner.

@radeksimko radeksimko force-pushed the f-plan-resource-counts branch from 25b3223 to c7e315c Compare June 24, 2015 21:14
ToChange: 0,
ToCreate: 0,
ToDestroy: 0,
ToDestroyAndCreate: 0,
Copy link
Contributor

Choose a reason for hiding this comment

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

These fields default to their zero values, but the extra clarity doesn't hurt. 👍

@phinze
Copy link
Contributor

phinze commented Jun 24, 2015

LGTM! Merge at will.

@mitchellh
Copy link
Contributor

I think it'd definitely be cleaner to do this with hooks. You can hook into PostDiff and do almost the same logic as is shown here, but in there.

I'd prefer that, but don't mind if this is merged as a stop-gap until then.

@radeksimko radeksimko force-pushed the f-plan-resource-counts branch from c7e315c to 05992df Compare June 25, 2015 08:58
@radeksimko radeksimko force-pushed the f-plan-resource-counts branch from 05992df to b505d15 Compare June 25, 2015 09:27
@radeksimko
Copy link
Member Author

@mitchellh Agreed, to be honest, I just struggled to find the right hook.

It's now reworked & using PostDiff hook and it feels so much cleaner 🚿 + it will be easier to use this for potential sanity check in the future (i.e. expected to change x, but changed y in reality) since these things now share the namespace.

Will anyone of you two give a final 👍 / 👎 , please? :)

@phinze
Copy link
Contributor

phinze commented Jun 25, 2015

Ooo much nicer. LGTM

radeksimko added a commit that referenced this pull request Jun 25, 2015
@radeksimko radeksimko merged commit 8c866a4 into hashicorp:master Jun 25, 2015
@radeksimko radeksimko deleted the f-plan-resource-counts branch June 25, 2015 14:35
@mitchellh
Copy link
Contributor

Much cleaner :)

@ghost
Copy link

ghost commented May 2, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators May 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

terraform plan should list changed/created/destroyed resources
3 participants