Skip to content

Commit 55a9bce

Browse files
committed
Fixed a bug in virtual servers that was preventing upgrades from working properly.
Added unit tests to preemptively check virtual server upgrade functionality Allow the use of symbols to locate services
1 parent 3d74d8d commit 55a9bce

5 files changed

Lines changed: 81 additions & 11 deletions

File tree

lib/softlayer/Client.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,20 +116,18 @@ def authentication_headers
116116
def service_named(service_name, service_options = {})
117117
raise ArgumentError,"Please provide a service name" if service_name.nil? || service_name.empty?
118118

119-
# strip whitespace from service_name and
120-
# ensure that it start with "SoftLayer_".
121-
#
122-
# if it does not, then add it
123-
service_name.strip!
124-
if not service_name =~ /\ASoftLayer_/
125-
service_name = "SoftLayer_#{service_name}"
119+
# Strip whitespace from service_name and ensure that it starts with "SoftLayer_".
120+
# If it does not, then add the prefix.
121+
full_name = service_name.to_s.strip
122+
if not full_name =~ /\ASoftLayer_/
123+
full_name = "SoftLayer_#{service_name}"
126124
end
127125

128126
# if we've already created this service, just return it
129127
# otherwise create a new service
130-
service_key = service_name.to_sym
128+
service_key = full_name.to_sym
131129
if !@services.has_key?(service_key)
132-
@services[service_key] = SoftLayer::Service.new(service_name, {:client => self}.merge(service_options))
130+
@services[service_key] = SoftLayer::Service.new(full_name, {:client => self}.merge(service_options))
133131
end
134132

135133
@services[service_key]

lib/softlayer/VirtualServer.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class VirtualServer < Server
7575
#
7676
sl_dynamic_attr :upgrade_options do |resource|
7777
resource.should_update? do
78-
@upgrade_items == nil
78+
@upgrade_options == nil
7979
end
8080

8181
resource.to_update do
@@ -365,7 +365,7 @@ def service
365365
# and whose capacity matches the value given. Returns the item_price or nil
366366
#
367367
def _item_price_in_category(which_category, capacity)
368-
item_prices_in_category = self.upgrade_items.select { |item_price| item_price["categories"].find { |category| category["categoryCode"] == which_category } }
368+
item_prices_in_category = self.upgrade_options.select { |item_price| item_price["categories"].find { |category| category["categoryCode"] == which_category } }
369369
item_prices_in_category.find { |ram_item| ram_item["item"]["capacity"].to_i == capacity}
370370
end
371371

spec/Client_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,5 +164,20 @@
164164

165165
expect(first_account_service).to be(second_account_service)
166166
end
167+
168+
it "recognizes a symbol as an acceptable service name" do
169+
account_service = test_client[:Account]
170+
expect(account_service).to_not be_nil
171+
172+
trying_again = test_client['Account']
173+
expect(trying_again).to be(account_service)
174+
175+
yet_again = test_client['SoftLayer_Account']
176+
expect(yet_again).to be(account_service)
177+
178+
once_more = test_client[:SoftLayer_Account]
179+
expect(once_more).to be(account_service)
180+
end
181+
167182
end
168183
end

spec/VirtualServer_spec.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,60 @@
6868
it_behaves_like "server with mutable hostname" do
6969
let (:server) { sample_server }
7070
end
71+
72+
describe "component upgrades" do
73+
let(:mock_client) do
74+
mock_client = SoftLayer::Client.new(:username => "fakeuser", :api_key => "DEADBEEFBADF00D")
75+
virtual_guest_service = mock_client[:Virtual_Guest]
76+
77+
allow(virtual_guest_service).to receive(:call_softlayer_api_with_params) do |api_method, parameters, api_arguments|
78+
api_return = nil
79+
80+
case api_method
81+
when :getUpgradeItemPrices
82+
api_return = fixture_from_json('virtual_server_upgrade_options')
83+
else
84+
fail "Unexpected call to the SoftLayer_Virtual_Guest service"
85+
end
86+
87+
api_return
88+
end
89+
90+
mock_client
91+
end
92+
93+
it "retrieves the item upgrades for a server from the API once" do
94+
fake_virtual_server = SoftLayer::VirtualServer.new(mock_client, {"id" => 12345})
95+
expect(fake_virtual_server.upgrade_options).to eq fixture_from_json('virtual_server_upgrade_options')
96+
97+
# once we've retrieve the options once, we shouldn't be calling back into the service to get them again
98+
expect(mock_client[:Virtual_Guest]).to_not receive(:call_softlayer_api_with_params)
99+
fake_virtual_server.upgrade_options
100+
end
101+
102+
describe "individual component upgrades" do
103+
before(:each) do
104+
expect(mock_client[:Product_Order]).to receive(:call_softlayer_api_with_params) do |api_method, parameters, api_arguments|
105+
expect(api_method).to be(:placeOrder)
106+
expect(parameters).to be_nil
107+
expect(api_method).to_not be_empty
108+
end
109+
end
110+
111+
it "upgrades cores" do
112+
fake_virtual_server = SoftLayer::VirtualServer.new(mock_client, {"id" => 12345})
113+
fake_virtual_server.upgrade_cores!(8)
114+
end
115+
116+
it "upgrades ram" do
117+
fake_virtual_server = SoftLayer::VirtualServer.new(mock_client, {"id" => 12345})
118+
fake_virtual_server.upgrade_RAM!(4)
119+
end
120+
121+
it "upgrades max port speed" do
122+
fake_virtual_server = SoftLayer::VirtualServer.new(mock_client, {"id" => 12345})
123+
fake_virtual_server.upgrade_max_port_speed!(100)
124+
end
125+
end # individual component upgrades
126+
end
71127
end

spec/fixtures/virtual_server_upgrade_options.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)