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

Allow attaching and detaching disks from instances #636

Merged
merged 2 commits into from
Nov 2, 2017

Conversation

danawillow
Copy link
Contributor

Third time's the charm? Fixes #33.

@danawillow
Copy link
Contributor Author

$ make testacc TEST=./google TESTARGS='-run=TestAccComputeInstance_attachedDisk -parallel 8'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./google -v -run=TestAccComputeInstance_attachedDisk -parallel 8 -timeout 120m
=== RUN   TestAccComputeInstance_attachedDisk
=== RUN   TestAccComputeInstance_attachedDisk_sourceUrl
=== RUN   TestAccComputeInstance_attachedDiskUpdate
--- PASS: TestAccComputeInstance_attachedDisk_sourceUrl (71.67s)
--- PASS: TestAccComputeInstance_attachedDisk (72.10s)
--- PASS: TestAccComputeInstance_attachedDiskUpdate (152.40s)
PASS
ok  	github.com/terraform-providers/terraform-provider-google/google	152.576s

Copy link
Contributor

@catsby catsby left a comment

Choose a reason for hiding this comment

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

Hi there! Drive-by thoughts:

  • is the ordering of attached_disk relevant? If so, TypeList is correct because we want the order of the config to be enforced, and reading from the API with a different order should trigger a diff. If not, we should use TypeSet for them; their key will be the hash and not their spot in line, if that makes sense. I realize the usage of TypeList is not part of this PR, but it's relevant to my next point..
  • Continuing down that path, Set’s offer an advantage with the Difference() method. See Set#Difference and usage example in AWS Security Groups UPDATE method. It seems in this code we’re doing a similar diff operation, and using Sets would greatly simply it for you
  • I see in the READ method (understandably not being changed here) there seems to be a notion of updating existing disks in config and also adding extra disks and storing them in attached_disk. It seems there is an issue here, in that we’re not completely overwriting attached_disk with what we find in the API results. Example, if there is a change and a disk Terraform thinks is attached (it’s in the config) is removed outside of Terraform, does Terraform notice on refresh? There doesn’t seem to be any part in the READ that’s removing disks not found. If we simply built up the list from the API and wrote those results, regardless of what we had in the config, we could detect that drift. Again, I realize this is outside of the current PR, but I noticed it.

// were at the time we ran terraform plan.
currDisks := map[string]struct{}{}
for _, disk := range instance.Disks {
if !disk.Boot {
Copy link
Contributor

Choose a reason for hiding this comment

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

Silly question: does this take scratch disks into account?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think it would have done anything unexpected since we're only using it to check whether a disk in state is still attached, but may as well do the check just in case. Done.

}
}

// Keep track of previous config's disks.
Copy link
Contributor

Choose a reason for hiding this comment

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

This isn't the previous config, it's the state. Terraform does not version configs itself, and so cannot tell you what was in the previous config.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@paddycarver
Copy link
Contributor

I see in the READ method (understandably not being changed here) there seems to be a notion of updating existing disks in config and also adding extra disks and storing them in attached_disk. It seems there is an issue here, in that we’re not completely overwriting attached_disk with what we find in the API results. Example, if there is a change and a disk Terraform thinks is attached (it’s in the config) is removed outside of Terraform, does Terraform notice on refresh? There doesn’t seem to be any part in the READ that’s removing disks not found. If we simply built up the list from the API and wrote those results, regardless of what we had in the config, we could detect that drift. Again, I realize this is outside of the current PR, but I noticed it.

From what I can see, the disks are being properly set based on the API response. That code should detect drift of:

  • Disks set in the config but not set on the API
  • Disks set in the API but not set in the config

I think the confusion may be around the extraAttachedDisks variable, which--as I understand it--exists to work around attached_disk being a List and not a Set, but I could be wrong on that. Essentially, the code looks to be ordering disks in the order they're in the config, with any disks not in the config appended to the end.

@danawillow
Copy link
Contributor Author

Yeah, having attached_disks as a Set would definitely simplify things, especially with that Difference method. I'll add that in a new PR and then come back to this one.

@danawillow
Copy link
Contributor Author

Paddy and I talked this over, and we're not going to try to make attached_disks into a set before this change, since it's a fair amount more work than expected, a breaking change, and not currently breaking anyone. See #656 for details.

@catsby
Copy link
Contributor

catsby commented Nov 2, 2017

@danawillow 👍

Copy link
Contributor

@catsby catsby left a comment

Choose a reason for hiding this comment

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

👍

@danawillow danawillow merged commit 51ed0b7 into hashicorp:master Nov 2, 2017
@danawillow danawillow deleted the attached-disk-update branch November 2, 2017 20:08
luis-silva pushed a commit to luis-silva/terraform-provider-google that referenced this pull request May 21, 2019
@ghost
Copy link

ghost commented Mar 30, 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 feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 [email protected]. Thanks!

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

Successfully merging this pull request may close these issues.

'google_compute_instance' adding 'google_compute_disk' forces new resource
3 participants