github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/google/resource_compute_snapshot.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 resourceComputeSnapshot() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceComputeSnapshotCreate,
    15  		Read:   resourceComputeSnapshotRead,
    16  		Delete: resourceComputeSnapshotDelete,
    17  		Exists: resourceComputeSnapshotExists,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"name": &schema.Schema{
    21  				Type:     schema.TypeString,
    22  				Required: true,
    23  				ForceNew: true,
    24  			},
    25  
    26  			"zone": &schema.Schema{
    27  				Type:     schema.TypeString,
    28  				Required: true,
    29  				ForceNew: true,
    30  			},
    31  
    32  			"snapshot_encryption_key_raw": &schema.Schema{
    33  				Type:      schema.TypeString,
    34  				Optional:  true,
    35  				ForceNew:  true,
    36  				Sensitive: true,
    37  			},
    38  
    39  			"snapshot_encryption_key_sha256": &schema.Schema{
    40  				Type:     schema.TypeString,
    41  				Computed: true,
    42  			},
    43  
    44  			"source_disk_encryption_key_raw": &schema.Schema{
    45  				Type:      schema.TypeString,
    46  				Optional:  true,
    47  				ForceNew:  true,
    48  				Sensitive: true,
    49  			},
    50  
    51  			"source_disk_encryption_key_sha256": &schema.Schema{
    52  				Type:     schema.TypeString,
    53  				Computed: true,
    54  			},
    55  
    56  			"source_disk": &schema.Schema{
    57  				Type:     schema.TypeString,
    58  				Required: true,
    59  				ForceNew: true,
    60  			},
    61  
    62  			"source_disk_link": &schema.Schema{
    63  				Type:     schema.TypeString,
    64  				Computed: true,
    65  			},
    66  
    67  			"project": &schema.Schema{
    68  				Type:     schema.TypeString,
    69  				Optional: true,
    70  				ForceNew: true,
    71  			},
    72  
    73  			"self_link": &schema.Schema{
    74  				Type:     schema.TypeString,
    75  				Computed: true,
    76  			},
    77  		},
    78  	}
    79  }
    80  
    81  func resourceComputeSnapshotCreate(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  	// Build the snapshot parameter
    90  	snapshot := &compute.Snapshot{
    91  		Name: d.Get("name").(string),
    92  	}
    93  
    94  	source_disk := d.Get("source_disk").(string)
    95  
    96  	if v, ok := d.GetOk("snapshot_encryption_key_raw"); ok {
    97  		snapshot.SnapshotEncryptionKey = &compute.CustomerEncryptionKey{}
    98  		snapshot.SnapshotEncryptionKey.RawKey = v.(string)
    99  	}
   100  
   101  	if v, ok := d.GetOk("source_disk_encryption_key_raw"); ok {
   102  		snapshot.SourceDiskEncryptionKey = &compute.CustomerEncryptionKey{}
   103  		snapshot.SourceDiskEncryptionKey.RawKey = v.(string)
   104  	}
   105  
   106  	op, err := config.clientCompute.Disks.CreateSnapshot(
   107  		project, d.Get("zone").(string), source_disk, snapshot).Do()
   108  	if err != nil {
   109  		return fmt.Errorf("Error creating snapshot: %s", err)
   110  	}
   111  
   112  	// It probably maybe worked, so store the ID now
   113  	d.SetId(snapshot.Name)
   114  
   115  	err = computeOperationWaitZone(config, op, project, d.Get("zone").(string), "Creating Snapshot")
   116  	if err != nil {
   117  		return err
   118  	}
   119  	return resourceComputeSnapshotRead(d, meta)
   120  }
   121  
   122  func resourceComputeSnapshotRead(d *schema.ResourceData, meta interface{}) error {
   123  	config := meta.(*Config)
   124  
   125  	project, err := getProject(d, config)
   126  	if err != nil {
   127  		return err
   128  	}
   129  
   130  	snapshot, err := config.clientCompute.Snapshots.Get(
   131  		project, d.Id()).Do()
   132  	if err != nil {
   133  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
   134  			log.Printf("[WARN] Removing Snapshot %q because it's gone", d.Get("name").(string))
   135  			// The resource doesn't exist anymore
   136  			d.SetId("")
   137  
   138  			return nil
   139  		}
   140  
   141  		return fmt.Errorf("Error reading snapshot: %s", err)
   142  	}
   143  
   144  	d.Set("self_link", snapshot.SelfLink)
   145  	d.Set("source_disk_link", snapshot.SourceDisk)
   146  	d.Set("name", snapshot.Name)
   147  
   148  	if snapshot.SnapshotEncryptionKey != nil && snapshot.SnapshotEncryptionKey.Sha256 != "" {
   149  		d.Set("snapshot_encryption_key_sha256", snapshot.SnapshotEncryptionKey.Sha256)
   150  	}
   151  
   152  	if snapshot.SourceDiskEncryptionKey != nil && snapshot.SourceDiskEncryptionKey.Sha256 != "" {
   153  		d.Set("source_disk_encryption_key_sha256", snapshot.SourceDiskEncryptionKey.Sha256)
   154  	}
   155  
   156  	return nil
   157  }
   158  
   159  func resourceComputeSnapshotDelete(d *schema.ResourceData, meta interface{}) error {
   160  	config := meta.(*Config)
   161  
   162  	project, err := getProject(d, config)
   163  	if err != nil {
   164  		return err
   165  	}
   166  
   167  	// Delete the snapshot
   168  	op, err := config.clientCompute.Snapshots.Delete(
   169  		project, d.Id()).Do()
   170  	if err != nil {
   171  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
   172  			log.Printf("[WARN] Removing Snapshot %q because it's gone", d.Get("name").(string))
   173  			// The resource doesn't exist anymore
   174  			d.SetId("")
   175  			return nil
   176  		}
   177  		return fmt.Errorf("Error deleting snapshot: %s", err)
   178  	}
   179  
   180  	err = computeOperationWaitGlobal(config, op, project, "Deleting Snapshot")
   181  	if err != nil {
   182  		return err
   183  	}
   184  
   185  	d.SetId("")
   186  	return nil
   187  }
   188  
   189  func resourceComputeSnapshotExists(d *schema.ResourceData, meta interface{}) (bool, error) {
   190  	config := meta.(*Config)
   191  
   192  	project, err := getProject(d, config)
   193  	if err != nil {
   194  		return false, err
   195  	}
   196  
   197  	_, err = config.clientCompute.Snapshots.Get(
   198  		project, d.Id()).Do()
   199  	if err != nil {
   200  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
   201  			log.Printf("[WARN] Removing Snapshot %q because it's gone", d.Get("name").(string))
   202  			// The resource doesn't exist anymore
   203  			d.SetId("")
   204  
   205  			return false, err
   206  		}
   207  		return true, err
   208  	}
   209  	return true, nil
   210  }