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

Don't set metadata_startup_script in some cases. #1081

Merged
merged 4 commits into from
Feb 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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