github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/scaleway/resource_scaleway_volume.go (about)

     1  package scaleway
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  	"github.com/scaleway/scaleway-cli/pkg/api"
     9  )
    10  
    11  const gb uint64 = 1000 * 1000 * 1000
    12  
    13  func resourceScalewayVolume() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceScalewayVolumeCreate,
    16  		Read:   resourceScalewayVolumeRead,
    17  		Update: resourceScalewayVolumeUpdate,
    18  		Delete: resourceScalewayVolumeDelete,
    19  		Importer: &schema.ResourceImporter{
    20  			State: schema.ImportStatePassthrough,
    21  		},
    22  
    23  		Schema: map[string]*schema.Schema{
    24  			"name": &schema.Schema{
    25  				Type:     schema.TypeString,
    26  				Required: true,
    27  			},
    28  			"size_in_gb": &schema.Schema{
    29  				Type:         schema.TypeInt,
    30  				Required:     true,
    31  				ValidateFunc: validateVolumeSize,
    32  			},
    33  			"type": &schema.Schema{
    34  				Type:         schema.TypeString,
    35  				Required:     true,
    36  				ValidateFunc: validateVolumeType,
    37  			},
    38  			"server": &schema.Schema{
    39  				Type:     schema.TypeString,
    40  				Computed: true,
    41  			},
    42  		},
    43  	}
    44  }
    45  
    46  func resourceScalewayVolumeCreate(d *schema.ResourceData, m interface{}) error {
    47  	scaleway := m.(*Client).scaleway
    48  	size := uint64(d.Get("size_in_gb").(int)) * gb
    49  	req := api.ScalewayVolumeDefinition{
    50  		Name:         d.Get("name").(string),
    51  		Size:         size,
    52  		Type:         d.Get("type").(string),
    53  		Organization: scaleway.Organization,
    54  	}
    55  	volumeID, err := scaleway.PostVolume(req)
    56  	if err != nil {
    57  		return fmt.Errorf("Error Creating volume: %q", err)
    58  	}
    59  	d.SetId(volumeID)
    60  	return resourceScalewayVolumeRead(d, m)
    61  }
    62  
    63  func resourceScalewayVolumeRead(d *schema.ResourceData, m interface{}) error {
    64  	scaleway := m.(*Client).scaleway
    65  	volume, err := scaleway.GetVolume(d.Id())
    66  	if err != nil {
    67  		if serr, ok := err.(api.ScalewayAPIError); ok {
    68  			log.Printf("[DEBUG] Error reading volume: %q\n", serr.APIMessage)
    69  
    70  			if serr.StatusCode == 404 {
    71  				d.SetId("")
    72  				return nil
    73  			}
    74  		}
    75  
    76  		return err
    77  	}
    78  	d.Set("name", volume.Name)
    79  	d.Set("size_in_gb", uint64(volume.Size)/gb)
    80  	d.Set("type", volume.VolumeType)
    81  	d.Set("server", "")
    82  	if volume.Server != nil {
    83  		d.Set("server", volume.Server.Identifier)
    84  	}
    85  	return nil
    86  }
    87  
    88  func resourceScalewayVolumeUpdate(d *schema.ResourceData, m interface{}) error {
    89  	scaleway := m.(*Client).scaleway
    90  
    91  	var req api.ScalewayVolumePutDefinition
    92  	if d.HasChange("name") {
    93  		req.Name = String(d.Get("name").(string))
    94  	}
    95  
    96  	if d.HasChange("size_in_gb") {
    97  		size := uint64(d.Get("size_in_gb").(int)) * gb
    98  		req.Size = &size
    99  	}
   100  
   101  	scaleway.PutVolume(d.Id(), req)
   102  	return resourceScalewayVolumeRead(d, m)
   103  }
   104  
   105  func resourceScalewayVolumeDelete(d *schema.ResourceData, m interface{}) error {
   106  	scaleway := m.(*Client).scaleway
   107  	err := scaleway.DeleteVolume(d.Id())
   108  	if err != nil {
   109  		if serr, ok := err.(api.ScalewayAPIError); ok {
   110  			if serr.StatusCode == 404 {
   111  				d.SetId("")
   112  				return nil
   113  			}
   114  		}
   115  		return err
   116  	}
   117  	d.SetId("")
   118  	return nil
   119  }