Merge pull request #3856 from h0lyalg0rithm/support_paging
Support paging for digitalocean
This commit is contained in:
commit
c0c24251a8
|
@ -1,7 +1,9 @@
|
|||
require 'fog/digitalocean/models/paging_collection'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class DigitalOceanV2
|
||||
class Images < Fog::Collection
|
||||
class Images < Fog::Compute::DigitalOceanV2::PagingCollection
|
||||
model Fog::Compute::DigitalOceanV2::Image
|
||||
|
||||
# Retrieves images
|
||||
|
@ -12,8 +14,11 @@ module Fog
|
|||
# @raise [Fog::Compute::DigitalOceanV2::ServiceError]
|
||||
# @see https://developers.digitalocean.com/documentation/v2/#list-all-images
|
||||
def all(filters = {})
|
||||
data = service.list_images(filters).body["images"]
|
||||
load(data)
|
||||
data = service.list_images(filters)
|
||||
links = data.body["links"]
|
||||
get_paged_links(links)
|
||||
images = data.body["images"]
|
||||
load(images)
|
||||
end
|
||||
|
||||
# Retrieves image
|
||||
|
@ -30,6 +35,7 @@ module Fog
|
|||
rescue Fog::Errors::NotFound
|
||||
nil
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/digitalocean/models/compute_v2/server'
|
||||
require 'fog/digitalocean/models/paging_collection'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class DigitalOceanV2
|
||||
class Servers < Fog::Collection
|
||||
class Servers < Fog::Compute::DigitalOceanV2::PagingCollection
|
||||
model Fog::Compute::DigitalOceanV2::Server
|
||||
|
||||
# Returns list of servers
|
||||
|
@ -15,8 +16,11 @@ module Fog
|
|||
# @raise [Fog::Compute::DigitalOceanV2::ServiceError]
|
||||
# @see https://developers.digitalocean.com/documentation/v2/#droplets
|
||||
def all(filters = {})
|
||||
data = service.list_servers(filters).body['droplets']
|
||||
load(data)
|
||||
data = service.list_servers(filters)
|
||||
links = data.body["links"]
|
||||
get_paged_links(links)
|
||||
droplets = data.body["droplets"]
|
||||
load(droplets)
|
||||
end
|
||||
|
||||
# Retrieves server
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
require 'fog/digitalocean/models/paging_collection'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class DigitalOceanV2
|
||||
class SshKeys < Fog::Collection
|
||||
class SshKeys < Fog::Compute::DigitalOceanV2::PagingCollection
|
||||
model Fog::Compute::DigitalOceanV2::SshKey
|
||||
|
||||
# Returns list of ssh keys
|
||||
|
@ -12,8 +14,11 @@ module Fog
|
|||
# @raise [Fog::Compute::DigitalOceanV2::ServiceError]
|
||||
# @see https://developers.digitalocean.com/documentation/v2/#list-all-keys
|
||||
def all(filters={})
|
||||
data = service.list_ssh_keys.body['ssh_keys']
|
||||
load(data)
|
||||
data = service.list_ssh_keys(filters)
|
||||
links = data.body["links"]
|
||||
get_paged_links(links)
|
||||
keys = data.body["ssh_keys"]
|
||||
load(keys)
|
||||
end
|
||||
|
||||
# Returns ssh key
|
||||
|
@ -32,4 +37,4 @@ module Fog
|
|||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
module Fog
|
||||
module Compute
|
||||
class DigitalOceanV2
|
||||
class PagingCollection < Fog::Collection
|
||||
|
||||
attribute :next
|
||||
attribute :last
|
||||
|
||||
def next_page
|
||||
all(page: @next) if @next != @last
|
||||
end
|
||||
|
||||
def previous_page
|
||||
if @next.to_i > 2
|
||||
all(page: @next.to_i - 2)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def deep_fetch(hash, *path)
|
||||
path.inject(hash) do |acc, key|
|
||||
acc.respond_to?(:keys) ? acc[key] : nil
|
||||
end
|
||||
end
|
||||
|
||||
def get_page(link)
|
||||
if match = link.match(/page=(?<page>\d+)/)
|
||||
match.captures.last
|
||||
end
|
||||
end
|
||||
|
||||
def get_paged_links(links)
|
||||
next_link = deep_fetch(links, "pages", "next").to_s
|
||||
last_link = deep_fetch(links, "pages", "last").to_s
|
||||
@next = get_page(next_link) || @next
|
||||
@last = get_page(last_link) || @last
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue