github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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  			"project": &schema.Schema{
    38  				Type:     schema.TypeString,
    39  				Optional: true,
    40  				ForceNew: true,
    41  			},
    42  
    43  			"size": &schema.Schema{
    44  				Type:     schema.TypeInt,
    45  				Optional: true,
    46  				ForceNew: true,
    47  			},
    48  
    49  			"self_link": &schema.Schema{
    50  				Type:     schema.TypeString,
    51  				Computed: true,
    52  			},
    53  
    54  			"snapshot": &schema.Schema{
    55  				Type:     schema.TypeString,
    56  				Optional: true,
    57  				ForceNew: true,
    58  			},
    59  
    60  			"type": &schema.Schema{
    61  				Type:     schema.TypeString,
    62  				Optional: true,
    63  				ForceNew: true,
    64  			},
    65  		},
    66  	}
    67  }
    68  
    69  func resourceComputeDiskCreate(d *schema.ResourceData, meta interface{}) error {
    70  	config := meta.(*Config)
    71  
    72  	project, err := getProject(d, config)
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	// Get the zone
    78  	log.Printf("[DEBUG] Loading zone: %s", d.Get("zone").(string))
    79  	zone, err := config.clientCompute.Zones.Get(
    80  		project, d.Get("zone").(string)).Do()
    81  	if err != nil {
    82  		return fmt.Errorf(
    83  			"Error loading zone '%s': %s", d.Get("zone").(string), err)
    84  	}
    85  
    86  	// Build the disk parameter
    87  	disk := &compute.Disk{
    88  		Name:   d.Get("name").(string),
    89  		SizeGb: int64(d.Get("size").(int)),
    90  	}
    91  
    92  	// If we were given a source image, load that.
    93  	if v, ok := d.GetOk("image"); ok {
    94  		log.Printf("[DEBUG] Resolving image name: %s", v.(string))
    95  		imageUrl, err := resolveImage(config, v.(string))
    96  		if err != nil {
    97  			return fmt.Errorf(
    98  				"Error resolving image name '%s': %s",
    99  				v.(string), err)
   100  		}
   101  
   102  		disk.SourceImage = imageUrl
   103  	}
   104  
   105  	if v, ok := d.GetOk("type"); ok {
   106  		log.Printf("[DEBUG] Loading disk type: %s", v.(string))
   107  		diskType, err := readDiskType(config, zone, v.(string))
   108  		if err != nil {
   109  			return fmt.Errorf(
   110  				"Error loading disk type '%s': %s",
   111  				v.(string), err)
   112  		}
   113  
   114  		disk.Type = diskType.SelfLink
   115  	}
   116  
   117  	if v, ok := d.GetOk("snapshot"); ok {
   118  		snapshotName := v.(string)
   119  		log.Printf("[DEBUG] Loading snapshot: %s", snapshotName)
   120  		snapshotData, err := config.clientCompute.Snapshots.Get(
   121  			project, snapshotName).Do()
   122  
   123  		if err != nil {
   124  			return fmt.Errorf(
   125  				"Error loading snapshot '%s': %s",
   126  				snapshotName, err)
   127  		}
   128  
   129  		disk.SourceSnapshot = snapshotData.SelfLink
   130  	}
   131  
   132  	op, err := config.clientCompute.Disks.Insert(
   133  		project, d.Get("zone").(string), disk).Do()
   134  	if err != nil {
   135  		return fmt.Errorf("Error creating disk: %s", err)
   136  	}
   137  
   138  	// It probably maybe worked, so store the ID now
   139  	d.SetId(disk.Name)
   140  
   141  	err = computeOperationWaitZone(config, op, project, d.Get("zone").(string), "Creating Disk")
   142  	if err != nil {
   143  		return err
   144  	}
   145  	return resourceComputeDiskRead(d, meta)
   146  }
   147  
   148  func resourceComputeDiskRead(d *schema.ResourceData, meta interface{}) error {
   149  	config := meta.(*Config)
   150  
   151  	project, err := getProject(d, config)
   152  	if err != nil {
   153  		return err
   154  	}
   155  
   156  	disk, err := config.clientCompute.Disks.Get(
   157  		project, d.Get("zone").(string), d.Id()).Do()
   158  	if err != nil {
   159  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
   160  			log.Printf("[WARN] Removing Disk %q because it's gone", d.Get("name").(string))
   161  			// The resource doesn't exist anymore
   162  			d.SetId("")
   163  
   164  			return nil
   165  		}
   166  
   167  		return fmt.Errorf("Error reading disk: %s", err)
   168  	}
   169  
   170  	d.Set("self_link", disk.SelfLink)
   171  
   172  	return nil
   173  }
   174  
   175  func resourceComputeDiskDelete(d *schema.ResourceData, meta interface{}) error {
   176  	config := meta.(*Config)
   177  
   178  	project, err := getProject(d, config)
   179  	if err != nil {
   180  		return err
   181  	}
   182  
   183  	// Delete the disk
   184  	op, err := config.clientCompute.Disks.Delete(
   185  		project, d.Get("zone").(string), d.Id()).Do()
   186  	if err != nil {
   187  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
   188  			log.Printf("[WARN] Removing Disk %q because it's gone", d.Get("name").(string))
   189  			// The resource doesn't exist anymore
   190  			d.SetId("")
   191  			return nil
   192  		}
   193  		return fmt.Errorf("Error deleting disk: %s", err)
   194  	}
   195  
   196  	zone := d.Get("zone").(string)
   197  	err = computeOperationWaitZone(config, op, project, zone, "Creating Disk")
   198  	if err != nil {
   199  		return err
   200  	}
   201  
   202  	d.SetId("")
   203  	return nil
   204  }