2018-09-07 11:34:59 +00:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/digitalocean/godo"
|
2019-10-22 21:44:03 +00:00
|
|
|
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
|
|
|
|
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
|
2018-09-07 11:34:59 +00:00
|
|
|
)
|
|
|
|
|
2018-09-07 15:49:28 +00:00
|
|
|
func resourceDigitalOceanVolumeSnapshot() *schema.Resource {
|
2018-09-07 11:34:59 +00:00
|
|
|
return &schema.Resource{
|
2018-09-07 15:49:28 +00:00
|
|
|
Create: resourceDigitalOceanVolumeSnapshotCreate,
|
|
|
|
Read: resourceDigitalOceanVolumeSnapshotRead,
|
|
|
|
Delete: resourceDigitalOceanVolumeSnapshotDelete,
|
2018-09-07 11:34:59 +00:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
ValidateFunc: validation.NoZeroValues,
|
|
|
|
},
|
|
|
|
|
|
|
|
"volume_id": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
ValidateFunc: validation.NoZeroValues,
|
|
|
|
},
|
|
|
|
|
|
|
|
"regions": {
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Computed: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
},
|
|
|
|
|
|
|
|
"size": {
|
|
|
|
Type: schema.TypeFloat,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"created_at": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"min_disk_size": {
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:49:28 +00:00
|
|
|
func resourceDigitalOceanVolumeSnapshotCreate(d *schema.ResourceData, meta interface{}) error {
|
2019-01-07 23:48:17 +00:00
|
|
|
client := meta.(*CombinedConfig).godoClient()
|
2018-09-07 11:34:59 +00:00
|
|
|
|
|
|
|
opts := &godo.SnapshotCreateRequest{
|
|
|
|
Name: d.Get("name").(string),
|
|
|
|
VolumeID: d.Get("volume_id").(string),
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:49:28 +00:00
|
|
|
log.Printf("[DEBUG] Volume Snapshot create configuration: %#v", opts)
|
2018-09-07 11:34:59 +00:00
|
|
|
snapshot, _, err := client.Storage.CreateSnapshot(context.Background(), opts)
|
|
|
|
if err != nil {
|
2018-09-07 15:49:28 +00:00
|
|
|
return fmt.Errorf("Error creating Volume Snapshot: %s", err)
|
2018-09-07 11:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(snapshot.ID)
|
2018-09-07 15:49:28 +00:00
|
|
|
log.Printf("[INFO] Volume Snapshot name: %s", snapshot.Name)
|
2018-09-07 11:34:59 +00:00
|
|
|
|
2018-09-07 15:49:28 +00:00
|
|
|
return resourceDigitalOceanVolumeSnapshotRead(d, meta)
|
2018-09-07 11:34:59 +00:00
|
|
|
}
|
|
|
|
|
2018-09-07 15:49:28 +00:00
|
|
|
func resourceDigitalOceanVolumeSnapshotRead(d *schema.ResourceData, meta interface{}) error {
|
2019-01-07 23:48:17 +00:00
|
|
|
client := meta.(*CombinedConfig).godoClient()
|
2018-09-07 11:34:59 +00:00
|
|
|
|
|
|
|
snapshot, resp, err := client.Snapshots.Get(context.Background(), d.Id())
|
|
|
|
if err != nil {
|
|
|
|
// If the snapshot is somehow already destroyed, mark as
|
|
|
|
// successfully gone
|
2019-07-23 16:11:05 +00:00
|
|
|
if resp != nil && resp.StatusCode == 404 {
|
2018-09-07 11:34:59 +00:00
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:49:28 +00:00
|
|
|
return fmt.Errorf("Error retrieving volume snapshot: %s", err)
|
2018-09-07 11:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("name", snapshot.Name)
|
|
|
|
d.Set("volume_id", snapshot.ResourceID)
|
|
|
|
d.Set("regions", snapshot.Regions)
|
|
|
|
d.Set("size", snapshot.SizeGigaBytes)
|
|
|
|
d.Set("created_at", snapshot.Created)
|
|
|
|
d.Set("min_disk_size", snapshot.MinDiskSize)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:49:28 +00:00
|
|
|
func resourceDigitalOceanVolumeSnapshotDelete(d *schema.ResourceData, meta interface{}) error {
|
2019-01-07 23:48:17 +00:00
|
|
|
client := meta.(*CombinedConfig).godoClient()
|
2018-09-07 11:34:59 +00:00
|
|
|
|
|
|
|
log.Printf("[INFO] Deleting snaphot: %s", d.Id())
|
|
|
|
_, err := client.Snapshots.Delete(context.Background(), d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting snapshot: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|