Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
Merge pull request #32 from rackspace/auth_support
Browse files Browse the repository at this point in the history
Updated to support multiple authentication endpoints.
  • Loading branch information
Kyle Rames committed Sep 5, 2013
2 parents c7a5c4c + 55975e7 commit 06318ce
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 23 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ This provider exposes quite a few provider-specific configuration options:
exact ID or name of the image, or this can be a regular expression to
partially match some image.
* `rackspace_region` - The region to hit. By default this is :dfw. Valid options are:
:dfw, :ord, :lon. User this OR rackspace_compute_url
:dfw, :ord, :lon, :iad, :syd. Users should preference using this setting over `rackspace_compute_url` setting.
* `rackspace_compute_url` - The compute_url to hit. This is good for custom endpoints.
Use this OR rackspace_region.
* `rackspace_auth_url` - The endpoint to authentication against. By default, vagrant will use the global
rackspace authentication endpoint for all regions with the exception of :lon. IF :lon region is specified
vagrant will authenticate against the UK authentication endpoint.
* `public_key_path` - The path to a public key to initialize with the remote
server. This should be the matching pair for the private key configured
with `config.ssh.private_key_path` on Vagrant.
Expand Down
34 changes: 16 additions & 18 deletions lib/vagrant-rackspace/action/connect_rackspace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,23 @@ def call(env)
api_key = config.api_key
username = config.username

if config.rackspace_compute_url.nil? then
@logger.info("Connecting to Rackspace region...")
env[:rackspace_compute] = Fog::Compute.new({
:provider => :rackspace,
:version => :v2,
:rackspace_api_key => api_key,
:rackspace_region => config.rackspace_region,
:rackspace_username => username
})
params = {
:provider => :rackspace,
:version => :v2,
:rackspace_api_key => api_key,
:rackspace_username => username,
:rackspace_auth_url => config.rackspace_auth_url
}

if config.rackspace_compute_url
@logger.info("Connecting to Rackspace compute_url...")
params[:rackspace_compute_url] = config.rackspace_compute_url
else
@logger.info("Connecting to Rackspace compute_url...")
env[:rackspace_compute] = Fog::Compute.new({
:provider => :rackspace,
:version => :v2,
:rackspace_api_key => api_key,
:rackspace_compute_url => config.rackspace_compute_url,
:rackspace_username => username
})
end
@logger.info("Connecting to Rackspace region...")
params[:rackspace_region] = config.rackspace_region
end

env[:rackspace_compute] = Fog::Compute.new params

@app.call(env)
end
Expand Down
30 changes: 27 additions & 3 deletions lib/vagrant-rackspace/config.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "vagrant"
require "fog"

module VagrantPlugins
module Rackspace
Expand All @@ -14,7 +15,7 @@ class Config < Vagrant.plugin("2", :config)
#
# expected to be a symbol - :dfw (default), :ord, :lon
#
# use this OR rackspace_compute_url
# Users should preference the rackspace_region setting over rackspace_compute_url
attr_accessor :rackspace_region

# The compute_url to access RackSpace. If nil, it will default
Expand All @@ -31,9 +32,14 @@ class Config < Vagrant.plugin("2", :config)
# Fog::Compute::RackspaceV2::ORD_ENDPOINT
# Fog::Compute::RackspaceV2::LON_ENDPOINT
#
# use this OR rackspace_region
# Users should preference the rackspace_region setting over rackspace_compute_url
attr_accessor :rackspace_compute_url

# The authenication endpoint. This defaults to Rackspace's global authentication endpoint.
# Users of the London data center should specify the following:
# https://lon.identity.api.rackspacecloud.com/v2.0
attr_writer :rackspace_auth_url

# The flavor of server to launch, either the ID or name. This
# can also be a regular expression to partially match a name.
attr_accessor :flavor
Expand Down Expand Up @@ -67,6 +73,7 @@ def initialize
@api_key = UNSET_VALUE
@rackspace_region = UNSET_VALUE
@rackspace_compute_url = UNSET_VALUE
@rackspace_auth_url = UNSET_VALUE
@flavor = UNSET_VALUE
@image = UNSET_VALUE
@public_key_path = UNSET_VALUE
Expand All @@ -79,9 +86,10 @@ def finalize!
@api_key = nil if @api_key == UNSET_VALUE
@rackspace_region = nil if @rackspace_region == UNSET_VALUE
@rackspace_compute_url = nil if @rackspace_compute_url == UNSET_VALUE
@rackspace_auth_url = nil if @rackspace_auth_url == UNSET_VALUE
@flavor = /512MB/ if @flavor == UNSET_VALUE
@image = /Ubuntu/ if @image == UNSET_VALUE
@rackconnect = false if @rackconnect == UNSET_VALUE
@rackconnect = nil if @rackconnect == UNSET_VALUE
@server_name = nil if @server_name == UNSET_VALUE
@username = nil if @username == UNSET_VALUE

