Skip to content

Commit

Permalink
Switch from SoftLayer::Service to SoftLayer::Client to add support fo…
Browse files Browse the repository at this point in the history
…r API timeout, bump softlayer_api required version to 2.2.0 or newer
  • Loading branch information
ju2wheels committed Oct 1, 2014
1 parent d8094b8 commit 871d5dd
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 48 deletions.
3 changes: 3 additions & 0 deletions QUICKSTART.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ Vagrant.configure(2) do |config|
#Note: If you use SL_GENERIC box you must set sl.image_guid or sl.operating_system/sl.dis_capacity, otherwise it is pre-set for you by the box
sl.api_key = ENV["SL_API_KEY"]
#sl.api_timeout = 60
#sl.datacenter = nil #Use first available
#sl.dedicated = false
#sl.disk_capacity = { 0 => 25 } # { disk_num => capacity_gb }, disk 1 reserved for swap by SoftLayer dont use
Expand All @@ -321,6 +322,8 @@ Vagrant.configure(2) do |config|
#sl.operating_system = "SL_CENTOS_6_64" #Set in box, here for override or if you used SL_GENERIC, see contrib/vagrant-softlayer-boxes of vagrant-softlayer plugin for box generator
#sl.post_install = nil #URL for post install script
#sl.private_only = false
#sl.provision_timeout = 1200
#sl.rebuild_timeout = 1200
sl.ssh_keys = [ "SL-root-pk" ]
#sl.start_cpus = 1
#sl.transaction_wait = true
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ Parameter | Description

Parameter | Description | Default | Required
------------------- | ------------------------------------------| ----------------| --------
`api_timeout` | SoftLayer API call timeout | 60 | no
`provision_timeout` | Provisioning wait timeout in seconds | 1200 | no
`rebuild_timeout` | Rebuild wait timeout in seconds | 1200 | no
`transaction_wait` | Wait for order transactions to complete | true | no
Expand Down
8 changes: 5 additions & 3 deletions contrib/vagrant-softlayer-boxes
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ $sl = {
:sl_credentials => {
:api_key => nil,
:endpoint_url => SoftLayer::API_PUBLIC_ENDPOINT,
:timeout => 60,
:user_agent => "vagrant-softlayer",
:username => nil
}
}
Expand Down Expand Up @@ -255,9 +257,9 @@ end

def getBoxData()
begin
$sl[:service][:virtual_guest] = SoftLayer::Service.new("SoftLayer_Virtual_Guest", $sl[:sl_credentials]) if $boxes[:templates]
$sl[:service][:account] = SoftLayer::Service.new("SoftLayer_Account", $sl[:sl_credentials]) if $boxes[:images][:private]
$sl[:service][:virtual_guest_block_device_template_group] = SoftLayer::Service.new("SoftLayer_Virtual_Guest_Block_Device_Template_Group", $sl[:sl_credentials]) if $boxes[:images][:public]
$sl[:service][:virtual_guest] = SoftLayer::Client.new($sl[:sl_credentials])["SoftLayer_Virtual_Guest"] if $boxes[:templates]
$sl[:service][:account] = SoftLayer::Client.new($sl[:sl_credentials])["SoftLayer_Account"] if $boxes[:images][:private]
$sl[:service][:virtual_guest_block_device_template_group] = SoftLayer::Client.new($sl[:sl_credentials])["SoftLayer_Virtual_Guest_Block_Device_Template_Group"] if $boxes[:images][:public]
rescue Exception => e
$stderr.puts "ERROR: Failed to create SoftLayer service object: " + e.message
exit 1
Expand Down
36 changes: 23 additions & 13 deletions contrib/vagrant-softlayer-vlans
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ require 'rubygems'
require 'softlayer_api'

class SoftLayerVlans
def initialize(sl_api_key, sl_username, sl_endpoint_url = SoftLayer::API_PUBLIC_ENDPOINT)
def initialize(sl_api_key, sl_username, sl_endpoint_url = SoftLayer::API_PUBLIC_ENDPOINT, sl_timeout = 60)
if ! [ SoftLayer::API_PUBLIC_ENDPOINT, SoftLayer::API_PRIVATE_ENDPOINT ].include?(sl_endpoint_url)
raise ArgumentError, "Invalid SoftLayer Endpoint URL provided, expected one of #{ [ SoftLayer::API_PUBLIC_ENDPOINT, SoftLayer::API_PRIVATE_ENDPOINT ].inspect }: #{ sl_endpoint_url.inspect }"
end

