Skip to content

Commit 47a994b

Browse files
committed
Added the ability to set a default client so that you can make calls without having to provide it repeatedly
resolves #22
1 parent 22d1438 commit 47a994b

26 files changed

Lines changed: 376 additions & 112 deletions

examples/account_info.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
$LOAD_PATH << File.join(File.dirname(__FILE__), "../lib" )
2-
31
#
42
# Copyright (c) 2014 SoftLayer Technologies, Inc. All rights reserved.
53
#

examples/account_servers.rb

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
$LOAD_PATH << File.join(File.dirname(__FILE__), "../lib" )
2-
31
#
42
# Copyright (c) 2014 SoftLayer Technologies, Inc. All rights reserved.
53
#
@@ -25,29 +23,26 @@
2523
require 'rubygems'
2624
require 'softlayer_api'
2725
require 'pp'
28-
29-
begin
30-
client = SoftLayer::Client.new(
26+
27+
# We can set the default client to be our client and that way
28+
# we can avoid supplying it later
29+
SoftLayer::Client.default_client = SoftLayer::Client.new(
3130
# :username => "joecustomer" # enter your username here
3231
# :api_key => "feeddeadbeefbadf00d..." # enter your api key here
3332
)
34-
35-
account = SoftLayer::Account.account_for_client(client)
36-
33+
34+
account = SoftLayer::Account.account_for_client()
35+
3736
# grab a list of all the servers on the account.
3837
servers = account.servers
39-
38+
4039
# measure their fully qualified domain names so we can print a pretty table
4140
max_name_len = servers.inject(0) { |max_name, server| [max_name, server.fullyQualifiedDomainName.length].max }
4241

4342
printf "%#{-max_name_len}s\tPrimary Public IP\n", "Server FQDN"
4443
printf "%#{-max_name_len}s\t-----------------\n", "-----------"
4544

46-
servers.each do |server|
45+
servers.each do |server|
4746
ip_field = server.primary_public_ip ? server.primary_public_ip : "No Public Interface"
4847
printf "%#{-max_name_len}s\t#{ip_field}\n", server.fullyQualifiedDomainName
49-
end
50-
51-
rescue
52-
puts "An unexpected exception occurred!"
53-
end
48+
end

examples/order_bare_metal_package.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def tl_dr_version
5353
config_options['server'] = 1417 # price id of Quad Processor Quad Core Intel 7420 - 2.13GHz (Dunnington) - 4 x 6MB / 8MB cache
5454

5555
# With all the config options in place we can now construct the product order.
56-
server_order = SoftLayer::BareMetalServerOrder_Package.new(client, quad_intel_package)
56+
server_order = SoftLayer::BareMetalServerOrder_Package.new(quad_intel_package, client)
5757
server_order.location = 'sng01'
5858
server_order.hostname = 'sample'
5959
server_order.domain = 'softlayerapi.org'
@@ -132,7 +132,7 @@ def tl_dr_version
132132
quad_intel_package.datacenter_options.each { |location| puts "\t#{location}"}
133133

134134
# With all the config options in place we can now construct the product order.
135-
server_order = SoftLayer::BareMetalServerOrder_Package.new(client, quad_intel_package)
135+
server_order = SoftLayer::BareMetalServerOrder_Package.new(quad_intel_package, client)
136136
server_order.datacenter = 'sng01'
137137
server_order.hostname = 'sample'
138138
server_order.domain = 'softlayerapi.org'

examples/order_bare_metal_server.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
$LOAD_PATH << File.join(File.dirname(__FILE__), "../lib")
2-
31
#
42
# Copyright (c) 2014 SoftLayer Technologies, Inc. All rights reserved.
53
#
@@ -55,7 +53,7 @@ def tl_dr_version
5553
config_options['server'] = 1417 # price id of Quad Processor Quad Core Intel 7420 - 2.13GHz (Dunnington) - 4 x 6MB / 8MB cache
5654

5755
# With all the config options in place we can now construct the product order.
58-
server_order = SoftLayer::BareMetalServerOrder_Package.new(client, quad_intel_package)
56+
server_order = SoftLayer::BareMetalServerOrder_Package.new(quad_intel_package, client)
5957
server_order.location = 'FIRST_AVAILABLE'
6058
server_order.hostname = 'sample'
6159
server_order.domain = 'softlayer_api.org'
@@ -136,7 +134,7 @@ def tl_dr_version
136134
quad_intel_package.locations.each { |location| printf "%#{max_key_length}s\t\t#{location[:description]}\n",location[:keyname]}
137135

138136
# With all the config options in place we can now construct the product order.
139-
server_order = SoftLayer::BareMetalServerOrder_Package.new(client, quad_intel_package)
137+
server_order = SoftLayer::BareMetalServerOrder_Package.new(quad_intel_package, client)
140138
server_order.location = 'FIRST_AVAILABLE'
141139
server_order.hostname = 'sample'
142140
server_order.domain = 'softlayer_api.org'

examples/order_virtual_server.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
$LOAD_PATH << File.join(File.dirname(__FILE__), "../lib")
2-
31
#
42
# Copyright (c) 2014 SoftLayer Technologies, Inc. All rights reserved.
53
#

lib/softlayer/APIParameterFilter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def object_filter(filter)
129129
##
130130
# A utility method that returns the server object ID (if any) stored
131131
# in this parameter set.
132-
def server_object_id
132+
def server_object_id
133133
self.parameters[:server_object_id]
134134
end
135135

lib/softlayer/Account.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class Account < SoftLayer::ModelBase
9191

9292
bare_metal.to_update do
9393
@last_bare_metal_update = Time.now
94-
BareMetalServer.find_servers(self.softlayer_client)
94+
BareMetalServer.find_servers(:client => self.softlayer_client)
9595
end
9696
end
9797

@@ -109,7 +109,7 @@ class Account < SoftLayer::ModelBase
109109

110110
virtual_servers.to_update do
111111
@last_virtual_server_update = Time.now
112-
VirtualServer.find_servers(self.softlayer_client)
112+
VirtualServer.find_servers(:client => self.softlayer_client)
113113
end
114114
end
115115

@@ -140,12 +140,15 @@ class Account < SoftLayer::ModelBase
140140
def service
141141
softlayer_client["Account"].object_with_id(self.id)
142142
end
143-
143+
144144
##
145145
# Using the login credentials in the client, retrieve
146146
# the account associated with those credentials.
147147
#
148-
def self.account_for_client(softlayer_client)
148+
def self.account_for_client(client = nil)
149+
softlayer_client = client || Client.default_client
150+
raise "#{__method__} requires a client but none was given and Client::default_client is not set" if !softlayer_client
151+
149152
account_service = softlayer_client['Account']
150153
network_hash = account_service.getObject()
151154
new(softlayer_client, network_hash)

lib/softlayer/BareMetalServer.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,17 @@ def self.cancellation_reasons
122122
##
123123
# Retrive the bare metal server with the given server ID from the
124124
# SoftLayer API
125-
def self.server_with_id(softlayer_client, server_id, options = {})
125+
#
126+
# The options parameter should contain:
127+
#
128+
# <b>+:client+</b> - The client used to connect to the API
129+
#
130+
# If no client is given, then the routine will try to use Client.default_client
131+
# If no client can be found the routine will raise an error.
132+
def self.server_with_id(server_id, options = {})
133+
softlayer_client = options[:client] || Client.default_client
134+
raise "#{__method__} requires a client but none was given and Client::default_client is not set" if !softlayer_client
135+
126136
hardware_service = softlayer_client["Hardware"]
127137
hardware_service = hardware_service.object_mask(default_object_mask.to_sl_object_mask)
128138

@@ -138,6 +148,13 @@ def self.server_with_id(softlayer_client, server_id, options = {})
138148
##
139149
# Retrieve a list of Bare Metal servers from the account
140150
#
151+
# The options parameter should contain:
152+
#
153+
# <b>+:client+</b> - The client used to connect to the API
154+
#
155+
# If no client is given, then the routine will try to use Client.default_client
156+
# If no client can be found the routine will raise an error.
157+
#
141158
# You may filter the list returned by adding options:
142159
#
143160
# * <b>+:tags+</b> (array) - an array of strings representing tags to search for on the instances
@@ -155,7 +172,10 @@ def self.server_with_id(softlayer_client, server_id, options = {})
155172
# * <b>+:object_mask+</b> (string, hash, or array) - The object mask of properties you wish to receive for the items returned If not provided, the result will use the default object mask
156173
# * <b>+:result_limit+</b> (hash with :limit, and :offset keys) - Limit the scope of results returned.
157174
#
158-
def self.find_servers(softlayer_client, options_hash = {})
175+
def self.find_servers(options_hash = {})
176+
softlayer_client = options_hash[:client] || Client.default_client
177+
raise "#{__method__} requires a client but none was given and Client::default_client is not set" if !softlayer_client
178+
159179
if(options_hash.has_key? :object_filter)
160180
object_filter = options_hash[:object_filter]
161181
else

lib/softlayer/BareMetalServerOrder.rb

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ class BareMetalServerOrder
111111

112112
##
113113
# Create a new order that works thorugh the given client connection
114-
def initialize (client)
115-
@softlayer_client = client
114+
def initialize (client = nil)
115+
@softlayer_client = client || Client.default_client
116+
raise "#{__method__} requires a client but none was given and Client::default_client is not set" if !@softlayer_client
116117
end
117118

118119
##
@@ -141,7 +142,7 @@ def place_order!()
141142
order_template = yield order_template if block_given?
142143

143144
server_hash = @softlayer_client["Hardware"].createObject(order_template)
144-
SoftLayer::BareMetalServer.server_with_id(@softlayer_client, server_hash["id"]) if server_hash
145+
SoftLayer::BareMetalServer.server_with_id(server_hash["id"], :client => @softlayer_client) if server_hash
145146
end
146147

147148
protected
@@ -185,35 +186,40 @@ def hardware_instance_template
185186
##
186187
# The first time this is called it requests SoftLayer_Hardware::getCreateObjectOptions
187188
# from the API and remembers the result. On subsequent calls it returns the remembered result.
188-
def self.create_object_options(client)
189-
@@create_object_options ||= client["Hardware"].getCreateObjectOptions()
189+
def self.create_object_options(client = nil)
190+
softlayer_client = client || Client.default_client
191+
raise "#{__method__} requires a client but none was given and Client::default_client is not set" if !softlayer_client
192+
193+
@@create_object_options ||= nil
194+
@@create_object_options = softlayer_client["Hardware"].getCreateObjectOptions() if !@@create_object_options
195+
@@create_object_options
190196
end
191197

192198
##
193199
# Return a list of values that are valid for the :datacenter attribute
194-
def self.datacenter_options(client)
200+
def self.datacenter_options(client = nil)
195201
create_object_options(client)["datacenters"].collect { |datacenter_spec| datacenter_spec['template']['datacenter']["name"] }.uniq.sort!
196202
end
197203

198-
def self.core_options(client)
204+
def self.core_options(client = nil)
199205
create_object_options(client)["processors"].collect { |processor_spec| processor_spec['template']['processorCoreAmount'] }.uniq.sort!
200206
end
201207

202208
##
203209
# Return a list of values that are valid the array given to the :disks
204-
def self.disk_options(client)
210+
def self.disk_options(client = nil)
205211
create_object_options(client)["hardDrives"].collect { |disk_spec| disk_spec['template']['hardDrives'][0]['capacity'].to_i}.uniq.sort!
206212
end
207213

208214
##
209215
# Returns a list of the valid :os_refrence_codes
210-
def self.os_reference_code_options(client)
216+
def self.os_reference_code_options(client = nil)
211217
create_object_options(client)["operatingSystems"].collect { |os_spec| os_spec['template']['operatingSystemReferenceCode'] }.uniq.sort!
212218
end
213219

214220
##
215221
# Returns a list of the :max_port_speeds
216-
def self.max_port_speed_options(client)
222+
def self.max_port_speed_options(client = nil)
217223
create_object_options(client)["networkComponents"].collect { |component_spec| component_spec['template']['networkComponents'][0]['maxSpeed'] }
218224
end
219225

lib/softlayer/BareMetalServerOrder_Package.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ class BareMetalServerOrder_Package < Server
8383
##
8484
# You initialize a BareMetalServerOrder_Package by passing in the package that you
8585
# are ordering from.
86-
def initialize(client, package)
87-
@softlayer_client = client
86+
def initialize(package, client = nil)
87+
@softlayer_client = client || Client.default_client
88+
raise "#{__method__} requires a client but none was given and Client::default_client is not set" if !@softlayer_client
89+
8890
@package = package
8991
@configuration_options = []
9092
end

0 commit comments

Comments
 (0)