2015-08-20 21:18:41 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"google.golang.org/api/compute/v1"
|
2015-11-13 21:36:03 +01: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,
|
|
|
|
},
|
2016-04-10 18:59:57 +02:00
|
|
|
|
|
|
|
"project": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2015-08-20 21:18:41 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeProjectMetadataCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
projectID, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-31 23:33:02 +02:00
|
|
|
createMD := func() error {
|
2015-08-20 21:18:41 +02:00
|
|
|
// Load project service
|
2016-04-10 18:59:57 +02:00
|
|
|
log.Printf("[DEBUG] Loading project service: %s", projectID)
|
|
|
|
project, err := config.clientCompute.Projects.Get(projectID).Do()
|
2015-08-20 21:18:41 +02:00
|
|
|
if err != nil {
|
2016-04-10 18:59:57 +02:00
|
|
|
return fmt.Errorf("Error loading project '%s': %s", projectID, err)
|
2015-08-20 21:18:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2016-04-10 18:59:57 +02:00
|
|
|
return fmt.Errorf("Error, key '%s' already exists in project '%s'", kv.Key, projectID)
|
2015-08-20 21:18:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
op, err := config.clientCompute.Projects.SetCommonInstanceMetadata(projectID, md).Do()
|
2015-08-20 21:18:41 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2016-06-06 19:35:13 +02:00
|
|
|
return computeOperationWaitGlobal(config, op, project.Name, "SetCommonMetadata")
|
2015-08-31 23:33:02 +02:00
|
|
|
}
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
err = MetadataRetryWrapper(createMD)
|
2015-08-31 23:33:02 +02:00
|
|
|
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)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
projectID, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-20 21:18:41 +02:00
|
|
|
// Load project service
|
2016-04-10 18:59:57 +02:00
|
|
|
log.Printf("[DEBUG] Loading project service: %s", projectID)
|
|
|
|
project, err := config.clientCompute.Projects.Get(projectID).Do()
|
2015-08-20 21:18:41 +02:00
|
|
|
if err != nil {
|
2015-11-13 21:36:03 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
return fmt.Errorf("Error loading project '%s': %s", projectID, err)
|
2015-08-20 21:18:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
md := project.CommonInstanceMetadata
|
|
|
|
|
2016-01-05 17:37:52 +01:00
|
|
|
if err = d.Set("metadata", MetadataFormatSchema(d.Get("metadata").(map[string]interface{}), 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)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
projectID, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-20 21:18:41 +02:00
|
|
|
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
|
2016-04-10 18:59:57 +02:00
|
|
|
log.Printf("[DEBUG] Loading project service: %s", projectID)
|
|
|
|
project, err := config.clientCompute.Projects.Get(projectID).Do()
|
2015-08-20 21:18:41 +02:00
|
|
|
if err != nil {
|
2016-04-10 18:59:57 +02:00
|
|
|
return fmt.Errorf("Error loading project '%s': %s", projectID, err)
|
2015-08-20 21:18:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
op, err := config.clientCompute.Projects.SetCommonInstanceMetadata(projectID, md).Do()
|
2015-08-20 21:18:41 +02:00
|
|
|
|
|
|
|
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
|
2016-06-06 19:35:13 +02:00
|
|
|
return computeOperationWaitGlobal(config, op, project.Name, "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)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
projectID, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-20 21:18:41 +02:00
|
|
|
// Load project service
|
2016-04-10 18:59:57 +02:00
|
|
|
log.Printf("[DEBUG] Loading project service: %s", projectID)
|
|
|
|
project, err := config.clientCompute.Projects.Get(projectID).Do()
|
2015-08-20 21:18:41 +02:00
|
|
|
if err != nil {
|
2016-04-10 18:59:57 +02:00
|
|
|
return fmt.Errorf("Error loading project '%s': %s", projectID, err)
|
2015-08-20 21:18:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
md := project.CommonInstanceMetadata
|
|
|
|
|
|
|
|
// Remove all items
|
|
|
|
md.Items = nil
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
op, err := config.clientCompute.Projects.SetCommonInstanceMetadata(projectID, md).Do()
|
2015-08-20 21:18:41 +02:00
|
|
|
|
2017-02-16 11:36:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error removing metadata from project %s: %s", projectID, err)
|
|
|
|
}
|
|
|
|
|
2015-08-20 21:18:41 +02:00
|
|
|
log.Printf("[DEBUG] SetCommonMetadata: %d (%s)", op.Id, op.SelfLink)
|
|
|
|
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitGlobal(config, op, project.Name, "SetCommonMetadata")
|
2015-08-20 21:18:41 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceComputeProjectMetadataRead(d, meta)
|
|
|
|
}
|