2015-08-20 21:18:41 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2015-08-31 16:06:25 +02:00
|
|
|
// "github.com/hashicorp/terraform/helper/hashcode"
|
2015-08-20 21:18:41 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"google.golang.org/api/compute/v1"
|
2015-08-31 16:06:25 +02:00
|
|
|
// "google.golang.org/api/googleapi"
|
2015-08-20 21:18:41 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func resourceComputeProjectMetadata() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceComputeProjectMetadataCreate,
|
2015-08-31 16:06:25 +02:00
|
|
|
Read: resourceComputeProjectMetadataRead,
|
2015-08-20 21:18:41 +02:00
|
|
|
Update: resourceComputeProjectMetadataUpdate,
|
|
|
|
Delete: resourceComputeProjectMetadataDelete,
|
|
|
|
|
|
|
|
SchemaVersion: 0,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2015-08-31 16:06:25 +02:00
|
|
|
"metadata": &schema.Schema{
|
|
|
|
Elem: schema.TypeString,
|
|
|
|
Type: schema.TypeMap,
|
2015-08-20 21:18:41 +02:00
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const FINGERPRINT_RETRIES = 10
|
|
|
|
const FINGERPRINT_FAIL = "Invalid fingerprint."
|
|
|
|
|
|
|
|
func resourceOperationWaitGlobal(config *Config, op *compute.Operation, activity string) error {
|
|
|
|
w := &OperationWaiter{
|
|
|
|
Service: config.clientCompute,
|
|
|
|
Op: op,
|
|
|
|
Project: config.Project,
|
|
|
|
Type: OperationWaitGlobal,
|
|
|
|
}
|
|
|
|
|
|
|
|
state := w.Conf()
|
|
|
|
state.Timeout = 2 * time.Minute
|
|
|
|
state.MinTimeout = 1 * time.Second
|
|
|
|
opRaw, err := state.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error waiting for %s: %s", activity, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
op = opRaw.(*compute.Operation)
|
|
|
|
if op.Error != nil {
|
|
|
|
return OperationError(*op.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeProjectMetadataCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
attempt := 0
|
|
|
|
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
|
|
|
for attempt < FINGERPRINT_RETRIES {
|
|
|
|
// Load project service
|
|
|
|
log.Printf("[DEBUG] Loading project service: %s", config.Project)
|
|
|
|
project, err := config.clientCompute.Projects.Get(config.Project).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error loading project '%s': %s", config.Project, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
md := project.CommonInstanceMetadata
|
|
|
|
|
|
|
|
newMDMap := d.Get("metadata").(map[string]interface{})
|
|
|
|
// Ensure that we aren't overwriting entries that already exist
|
2015-08-31 16:06:25 +02:00
|
|
|
for _, kv := range md.Items {
|
2015-08-20 21:18:41 +02:00
|
|
|
if _, ok := newMDMap[kv.Key]; ok {
|
|
|
|
return fmt.Errorf("Error, key '%s' already exists in project '%s'", kv.Key, config.Project)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append new metadata to existing metadata
|
2015-08-31 16:06:25 +02:00
|
|
|
for key, val := range newMDMap {
|
|
|
|
v := val.(string)
|
|
|
|
md.Items = append(md.Items, &compute.MetadataItems{
|
2015-08-20 21:18:41 +02:00
|
|
|
Key: key,
|
2015-08-31 16:06:25 +02:00
|
|
|
Value: &v,
|
2015-08-20 21:18:41 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
op, err := config.clientCompute.Projects.SetCommonInstanceMetadata(config.Project, md).Do()
|
|
|
|
|
|
|
|
if err != nil {
|
2015-08-31 16:06:25 +02:00
|
|
|
return fmt.Errorf("SetCommonInstanceMetadata failed: %s", err)
|
2015-08-20 21:18:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] SetCommonMetadata: %d (%s)", op.Id, op.SelfLink)
|
|
|
|
|
|
|
|
// Optimistic locking requires the fingerprint recieved to match
|
|
|
|
// the fingerprint we send the server, if there is a mismatch then we
|
|
|
|
// are working on old data, and must retry
|
|
|
|
err = resourceOperationWaitGlobal(config, op, "SetCommonMetadata")
|
|
|
|
if err == nil {
|
|
|
|
return resourceComputeProjectMetadataRead(d, meta)
|
|
|
|
} else if err.Error() == FINGERPRINT_FAIL {
|
|
|
|
attempt++
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Error, unable to set metadata resource after %d attempts", attempt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeProjectMetadataRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
|
|
|
// Load project service
|
|
|
|
log.Printf("[DEBUG] Loading project service: %s", config.Project)
|
|
|
|
project, err := config.clientCompute.Projects.Get(config.Project).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error loading project '%s': %s", config.Project, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
md := project.CommonInstanceMetadata
|
|
|
|
|
|
|
|
newMD := make(map[string]interface{})
|
|
|
|
|
2015-08-31 16:06:25 +02:00
|
|
|
for _, kv := range md.Items {
|
2015-08-31 20:43:45 +02:00
|
|
|
newMD[kv.Key] = *kv.Value
|
2015-08-20 21:18:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = d.Set("metadata", newMD); err != nil {
|
2015-08-31 16:06:25 +02:00
|
|
|
return fmt.Errorf("Error setting metadata: %s", err)
|
2015-08-20 21:18:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("common_metadata")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeProjectMetadataUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
attempt := 0
|
|
|
|
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
|
|
|
if d.HasChange("metadata") {
|
|
|
|
o, n := d.GetChange("metadata")
|
|
|
|
oMDMap, nMDMap := o.(map[string]interface{}), n.(map[string]interface{})
|
|
|
|
|
|
|
|
for attempt < FINGERPRINT_RETRIES {
|
|
|
|
// Load project service
|
|
|
|
log.Printf("[DEBUG] Loading project service: %s", config.Project)
|
|
|
|
project, err := config.clientCompute.Projects.Get(config.Project).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error loading project '%s': %s", config.Project, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
md := project.CommonInstanceMetadata
|
|
|
|
|
|
|
|
curMDMap := make(map[string]string)
|
|
|
|
// Load metadata on server into map
|
2015-08-31 16:06:25 +02:00
|
|
|
for _, kv := range md.Items {
|
|
|
|
// If the server state has a key that we had in our old
|
2015-08-20 21:18:41 +02:00
|
|
|
// state, but not in our new state, we should delete it
|
|
|
|
_, okOld := oMDMap[kv.Key]
|
|
|
|
_, okNew := nMDMap[kv.Key]
|
|
|
|
if okOld && !okNew {
|
|
|
|
continue
|
|
|
|
} else {
|
2015-08-31 16:06:25 +02:00
|
|
|
if kv.Value != nil {
|
|
|
|
curMDMap[kv.Key] = *kv.Value
|
|
|
|
}
|
2015-08-20 21:18:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert new metadata into existing metadata (overwriting when needed)
|
2015-08-31 16:06:25 +02:00
|
|
|
for key, val := range nMDMap {
|
2015-08-20 21:18:41 +02:00
|
|
|
curMDMap[key] = val.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reformat old metadata into a list
|
|
|
|
md.Items = nil
|
2015-08-31 16:06:25 +02:00
|
|
|
for key, val := range curMDMap {
|
|
|
|
md.Items = append(md.Items, &compute.MetadataItems{
|
2015-08-20 21:18:41 +02:00
|
|
|
Key: key,
|
2015-08-31 16:06:25 +02:00
|
|
|
Value: &val,
|
2015-08-20 21:18:41 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
op, err := config.clientCompute.Projects.SetCommonInstanceMetadata(config.Project, md).Do()
|
|
|
|
|
|
|
|
if err != nil {
|
2015-08-31 16:06:25 +02:00
|
|
|
return fmt.Errorf("SetCommonInstanceMetadata failed: %s", err)
|
2015-08-20 21:18:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] SetCommonMetadata: %d (%s)", op.Id, op.SelfLink)
|
|
|
|
|
|
|
|
// Optimistic locking requires the fingerprint recieved to match
|
|
|
|
// the fingerprint we send the server, if there is a mismatch then we
|
|
|
|
// are working on old data, and must retry
|
|
|
|
err = resourceOperationWaitGlobal(config, op, "SetCommonMetadata")
|
|
|
|
if err == nil {
|
|
|
|
return resourceComputeProjectMetadataRead(d, meta)
|
|
|
|
} else if err.Error() == FINGERPRINT_FAIL {
|
|
|
|
attempt++
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Error, unable to set metadata resource after %d attempts", attempt)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeProjectMetadataDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
|
|
|
// Load project service
|
|
|
|
log.Printf("[DEBUG] Loading project service: %s", config.Project)
|
|
|
|
project, err := config.clientCompute.Projects.Get(config.Project).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error loading project '%s': %s", config.Project, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
md := project.CommonInstanceMetadata
|
|
|
|
|
|
|
|
// Remove all items
|
|
|
|
md.Items = nil
|
|
|
|
|
|
|
|
op, err := config.clientCompute.Projects.SetCommonInstanceMetadata(config.Project, md).Do()
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] SetCommonMetadata: %d (%s)", op.Id, op.SelfLink)
|
|
|
|
|
|
|
|
err = resourceOperationWaitGlobal(config, op, "SetCommonMetadata")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceComputeProjectMetadataRead(d, meta)
|
|
|
|
}
|