Improved finding in snapshot list. can only handle <200 SSs

This commit is contained in:
Neil Aldred 2018-11-07 22:40:33 +13:00
parent 42d4112896
commit b139aaef16
2 changed files with 33 additions and 27 deletions

View File

@ -64,27 +64,31 @@ func resourceDigitalOceanDropletSnapshotCreate(d *schema.ResourceData, meta inte
return fmt.Errorf("Error creating Droplet Snapshot: %s", err)
}
for ok := true; ok; ok = checkActionProgress(action.ID, meta) {
for ok := true; ok; ok = checkActionProgress(action.Status) {
action, _, _ = client.Actions.Get(context.Background(), action.ID)
time.Sleep(10)
}
opt := godo.ListOptions{}
opt := godo.ListOptions{Page: 1, PerPage: 200}
snapshotList, _, _ := client.Droplets.Snapshots(context.Background(), action.ResourceID, &opt)
d.SetId(strconv.Itoa(snapshotList[len(snapshotList)-1].ID))
d.Set("name", snapshotList[len(snapshotList)-1].Name)
d.Set("resource_id", strconv.Itoa(snapshotList[len(snapshotList)-1].ID))
d.Set("regions", snapshotList[len(snapshotList)-1].Regions)
d.Set("created_at", snapshotList[len(snapshotList)-1].Created)
d.Set("min_disk_size", snapshotList[len(snapshotList)-1].MinDiskSize)
for _, v := range snapshotList {
createdTime, _ := time.Parse("2006-01-02T15:04:05Z", v.Created)
checkTime := godo.Timestamp{createdTime}
if checkTime == *action.StartedAt {
d.SetId(strconv.Itoa(v.ID))
d.Set("name", v.Name)
d.Set("resource_id", strconv.Itoa(v.ID))
d.Set("regions", v.Regions)
d.Set("created_at", v.Created)
d.Set("min_disk_size", v.MinDiskSize)
}
}
return resourceDigitalOceanDropletSnapshotRead(d, meta)
}
func checkActionProgress(actionId int, meta interface{}) bool {
client := meta.(*godo.Client)
action, _, _ := client.Actions.Get(context.Background(), actionId)
if action.Status == "in-progress" {
func checkActionProgress(actionProgress string) bool {
if actionProgress == "in-progress" {
return true
}
return false

View File

@ -49,7 +49,9 @@ func testSweepDropletSnapshots(region string) error {
func TestAccDigitalOceanDropletSnapshot_Basic(t *testing.T) {
var snapshot godo.Snapshot
rInt := acctest.RandInt()
rInt1 := acctest.RandInt()
rInt2 := acctest.RandInt()
rInt3 := acctest.RandInt()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
@ -57,17 +59,13 @@ func TestAccDigitalOceanDropletSnapshot_Basic(t *testing.T) {
CheckDestroy: testAccCheckDigitalOceanDropletSnapshotDestroy,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(testAccCheckDigitalOceanDropletSnapshotConfig_basic, rInt, rInt),
Config: fmt.Sprintf(testAccCheckDigitalOceanDropletSnapshotConfig_basic, rInt1, rInt2, rInt3),
Check: resource.ComposeTestCheckFunc(
testAccCheckDigitalOceanDropletSnapshotExists("digitalocean_droplet_snapshot.foobar", &snapshot),
testAccCheckDigitalOceanDropletSnapshotExists("digitalocean_droplet_snapshot.foobar_one", &snapshot),
resource.TestCheckResourceAttr(
"digitalocean_droplet_snapshot.foobar", "name", fmt.Sprintf("snapshot-%d", rInt)),
"digitalocean_droplet_snapshot.foobar_one", "name", fmt.Sprintf("snapshotone-%d", rInt2)),
resource.TestCheckResourceAttr(
"digitalocean_droplet_snapshot.foobar", "regions.#", "1"),
resource.TestCheckResourceAttr(
"digitalocean_droplet_snapshot.foobar", "min_disk_size", "20"),
resource.TestCheckResourceAttrSet(
"digitalocean_droplet_snapshot.foobar", "resource_id"),
"digitalocean_droplet_snapshot.foobar_two", "name", fmt.Sprintf("snapshottwo-%d", rInt3)),
),
},
},
@ -123,10 +121,6 @@ func testAccCheckDigitalOceanDropletSnapshotDestroy(s *terraform.State) error {
}
const testAccCheckDigitalOceanDropletSnapshotConfig_basic = `
resource "digitalocean_droplet_snapshot" "foobar" {
name = "snapshot-%d"
resource_id = "${digitalocean_droplet.foo.id}"
}
resource "digitalocean_droplet" "foo" {
name = "foo-%d"
size = "512mb"
@ -134,4 +128,12 @@ resource "digitalocean_droplet" "foo" {
region = "nyc3"
user_data = "foobar"
}
`
resource "digitalocean_droplet_snapshot" "foobar_one" {
name = "snapshotone-%d"
resource_id = "${digitalocean_droplet.foo.id}"
}
resource "digitalocean_droplet_snapshot" "foobar_two" {
name = "snapshottwo-%d"
resource_id = "${digitalocean_droplet.foo.id}"
}
`