@sl_credentials = {
:api_key => sl_api_key,
:endpoint_url => sl_endpoint_url,
:timeout => sl_timeout,
:user_agent => "vagrant-softlayer",
:username => sl_username
}

begin
@sl_services = {
:account => SoftLayer::Service.new("SoftLayer_Account", @sl_credentials)
:account => SoftLayer::Client.new( @sl_credentials)["SoftLayer_Account"]
}
rescue Exception => e
raise Exception, "Failed to initialize SoftLayer Account service for retrieving network vlan details: #{ e.message }"
Expand Down Expand Up @@ -110,6 +112,7 @@ class VagrantSoftLayerVlans
[ '--help', '-h', GetoptLong::NO_ARGUMENT ],
[ '--sl_api_key', '-k', GetoptLong::REQUIRED_ARGUMENT ],
[ '--sl_endpoint_url', '-e', GetoptLong::REQUIRED_ARGUMENT ],
[ '--sl_timeout', '-t', GetoptLong::REQUIRED_ARGUMENT ],
[ '--sl_username', '-u', GetoptLong::REQUIRED_ARGUMENT ],
[ '--vlan_name', '-v', GetoptLong::REQUIRED_ARGUMENT ],
[ '--vlan_space', '-s', GetoptLong::REQUIRED_ARGUMENT ]
Expand All @@ -129,6 +132,7 @@ class VagrantSoftLayerVlans
:sl_credentials => {
:api_key => nil,
:endpoint_url => SoftLayer::API_PUBLIC_ENDPOINT,
:timeout => 60,
:username => nil
},
:vlan_name => nil,
Expand All @@ -137,30 +141,33 @@ class VagrantSoftLayerVlans
@help = <<-EOF
vagrant-softlayer-vlans [OPTION]
--datacenter_name, -d DCNAME,...:
--datacenter_name|-d DCNAME,...:
Comma separated list of datacenter names to filter vlan results on.
--format, -f FORMAT:
--format|-f FORMAT:
Sets the output format of the vlan results. Acceptable values are 'csv', 'json', 'perty', 'pretty', or 'raw'.
Defaults to 'pretty'.
--help, -h:
--help|-h:
Print this help.
--sl_username USERNAME, -u USERNAME:
Sets the SoftLayer account user name. If not specified, it is assumed SL_API_USERNAME environment variable is set.
--sl_api_key SL_API_KEY, -k SL_API_KEY:
--sl_api_key|-k SL_API_KEY
Sets the SoftLayer API key. If not specified, it is assumed SL_API_KEY environment variable is set.
--sl_endpoint_url SL_API_BASE_URL, -e SL_API_BASE_URL:
--sl_endpoint_url|-e SL_API_BASE_URL
Sets the SoftLayer endpoint URL. If not specified, it assumed SL_API_BASE_URL environment variable is set to API_PUBLIC_ENDPOINT or API_PRIVATE_ENDPOINT.
Defaults to API_PUBLIC_ENDPOINT.
--vlan_name, -v VLAN_NAME,...:
--sl_timeout|-t SL_TIMEOUT
Sets the SoftLayer API call timeout value in seconds.
--sl_username|-u USERNAME
Sets the SoftLayer account user name. If not specified, it is assumed SL_API_USERNAME environment variable is set.
--vlan_name|-v VLAN_NAME,...
Comma separated list of vlan names to filter vlan results on. A vlan name can be the vlan name or qualified name.
--vlan_space, -s VLAN_SPACE:
--vlan_space|-s VLAN_SPACE
Sets the vlan space to filter vlan results on. Acceptable spaces are 'public' or 'private'.
EOF
Expand All @@ -170,7 +177,7 @@ EOF
proc_cli_options

begin
sl_vlans = SoftLayerVlans.new(@config[:sl_credentials][:api_key], @config[:sl_credentials][:username], @config[:sl_credentials][:endpoint_url])
sl_vlans = SoftLayerVlans.new(@config[:sl_credentials][:api_key], @config[:sl_credentials][:username], @config[:sl_credentials][:endpoint_url], @config[:sl_credentials][:timeout])
rescue Exception => e
$stderr.puts "ERROR: Failed to instantiate SoftLayerVlans instance: #{ e.message }"
exit 1
Expand Down Expand Up @@ -221,6 +228,9 @@ EOF

@config[:sl_credentials][:endpoint_url] = (optval.to_s.upcase == 'API_PUBLIC_ENDPOINT' ? SoftLayer::API_PUBLIC_ENDPOINT : SoftLayer::API_PRIVATE_ENDPOINT)

