mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-09-07 09:36:44 +00:00
179 lines
3.9 KiB
Go
179 lines
3.9 KiB
Go
![]() |
package google
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"log"
|
||
|
|
||
|
"github.com/hashicorp/terraform/helper/schema"
|
||
|
"google.golang.org/api/compute/v1"
|
||
|
)
|
||
|
|
||
|
func resourceComputeProjectMetadataItem() *schema.Resource {
|
||
|
return &schema.Resource{
|
||
|
Create: resourceComputeProjectMetadataItemCreate,
|
||
|
Read: resourceComputeProjectMetadataItemRead,
|
||
|
Update: resourceComputeProjectMetadataItemUpdate,
|
||
|
Delete: resourceComputeProjectMetadataItemDelete,
|
||
|
Importer: &schema.ResourceImporter{
|
||
|
State: schema.ImportStatePassthrough,
|
||
|
},
|
||
|
|
||
|
Schema: map[string]*schema.Schema{
|
||
|
"key": {
|
||
|
Type: schema.TypeString,
|
||
|
Required: true,
|
||
|
ForceNew: true,
|
||
|
},
|
||
|
"value": {
|
||
|
Type: schema.TypeString,
|
||
|
Required: true,
|
||
|
},
|
||
|
"project": {
|
||
|
Type: schema.TypeString,
|
||
|
Optional: true,
|
||
|
ForceNew: true,
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func resourceComputeProjectMetadataItemCreate(d *schema.ResourceData, meta interface{}) error {
|
||
|
config := meta.(*Config)
|
||
|
|
||
|
projectID, err := getProject(d, config)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
key := d.Get("key").(string)
|
||
|
val := d.Get("value").(string)
|
||
|
|
||
|
err = updateComputeCommonInstanceMetadata(config, projectID, key, &val)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
d.SetId(key)
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func resourceComputeProjectMetadataItemRead(d *schema.ResourceData, meta interface{}) error {
|
||
|
config := meta.(*Config)
|
||
|
|
||
|
projectID, err := getProject(d, config)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
log.Printf("[DEBUG] Loading project metadata: %s", projectID)
|
||
|
project, err := config.clientCompute.Projects.Get(projectID).Do()
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("Error loading project '%s': %s", projectID, err)
|
||
|
}
|
||
|
|
||
|
md := flattenComputeMetadata(project.CommonInstanceMetadata.Items)
|
||
|
val, ok := md[d.Id()]
|
||
|
if !ok {
|
||
|
// Resource no longer exists
|
||
|
d.SetId("")
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
d.Set("key", d.Id())
|
||
|
d.Set("value", val)
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func resourceComputeProjectMetadataItemUpdate(d *schema.ResourceData, meta interface{}) error {
|
||
|
config := meta.(*Config)
|
||
|
|
||
|
projectID, err := getProject(d, config)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if d.HasChange("value") {
|
||
|
key := d.Get("key").(string)
|
||
|
_, n := d.GetChange("value")
|
||
|
new := n.(string)
|
||
|
|
||
|
err = updateComputeCommonInstanceMetadata(config, projectID, key, &new)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func resourceComputeProjectMetadataItemDelete(d *schema.ResourceData, meta interface{}) error {
|
||
|
config := meta.(*Config)
|
||
|
|
||
|
projectID, err := getProject(d, config)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
key := d.Get("key").(string)
|
||
|
|
||
|
err = updateComputeCommonInstanceMetadata(config, projectID, key, nil)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
d.SetId("")
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func updateComputeCommonInstanceMetadata(config *Config, projectID string, key string, afterVal *string) error {
|
||
|
updateMD := func() error {
|
||
|
log.Printf("[DEBUG] Loading project metadata: %s", projectID)
|
||
|
project, err := config.clientCompute.Projects.Get(projectID).Do()
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("Error loading project '%s': %s", projectID, err)
|
||
|
}
|
||
|
|
||
|
md := flattenComputeMetadata(project.CommonInstanceMetadata.Items)
|
||
|
|
||
|
val, ok := md[key]
|
||
|
|
||
|
if !ok {
|
||
|
if afterVal == nil {
|
||
|
// Asked to set no value and we didn't find one - we're done
|
||
|
return nil
|
||
|
}
|
||
|
} else {
|
||
|
if afterVal != nil && *afterVal == val {
|
||
|
// Asked to set a value and it's already set - we're done.
|
||
|
return nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if afterVal == nil {
|
||
|
delete(md, key)
|
||
|
} else {
|
||
|
md[key] = *afterVal
|
||
|
}
|
||
|
|
||
|
// Attempt to write the new value now
|
||
|
op, err := config.clientCompute.Projects.SetCommonInstanceMetadata(
|
||
|
projectID,
|
||
|
&compute.Metadata{
|
||
|
Fingerprint: project.CommonInstanceMetadata.Fingerprint,
|
||
|
Items: expandComputeMetadata(md),
|
||
|
},
|
||
|
).Do()
|
||
|
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
log.Printf("[DEBUG] SetCommonInstanceMetadata: %d (%s)", op.Id, op.SelfLink)
|
||
|
|
||
|
return computeOperationWait(config, op, project.Name, "SetCommonInstanceMetadata")
|
||
|
}
|
||
|
|
||
|
return MetadataRetryWrapper(updateMD)
|
||
|
}
|