github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/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  		return handleNotFoundError(err, d, fmt.Sprintf("Snapshot %q", d.Get("name").(string)))
   134  	}
   135  
   136  	d.Set("self_link", snapshot.SelfLink)
   137  	d.Set("source_disk_link", snapshot.SourceDisk)
   138  	d.Set("name", snapshot.Name)
   139  
   140  	if snapshot.SnapshotEncryptionKey != nil && snapshot.SnapshotEncryptionKey.Sha256 != "" {
   141  		d.Set("snapshot_encryption_key_sha256", snapshot.SnapshotEncryptionKey.Sha256)
   142  	}
   143  
   144  	if snapshot.SourceDiskEncryptionKey != nil && snapshot.SourceDiskEncryptionKey.Sha256 != "" {
   145  		d.Set("source_disk_encryption_key_sha256", snapshot.SourceDiskEncryptionKey.Sha256)
   146  	}
   147  
   148  	return nil
   149  }
   150  
   151  func resourceComputeSnapshotDelete(d *schema.ResourceData, meta interface{}) error {
   152  	config := meta.(*Config)
   153  
   154  	project, err := getProject(d, config)
   155  	if err != nil {
   156  		return err
   157  	}
   158  
   159  	// Delete the snapshot
   160  	op, err := config.clientCompute.Snapshots.Delete(
   161  		project, d.Id()).Do()
   162  	if err != nil {
   163  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
   164  			log.Printf("[WARN] Removing Snapshot %q because it's gone", d.Get("name").(string))
   165  			// The resource doesn't exist anymore
   166  			d.SetId("")
   167  			return nil
   168  		}
   169  		return fmt.Errorf("Error deleting snapshot: %s", err)
   170  	}
   171  
   172  	err = computeOperationWaitGlobal(config, op, project, "Deleting Snapshot")
   173  	if err != nil {
   174  		return err
   175  	}
   176  
   177  	d.SetId("")
   178  	return nil
   179  }
   180  
   181  func resourceComputeSnapshotExists(d *schema.ResourceData, meta interface{}) (bool, error) {
   182  	config := meta.(*Config)
   183  
   184  	project, err := getProject(d, config)
   185  	if err != nil {
   186  		return false, err
   187  	}
   188  
   189  	_, err = config.clientCompute.Snapshots.Get(
   190  		project, d.Id()).Do()
   191  	if err != nil {
   192  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
   193  			log.Printf("[WARN] Removing Snapshot %q because it's gone", d.Get("name").(string))
   194  			// The resource doesn't exist anymore
   195  			d.SetId("")
   196  
   197  			return false, err
   198  		}
   199  		return true, err
   200  	}
   201  	return true, nil
   202  }