Skip to content

Commit

Permalink
Merge branch 'hexogen-volume_snapshots'
Browse files Browse the repository at this point in the history
  • Loading branch information
yassirh committed May 27, 2017
2 parents 4464ceb + a5b8996 commit 8c959ec
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -483,6 +483,9 @@ $volumes = $volume->getByNameAndRegion('example', 'nyc1');
// returns a volume by id
$myvolume = $volume->getById('506f78a4-e098-11e5-ad9f-000f53306ae1');

// returns a volumes snapshots by volume id
$mySnapshots = $volume->getSnapshots('506f78a4-e098-11e5-ad9f-000f53306ae1');

// creates a volume
$myvolume = $volume->create('example', 'Block store for examples', 10, 'nyc1');

Expand All @@ -501,6 +504,9 @@ $volume->detach('506f78a4-e098-11e5-ad9f-000f53306ae1', 123, 'nyc1');
// resize a volume
$volume->resize('506f78a4-e098-11e5-ad9f-000f53306ae1', 20, 'nyc1');

// take a snapshot of volume and name it 'my-snapshot'. Returns the Snapshot entity
$snapshot = $volume->snapshot('506f78a4-e098-11e5-ad9f-000f53306ae1', 'my-snapshot');

// get a volume action by its id
$volume->getActionById(123, '506f78a4-e098-11e5-ad9f-000f53306ae1');

Expand Down
96 changes: 96 additions & 0 deletions spec/DigitalOceanV2/Api/VolumeSpec.php
Expand Up @@ -626,4 +626,100 @@ public function it_returns_an_array_of_action_entity($adapter)
$meta->shouldBeAnInstanceOf('DigitalOceanV2\Entity\Meta');
$meta->total->shouldBe($total);
}

public function it_returns_an_array_of_volumes_snapshots_which_are_snapshot_entity($adapter)
{
$total = 3;

$response = <<<'EOT'
{
"snapshots": [
{
"id": "ddcd0c62-3b45-11e7-b079-0242ac110606",
"name": "test_1",
"regions": [
"fra1"
],
"created_at": "2017-05-17T21:14:51Z",
"resource_id": "506f78a4-e098-11e5-ad9f-000f53306ae1",
"resource_type": "volume",
"min_disk_size": 40,
"size_gigabytes": 25
},
{
"id": "dfaeb3f1-3b45-11e7-889c-0242ac110705",
"name": "test_2",
"regions": [
"fra1"
],
"created_at": "2017-05-17T21:14:54Z",
"resource_id": "506f78a4-e098-11e5-ad9f-000f53306ae1",
"resource_type": "volume",
"min_disk_size": 40,
"size_gigabytes": 25
},
{
"id": "e1f9100f-3b45-11e7-b079-0242ac110606",
"name": "test_3",
"regions": [
"fra1"
],
"created_at": "2017-05-17T21:14:58Z",
"resource_id": "506f78a4-e098-11e5-ad9f-000f53306ae1",
"resource_type": "volume",
"min_disk_size": 40,
"size_gigabytes": 25
}
],
"links": {},
"meta": {
"total": 3
}
}
EOT;

$adapter
->get('https://api.digitalocean.com/v2/volumes/506f78a4-e098-11e5-ad9f-000f53306ae1/snapshots?per_page=200')
->willReturn($response);

$snapshots = $this->getSnapshots('506f78a4-e098-11e5-ad9f-000f53306ae1');
$snapshots->shouldBeArray();
$snapshots->shouldHaveCount($total);
foreach ($snapshots as $snapshot) {
$snapshot->shouldReturnAnInstanceOf('DigitalOceanV2\Entity\Snapshot');
}
$meta = $this->getMeta();
$meta->shouldBeAnInstanceOf('DigitalOceanV2\Entity\Meta');
$meta->total->shouldBe($total);
}

public function it_returns_snapshot_entity_after_snapshot_creation($adapter)
{
$response = <<<'EOT'
{
"snapshot": {
"id": "902068ee-3b3f-11e7-93a1-0242ac116705",
"name": "snapshot1-volume",
"regions": [
"fra1"
],
"created_at": "2017-05-17T20:29:44Z",
"resource_id": "506f78a4-e098-11e5-ad9f-000f53306ae1",
"resource_type": "volume",
"min_disk_size": 40,
"size_gigabytes": 25
}
}
EOT;
$adapter
->post('https://api.digitalocean.com/v2/volumes/506f78a4-e098-11e5-ad9f-000f53306ae1/snapshots',
['name' => 'snapshot1-volume']
)
->willReturn($response);

$snapshot = $this->snapshot('506f78a4-e098-11e5-ad9f-000f53306ae1', 'snapshot1-volume');
$snapshot->shouldBeAnInstanceOf('DigitalOceanV2\Entity\Snapshot');
$snapshot->id->shouldBe('902068ee-3b3f-11e7-93a1-0242ac116705');
$snapshot->name->shouldBe('snapshot1-volume');
}
}
46 changes: 46 additions & 0 deletions src/Api/Volume.php
Expand Up @@ -12,6 +12,7 @@
namespace DigitalOceanV2\Api;

use DigitalOceanV2\Entity\Action as ActionEntity;
use DigitalOceanV2\Entity\Snapshot as SnapshotEntity;
use DigitalOceanV2\Entity\Volume as VolumeEntity;

/**
Expand Down Expand Up @@ -71,6 +72,28 @@ public function getById($id)
return new VolumeEntity($volume->volume);
}

/**
* Get all volume snapshots.
*
* @param string $id
*
* @return ImageEntity[]
*/
public function getSnapshots($id)
{
$snapshots = $this->adapter->get(sprintf('%s/volumes/%s/snapshots?per_page=%d', $this->endpoint, $id, 200));

$snapshots = json_decode($snapshots);

$this->meta = $this->extractMeta($snapshots);

return array_map(function ($snapshot) {
$snapshot = new SnapshotEntity($snapshot);

return $snapshot;
}, $snapshots->snapshots);
}

/**
* @param string $name A human-readable name for the Block Storage volume
* @param string $description Free-form text field to describe a Block Storage volume
Expand Down Expand Up @@ -184,6 +207,29 @@ public function resize($id, $newSize, $regionSlug)
return new ActionEntity($action->action);
}

/**
* Create a new snapshot of the volume.
*
* @param string $id the id of the volume
* @param string $name a human-readable name for the volume snapshot
*
* @throws HttpException
*
* @return SnapshotEntity
*/
public function snapshot($id, $name)
{
$data = [
'name' => $name,
];

$snapshot = $this->adapter->post(sprintf('%s/volumes/%s/snapshots', $this->endpoint, $id), $data);

$snapshot = json_decode($snapshot);

return new SnapshotEntity($snapshot->snapshot);
}

/**
* @param string $id
* @param int $actionId
Expand Down

0 comments on commit 8c959ec

Please sign in to comment.