github.com/peterbale/terraform@v0.9.0-beta2.0.20170315142748-5723acd55547/builtin/providers/profitbricks/resource_profitbricks_volume.go (about) 1 package profitbricks 2 3 import ( 4 "fmt" 5 "github.com/hashicorp/terraform/helper/schema" 6 "github.com/profitbricks/profitbricks-sdk-go" 7 "log" 8 ) 9 10 func resourceProfitBricksVolume() *schema.Resource { 11 return &schema.Resource{ 12 Create: resourceProfitBricksVolumeCreate, 13 Read: resourceProfitBricksVolumeRead, 14 Update: resourceProfitBricksVolumeUpdate, 15 Delete: resourceProfitBricksVolumeDelete, 16 Schema: map[string]*schema.Schema{ 17 "image_name": { 18 Type: schema.TypeString, 19 Optional: true, 20 }, 21 "size": { 22 Type: schema.TypeInt, 23 Required: true, 24 }, 25 26 "disk_type": { 27 Type: schema.TypeString, 28 Required: true, 29 }, 30 "image_password": { 31 Type: schema.TypeString, 32 Optional: true, 33 }, 34 "licence_type": { 35 Type: schema.TypeString, 36 Optional: true, 37 }, 38 "ssh_key_path": { 39 Type: schema.TypeList, 40 Elem: &schema.Schema{Type: schema.TypeString}, 41 Optional: true, 42 }, 43 "sshkey": { 44 Type: schema.TypeString, 45 Computed: true, 46 }, 47 "bus": { 48 Type: schema.TypeString, 49 Optional: true, 50 }, 51 "name": { 52 Type: schema.TypeString, 53 Optional: true, 54 }, 55 "availability_zone": { 56 Type: schema.TypeString, 57 Optional: true, 58 }, 59 "server_id": { 60 Type: schema.TypeString, 61 Required: true, 62 }, 63 "datacenter_id": { 64 Type: schema.TypeString, 65 Required: true, 66 }, 67 }, 68 } 69 } 70 71 func resourceProfitBricksVolumeCreate(d *schema.ResourceData, meta interface{}) error { 72 var err error 73 var ssh_keypath []interface{} 74 dcId := d.Get("datacenter_id").(string) 75 serverId := d.Get("server_id").(string) 76 imagePassword := d.Get("image_password").(string) 77 ssh_keypath = d.Get("ssh_key_path").([]interface{}) 78 image_name := d.Get("image_name").(string) 79 80 if image_name != "" { 81 if imagePassword == "" && len(ssh_keypath) == 0 { 82 return fmt.Errorf("Either 'image_password' or 'sshkey' must be provided.") 83 } 84 } 85 86 licenceType := d.Get("licence_type").(string) 87 88 if image_name == "" && licenceType == "" { 89 return fmt.Errorf("Either 'image_name', or 'licenceType' must be set.") 90 } 91 92 var publicKeys []string 93 if len(ssh_keypath) != 0 { 94 for _, path := range ssh_keypath { 95 log.Printf("[DEBUG] Reading file %s", path) 96 publicKey, err := readPublicKey(path.(string)) 97 if err != nil { 98 return fmt.Errorf("Error fetching sshkey from file (%s) (%s)", path, err.Error()) 99 } 100 publicKeys = append(publicKeys, publicKey) 101 } 102 } 103 104 var image string 105 if !IsValidUUID(image_name) { 106 image = getImageId(d.Get("datacenter_id").(string), image_name, d.Get("disk_type").(string)) 107 } else { 108 image = image_name 109 } 110 111 volume := profitbricks.Volume{ 112 Properties: profitbricks.VolumeProperties{ 113 Name: d.Get("name").(string), 114 Size: d.Get("size").(int), 115 Type: d.Get("disk_type").(string), 116 ImagePassword: imagePassword, 117 Image: image, 118 Bus: d.Get("bus").(string), 119 LicenceType: licenceType, 120 }, 121 } 122 123 if len(publicKeys) != 0 { 124 volume.Properties.SshKeys = publicKeys 125 126 } else { 127 volume.Properties.SshKeys = nil 128 } 129 130 if _, ok := d.GetOk("availability_zone"); ok { 131 raw := d.Get("availability_zone").(string) 132 volume.Properties.AvailabilityZone = raw 133 } 134 135 volume = profitbricks.CreateVolume(dcId, volume) 136 137 if volume.StatusCode > 299 { 138 return fmt.Errorf("An error occured while creating a volume: %s", volume.Response) 139 } 140 141 err = waitTillProvisioned(meta, volume.Headers.Get("Location")) 142 if err != nil { 143 return err 144 } 145 volume = profitbricks.AttachVolume(dcId, serverId, volume.Id) 146 if volume.StatusCode > 299 { 147 return fmt.Errorf("An error occured while attaching a volume dcId: %s server_id: %s ID: %s Response: %s", dcId, serverId, volume.Id, volume.Response) 148 } 149 150 err = waitTillProvisioned(meta, volume.Headers.Get("Location")) 151 if err != nil { 152 return err 153 } 154 d.SetId(volume.Id) 155 156 return resourceProfitBricksVolumeRead(d, meta) 157 } 158 159 func resourceProfitBricksVolumeRead(d *schema.ResourceData, meta interface{}) error { 160 config := meta.(*Config) 161 profitbricks.SetAuth(config.Username, config.Password) 162 163 dcId := d.Get("datacenter_id").(string) 164 165 volume := profitbricks.GetVolume(dcId, d.Id()) 166 if volume.StatusCode > 299 { 167 return fmt.Errorf("An error occured while fetching a volume ID %s %s", d.Id(), volume.Response) 168 169 } 170 171 d.Set("name", volume.Properties.Name) 172 d.Set("disk_type", volume.Properties.Type) 173 d.Set("size", volume.Properties.Size) 174 d.Set("bus", volume.Properties.Bus) 175 d.Set("image_name", volume.Properties.Image) 176 177 return nil 178 } 179 180 func resourceProfitBricksVolumeUpdate(d *schema.ResourceData, meta interface{}) error { 181 properties := profitbricks.VolumeProperties{} 182 dcId := d.Get("datacenter_id").(string) 183 184 if d.HasChange("name") { 185 _, newValue := d.GetChange("name") 186 properties.Name = newValue.(string) 187 } 188 if d.HasChange("disk_type") { 189 _, newValue := d.GetChange("disk_type") 190 properties.Type = newValue.(string) 191 } 192 if d.HasChange("size") { 193 _, newValue := d.GetChange("size") 194 properties.Size = newValue.(int) 195 } 196 if d.HasChange("bus") { 197 _, newValue := d.GetChange("bus") 198 properties.Bus = newValue.(string) 199 } 200 if d.HasChange("availability_zone") { 201 _, newValue := d.GetChange("availability_zone") 202 properties.AvailabilityZone = newValue.(string) 203 } 204 205 volume := profitbricks.PatchVolume(dcId, d.Id(), properties) 206 err := waitTillProvisioned(meta, volume.Headers.Get("Location")) 207 if err != nil { 208 return err 209 } 210 if volume.StatusCode > 299 { 211 return fmt.Errorf("An error occured while updating a volume ID %s %s", d.Id(), volume.Response) 212 213 } 214 err = resourceProfitBricksVolumeRead(d, meta) 215 if err != nil { 216 return err 217 } 218 d.SetId(d.Get("server_id").(string)) 219 err = resourceProfitBricksServerRead(d, meta) 220 if err != nil { 221 return err 222 } 223 224 d.SetId(volume.Id) 225 return nil 226 } 227 228 func resourceProfitBricksVolumeDelete(d *schema.ResourceData, meta interface{}) error { 229 dcId := d.Get("datacenter_id").(string) 230 231 resp := profitbricks.DeleteVolume(dcId, d.Id()) 232 if resp.StatusCode > 299 { 233 return fmt.Errorf("An error occured while deleting a volume ID %s %s", d.Id(), string(resp.Body)) 234 235 } 236 err := waitTillProvisioned(meta, resp.Headers.Get("Location")) 237 if err != nil { 238 return err 239 } 240 d.SetId("") 241 return nil 242 }