Don't set metadata_startup_script in some cases. (#1081)

This commit is contained in:
Nathan McKinley 2018-02-13 23:55:37 -08:00 committed by GitHub
parent 1966a791a6
commit f7851162a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package google package google
import ( import (
"errors"
"fmt" "fmt"
"strings" "strings"
@ -151,6 +152,9 @@ func resourceInstanceMetadata(d *schema.ResourceData) (*computeBeta.Metadata, er
m := &computeBeta.Metadata{} m := &computeBeta.Metadata{}
mdMap := d.Get("metadata").(map[string]interface{}) mdMap := d.Get("metadata").(map[string]interface{})
if v, ok := d.GetOk("metadata_startup_script"); ok && v.(string) != "" { 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 mdMap["startup-script"] = v
} }
if len(mdMap) > 0 { if len(mdMap) > 0 {

View File

@ -778,14 +778,20 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error
} }
md := flattenMetadataBeta(instance.Metadata) 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{}) 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 // Delete any keys not explicitly set in our config file
for k := range md { for k := range md {
if _, ok := existingMetadata[k]; !ok { if _, ok := existingMetadata[k]; !ok {
@ -928,11 +934,14 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err
// If the Metadata has changed, then update that. // If the Metadata has changed, then update that.
if d.HasChange("metadata") { if d.HasChange("metadata") {
o, n := d.GetChange("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 { 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") 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 n.(map[string]interface{})["startup-script"] = script
} }
@ -969,10 +978,12 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err
} }
d.SetPartial("metadata") d.SetPartial("metadata")
return nil return nil
} }
MetadataRetryWrapper(updateMD) MetadataRetryWrapper(updateMD)
} }
if d.HasChange("tags") { if d.HasChange("tags") {

View File

@ -1478,7 +1478,9 @@ resource "google_compute_instance" "foobar" {
create_timeout = 5 create_timeout = 5
metadata_startup_script = "echo Hello" metadata {
startup-script = "echo Hello"
}
labels { labels {
my_key = "my_value" my_key = "my_value"