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