-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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 boot_disk
property to google_compute_instance
#122
Conversation
Also note that tests are currently failing, but will be fixed in #1. |
google/resource_compute_instance.go
Outdated
@@ -394,12 +469,23 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err | |||
} | |||
|
|||
// Build up the list of disks | |||
|
|||
disks := []*compute.AttachedDisk{} | |||
var bootDisk *compute.AttachedDisk |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We keep the bootdisk
object in scope for the entire function but only use it to perform nil
checks outside of the if
block below. What if we moved it inside the if
block, and pulled up the ok
result of d.GetOk("boot_disk");
, giving it a name like hasBootDisk
?
That way, we know that code later in the function can only interact with the bootDisk
as part of disks
, and we can make lines like
disk.Boot = i == 0 && bootDisk == nil
look like:
disk.Boot = i == 0 && !hasBootDisk
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
@@ -1142,3 +1235,79 @@ func resourceInstanceTags(d *schema.ResourceData) *compute.Tags { | |||
|
|||
return tags | |||
} | |||
|
|||
func expandBootDisk(d *schema.ResourceData, config *Config, zone *compute.Zone, project string) (*compute.AttachedDisk, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like we only use schema elements that are properties of boot_disk.0
; we can reduce the information this function needs by passing in a limited set of data, like BackendService's expandBackends()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with that approach is that it auto-populates all the map keys, and sets them to default values. So if you want to check the existence of something, you have to check against "", 0, false, and nil, rather than being able to use a consistent d.GetOk("whatever"); ok
. It seems like you should be able to just do v, ok := data["whatever"]; ok
, but that will return true for elements not set in the config, which is a problem when you're then trying to use that value in another call, like in line 1274. This way feels less bug-prone.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense, thanks!
return disk, nil | ||
} | ||
|
||
func flattenBootDisk(d *schema.ResourceData, disk *compute.AttachedDisk) []map[string]interface{} { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as expandBootDisk - do we need access to all of schema.ResourceData
here? Or just a subset?
@@ -1187,6 +1248,49 @@ func testAccComputeInstance_attachedDisk(disk, instance string) string { | |||
}`, disk, instance) | |||
} | |||
|
|||
func testAccComputeInstance_bootDisk(instance string) string { | |||
return fmt.Sprintf(` | |||
resource "google_compute_instance" "foobar" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Can you de-indent both of these configs so that the top level is against the left side of the file? That way, they are easier to copy out and modify/test interactively, or for users who browse the source to pull out pre-tested .tf configurations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll do it in a separate PR so it's the same for all of them since there are a lot of tests in this file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Separate PR is submitted, updated here (and other comment as well)
network_interface { | ||
network = "default" | ||
} | ||
}`, instance) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: can the ` be moved to the next line? This would also make using these tests in .tf configs easier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
likewise
LGTM 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only a couple minor comments. This looks great.
"device_name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to be ForceNew?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup. Fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Forcenew:true would cause the current resource such as a db instance to be recreated. is this desired? maybe I am asking a terraform basic question instead of a google provider one here. thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It means that if the field in question changes in value then it would, yes.
|
||
disks := []*compute.AttachedDisk{} | ||
var hasBootDisk bool | ||
if _, hasBootDisk = d.GetOk("boot_disk"); hasBootDisk { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: if _, hasBootDisk :=
means you don't need var hasBootDisk bool
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need it so I can refer to it outside of the if statement
"device_name": disk.DeviceName, | ||
"source": sourceUrl[len(sourceUrl)-1], | ||
// disk_encryption_key_raw is not returned from the API, so don't store it in state. | ||
// If necessary in the future, this can be copied from what the user originally specified. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in other places we do the fingerprint of the key. Is that returned from the API?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, added
Rebase and LGTM. 👍 |
* Add boot_disk property to google_compute_instance * docs for boot_disk * limit scope of bootDisk, use bool instead * test formatting * make device_name forcenew, add sha256 encryption key
* Add boot_disk property to google_compute_instance * docs for boot_disk * limit scope of bootDisk, use bool instead * test formatting * make device_name forcenew, add sha256 encryption key
This seems like the best place to ask without opening a new ticket-- why does resizing the boot disk force creation of a new resource? Example code below:
If the size param is changed and
In the console, you can resize (or at least increase the size of) a the boot disk without destroying and recreating it. Is it possible to do so in Terraform? |
Hey @johnmehler, that's because the size there is in the |
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! |
This is change 1 in a series of changes to provide a better experience for attaching/detaching disks to/from instances. The end goal is to have a separate resource for attached disks, in order to have some attached disks that are managed by terraform side-by-side with disks that are managed some other way (such as by k8s), and to allow users to attach/detach disks (see #33)
The order of the changes will look roughly like:
boot_disk
propertyscratch_disk
property and deprecatedisk
disk
property, makeboot_disk
Requiredgoogle_compute_attached_disk
resource and deprecate theattached_disk
property