providers/google: fix instance creation

with this commit, the google compute instance acceptance tests are
passing

 - remove GOOGLE_CLIENT_FILE requirement from provider tests to finish
   out #452
 - skip extra "#" key that shows up in metadata maps, fixes #757 and
   sprouts #883 to figure out core issue
 - more verbose variablenames in metadata parsing, since it took me
   awhile to grok and i thought there might have been a shadowing bug in
   there for a minute. maybe someday when i'm a golang master i'll be
   smart enough to be comfortable with one-char varnames. :)
This commit is contained in:
Paul Hinze 2015-01-28 15:38:02 -06:00
parent d3814d6180
commit 9fff0b1729
2 changed files with 20 additions and 19 deletions

View File

@ -33,10 +33,6 @@ func testAccPreCheck(t *testing.T) {
t.Fatal("GOOGLE_ACCOUNT_FILE must be set for acceptance tests") t.Fatal("GOOGLE_ACCOUNT_FILE must be set for acceptance tests")
} }
if v := os.Getenv("GOOGLE_CLIENT_FILE"); v == "" {
t.Fatal("GOOGLE_CLIENT_FILE must be set for acceptance tests")
}
if v := os.Getenv("GOOGLE_PROJECT"); v == "" { if v := os.Getenv("GOOGLE_PROJECT"); v == "" {
t.Fatal("GOOGLE_PROJECT must be set for acceptance tests") t.Fatal("GOOGLE_PROJECT must be set for acceptance tests")
} }

View File

@ -137,11 +137,11 @@ func resourceComputeInstance() *schema.Resource {
}, },
"scopes": &schema.Schema{ "scopes": &schema.Schema{
Type: schema.TypeList, Type: schema.TypeList,
Required: true, Required: true,
ForceNew: true, ForceNew: true,
Elem: &schema.Schema{ Elem: &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
StateFunc: func(v interface{}) string { StateFunc: func(v interface{}) string {
return canonicalizeServiceScope(v.(string)) return canonicalizeServiceScope(v.(string))
}, },
@ -294,11 +294,11 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
scopesCount := d.Get(prefix + ".scopes.#").(int) scopesCount := d.Get(prefix + ".scopes.#").(int)
scopes := make([]string, 0, scopesCount) scopes := make([]string, 0, scopesCount)
for j := 0; j < scopesCount; j++ { for j := 0; j < scopesCount; j++ {
scope := d.Get(fmt.Sprintf(prefix + ".scopes.%d", j)).(string) scope := d.Get(fmt.Sprintf(prefix+".scopes.%d", j)).(string)
scopes = append(scopes, canonicalizeServiceScope(scope)) scopes = append(scopes, canonicalizeServiceScope(scope))
} }
serviceAccount := &compute.ServiceAccount { serviceAccount := &compute.ServiceAccount{
Email: "default", Email: "default",
Scopes: scopes, Scopes: scopes,
} }
@ -378,8 +378,8 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error
// Set the service accounts // Set the service accounts
for i, serviceAccount := range instance.ServiceAccounts { for i, serviceAccount := range instance.ServiceAccounts {
prefix := fmt.Sprintf("service_account.%d", i) prefix := fmt.Sprintf("service_account.%d", i)
d.Set(prefix + ".email", serviceAccount.Email) d.Set(prefix+".email", serviceAccount.Email)
d.Set(prefix + ".scopes.#", len(serviceAccount.Scopes)) d.Set(prefix+".scopes.#", len(serviceAccount.Scopes))
for j, scope := range serviceAccount.Scopes { for j, scope := range serviceAccount.Scopes {
d.Set(fmt.Sprintf("%s.scopes.%d", prefix, j), scope) d.Set(fmt.Sprintf("%s.scopes.%d", prefix, j), scope)
} }
@ -534,14 +534,19 @@ func resourceComputeInstanceDelete(d *schema.ResourceData, meta interface{}) err
func resourceInstanceMetadata(d *schema.ResourceData) *compute.Metadata { func resourceInstanceMetadata(d *schema.ResourceData) *compute.Metadata {
var metadata *compute.Metadata var metadata *compute.Metadata
if v := d.Get("metadata").([]interface{}); len(v) > 0 { if metadataList := d.Get("metadata").([]interface{}); len(metadataList) > 0 {
m := new(compute.Metadata) m := new(compute.Metadata)
m.Items = make([]*compute.MetadataItems, 0, len(v)) m.Items = make([]*compute.MetadataItems, 0, len(metadataList))
for _, v := range v { for _, metadataMap := range metadataList {
for k, v := range v.(map[string]interface{}) { for key, val := range metadataMap.(map[string]interface{}) {
// TODO: fix https://github.com/hashicorp/terraform/issues/883
// and remove this workaround <3 phinze
if key == "#" {
continue
}
m.Items = append(m.Items, &compute.MetadataItems{ m.Items = append(m.Items, &compute.MetadataItems{
Key: k, Key: key,
Value: v.(string), Value: val.(string),
}) })
} }
} }