mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-09-06 17:16:44 +00:00
![Igor Wiedler](/assets/img/avatar_default.png)
Creating most google cloud resources uses the compute_operation to wait for the creation to complete. However, the computeOperationWait* functions always uses the global `config.Project`, instead of the resource- specific one. This means that creating resource in a project other than the main one fails with a 404 on the operation resource. This patch uses the project from google_compute_instance instead of the global one.
204 lines
5.2 KiB
Go
204 lines
5.2 KiB
Go
package google
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
"google.golang.org/api/compute/v1"
|
|
"google.golang.org/api/googleapi"
|
|
)
|
|
|
|
func resourceComputeProjectMetadata() *schema.Resource {
|
|
return &schema.Resource{
|
|
Create: resourceComputeProjectMetadataCreate,
|
|
Read: resourceComputeProjectMetadataRead,
|
|
Update: resourceComputeProjectMetadataUpdate,
|
|
Delete: resourceComputeProjectMetadataDelete,
|
|
|
|
SchemaVersion: 0,
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
"metadata": &schema.Schema{
|
|
Elem: schema.TypeString,
|
|
Type: schema.TypeMap,
|
|
Required: true,
|
|
},
|
|
|
|
"project": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Optional: true,
|
|
ForceNew: true,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func resourceComputeProjectMetadataCreate(d *schema.ResourceData, meta interface{}) error {
|
|
config := meta.(*Config)
|
|
|
|
projectID, err := getProject(d, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
createMD := func() error {
|
|
// Load project service
|
|
log.Printf("[DEBUG] Loading project service: %s", projectID)
|
|
project, err := config.clientCompute.Projects.Get(projectID).Do()
|
|
if err != nil {
|
|
return fmt.Errorf("Error loading project '%s': %s", projectID, err)
|
|
}
|
|
|
|
md := project.CommonInstanceMetadata
|
|
|
|
newMDMap := d.Get("metadata").(map[string]interface{})
|
|
// Ensure that we aren't overwriting entries that already exist
|
|
for _, kv := range md.Items {
|
|
if _, ok := newMDMap[kv.Key]; ok {
|
|
return fmt.Errorf("Error, key '%s' already exists in project '%s'", kv.Key, projectID)
|
|
}
|
|
}
|
|
|
|
// Append new metadata to existing metadata
|
|
for key, val := range newMDMap {
|
|
v := val.(string)
|
|
md.Items = append(md.Items, &compute.MetadataItems{
|
|
Key: key,
|
|
Value: &v,
|
|
})
|
|
}
|
|
|
|
op, err := config.clientCompute.Projects.SetCommonInstanceMetadata(projectID, md).Do()
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("SetCommonInstanceMetadata failed: %s", err)
|
|
}
|
|
|
|
log.Printf("[DEBUG] SetCommonMetadata: %d (%s)", op.Id, op.SelfLink)
|
|
|
|
return computeOperationWaitGlobal(config, op, project.Name, "SetCommonMetadata")
|
|
}
|
|
|
|
err = MetadataRetryWrapper(createMD)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return resourceComputeProjectMetadataRead(d, meta)
|
|
}
|
|
|
|
func resourceComputeProjectMetadataRead(d *schema.ResourceData, meta interface{}) error {
|
|
config := meta.(*Config)
|
|
|
|
projectID, err := getProject(d, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Load project service
|
|
log.Printf("[DEBUG] Loading project service: %s", projectID)
|
|
project, err := config.clientCompute.Projects.Get(projectID).Do()
|
|
if err != nil {
|
|
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
|
|
log.Printf("[WARN] Removing Project Metadata because it's gone")
|
|
// The resource doesn't exist anymore
|
|
d.SetId("")
|
|
|
|
return nil
|
|
}
|
|
|
|
return fmt.Errorf("Error loading project '%s': %s", projectID, err)
|
|
}
|
|
|
|
md := project.CommonInstanceMetadata
|
|
|
|
if err = d.Set("metadata", MetadataFormatSchema(d.Get("metadata").(map[string]interface{}), md)); err != nil {
|
|
return fmt.Errorf("Error setting metadata: %s", err)
|
|
}
|
|
|
|
d.SetId("common_metadata")
|
|
|
|
return nil
|
|
}
|
|
|
|
func resourceComputeProjectMetadataUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
config := meta.(*Config)
|
|
|
|
projectID, err := getProject(d, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if d.HasChange("metadata") {
|
|
o, n := d.GetChange("metadata")
|
|
|
|
updateMD := func() error {
|
|
// Load project service
|
|
log.Printf("[DEBUG] Loading project service: %s", projectID)
|
|
project, err := config.clientCompute.Projects.Get(projectID).Do()
|
|
if err != nil {
|
|
return fmt.Errorf("Error loading project '%s': %s", projectID, err)
|
|
}
|
|
|
|
md := project.CommonInstanceMetadata
|
|
|
|
MetadataUpdate(o.(map[string]interface{}), n.(map[string]interface{}), md)
|
|
|
|
op, err := config.clientCompute.Projects.SetCommonInstanceMetadata(projectID, md).Do()
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("SetCommonInstanceMetadata failed: %s", err)
|
|
}
|
|
|
|
log.Printf("[DEBUG] SetCommonMetadata: %d (%s)", op.Id, op.SelfLink)
|
|
|
|
// Optimistic locking requires the fingerprint received to match
|
|
// the fingerprint we send the server, if there is a mismatch then we
|
|
// are working on old data, and must retry
|
|
return computeOperationWaitGlobal(config, op, project.Name, "SetCommonMetadata")
|
|
}
|
|
|
|
err := MetadataRetryWrapper(updateMD)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return resourceComputeProjectMetadataRead(d, meta)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func resourceComputeProjectMetadataDelete(d *schema.ResourceData, meta interface{}) error {
|
|
config := meta.(*Config)
|
|
|
|
projectID, err := getProject(d, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Load project service
|
|
log.Printf("[DEBUG] Loading project service: %s", projectID)
|
|
project, err := config.clientCompute.Projects.Get(projectID).Do()
|
|
if err != nil {
|
|
return fmt.Errorf("Error loading project '%s': %s", projectID, err)
|
|
}
|
|
|
|
md := project.CommonInstanceMetadata
|
|
|
|
// Remove all items
|
|
md.Items = nil
|
|
|
|
op, err := config.clientCompute.Projects.SetCommonInstanceMetadata(projectID, md).Do()
|
|
|
|
log.Printf("[DEBUG] SetCommonMetadata: %d (%s)", op.Id, op.SelfLink)
|
|
|
|
err = computeOperationWaitGlobal(config, op, project.Name, "SetCommonMetadata")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return resourceComputeProjectMetadataRead(d, meta)
|
|
}
|