Skip to content

Commit

Permalink
Merge pull request #10 from fog/add_block_storage
Browse files Browse the repository at this point in the history
Add block storage
  • Loading branch information
h0lyalg0rithm committed Aug 23, 2016
2 parents 978be9f + d7f1743 commit f3c2c43
Show file tree
Hide file tree
Showing 17 changed files with 710 additions and 18 deletions.
42 changes: 27 additions & 15 deletions lib/fog/digitalocean/compute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,52 @@ class DigitalOcean < Fog::Service
collection :flavors
model :ssh_key
collection :ssh_keys
model :volume
collection :volumes

request_path 'fog/digitalocean/requests/compute'
request :change_kernel
request :list_volumes
request :create_volume
request :attach_volume
request :detach_volume
request :destroy_volume
request :list_volume_actions

request :list_servers
request :create_server
request :create_ssh_key
request :destroy_server
request :delete_ssh_key
request :get_server_details
request :change_kernel
request :disable_backups
request :enable_ipv6
request :enable_private_networking
request :get_droplet_action
request :get_image_details
request :get_server_details
request :get_ssh_key
request :list_droplet_actions
request :list_flavors
request :list_images
request :list_regions
request :list_servers
request :list_ssh_keys
request :password_reset
request :power_cycle
request :power_off
request :power_on
request :reboot_server
request :password_reset
request :rebuild
request :rename
request :resize
request :restore
request :shutdown
request :snapshot
request :update_ssh_key
request :upgrade

request :list_ssh_keys
request :create_ssh_key
request :get_ssh_key
request :update_ssh_key
request :delete_ssh_key

request :list_images
request :list_flavors
request :get_image_details

request :get_droplet_action
request :list_droplet_actions
request :list_regions


class Mock
def self.data
Expand Down
15 changes: 15 additions & 0 deletions lib/fog/digitalocean/models/compute/volume.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Fog
module Compute
class DigitalOcean
class Volume < Fog::Model
identity :id
attribute :name
attribute :region
attribute :droplet_ids
attribute :description
attribute :size_gigabytes
attribute :created_at
end
end
end
end
42 changes: 42 additions & 0 deletions lib/fog/digitalocean/models/compute/volumes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'fog/digitalocean/models/paging_collection'

module Fog
module Compute
class DigitalOcean
class Volumes < Fog::Compute::DigitalOcean::PagingCollection
model Fog::Compute::DigitalOcean::Volume

# 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_volumes(filters)
links = data.body["links"]
get_paged_links(links)
volumes = data.body["volumes"]
load(volumes)
end

# 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)
volume = service.get_volume_details(id).body['volume']
new(volume) if volume
rescue Fog::Errors::NotFound
nil
end

end
end
end
end
122 changes: 122 additions & 0 deletions lib/fog/digitalocean/requests/compute/attach_volume.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
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/volumes/#{volume_id}/actions",
:body => encoded_body,
)
end

def attach_volume_by_name(volume_name, droplet_id, region)
body = { :type => 'attach', volume_name: volume_name, 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/volumes/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" => 72531856,
"status" => "completed",
"type" => "attach_volume",
"started_at" => "2015-11-12T17:51:03Z",
"completed_at" => "2015-11-12T17:51:14Z",
"resource_id" => "null",
"resource_type" => "volume",
"region" => {
"name" => "New York 1",
"slug" => "nyc1",
"sizes" => [
"1gb",
"2gb",
"4gb",
"8gb",
"32gb",
"64gb",
"512mb",
"48gb",
"16gb"
],
"features" => [
"private_networking",
"backups",
"ipv6",
"metadata"
],
"available" => true
},
"region_slug" => "nyc1"
}
}

response
end
def attach_volume_by_name(volume_name, droplet_id, region)
response = Excon::Response.new
response.status = 201
response.body = {
"action" => {
"id" => 72531856,
"status" => "completed",
"type" => "attach_volume",
"started_at" => "2015-11-12T17:51:03Z",
"completed_at" => "2015-11-12T17:51:14Z",
"resource_id" => "null",
"resource_type" => "volume",
"region" => {
"name" => "New York 1",
"slug" => "nyc1",
"sizes" => [
"1gb",
"2gb",
"4gb",
"8gb",
"32gb",
"64gb",
"512mb",
"48gb",
"16gb"
],
"features" => [
"private_networking",
"backups",
"ipv6",
"metadata"
],
"available" => true
},
"region_slug" => "nyc1"
}
}

response
end
end
end
end
end
2 changes: 2 additions & 0 deletions lib/fog/digitalocean/requests/compute/create_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def create_server(name,
create_options[opt] = options[opt] if options[opt]
end

create_options[:volume] = options[:volume] || []

encoded_body = Fog::JSON.encode(create_options)

request(
Expand Down
50 changes: 50 additions & 0 deletions lib/fog/digitalocean/requests/compute/create_volume.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module Fog
module Compute
class DigitalOcean
# noinspection RubyStringKeysInHashInspection
class Real

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

encoded_body = Fog::JSON.encode(create_options)

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

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

response.body ={
'volume' => {
"id" => Fog::Mock.random_numbers(6).to_i,
"fingerprint" => (["00"] * 16).join(':'),
"region" => options[:region],
"size_gigabytes" => 10,
"description" => options[:description],
"name" => options[:name]
}
}

response
end
end
end
end
end
32 changes: 32 additions & 0 deletions lib/fog/digitalocean/requests/compute/destroy_volume.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Fog
module Compute
class DigitalOcean
# noinspection RubyStringKeysInHashInspection
class Real
def destroy_volume(id)
request(
:expects => [204],
:headers => {
'Content-Type' => "application/json; charset=UTF-8",
},
:method => 'DELETE',
:path => "/v2/volumes/#{id}",
)
end
end

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

response = Excon::Response.new
response.status = 204
response
end
end
end
end
end
Loading

0 comments on commit f3c2c43

Please sign in to comment.