2016-08-12 04:35:33 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"google.golang.org/api/compute/v1"
|
|
|
|
"google.golang.org/api/googleapi"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceComputeImage() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceComputeImageCreate,
|
|
|
|
Read: resourceComputeImageRead,
|
|
|
|
Delete: resourceComputeImageDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-11-01 11:32:47 +01:00
|
|
|
// TODO(cblecker): one of source_disk or raw_disk is required
|
|
|
|
|
2016-08-12 04:35:33 +02:00
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"family": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"project": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-11-01 11:32:47 +01:00
|
|
|
"source_disk": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-08-12 04:35:33 +02:00
|
|
|
"raw_disk": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
2016-11-01 11:32:47 +01:00
|
|
|
Optional: true,
|
2016-08-12 04:35:33 +02:00
|
|
|
ForceNew: true,
|
|
|
|
MaxItems: 1,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"source": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"sha1": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"container_type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Default: "TAR",
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"self_link": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2016-11-13 03:01:32 +01:00
|
|
|
|
|
|
|
"create_timeout": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Default: 4,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2016-08-12 04:35:33 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeImageCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the image
|
|
|
|
image := &compute.Image{
|
|
|
|
Name: d.Get("name").(string),
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("description"); ok {
|
|
|
|
image.Description = v.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("family"); ok {
|
|
|
|
image.Family = v.(string)
|
|
|
|
}
|
|
|
|
|
2016-11-01 11:32:47 +01:00
|
|
|
// Load up the source_disk for this image if specified
|
|
|
|
if v, ok := d.GetOk("source_disk"); ok {
|
|
|
|
image.SourceDisk = v.(string)
|
2016-08-12 04:35:33 +02:00
|
|
|
}
|
2016-11-01 11:32:47 +01:00
|
|
|
|
|
|
|
// Load up the raw_disk for this image if specified
|
|
|
|
if v, ok := d.GetOk("raw_disk"); ok {
|
|
|
|
rawDiskEle := v.([]interface{})[0].(map[string]interface{})
|
|
|
|
imageRawDisk := &compute.ImageRawDisk{
|
|
|
|
Source: rawDiskEle["source"].(string),
|
|
|
|
ContainerType: rawDiskEle["container_type"].(string),
|
|
|
|
}
|
|
|
|
if val, ok := rawDiskEle["sha1"]; ok {
|
|
|
|
imageRawDisk.Sha1Checksum = val.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
image.RawDisk = imageRawDisk
|
2016-08-12 04:35:33 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 03:01:32 +01:00
|
|
|
// Read create timeout
|
|
|
|
var createTimeout int
|
|
|
|
if v, ok := d.GetOk("create_timeout"); ok {
|
|
|
|
createTimeout = v.(int)
|
|
|
|
}
|
|
|
|
|
2016-08-12 04:35:33 +02:00
|
|
|
// Insert the image
|
|
|
|
op, err := config.clientCompute.Images.Insert(
|
|
|
|
project, image).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating image: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the ID
|
|
|
|
d.SetId(image.Name)
|
|
|
|
|
2016-11-13 03:01:32 +01:00
|
|
|
err = computeOperationWaitGlobalTime(config, op, project, "Creating Image", createTimeout)
|
2016-08-12 04:35:33 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceComputeImageRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeImageRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
image, err := config.clientCompute.Images.Get(
|
|
|
|
project, d.Id()).Do()
|
|
|
|
if err != nil {
|
|
|
|
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
|
|
|
|
// The resource doesn't exist anymore
|
|
|
|
log.Printf("[WARN] Removing Image %q because it's gone", d.Get("name").(string))
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Error reading image: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("self_link", image.SelfLink)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeImageDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the image
|
|
|
|
log.Printf("[DEBUG] image delete request")
|
|
|
|
op, err := config.clientCompute.Images.Delete(
|
|
|
|
project, d.Id()).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting image: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = computeOperationWaitGlobal(config, op, project, "Deleting image")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|