Skip to content

Commit

Permalink
Make google_compute_instance metadata authoritative. (#2208)
Browse files Browse the repository at this point in the history
  • Loading branch information
rileykarson authored Oct 9, 2018
1 parent 99c8a17 commit 45db116
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 59 deletions.
74 changes: 16 additions & 58 deletions google/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
metadataV1 := &compute.Metadata{}
if err := Convert(metadata, metadataV1); err != nil {
return err
}

return nil
op, err := config.clientCompute.Instances.SetMetadata(project, zone, d.Id(), metadataV1).Do()
if err != nil {
return fmt.Errorf("Error updating metadata: %s", err)
}

MetadataRetryWrapper(updateMD)
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") {
Expand Down
3 changes: 2 additions & 1 deletion website/docs/r/compute_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 45db116

Please sign in to comment.