Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds resource of Volumes to Barge #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -528,3 +528,36 @@ barge.floating_ip.assign(ip_address, droplet_id: droplet_id)
``` ruby
barge.floating_ip.unassign(ip_address)
```

Volume (aka Block Storage)
-----------

### Show all volumes

``` ruby
barge.volume.all
barge.volume.all(region: 'nyc1')
```

### Create volume

``` ruby
barge.volume.create(
size_gigabytes: 10,
name: "Example",
description: "Block store for examples",
region: "nyc1"
)
```

### Show volume

``` ruby
barge.volume.show(volume_id)
```

### Destroy volume

``` ruby
barge.volume.destroy(volume_id)
```
4 changes: 4 additions & 0 deletions lib/barge/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def floating_ip
@floating_ip ||= Resource::FloatingIP.new(faraday)
end

def volume
@volume ||= Resource::Volume.new(faraday)
end

def faraday
@faraday ||= Faraday.new faraday_options do |f|
f.adapter :net_http
Expand Down
1 change: 1 addition & 0 deletions lib/barge/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'barge/resource/account'
require 'barge/resource/action'
require 'barge/resource/volume'
require 'barge/resource/domain'
require 'barge/resource/droplet'
require 'barge/resource/floating_ip'
Expand Down
43 changes: 43 additions & 0 deletions lib/barge/resource/volume.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module Barge
module Resource
class Volume
include Resource::Base

def create(options)
post('volumes', options.to_json)
end

def all(options = {})
get('volumes', options)
end

def show(volume_id)
get("volumes/#{volume_id}")
end

def destroy(volume_id)
delete("volumes/#{volume_id}")
end

def create_record(volume_id, options)
post("volumes/#{volume_id}/records", options.to_json)
end

def records(volume_id, options = {})
get("volumes/#{volume_id}/records", options)
end

def show_record(volume_id, record_id)
get("volumes/#{volume_id}/records/#{record_id}")
end

def update_record(volume_id, record_id, options)
put("volumes/#{volume_id}/records/#{record_id}", options.to_json)
end

def destroy_record(volume_id, record_id)
delete("volumes/#{volume_id}/records/#{record_id}")
end
end
end
end
63 changes: 63 additions & 0 deletions spec/barge/resource/volume_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require 'spec_helper'

describe Barge::Resource::Volume do
include_context 'resource'
it_behaves_like 'a resource'

describe '#all' do
it 'lists all volumes' do
stubbed_request =
stub_request!(:get, '/volumes')
.to_return(body: fixture('volumes/all'), status: 200)
expect(volume.all.volumes)
.to include a_hash_including(id: "7724db7c-e098-11e5-b522-000f53304e51")
expect(stubbed_request).to have_been_requested
end

it 'accepts an options hash' do
stubbed_request =
stub_request!(:get, '/volumes?page=10&per_page=20&region=nyc1')
.to_return(body: fixture('volumes/all'), status: 200)
expect(volume.all(per_page: 20, page: 10, region: 'nyc1').volumes)
.to include a_hash_including(id: "7724db7c-e098-11e5-b522-000f53304e51")
expect(stubbed_request).to have_been_requested
end
end

describe '#create' do
it 'creates a volume' do
stubbed_request =
stub_request!(:post, '/volumes')
.to_return(body: fixture('volumes/create'), status: 201)
options = {
size_gigabytes: 10,
name: "Example",
description: "Block store for examples",
region: "nyc1"
}
expect(volume.create(options).volume.id).to eql('7724db7c-e098-11e5-b522-000f53304e51')
expect(stubbed_request.with(body: options.to_json))
.to have_been_requested
end
end

describe '#destroy' do
it 'destroys a volume' do
stubbed_request =
stub_request!(:delete, '/volumes/7724db7c-e098-11e5-b522-000f53304e51').to_return(status: 204)
expect(volume.destroy('7724db7c-e098-11e5-b522-000f53304e51').success?).to be true
expect(stubbed_request).to have_been_requested
end
end

describe '#show' do
it 'returns information about a given volume' do
stubbed_request =
stub_request!(:get, '/volumes/7724db7c-e098-11e5-b522-000f53304e51')
.to_return(body: fixture('volumes/show'), status: 200)
expect(volume.show('7724db7c-e098-11e5-b522-000f53304e51').volume.name).to eq 'Example'
expect(stubbed_request).to have_been_requested
end
end

end
41 changes: 41 additions & 0 deletions spec/fixtures/volumes/all.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"volumes": [
{
"id": "7724db7c-e098-11e5-b522-000f53304e51",
"region": {
"name": "New York 1",
"slug": "nyc1",
"sizes": [
"512mb",
"1gb",
"2gb",
"4gb",
"8gb",
"16gb",
"32gb",
"48gb",
"64gb"
],
"features": [
"private_networking",
"backups",
"ipv6",
"metadata"
],
"available": true
},
"droplet_ids": [

],
"name": "Example",
"description": "Block store for examples",
"size_gigabytes": 10,
"created_at": "2016-03-02T17:00:49Z"
}
],
"links": {
},
"meta": {
"total": 1
}
}
34 changes: 34 additions & 0 deletions spec/fixtures/volumes/create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"volume": {
"id": "7724db7c-e098-11e5-b522-000f53304e51",
"region": {
"name": "New York 1",
"slug": "nyc1",
"sizes": [
"512mb",
"1gb",
"2gb",
"4gb",
"8gb",
"16gb",
"32gb",
"48gb",
"64gb"
],
"features": [
"private_networking",
"backups",
"ipv6",
"metadata"
],
"available": true
},
"droplet_ids": [

],
"name": "Example",
"description": "Block store for examples",
"size_gigabytes": 10,
"created_at": "2016-03-02T17:00:49Z"
}
}
34 changes: 34 additions & 0 deletions spec/fixtures/volumes/show.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"volume": {
"id": "7724db7c-e098-11e5-b522-000f53304e51",
"region": {
"name": "New York 1",
"slug": "nyc1",
"sizes": [
"512mb",
"1gb",
"2gb",
"4gb",
"8gb",
"16gb",
"32gb",
"48gb",
"64gb"
],
"features": [
"private_networking",
"backups",
"ipv6",
"metadata"
],
"available": true
},
"droplet_ids": [

],
"name": "Example",
"description": "Block store for examples",
"size_gigabytes": 10,
"created_at": "2016-03-02T17:00:49Z"
}
}
1 change: 1 addition & 0 deletions spec/support/shared_contexts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

let(:account) { barge.account }
let(:action) { barge.action }
let(:volume) { barge.volume }
let(:domain) { barge.domain }
let(:droplet) { barge.droplet }
let(:floating_ip) { barge.floating_ip }
Expand Down