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 29d7c9c
Show file tree
Hide file tree
Showing 7 changed files with 152 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
28 changes: 28 additions & 0 deletions lib/tugboat/middleware/list_snapshots.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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
end
end
end
46 changes: 46 additions & 0 deletions spec/cli/snapshots_cli_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'spec_helper'

describe Tugboat::CLI do
include_context 'spec'

describe 'snapshots' do
it 'shows a list when snapshots exist' do
stub_request(:get, 'https://api.digitalocean.com/v2/droplets?page=1&per_page=1').
with(headers: { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization' => 'Bearer foo', 'Content-Type' => 'application/json', 'User-Agent' => 'Faraday v0.9.2' }).
to_return(status: 200, body: fixture('show_droplets'), headers: {})

stub_request(:get, "https://api.digitalocean.com/v2/snapshots?page=1&per_page=20").
with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Bearer foo', 'Content-Type'=>'application/json', 'User-Agent'=>'Faraday v0.9.2'}).
to_return(status: 200, body: fixture('show_snapshots'), headers: {})

expected_string = <<-eos
5.10 x64 (id: 6372321, resource_type: droplet, created_at: 2014-09-26T16:40:18Z)
eos

expect { cli.snapshots }.to output(expected_string).to_stdout

expect(a_request(:get, 'https://api.digitalocean.com/v2/droplets?page=1&per_page=1')).to have_been_made.once
expect(a_request(:get, 'https://api.digitalocean.com/v2/snapshots?page=1&per_page=20')).to have_been_made.once
end

it 'shows a message when no snapshots exist' do
stub_request(:get, 'https://api.digitalocean.com/v2/droplets?page=1&per_page=1').
with(headers: { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization' => 'Bearer foo', 'Content-Type' => 'application/json', 'User-Agent' => 'Faraday v0.9.2' }).
to_return(status: 200, body: fixture('show_droplets'), headers: {})

stub_request(:get, "https://api.digitalocean.com/v2/snapshots?page=1&per_page=20").
with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Bearer foo', 'Content-Type'=>'application/json', 'User-Agent'=>'Faraday v0.9.2'}).
to_return(status: 200, body: fixture('show_snapshots_empty'), headers: {})

expected_string = <<-eos
You don't appear to have any snapshots.
eos

expect { cli.snapshots }.to output(expected_string).to_stdout

expect(a_request(:get, 'https://api.digitalocean.com/v2/droplets?page=1&per_page=1')).to have_been_made.once
expect(a_request(:get, 'https://api.digitalocean.com/v2/snapshots?page=1&per_page=20')).to have_been_made.once
end

end
end
34 changes: 34 additions & 0 deletions spec/fixtures/show_snapshots.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"snapshots": [
{
"id": 6372321,
"name": "5.10 x64",
"regions": [
"nyc1",
"ams1",
"sfo1",
"nyc2",
"ams2",
"sgp1",
"lon1",
"nyc3",
"ams3",
"fra1",
"tor1"
],
"created_at": "2014-09-26T16:40:18Z",
"resource_id": 2713828,
"resource_type": "droplet",
"min_disk_size": 20,
"size_gigabytes": 1.42
}
],
"links": {
"pages": {
"last": "https://api.digitalocean.com/v2/snapshots?page=1&per_page=1"
}
},
"meta": {
"total": 1
}
}
7 changes: 7 additions & 0 deletions spec/fixtures/show_snapshots_empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"snapshots": [],
"links": {},
"meta": {
"total": 0
}
}

0 comments on commit 29d7c9c

Please sign in to comment.