github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/digitalocean/resource_digitalocean_volume.go (about)

     1  package digitalocean
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/digitalocean/godo"
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  )
    10  
    11  func resourceDigitalOceanVolume() *schema.Resource {
    12  	return &schema.Resource{
    13  		Create: resourceDigitalOceanVolumeCreate,
    14  		Read:   resourceDigitalOceanVolumeRead,
    15  		Delete: resourceDigitalOceanVolumeDelete,
    16  		Importer: &schema.ResourceImporter{
    17  			State: resourceDigitalOceanVolumeImport,
    18  		},
    19  
    20  		Schema: map[string]*schema.Schema{
    21  			"region": &schema.Schema{
    22  				Type:     schema.TypeString,
    23  				Required: true,
    24  				ForceNew: true,
    25  			},
    26  
    27  			"id": &schema.Schema{
    28  				Type:     schema.TypeString,
    29  				Computed: true,
    30  			},
    31  
    32  			"name": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Required: true,
    35  				ForceNew: true,
    36  			},
    37  
    38  			"droplet_ids": &schema.Schema{
    39  				Type:     schema.TypeSet,
    40  				Elem:     &schema.Schema{Type: schema.TypeInt},
    41  				Computed: true,
    42  			},
    43  
    44  			"size": &schema.Schema{
    45  				Type:     schema.TypeInt,
    46  				Required: true,
    47  				ForceNew: true, // Update-ability Coming Soon ™
    48  			},
    49  
    50  			"description": &schema.Schema{
    51  				Type:     schema.TypeString,
    52  				Optional: true,
    53  				ForceNew: true, // Update-ability Coming Soon ™
    54  			},
    55  		},
    56  	}
    57  }
    58  
    59  func resourceDigitalOceanVolumeCreate(d *schema.ResourceData, meta interface{}) error {
    60  	client := meta.(*godo.Client)
    61  
    62  	opts := &godo.VolumeCreateRequest{
    63  		Region:        d.Get("region").(string),
    64  		Name:          d.Get("name").(string),
    65  		Description:   d.Get("description").(string),
    66  		SizeGigaBytes: int64(d.Get("size").(int)),
    67  	}
    68  
    69  	log.Printf("[DEBUG] Volume create configuration: %#v", opts)
    70  	volume, _, err := client.Storage.CreateVolume(opts)
    71  	if err != nil {
    72  		return fmt.Errorf("Error creating Volume: %s", err)
    73  	}
    74  
    75  	d.SetId(volume.ID)
    76  	log.Printf("[INFO] Volume name: %s", volume.Name)
    77  
    78  	return resourceDigitalOceanVolumeRead(d, meta)
    79  }
    80  
    81  func resourceDigitalOceanVolumeRead(d *schema.ResourceData, meta interface{}) error {
    82  	client := meta.(*godo.Client)
    83  
    84  	volume, resp, err := client.Storage.GetVolume(d.Id())
    85  	if err != nil {
    86  		// If the volume is somehow already destroyed, mark as
    87  		// successfully gone
    88  		if resp.StatusCode == 404 {
    89  			d.SetId("")
    90  			return nil
    91  		}
    92  
    93  		return fmt.Errorf("Error retrieving volume: %s", err)
    94  	}
    95  
    96  	d.Set("id", volume.ID)
    97  
    98  	dids := make([]interface{}, 0, len(volume.DropletIDs))
    99  	for _, did := range volume.DropletIDs {
   100  		dids = append(dids, did)
   101  	}
   102  	d.Set("droplet_ids", schema.NewSet(
   103  		func(dropletID interface{}) int { return dropletID.(int) },
   104  		dids,
   105  	))
   106  
   107  	return nil
   108  }
   109  
   110  func resourceDigitalOceanVolumeDelete(d *schema.ResourceData, meta interface{}) error {
   111  	client := meta.(*godo.Client)
   112  
   113  	log.Printf("[INFO] Deleting volume: %s", d.Id())
   114  	_, err := client.Storage.DeleteVolume(d.Id())
   115  	if err != nil {
   116  		return fmt.Errorf("Error deleting volume: %s", err)
   117  	}
   118  
   119  	d.SetId("")
   120  	return nil
   121  }
   122  
   123  func resourceDigitalOceanVolumeImport(rs *schema.ResourceData, v interface{}) ([]*schema.ResourceData, error) {
   124  	client := v.(*godo.Client)
   125  	volume, _, err := client.Storage.GetVolume(rs.Id())
   126  	if err != nil {
   127  		return nil, err
   128  	}
   129  
   130  	rs.Set("id", volume.ID)
   131  	rs.Set("name", volume.Name)
   132  	rs.Set("region", volume.Region.Slug)
   133  	rs.Set("description", volume.Description)
   134  	rs.Set("size", int(volume.SizeGigaBytes))
   135  
   136  	dids := make([]interface{}, 0, len(volume.DropletIDs))
   137  	for _, did := range volume.DropletIDs {
   138  		dids = append(dids, did)
   139  	}
   140  	rs.Set("droplet_ids", schema.NewSet(
   141  		func(dropletID interface{}) int { return dropletID.(int) },
   142  		dids,
   143  	))
   144  
   145  	return []*schema.ResourceData{rs}, nil
   146  }