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