Skip to content

Commit

Permalink
Add block storage
Browse files Browse the repository at this point in the history
  • Loading branch information
h0lyalg0rithm committed Aug 6, 2016
1 parent 3a49b46 commit 02ed041
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 40 deletions.
3 changes: 3 additions & 0 deletions lib/fog/digitalocean/compute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ class DigitalOcean < Fog::Service
collection :flavors
model :ssh_key
collection :ssh_keys
model :volume
collection :volumes

request_path 'fog/digitalocean/requests/compute'
request :attach_volume
request :change_kernel
request :create_server
request :create_ssh_key
Expand Down
10 changes: 1 addition & 9 deletions lib/fog/digitalocean/models/compute/volume.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,10 @@ class Volume < Fog::Model
attribute :name
attribute :region
attribute :droplet_ids
attribute :distribution
attribute :description
attribute :size_gigabytes
attribute :created_at
end

def transfer
perform_action :transfer_image
end

def convert_to_snapshot
perform_action :convert_image_to_snapshot
end
end
end
end
38 changes: 19 additions & 19 deletions lib/fog/digitalocean/models/compute/volumes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,35 @@
module Fog
module Compute
class DigitalOcean
class Volume < Fog::Compute::DigitalOcean::PagingCollection
class Volumes < Fog::Compute::DigitalOcean::PagingCollection
model Fog::Compute::DigitalOcean::Volume

# Retrieves images
# @return [Fog::Compute::DigitalOceanV2:Image]
# @raise [Fog::Compute::DigitalOceanV2::NotFound] - HTTP 404
# @raise [Fog::Compute::DigitalOceanV2::BadRequest] - HTTP 400
# @raise [Fog::Compute::DigitalOceanV2::InternalServerError] - HTTP 500
# @raise [Fog::Compute::DigitalOceanV2::ServiceError]
# Retrieves volumes
# @return [Fog::Compute::DigitalOcean::Volume]
# @raise [Fog::Compute::DigitalOcean::NotFound] - HTTP 404
# @raise [Fog::Compute::DigitalOcean::BadRequest] - HTTP 400
# @raise [Fog::Compute::DigitalOcean::InternalServerError] - HTTP 500
# @raise [Fog::Compute::DigitalOcean::ServiceError]
# @see https://developers.digitalocean.com/documentation/v2/#list-all-images
def all(filters = {})
data = service.list_images(filters)
data = service.list_volumes(filters)
links = data.body["links"]
get_paged_links(links)
images = data.body["images"]
load(images)
volumes = data.body["volumes"]
load(volumes)
end

# Retrieves image
# @param [String] id for image to be returned
# @return [Fog::Compute::DigitalOceanV2:Image]
# @raise [Fog::Compute::DigitalOceanV2::NotFound] - HTTP 404
# @raise [Fog::Compute::DigitalOceanV2::BadRequest] - HTTP 400
# @raise [Fog::Compute::DigitalOceanV2::InternalServerError] - HTTP 500
# @raise [Fog::Compute::DigitalOceanV2::ServiceError]
# Retrieves volume
# @param [String] id for volume to be returned
# @return [Fog::Compute::DigitalOcean:Image]
# @raise [Fog::Compute::DigitalOcean::NotFound] - HTTP 404
# @raise [Fog::Compute::DigitalOcean::BadRequest] - HTTP 400
# @raise [Fog::Compute::DigitalOcean::InternalServerError] - HTTP 500
# @raise [Fog::Compute::DigitalOcean::ServiceError]
# @see https://developers.digitalocean.com/documentation/v2/#retrieve-an-existing-image-by-id
def get(id)
image = service.get_image_details(id).body['image']
new(image) if image
volume = service.get_volume_details(id).body['volume']
new(volume) if volume
rescue Fog::Errors::NotFound
nil
end
Expand Down
44 changes: 44 additions & 0 deletions lib/fog/digitalocean/requests/compute/attach_volume.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module Fog
module Compute
class DigitalOcean
class Real
def attach_volume(volume_id, droplet_id, region)
body = { :type => 'attach', droplet_id: droplet_id, region: region}

encoded_body = Fog::JSON.encode(body)

request(
:expects => [201],
:headers => {
'Content-Type' => "application/json; charset=UTF-8",
},
:method => 'POST',
:path => "v2/images/#{volume_id}/actions",
:body => encoded_body,
)
end
end

class Mock
def attach_volume(volume_id, droplet_id, region)
response = Excon::Response.new
response.status = 201
response.body = {
'action' => {
'id' => 46592838,
'status' => 'completed',
'type' => 'convert_to_snapshot',
'started_at' => '2015-03-24T19:02:47Z',
'completed_at' => '2015-03-24T19:02:47Z',
'resource_id' => 11060029,
'resource_type' => 'image',
'region' => null,
'region_slug' => null
}
}
response
end
end
end
end
end
23 changes: 13 additions & 10 deletions lib/fog/digitalocean/requests/compute/create_volume.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ class DigitalOcean
# noinspection RubyStringKeysInHashInspection
class Real

def create_volume(name, public_key)
def create_volume(options)
create_options = {
:name => name,
:public_key => public_key,
}
:name => "name",
:region => "nyc1",
:size_gigabytes => 1
}.merge(options)

encoded_body = Fog::JSON.encode(create_options)

Expand All @@ -18,27 +19,29 @@ def create_volume(name, public_key)
'Content-Type' => "application/json; charset=UTF-8",
},
:method => 'POST',
:path => '/v2/account/keys',
:path => '/v2/volumes',
:body => encoded_body,
)
end
end

# noinspection RubyStringKeysInHashInspection
class Mock
def create_volume(name, public_key)
def create_volume(options)
response = Excon::Response.new
response.status = 201

data[:ssh_keys] << {
data[:volumes] << {
"id" => Fog::Mock.random_numbers(6).to_i,
"fingerprint" => (["00"] * 16).join(':'),
"public_key" => public_key,
"name" => name
"region" => options[:region],
"size_gigabytes" => 10,
"description" => options[:description],
"name" => options[:name]
}

response.body ={
'ssh_key' => data[:ssh_keys].last
'volume' => data[:volumes].last
}

response
Expand Down
4 changes: 2 additions & 2 deletions lib/fog/digitalocean/requests/compute/destroy_volume.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ def destroy_volume(id)
'Content-Type' => "application/json; charset=UTF-8",
},
:method => 'DELETE',
:path => "/v2/account/keys/#{id}",
:path => "/v2/volumes/#{id}",
)
end
end

# noinspection RubyStringKeysInHashInspection
class Mock
def destroy_volume(id)
self.data[:ssh_keys].select! do |key|
self.data[:volumes].select! do |key|
key["id"] != id
end

Expand Down
60 changes: 60 additions & 0 deletions lib/fog/digitalocean/requests/compute/get_volume_details.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module Fog
module Compute
class DigitalOcean
class Real
def get_volume_details(volume_id)
request(
:expects => [200],
:method => 'GET',
:path => "/v2/volumes/#{volume_id}"
)
end
end

# noinspection RubyStringKeysInHashInspection
class Mock
def get_volume_details(_)
response = Excon::Response.new
response.status = 200

response.body = {
'volume' => {
"id" => "v4-e098-11e5-ad9f-000f53306ae1",
"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"
}
}

response
end
end
end
end
end

0 comments on commit 02ed041

Please sign in to comment.