forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provider/digitalocean: adds a volume resource (hashicorp#7560)
* provider/digitalocean: add support for volumes * provider/digitalocean: add documentation for volume resource
- Loading branch information
Showing
9 changed files
with
489 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
builtin/providers/digitalocean/import_digitalocean_volume_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package digitalocean | ||
|
||
import ( | ||
"testing" | ||
|
||
"fmt" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccDigitalOceanVolume_importBasic(t *testing.T) { | ||
resourceName := "digitalocean_volume.foobar" | ||
volumeName := fmt.Sprintf("volume-%s", acctest.RandString(10)) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckDigitalOceanVolumeDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: fmt.Sprintf(testAccCheckDigitalOceanVolumeConfig_basic, volumeName), | ||
}, | ||
|
||
resource.TestStep{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
builtin/providers/digitalocean/resource_digitalocean_volume.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package digitalocean | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/digitalocean/godo" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceDigitalOceanVolume() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceDigitalOceanVolumeCreate, | ||
Read: resourceDigitalOceanVolumeRead, | ||
Delete: resourceDigitalOceanVolumeDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: resourceDigitalOceanVolumeImport, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"region": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"droplet_ids": &schema.Schema{ | ||
Type: schema.TypeSet, | ||
Elem: &schema.Schema{Type: schema.TypeInt}, | ||
Computed: true, | ||
}, | ||
|
||
"size": &schema.Schema{ | ||
Type: schema.TypeInt, | ||
Required: true, | ||
ForceNew: true, // Update-ability Coming Soon ™ | ||
}, | ||
|
||
"description": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, // Update-ability Coming Soon ™ | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceDigitalOceanVolumeCreate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*godo.Client) | ||
|
||
opts := &godo.VolumeCreateRequest{ | ||
Region: d.Get("region").(string), | ||
Name: d.Get("name").(string), | ||
Description: d.Get("description").(string), | ||
SizeGigaBytes: int64(d.Get("size").(int)), | ||
} | ||
|
||
log.Printf("[DEBUG] Volume create configuration: %#v", opts) | ||
volume, _, err := client.Storage.CreateVolume(opts) | ||
if err != nil { | ||
return fmt.Errorf("Error creating Volume: %s", err) | ||
} | ||
|
||
d.SetId(volume.ID) | ||
log.Printf("[INFO] Volume name: %s", volume.Name) | ||
|
||
return resourceDigitalOceanVolumeRead(d, meta) | ||
} | ||
|
||
func resourceDigitalOceanVolumeRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*godo.Client) | ||
|
||
volume, resp, err := client.Storage.GetVolume(d.Id()) | ||
if err != nil { | ||
// If the volume is somehow already destroyed, mark as | ||
// successfully gone | ||
if resp.StatusCode == 404 { | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("Error retrieving volume: %s", err) | ||
} | ||
|
||
d.Set("id", volume.ID) | ||
|
||
dids := make([]interface{}, 0, len(volume.DropletIDs)) | ||
for _, did := range volume.DropletIDs { | ||
dids = append(dids, did) | ||
} | ||
d.Set("droplet_ids", schema.NewSet( | ||
func(dropletID interface{}) int { return dropletID.(int) }, | ||
dids, | ||
)) | ||
|
||
return nil | ||
} | ||
|
||
func resourceDigitalOceanVolumeDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*godo.Client) | ||
|
||
log.Printf("[INFO] Deleting volume: %s", d.Id()) | ||
_, err := client.Storage.DeleteVolume(d.Id()) | ||
if err != nil { | ||
return fmt.Errorf("Error deleting volume: %s", err) | ||
} | ||
|
||
d.SetId("") | ||
return nil | ||
} | ||
|
||
func resourceDigitalOceanVolumeImport(rs *schema.ResourceData, v interface{}) ([]*schema.ResourceData, error) { | ||
client := v.(*godo.Client) | ||
volume, _, err := client.Storage.GetVolume(rs.Id()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rs.Set("id", volume.ID) | ||
rs.Set("name", volume.Name) | ||
rs.Set("region", volume.Region.Slug) | ||
rs.Set("description", volume.Description) | ||
rs.Set("size", int(volume.SizeGigaBytes)) | ||
|
||
dids := make([]interface{}, 0, len(volume.DropletIDs)) | ||
for _, did := range volume.DropletIDs { | ||
dids = append(dids, did) | ||
} | ||
rs.Set("droplet_ids", schema.NewSet( | ||
func(dropletID interface{}) int { return dropletID.(int) }, | ||
dids, | ||
)) | ||
|
||
return []*schema.ResourceData{rs}, nil | ||
} |
Oops, something went wrong.