Skip to content

Commit

Permalink
Don't set metadata_startup_script in some cases. (#1081)
Browse files Browse the repository at this point in the history
  • Loading branch information
nat-henderson authored Feb 14, 2018
1 parent 1966a79 commit f785116
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
4 changes: 4 additions & 0 deletions google/metadata.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package google

import (
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -151,6 +152,9 @@ func resourceInstanceMetadata(d *schema.ResourceData) (*computeBeta.Metadata, er
m := &computeBeta.Metadata{}
mdMap := d.Get("metadata").(map[string]interface{})
if v, ok := d.GetOk("metadata_startup_script"); ok && v.(string) != "" {
if ss, ok := mdMap["startup-script"]; ok && ss != "" {
return nil, errors.New("Cannot provide both metadata_startup_script and metadata.startup-script.")
}
mdMap["startup-script"] = v
}
if len(mdMap) > 0 {
Expand Down
25 changes: 18 additions & 7 deletions google/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,14 +778,20 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error
}

md := flattenMetadataBeta(instance.Metadata)

d.Set("metadata_startup_script", md["startup-script"])
// Note that here we delete startup-script from our metadata list. This is to prevent storing the startup-script
// as a value in the metadata since the config specifically tracks it under 'metadata_startup_script'
delete(md, "startup-script")

existingMetadata := d.Get("metadata").(map[string]interface{})

// If the existing config specifies "metadata.startup-script" instead of "metadata_startup_script",
// we shouldn't move the remote metadata.startup-script to metadata_startup_script. Otherwise,
// we should.
if ss, ok := existingMetadata["startup-script"]; !ok || ss == "" {
d.Set("metadata_startup_script", md["startup-script"])
// Note that here we delete startup-script from our metadata list. This is to prevent storing the startup-script
// as a value in the metadata since the config specifically tracks it under 'metadata_startup_script'
delete(md, "startup-script")
} else if _, ok := d.GetOk("metadata_startup_script"); ok {
delete(md, "startup-script")
}

// Delete any keys not explicitly set in our config file
for k := range md {
if _, ok := existingMetadata[k]; !ok {
Expand Down Expand Up @@ -928,11 +934,14 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err
// 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 {
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
}

Expand Down Expand Up @@ -969,10 +978,12 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err
}

d.SetPartial("metadata")

return nil
}

MetadataRetryWrapper(updateMD)

}

if d.HasChange("tags") {
Expand Down
4 changes: 3 additions & 1 deletion google/resource_compute_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1478,7 +1478,9 @@ resource "google_compute_instance" "foobar" {
create_timeout = 5
metadata_startup_script = "echo Hello"
metadata {
startup-script = "echo Hello"
}
labels {
my_key = "my_value"
Expand Down

0 comments on commit f785116

Please sign in to comment.