Skip to content

Commit

Permalink
Merge pull request #55 from pearkes/pm_custom_faraday
Browse files Browse the repository at this point in the history
Custom faraday parameters and verify method added
  • Loading branch information
petems committed Aug 9, 2013
2 parents 5bf54db + 81252a6 commit 80be81b
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 4 deletions.
9 changes: 9 additions & 0 deletions lib/tugboat/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ def authorize
Middleware.sequence_authorize.call({})
end

desc "verify", "Check your DigitalOcean credentials"
long_desc "This tests that your credentials created by the \`authorize\`
command that are stored in ~/.tugboat are correct and allow you to connect
to the API without errors.
"
def verify
Middleware.sequence_verify.call({})
end

desc "droplets", "Retrieve a list of your droplets"
def droplets
Middleware.sequence_list_droplets.call({})
Expand Down
10 changes: 10 additions & 0 deletions lib/tugboat/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ def self.sequence_authorize
end
end

# This checks that the credentials in ~/.tugboat are valid
def self.sequence_verify
::Middleware::Builder.new do
use InjectConfiguration
use CheckConfiguration
use InjectClient
use CheckCredentials
end
end

# Display a list of droplets
def self.sequence_list_droplets
::Middleware::Builder.new do
Expand Down
37 changes: 37 additions & 0 deletions lib/tugboat/middleware/authentication_middleware.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Tugboat
class AuthenticationMiddleware < Faraday::Middleware
extend Forwardable
def_delegators :'Faraday::Utils', :parse_query, :build_query
RED = "\e[31m"
CLEAR = "\e[0m"

def initialize(app, client_id, api_key)
@client_id = client_id
@api_key = api_key

super(app)
end

def call(env)
params = { 'client_id' => @client_id, 'api_key' => @api_key }.update query_params(env[:url])

env[:url].query = build_query params

begin
@app.call(env)
rescue Faraday::Error::ClientError => e
puts "#{RED} Authentication with DigitalOcean failed (#{e})"
puts "#{CLEAR} Check your API keys and run `tugboat authorize` to re-enter them if needed"
exit 1
end
end

def query_params(url)
if url.query.nil? or url.query.empty?
{}
else
parse_query url.query
end
end
end
end
2 changes: 1 addition & 1 deletion lib/tugboat/middleware/check_credentials.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def call(env)
# work.
begin
env["ocean"].droplets.list
rescue Faraday::Error::ParsingError
rescue Faraday::Error::ClientError => e
say "Authentication with DigitalOcean failed. Run `tugboat authorize`", :red
exit 1
end
Expand Down
24 changes: 21 additions & 3 deletions lib/tugboat/middleware/inject_client.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
require 'digital_ocean'
require File.expand_path('../authentication_middleware', __FILE__)

module Tugboat
module Middleware
# Inject the digital ocean client into the environment
class InjectClient < Base

def tugboat_faraday
Faraday.new(:url => 'https://api.digitalocean.com/') do |faraday|
faraday.use AuthenticationMiddleware, @client_id, @api_key
faraday.use Faraday::Response::RaiseError
faraday.request :url_encoded
faraday.response :rashify
faraday.response :json
faraday.response(:logger) if ENV['DEBUG']
faraday.adapter Faraday.default_adapter
end
end

def call(env)
# Sets the digital ocean client into the environment for use
# later.
@client_id = env["config"].client_key
@api_key = env["config"].api_key

env["ocean"] = DigitalOcean::API.new \
:client_id => env["config"].client_key,
:api_key => env["config"].api_key,
:debug => ENV['DEBUG'] || false
:client_id => @client_id,
:api_key => @api_key,
:faraday => tugboat_faraday

@app.call(env)
end
Expand Down
26 changes: 26 additions & 0 deletions spec/cli/verify_cli_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'spec_helper'

describe Tugboat::CLI do
include_context "spec"

describe "verify" do
it "returns confirmation text when verify passes" do
stub_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200)
@cli.verify
expect($stdout.string).to eq "Authentication with DigitalOcean was successful.\n"
expect(a_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made
end

it "returns error string when verify fails" do
stub_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 401, :body => '{"status":"ERROR", "message":"Access Denied"}')
expect { @cli.resize("verify") }.to raise_error(SystemExit)
expect($stdout.string).to eq "Droplet fuzzy name provided. Finding droplet ID...\e[31m Authentication with DigitalOcean failed (the server responded with status 401)\n\e[0m Check your API keys and run `tugboat authorize` to re-enter them if needed\n"
expect(a_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made
end

end

end

0 comments on commit 80be81b

Please sign in to comment.