when '--sl_timeoout'
@config[:sl_credentials][:timeout] = optval.to_i

when '--sl_username'
@config[:sl_credentials][:username] = optval.to_s

Expand Down
6 changes: 3 additions & 3 deletions lib/vagrant-softlayer/action/create_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def call(env)

@env[:ui].info I18n.t("vagrant_softlayer.vm.creating")

sl_warden { env[:sl_product_order].verifyOrder(env[:sl_virtual_guest].generateOrderTemplate(order_template)) }
sl_warden { env[:sl_client]["SoftLayer_Product_Order"].verifyOrder(env[:sl_client]["SoftLayer_Virtual_Guest"].generateOrderTemplate(order_template)) }

result = sl_warden { env[:sl_virtual_guest].createObject(order_template) }
result = sl_warden { env[:sl_client]["SoftLayer_Virtual_Guest"].createObject(order_template) }
@env[:machine].id = result["id"].to_s

@app.call(@env)
Expand All @@ -30,7 +30,7 @@ def get_hostname
def get_vlan_id(vlan_name, vlan_space)
return vlan_name unless vlan_name.kind_of?(String)

routers = @env[:sl_account].object_mask("mask[routers,routers.datacenter,routers.networkVlans,routers.networkVlans.networkSpace,routers.networkVlans.type]").getObject["routers"]
routers = @env[:sl_client]["SoftLayer_Account"].object_mask("mask[routers,routers.datacenter,routers.networkVlans,routers.networkVlans.networkSpace,routers.networkVlans.type]").getObject["routers"]

routers.each do |router|
next if @env[:machine].provider_config.datacenter && router["datacenter"]["name"] != @env[:machine].provider_config.datacenter
Expand Down
8 changes: 4 additions & 4 deletions lib/vagrant-softlayer/action/setup_softlayer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ def call(env)
env[:sl_credentials] = {
:api_key => env[:machine].provider_config.api_key,
:endpoint_url => env[:machine].provider_config.endpoint_url,
:timeout => env[:machine].provider_config.api_timeout,
:user_agent => "vagrant-softlayer",
:username => env[:machine].provider_config.username
}

env[:sl_account] = ::SoftLayer::Service.new("SoftLayer_Account", env[:sl_credentials])
env[:sl_product_order] = ::SoftLayer::Service.new("SoftLayer_Product_Order", env[:sl_credentials])
env[:sl_virtual_guest] = ::SoftLayer::Service.new("SoftLayer_Virtual_Guest", env[:sl_credentials])
env[:sl_client] = ::SoftLayer::Client.new(env[:sl_credentials])

unless env[:machine].id.nil? || env[:machine].id.empty?
env[:sl_machine] = env[:sl_virtual_guest].object_with_id(env[:machine].id.to_i)
env[:sl_machine] = env[:sl_client]["SoftLayer_Virtual_Guest"].object_with_id(env[:machine].id.to_i)
end

# Carry on
Expand Down
4 changes: 2 additions & 2 deletions lib/vagrant-softlayer/action/update_dns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def update_dns
end

# Lookup the DNS zone
zone = ::SoftLayer::Service.new("SoftLayer_Dns_Domain", @env[:sl_credentials])
zone = @env[:sl_client]["SoftLayer_Dns_Domain"]
domain = @env[:machine].provider_config.domain

@logger.debug("Looking for #{domain} zone into the SoftLayer zone list.")
Expand All @@ -80,7 +80,7 @@ def update_dns
@logger.debug("Found DNS zone: #{@dns_zone.inspect}")

# Add or remove the resource record
@resource = ::SoftLayer::Service.new("SoftLayer_Dns_Domain_ResourceRecord", @env[:sl_credentials])
@resource = @env[:sl_client]["SoftLayer_Dns_Domain_ResourceRecord"]
case @env[:machine_action]
when :up
add_record unless self.dns_id
Expand Down
10 changes: 1 addition & 9 deletions lib/vagrant-softlayer/action/wait_for_provision.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,7 @@ def initialize(app, env)
def call(env)
env[:ui].info I18n.t("vagrant_softlayer.vm.wait_for_provision")

virtual_guest_object_mask = [
"activeTransaction.id",
"activeTransaction.transactionStatus.friendlyName",
"activeTransaction.transactionStatus.name",
"lastOperatingSystemReload.id",
"provisionDate"
]

