Skip to content

Commit

Permalink
Add ability to list snapshots
Browse files Browse the repository at this point in the history
* First step for #249 
* Listing of all snapshots
  • Loading branch information
petems committed Dec 5, 2017
1 parent 330aeb1 commit d39e34c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,18 @@ Or just list images that you have created.
My application image (id: 6376601, distro: Ubuntu)
....

### List Current Snapshots

$ tugboat snapshots
code-freeze-backup-october (id: 2013184, resource_type: droplet, created_at: 2016-10-06T11:43:06Z)
test-admin 2017-05-31 (id: 20234485, resource_type: droplet, created_at: 2017-05-31T02:07:07Z)
test-admin 2017-11-08 (id: 21133567, resource_type: droplet, created_at: 2017-11-08T02:49:09Z)
test-admin 2017-11-15 (id: 22355454, resource_type: droplet, created_at: 2017-11-15T03:11:08Z)
test-admin 2017-11-22 (id: 24523423, resource_type: droplet, created_at: 2017-11-22T03:10:09Z)
test-admin 2017-11-29 (id: 26212345, resource_type: droplet, created_at: 2017-11-29T03:15:25Z)
....


### List Available Sizes

$ tugboat sizes
Expand Down
14 changes: 14 additions & 0 deletions lib/tugboat/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,20 @@ def resize(name = nil)
'user_quiet' => options[:quiet])
end

method_option 'per_page',
type: :boolean,
default: 20,
aliases: '-p',
desc: 'Chose how many results to fetch from the DigitalOcean API (larger is slower)'
desc 'snapshots [OPTIONS]', 'Retrieve a list of your snapshots'
def snapshots
Middleware.sequence_list_snapshots.call(
'tugboat_action' => __method__,
'user_quiet' => options[:quiet],
'per_page' => options['per_page'],
)
end

desc 'password-reset FUZZY_NAME', 'Reset root password'
method_option 'id',
type: :numeric,
Expand Down
11 changes: 11 additions & 0 deletions lib/tugboat/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module Middleware
autoload :ListImages, 'tugboat/middleware/list_images'
autoload :ListRegions, 'tugboat/middleware/list_regions'
autoload :ListSizes, 'tugboat/middleware/list_sizes'
autoload :ListSnapshots, 'tugboat/middleware/list_snapshots'
autoload :ListSSHKeys, 'tugboat/middleware/list_ssh_keys'
autoload :PasswordReset, 'tugboat/middleware/password_reset'
autoload :ResizeDroplet, 'tugboat/middleware/resize_droplet'
Expand Down Expand Up @@ -231,6 +232,16 @@ def self.sequence_ssh_keys
end
end

# Display a list of available SSH keys
def self.sequence_list_snapshots
::Middleware::Builder.new do
use InjectConfiguration
use CheckConfiguration
use InjectClient
use ListSnapshots
end
end

# Create a droplet
def self.sequence_add_key
::Middleware::Builder.new do
Expand Down
34 changes: 34 additions & 0 deletions lib/tugboat/middleware/list_snapshots.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Tugboat
module Middleware
# Check if the client has set-up configuration yet.
class ListSnapshots < Base
def call(env)
ocean = env['droplet_kit']

verify_credentials(ocean)

response = ocean.snapshots.all(per_page: env['per_page'])

has_one = false

response.each do |snapshot|
has_one = true

say "#{snapshot.name} (id: #{snapshot.id}, resource_type: #{snapshot.resource_type}, created_at: #{snapshot.created_at})"
end

unless has_one
say "You don't appear to have any snapshots.", :red
end

@app.call(env)
end

private

def droplet_id_to_url(id)
", url: 'https://cloud.digitalocean.com/droplets/#{id}'"
end
end
end
end

0 comments on commit d39e34c

Please sign in to comment.