2015-08-20 21:18:41 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeProjectMetadataCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2015-08-31 23:33:02 +02:00
|
|
|
createMD := func() error {
|
2015-08-20 21:18:41 +02:00
|
|
|
// 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)
|
|
|
|
|
2015-09-24 22:30:12 +02:00
|
|
|
return computeOperationWaitGlobal(config, op, "SetCommonMetadata")
|
2015-08-31 23:33:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err := MetadataRetryWrapper(createMD)
|
|
|
|
if err != nil {
|
2015-10-07 22:35:06 +02:00
|
|
|
return err
|
2015-08-20 21:18:41 +02:00
|
|
|
}
|
|
|
|
|
2015-10-07 22:35:06 +02:00
|
|
|
return resourceComputeProjectMetadataRead(d, meta)
|
2015-08-20 21:18:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2015-08-31 23:33:02 +02:00
|
|
|
if err = d.Set("metadata", MetadataFormatSchema(md)); 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 {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
|
|
|
if d.HasChange("metadata") {
|
|
|
|
o, n := d.GetChange("metadata")
|
|
|
|
|
2015-08-31 23:33:02 +02:00
|
|
|
updateMD := func() error {
|
2015-08-20 21:18:41 +02:00
|
|
|
// 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
|
|
|
|
|
2015-10-07 22:35:06 +02:00
|
|
|
MetadataUpdate(o.(map[string]interface{}), n.(map[string]interface{}), md)
|
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)
|
|
|
|
|
2015-09-11 20:56:20 +02:00
|
|
|
// Optimistic locking requires the fingerprint received to match
|
2015-08-20 21:18:41 +02:00
|
|
|
// the fingerprint we send the server, if there is a mismatch then we
|
|
|
|
// are working on old data, and must retry
|
2015-09-24 22:30:12 +02:00
|
|
|
return computeOperationWaitGlobal(config, op, "SetCommonMetadata")
|
2015-08-31 23:33:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err := MetadataRetryWrapper(updateMD)
|
|
|
|
if err != nil {
|
2015-10-07 22:35:06 +02:00
|
|
|
return err
|
2015-08-20 21:18:41 +02:00
|
|
|
}
|
|
|
|
|
2015-10-07 22:35:06 +02:00
|
|
|
return resourceComputeProjectMetadataRead(d, meta)
|
2015-08-20 21:18:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2015-09-24 22:30:12 +02:00
|
|
|
err = computeOperationWaitGlobal(config, op, "SetCommonMetadata")
|
2015-08-20 21:18:41 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceComputeProjectMetadataRead(d, meta)
|
|
|
|
}
|