Expand All @@ -90,6 +98,16 @@ def finalize!
end
end

# @note Currently, you must authenticate against the UK authenication endpoint to access the London Data center.
# Hopefully this method makes the experience more seemless for users of the UK cloud.
def rackspace_auth_url
if (@rackspace_auth_url.nil? || @rackspace_auth_url == UNSET_VALUE) && lon_region?
Fog::Rackspace::UK_AUTH_ENDPOINT
else
@rackspace_auth_url
end
end

def validate(machine)
errors = []

Expand All @@ -103,6 +121,12 @@ def validate(machine)

{ "RackSpace Provider" => errors }
end

private

def lon_region?
rackspace_region && rackspace_region != UNSET_VALUE && rackspace_region.to_sym == :lon
end
end
end
end
52 changes: 52 additions & 0 deletions spec/vagrant-rackspace/config_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "vagrant-rackspace/config"
require 'fog'

describe VagrantPlugins::Rackspace::Config do
describe "defaults" do
Expand All @@ -13,6 +14,7 @@
its(:api_key) { should be_nil }
its(:rackspace_region) { should be_nil }
its(:rackspace_compute_url) { should be_nil }
its(:rackspace_auth_url) { should be_nil }
its(:flavor) { should eq(/512MB/) }
its(:image) { should eq(/Ubuntu/) }
its(:public_key_path) { should eql(vagrant_public_key) }
Expand All @@ -25,6 +27,7 @@
[:api_key,
:rackspace_region,
:rackspace_compute_url,
:rackspace_auth_url,
:flavor,
:image,
:public_key_path,
Expand Down Expand Up @@ -66,4 +69,53 @@
it "should error if not given"
end
end

describe "rackspace_auth_url" do
it "should return UNSET_VALUE if rackspace_auth_url and rackspace_region are UNSET" do
subject.rackspace_auth_url.should == VagrantPlugins::Rackspace::Config::UNSET_VALUE
end
it "should return UNSET_VALUE if rackspace_auth_url is UNSET and rackspace_region is :ord" do
subject.rackspace_region = :ord
subject.rackspace_auth_url.should == VagrantPlugins::Rackspace::Config::UNSET_VALUE
end
it "should return UK Authentication endpoint if rackspace_auth_url is UNSET and rackspace_region is :lon" do
subject.rackspace_region = :lon
subject.rackspace_auth_url.should == Fog::Rackspace::UK_AUTH_ENDPOINT
end
it "should return custom endpoint if supplied and rackspace_region is :lon" do
my_endpoint = 'http://custom-endpoint.com'
subject.rackspace_region = :lon
subject.rackspace_auth_url = my_endpoint
subject.rackspace_auth_url.should == my_endpoint
end
it "should return custom endpoint if supplied and rackspace_region is UNSET" do
my_endpoint = 'http://custom-endpoint.com'
subject.rackspace_auth_url = my_endpoint
subject.rackspace_auth_url.should == my_endpoint
end
end


describe "lon_region?" do
it "should return false if rackspace_region is UNSET_VALUE" do
subject.rackspace_region = VagrantPlugins::Rackspace::Config::UNSET_VALUE
subject.send(:lon_region?).should be_false
end
it "should return false if rackspace_region is nil" do
subject.rackspace_region = nil
subject.send(:lon_region?).should be_false
end
it "should return false if rackspace_region is :ord" do
subject.rackspace_region = :ord
subject.send(:lon_region?).should be_false
end
it "should return true if rackspace_region is 'lon'" do
subject.rackspace_region = 'lon'
subject.send(:lon_region?).should be_true
end
it "should return true if rackspace_Region is :lon" do
subject.rackspace_region = :lon
subject.send(:lon_region?).should be_true
end
end
end

0 comments on commit 06318ce

Please sign in to comment.