env[:sl_machine] = env[:sl_virtual_guest].object_with_id(env[:machine].id.to_i)
env[:sl_machine] = env[:sl_client]["SoftLayer_Virtual_Guest"].object_with_id(env[:machine].id.to_i)

retry_msg = lambda { @logger.debug("Object not found, retrying in 10 seconds.") }

Expand Down
7 changes: 7 additions & 0 deletions lib/vagrant-softlayer/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ class Config < Vagrant.plugin("2", :config)
# The API key to access SoftLayer.
attr_accessor :api_key

# Th SoftLayer API request timeout value in seconds
attr_accessor :api_timeout

# The endpoint SoftLayer API url.
attr_accessor :endpoint_url

Expand Down Expand Up @@ -86,6 +89,7 @@ class Config < Vagrant.plugin("2", :config)

def initialize
@api_key = UNSET_VALUE
@api_timeout = UNSET_VALUE
@endpoint_url = UNSET_VALUE
@username = UNSET_VALUE

Expand Down Expand Up @@ -155,6 +159,9 @@ def finalize!
@api_key = ENV["SL_API_KEY"] if @api_key == UNSET_VALUE
@username = ENV["SL_USERNAME"] if @username == UNSET_VALUE

# Th SoftLayer API request timeout value in seconds
@api_timeout = 60 if @api_timeout == UNSET_VALUE

# Endpoint url defaults to public SoftLayer API url.
@endpoint_url = API_PUBLIC_ENDPOINT if @endpoint_url == UNSET_VALUE

Expand Down
11 changes: 2 additions & 9 deletions lib/vagrant-softlayer/util/load_balancer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ def rebalance!
def setup
# A plethora of service objects is required for managing
# load balancers. We instanciate'em all here.
@services = {
"Account" => ::SoftLayer::Service.new("SoftLayer_Account", @env[:sl_credentials])
}
@services = { "Account" => @env[:sl_client]["SoftLayer_Account"] }
[
"Health_Check_Type",
"Routing_Method",
Expand All @@ -73,12 +71,7 @@ def setup
"Service_Group",
"VirtualIpAddress",
"VirtualServer"
].each do |service|
@services[service] = ::SoftLayer::Service.new(
"SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_#{service}",
@env[:sl_credentials]
)
end
].each { |service| @services[service] = @env[:sl_client]["SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_#{service}"] }

# We create enumerations for the various configurables.
@enums = {}
Expand Down
2 changes: 1 addition & 1 deletion lib/vagrant-softlayer/util/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def ip_address_record(env)
# returned (this latter case is needed instead for creating
# an instance).
def ssh_keys(env, ids_only = false)
account = ::SoftLayer::Service.new("SoftLayer_Account", env[:sl_credentials])
account = env[:sl_client]["SoftLayer_Account"]
acc_keys = sl_warden { account.object_mask("id", "label").getSshKeys }
key_ids = []
Array(env[:machine].provider_config.ssh_key).each do |key|
Expand Down
8 changes: 5 additions & 3 deletions spec/vagrant-softlayer/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
end

its("api_key") { should be_nil }
its("api_timeout") { should eq 60 }
its("endpoint_url") { should eq VagrantPlugins::SoftLayer::API_PUBLIC_ENDPOINT }
its("username") { should be_nil }

Expand Down Expand Up @@ -56,7 +57,7 @@
end

context "integers" do
[:max_memory, :network_speed, :provision_timeout, :rebuild_timeout, :ssh_key, :start_cpus, :vlan_private, :vlan_public].each do |attribute|
[:api_timeout, :max_memory, :network_speed, :provision_timeout, :rebuild_timeout, :ssh_key, :start_cpus, :vlan_private, :vlan_public].each do |attribute|
it "should not default #{attribute} if overridden" do
config.send("#{attribute}=".to_sym, 999)
config.finalize!
Expand Down Expand Up @@ -131,8 +132,9 @@
describe "validation" do
before :each do
# Setup some good configuration values
config.api_key = "An API key"
config.username = "An username"
config.api_key = "An API key"
config.api_timeout = 60
config.username = "An username"

config.datacenter = "ams01"
config.dedicated = false
Expand Down
2 changes: 1 addition & 1 deletion vagrant-softlayer.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_path = "lib"

spec.add_dependency "softlayer_api", "~> 1.0"
spec.add_dependency "softlayer_api", ">= 2.2.0"

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
Expand Down

0 comments on commit 871d5dd

Please sign in to comment.