Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check credentials for all find droplets #219

Merged
merged 2 commits into from
Nov 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/tugboat/middleware/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,28 @@ def call(env)
@app.call(env)
end

def verify_credentials(ocean, say_success=false)
begin
response = ocean.droplet.all({:per_page =>'1', :page =>'1'})
rescue Faraday::ClientError => e
say "Authentication with DigitalOcean failed at an early stage"
say "Error was: #{e}"
exit 1
end

unless response.success?
say "Failed to connect to DigitalOcean. Reason given from API: #{response.id} - #{response.message}", :red
exit 1
end

say "Authentication with DigitalOcean was successful.", :green if say_success
end

# Get all pages of droplets
def get_droplet_list(ocean)

verify_credentials(ocean)

page = ocean.droplet.all(per_page: 200, page: 1)
if not page.paginated?
return page.droplets
Expand All @@ -38,6 +58,7 @@ def get_droplet_list(ocean)
end
end
end

end
end

15 changes: 1 addition & 14 deletions lib/tugboat/middleware/check_credentials.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,7 @@ class CheckCredentials < Base
def call(env)
# We use a harmless API call to check if the authentication will
# work.
begin
response = env['barge'].droplet.all({:per_page =>'1', :page =>'1'})
rescue Faraday::ClientError => e
say "Authentication with DigitalOcean failed at an early stage"
say "Error was: #{e}"
exit 1
end

unless response.success?
say "Failed to connect to DigitalOcean. Reason given from API: #{response.id} - #{response.message}", :red
exit 1
else
say "Authentication with DigitalOcean was successful.", :green
end
verify_credentials(env['barge'], true)

@app.call(env)
end
Expand Down
2 changes: 2 additions & 0 deletions lib/tugboat/middleware/list_droplets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class ListDroplets < Base
def call(env)
ocean = env['barge']

verify_credentials(ocean)

droplet_list = get_droplet_list ocean

has_one = false
Expand Down
1 change: 0 additions & 1 deletion lib/tugboat/middleware/ssh_droplet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ def call(env)
end

host_string = "#{ssh_user}@#{host_ip}"

say "Attempting SSH: #{host_string}"

options << host_string
Expand Down
22 changes: 19 additions & 3 deletions spec/cli/destroy_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

describe "destroy" do
it "destroys a droplet with a fuzzy name" 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/droplets?page=1&per_page=200").
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 => {})
Expand Down Expand Up @@ -43,6 +47,10 @@


it "destroys a droplet with a name" 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/droplets?page=1&per_page=200").
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 => {})
Expand All @@ -62,6 +70,10 @@
end

it "destroys a droplet with confirm flag set" 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/droplets?page=1&per_page=200").
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 => {})
Expand All @@ -80,9 +92,13 @@
end

it "does not destroy a droplet if no is chosen" do
stub_request(:get, "https://api.digitalocean.com/v2/droplets?page=1&per_page=200").
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/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/droplets?page=1&per_page=200").
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 => {})

$stdin.should_receive(:gets).and_return("n")

Expand Down
20 changes: 20 additions & 0 deletions spec/cli/droplets_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

describe "droplets" do
it "shows a list when droplets 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/droplets?page=1&per_page=200").
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 => {'Content-Type' => 'application/json'},)
Expand All @@ -21,6 +25,10 @@
end

it "returns an error message when no droplets 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/droplets?page=1&per_page=200").
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_empty"), :headers => {'Content-Type' => 'application/json'},)
Expand All @@ -36,6 +44,10 @@
end

it "shows no output when --quiet is set" 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/droplets?page=1&per_page=200").
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 => {'Content-Type' => 'application/json'},)
Expand All @@ -50,6 +62,10 @@
end

it "includes urls when --include-urls is set" 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/droplets?page=1&per_page=200").
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 => {'Content-Type' => 'application/json'},)
Expand All @@ -67,6 +83,10 @@
end

it "paginates when multiple pages are returned" 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/droplets?page=1&per_page=200").
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_paginated_first"), :headers => {'Content-Type' => 'application/json'},)
Expand Down
20 changes: 20 additions & 0 deletions spec/cli/halt_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

describe "halt" do
it "halts a droplet with a fuzzy name" 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/droplets?page=1&per_page=200").
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 => {})
Expand All @@ -23,6 +27,10 @@
end

it "halts a droplet hard when the hard option is used" 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/droplets?page=1&per_page=200").
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 => {})
Expand All @@ -42,6 +50,10 @@
end

it "halts a droplet with an id" 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/droplets/?per_page=200").
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 => {'Content-Type' => 'application/json'},)
Expand All @@ -66,6 +78,10 @@


it "halts a droplet with a name" 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/droplets?page=1&per_page=200").
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 => {'Content-Type' => 'application/json'},)
Expand All @@ -86,6 +102,10 @@


it "does not halt a droplet that is off" 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/droplets?page=1&per_page=200").
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 => {'Content-Type' => 'application/json'},)
Expand Down
Loading