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