github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/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  		Schema: map[string]*schema.Schema{
    20  			"name": &schema.Schema{
    21  				Type:     schema.TypeString,
    22  				Required: true,
    23  			},
    24  			"size_in_gb": &schema.Schema{
    25  				Type:     schema.TypeInt,
    26  				Required: true,
    27  				ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
    28  					value := v.(int)
    29  					if value < 1 || value > 150 {
    30  						errors = append(errors, fmt.Errorf("%q be more than 1 and less than 150", k))
    31  					}
    32  					return
    33  				},
    34  			},
    35  			"type": &schema.Schema{
    36  				Type:     schema.TypeString,
    37  				Required: true,
    38  				ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
    39  					value := v.(string)
    40  					if value != "l_ssd" {
    41  						errors = append(errors, fmt.Errorf("%q must be l_ssd", k))
    42  					}
    43  					return
    44  				},
    45  			},
    46  			"server": &schema.Schema{
    47  				Type:     schema.TypeString,
    48  				Computed: true,
    49  			},
    50  		},
    51  	}
    52  }
    53  
    54  func resourceScalewayVolumeCreate(d *schema.ResourceData, m interface{}) error {
    55  	scaleway := m.(*Client).scaleway
    56  	size := uint64(d.Get("size_in_gb").(int)) * gb
    57  	req := api.ScalewayVolumeDefinition{
    58  		Name:         d.Get("name").(string),
    59  		Size:         size,
    60  		Type:         d.Get("type").(string),
    61  		Organization: scaleway.Organization,
    62  	}
    63  	volumeID, err := scaleway.PostVolume(req)
    64  	if err != nil {
    65  		return fmt.Errorf("Error Creating volume: %q", err)
    66  	}
    67  	d.SetId(volumeID)
    68  	return resourceScalewayVolumeRead(d, m)
    69  }
    70  
    71  func resourceScalewayVolumeRead(d *schema.ResourceData, m interface{}) error {
    72  	scaleway := m.(*Client).scaleway
    73  	volume, err := scaleway.GetVolume(d.Id())
    74  	if err != nil {
    75  		if serr, ok := err.(api.ScalewayAPIError); ok {
    76  			log.Printf("[DEBUG] Error reading volume: %q\n", serr.APIMessage)
    77  
    78  			if serr.StatusCode == 404 {
    79  				d.SetId("")
    80  				return nil
    81  			}
    82  		}
    83  
    84  		return err
    85  	}
    86  	d.Set("name", volume.Name)
    87  	d.Set("size_in_gb", volume.Size/gb)
    88  	d.Set("type", volume.VolumeType)
    89  	d.Set("server", "")
    90  	if volume.Server != nil {
    91  		d.Set("server", volume.Server.Identifier)
    92  	}
    93  	return nil
    94  }
    95  
    96  func resourceScalewayVolumeUpdate(d *schema.ResourceData, m interface{}) error {
    97  	scaleway := m.(*Client).scaleway
    98  
    99  	var req api.ScalewayVolumePutDefinition
   100  	if d.HasChange("name") {
   101  		req.Name = String(d.Get("name").(string))
   102  	}
   103  
   104  	if d.HasChange("size_in_gb") {
   105  		size := uint64(d.Get("size_in_gb").(int)) * gb
   106  		req.Size = &size
   107  	}
   108  
   109  	scaleway.PutVolume(d.Id(), req)
   110  	return resourceScalewayVolumeRead(d, m)
   111  }
   112  
   113  func resourceScalewayVolumeDelete(d *schema.ResourceData, m interface{}) error {
   114  	scaleway := m.(*Client).scaleway
   115  	err := scaleway.DeleteVolume(d.Id())
   116  	if err != nil {
   117  		if serr, ok := err.(api.ScalewayAPIError); ok {
   118  			if serr.StatusCode == 404 {
   119  				d.SetId("")
   120  				return nil
   121  			}
   122  		}
   123  		return err
   124  	}
   125  	d.SetId("")
   126  	return nil
   127  }