Make google_compute_instance metadata authoritative. (#2208)

This commit is contained in:
Riley Karson 2018-10-09 15:29:26 -07:00 committed by Nathan McKinley
parent 902675c324
commit 8c42015451
2 changed files with 19 additions and 60 deletions

View File

@ -789,17 +789,11 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error
delete(md, "startup-script")
}
// Delete any keys not explicitly set in our config file
for k := range md {
if _, ok := existingMetadata[k]; !ok {
delete(md, k)
}
}
if err = d.Set("metadata", md); err != nil {
return fmt.Errorf("Error setting metadata: %s", err)
}
d.Set("metadata_fingerprint", instance.Metadata.Fingerprint)
d.Set("can_ip_forward", instance.CanIpForward)
d.Set("machine_type", GetResourceNameFromSelfLink(instance.MachineType))
@ -827,11 +821,6 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error
"host": sshIP,
})
// Set the metadata fingerprint if there is one.
if instance.Metadata != nil {
d.Set("metadata_fingerprint", instance.Metadata.Fingerprint)
}
// Set the tags fingerprint if there is one.
if instance.Tags != nil {
d.Set("tags_fingerprint", instance.Tags.Fingerprint)
@ -966,59 +955,28 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err
// Enable partial mode for the resource since it is possible
d.Partial(true)
// If the Metadata has changed, then update that.
if d.HasChange("metadata") {
o, n := d.GetChange("metadata")
if script, scriptExists := d.GetOk("metadata_startup_script"); scriptExists && script != "" {
if _, ok := n.(map[string]interface{})["startup-script"]; ok {
return fmt.Errorf("Only one of metadata.startup-script and metadata_startup_script may be defined")
}
if err = d.Set("metadata", n); err != nil {
return err
}
n.(map[string]interface{})["startup-script"] = script
metadata, err := resourceInstanceMetadata(d)
if err != nil {
return fmt.Errorf("Error parsing metadata: %s", err)
}
updateMD := func() error {
// Reload the instance in the case of a fingerprint mismatch
instance, err = getInstance(config, d)
if err != nil {
return err
}
md := instance.Metadata
BetaMetadataUpdate(o.(map[string]interface{}), n.(map[string]interface{}), md)
if err != nil {
return fmt.Errorf("Error updating metadata: %s", err)
}
mdV1 := &compute.Metadata{}
err = Convert(md, mdV1)
if err != nil {
return err
}
op, err := config.clientCompute.Instances.SetMetadata(
project, zone, d.Id(), mdV1).Do()
if err != nil {
return fmt.Errorf("Error updating metadata: %s", err)
}
opErr := computeOperationWaitTime(config.clientCompute, op, project, "metadata to update", int(d.Timeout(schema.TimeoutUpdate).Minutes()))
if opErr != nil {
return opErr
}
d.SetPartial("metadata")
return nil
metadataV1 := &compute.Metadata{}
if err := Convert(metadata, metadataV1); err != nil {
return err
}
MetadataRetryWrapper(updateMD)
op, err := config.clientCompute.Instances.SetMetadata(project, zone, d.Id(), metadataV1).Do()
if err != nil {
return fmt.Errorf("Error updating metadata: %s", err)
}
opErr := computeOperationWaitTime(config.clientCompute, op, project, "metadata to update", int(d.Timeout(schema.TimeoutUpdate).Minutes()))
if opErr != nil {
return opErr
}
d.SetPartial("metadata")
}
if d.HasChange("tags") {

View File

@ -100,7 +100,8 @@ The following arguments are supported:
* `labels` - (Optional) A set of key/value label pairs to assign to the instance.
* `metadata` - (Optional) Metadata key/value pairs to make available from
within the instance.
within the instance. Ssh keys attached in the Cloud Console will be removed.
Add them to your config in order to keep them attached to your instance.
* `metadata_startup_script` - (Optional) An alternative to using the
startup-script metadata key, except this one forces the instance to be