github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/google/resource_compute_disk.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  	"google.golang.org/api/compute/v1"
     9  	"google.golang.org/api/googleapi"
    10  )
    11  
    12  func resourceComputeDisk() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceComputeDiskCreate,
    15  		Read:   resourceComputeDiskRead,
    16  		Delete: resourceComputeDiskDelete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"name": &schema.Schema{
    20  				Type:     schema.TypeString,
    21  				Required: true,
    22  				ForceNew: true,
    23  			},
    24  
    25  			"zone": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Required: true,
    28  				ForceNew: true,
    29  			},
    30  
    31  			"image": &schema.Schema{
    32  				Type:     schema.TypeString,
    33  				Optional: true,
    34  				ForceNew: true,
    35  			},
    36  
    37  			"size": &schema.Schema{
    38  				Type:     schema.TypeInt,
    39  				Optional: true,
    40  				ForceNew: true,
    41  			},
    42  
    43  			"type": &schema.Schema{
    44  				Type:     schema.TypeString,
    45  				Optional: true,
    46  				ForceNew: true,
    47  			},
    48  
    49  			"snapshot": &schema.Schema{
    50  				Type:     schema.TypeString,
    51  				Optional: true,
    52  				ForceNew: true,
    53  			},
    54  
    55  			"self_link": &schema.Schema{
    56  				Type:     schema.TypeString,
    57  				Computed: true,
    58  			},
    59  		},
    60  	}
    61  }
    62  
    63  func resourceComputeDiskCreate(d *schema.ResourceData, meta interface{}) error {
    64  	config := meta.(*Config)
    65  
    66  	// Get the zone
    67  	log.Printf("[DEBUG] Loading zone: %s", d.Get("zone").(string))
    68  	zone, err := config.clientCompute.Zones.Get(
    69  		config.Project, d.Get("zone").(string)).Do()
    70  	if err != nil {
    71  		return fmt.Errorf(
    72  			"Error loading zone '%s': %s", d.Get("zone").(string), err)
    73  	}
    74  
    75  	// Build the disk parameter
    76  	disk := &compute.Disk{
    77  		Name:   d.Get("name").(string),
    78  		SizeGb: int64(d.Get("size").(int)),
    79  	}
    80  
    81  	// If we were given a source image, load that.
    82  	if v, ok := d.GetOk("image"); ok {
    83  		log.Printf("[DEBUG] Resolving image name: %s", v.(string))
    84  		imageUrl, err := resolveImage(config, v.(string))
    85  		if err != nil {
    86  			return fmt.Errorf(
    87  				"Error resolving image name '%s': %s",
    88  				v.(string), err)
    89  		}
    90  
    91  		disk.SourceImage = imageUrl
    92  	}
    93  
    94  	if v, ok := d.GetOk("type"); ok {
    95  		log.Printf("[DEBUG] Loading disk type: %s", v.(string))
    96  		diskType, err := readDiskType(config, zone, v.(string))
    97  		if err != nil {
    98  			return fmt.Errorf(
    99  				"Error loading disk type '%s': %s",
   100  				v.(string), err)
   101  		}
   102  
   103  		disk.Type = diskType.SelfLink
   104  	}
   105  
   106  	if v, ok := d.GetOk("snapshot"); ok {
   107  		snapshotName := v.(string)
   108  		log.Printf("[DEBUG] Loading snapshot: %s", snapshotName)
   109  		snapshotData, err := config.clientCompute.Snapshots.Get(
   110  			config.Project, snapshotName).Do()
   111  
   112  		if err != nil {
   113  			return fmt.Errorf(
   114  				"Error loading snapshot '%s': %s",
   115  				snapshotName, err)
   116  		}
   117  
   118  		disk.SourceSnapshot = snapshotData.SelfLink
   119  	}
   120  
   121  	op, err := config.clientCompute.Disks.Insert(
   122  		config.Project, d.Get("zone").(string), disk).Do()
   123  	if err != nil {
   124  		return fmt.Errorf("Error creating disk: %s", err)
   125  	}
   126  
   127  	// It probably maybe worked, so store the ID now
   128  	d.SetId(disk.Name)
   129  
   130  	err = computeOperationWaitZone(config, op, d.Get("zone").(string), "Creating Disk")
   131  	if err != nil {
   132  		return err
   133  	}
   134  	return resourceComputeDiskRead(d, meta)
   135  }
   136  
   137  func resourceComputeDiskRead(d *schema.ResourceData, meta interface{}) error {
   138  	config := meta.(*Config)
   139  
   140  	disk, err := config.clientCompute.Disks.Get(
   141  		config.Project, d.Get("zone").(string), d.Id()).Do()
   142  	if err != nil {
   143  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
   144  			// The resource doesn't exist anymore
   145  			d.SetId("")
   146  
   147  			return nil
   148  		}
   149  
   150  		return fmt.Errorf("Error reading disk: %s", err)
   151  	}
   152  
   153  	d.Set("self_link", disk.SelfLink)
   154  
   155  	return nil
   156  }
   157  
   158  func resourceComputeDiskDelete(d *schema.ResourceData, meta interface{}) error {
   159  	config := meta.(*Config)
   160  
   161  	// Delete the disk
   162  	op, err := config.clientCompute.Disks.Delete(
   163  		config.Project, d.Get("zone").(string), d.Id()).Do()
   164  	if err != nil {
   165  		return fmt.Errorf("Error deleting disk: %s", err)
   166  	}
   167  
   168  	zone := d.Get("zone").(string)
   169  	err = computeOperationWaitZone(config, op, zone, "Creating Disk")
   170  	if err != nil {
   171  		return err
   172  	}
   173  
   174  	d.SetId("")
   175  	return nil
   176  }