|
| 1 | +#-- |
| 2 | +# Copyright (c) 2014 SoftLayer Technologies, Inc. All rights reserved. |
| 3 | +# |
| 4 | +# For licensing information see the LICENSE.md file in the project root. |
| 5 | +#++ |
| 6 | + |
| 7 | + |
| 8 | +module SoftLayer |
| 9 | + # This class is used to order changes to a virtual server. Although |
| 10 | + # the class is named "upgrade" this class can also be used for "downgrades" |
| 11 | + # (i.e. changing attributes to a smaller, or slower, value) |
| 12 | + # |
| 13 | + # The class can also be used to discover what upgrades are available |
| 14 | + # for a given virtual server. |
| 15 | + # |
| 16 | + class VirtualServerUpgradeOrder |
| 17 | + # The virtual server that this order is designed to upgrade. |
| 18 | + attr_reader :virtual_server |
| 19 | + |
| 20 | + # The number of cores the server should have after the upgrade. |
| 21 | + # If this is nil, the the number of cores will not change |
| 22 | + attr_accessor :cores |
| 23 | + |
| 24 | + # The amount of RAM (in GB) that the server should have after the upgrade |
| 25 | + # If this is nil, the ram will not change |
| 26 | + attr_accessor :ram |
| 27 | + |
| 28 | + # The port speed (in Mega bits per second) that the server should have |
| 29 | + # after the upgrade. This is typically a value like 100, or 1000 |
| 30 | + # If this is nil, the port speeds will not change |
| 31 | + attr_accessor :max_port_speed |
| 32 | + |
| 33 | + # The date and time when you would like the upgrade to be processed. |
| 34 | + # This should simply be a Time object. If nil then the upgrade |
| 35 | + # will be performed immediately |
| 36 | + attr_accessor :upgrade_at |
| 37 | + |
| 38 | + def initialize(virtual_server) |
| 39 | + raise "A virtual server must be provided at the time a virtual server order is created" if !virtual_server || !virtual_server.kind_of?(SoftLayer::VirtualServer) |
| 40 | + @virtual_server = virtual_server |
| 41 | + end |
| 42 | + |
| 43 | + def verify() |
| 44 | + if has_order_items? |
| 45 | + order_object = self.order_object |
| 46 | + order_object = yield order_object if block_given? |
| 47 | + |
| 48 | + @virtual_server.softlayer_client["Product_Order"].verifyOrder(order_object) |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + def place_order!() |
| 53 | + if has_order_items? |
| 54 | + order_object = self.order_object |
| 55 | + order_object = yield order_object if block_given? |
| 56 | + |
| 57 | + @virtual_server.softlayer_client["Product_Order"].placeOrder(order_object) |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + ## |
| 62 | + # Return a list of values that are valid for the :cores attribute |
| 63 | + def core_options() |
| 64 | + self._item_prices_in_category("guest_core").map { |item_price| item_price["item"]["capacity"].to_i}.sort.uniq |
| 65 | + end |
| 66 | + |
| 67 | + ## |
| 68 | + # Return a list of values that are valid for the :memory attribute |
| 69 | + def memory_options() |
| 70 | + self._item_prices_in_category("ram").map { |item_price| item_price["item"]["capacity"].to_i}.sort.uniq |
| 71 | + end |
| 72 | + |
| 73 | + ## |
| 74 | + # Returns a list of valid values for max_port_speed |
| 75 | + def max_port_speed_options(client = nil) |
| 76 | + self._item_prices_in_category("port_speed").map { |item_price| item_price["item"]["capacity"].to_i}.sort.uniq |
| 77 | + end |
| 78 | + |
| 79 | + private |
| 80 | + |
| 81 | + def has_order_items? |
| 82 | + @cores != nil || @ram != nil || @max_port_speed != nil |
| 83 | + end |
| 84 | + |
| 85 | + def _item_prices_in_category(which_category) |
| 86 | + @virtual_server.upgrade_options.select { |item_price| item_price["categories"].find { |category| category["categoryCode"] == which_category } } |
| 87 | + end |
| 88 | + |
| 89 | + ## |
| 90 | + # Searches through the upgrade items pricess known to this server for the one that is in a particular category |
| 91 | + # and whose capacity matches the value given. Returns the item_price or nil |
| 92 | + # |
| 93 | + def _item_price_with_capacity(which_category, capacity) |
| 94 | + self._item_prices_in_category(which_category).find { |item_price| item_price["item"]["capacity"].to_i == capacity} |
| 95 | + end |
| 96 | + |
| 97 | + def order_object |
| 98 | + prices = [] |
| 99 | + |
| 100 | + cores_price_item = @cores ? _item_price_with_capacity("guest_core", @cores) : nil |
| 101 | + ram_price_item = @ram ? _item_price_with_capacity("ram", @ram) : nil |
| 102 | + max_port_speed_price_item = @max_port_speed ? _item_price_with_capacity("port_speed", @max_port_speed) : nil |
| 103 | + |
| 104 | + prices << { "id" => cores_price_item["id"] } if cores_price_item |
| 105 | + prices << { "id" => ram_price_item["id"] } if ram_price_item |
| 106 | + prices << { "id" => max_port_speed_price_item["id"] } if max_port_speed_price_item |
| 107 | + |
| 108 | + # put together an order |
| 109 | + upgrade_order = { |
| 110 | + 'complexType' => 'SoftLayer_Container_Product_Order_Virtual_Guest_Upgrade', |
| 111 | + 'virtualGuests' => [{'id' => @virtual_server.id }], |
| 112 | + 'properties' => [{'name' => 'MAINTENANCE_WINDOW', 'value' => @upgrade_at ? @upgrade_at.iso8601 : Time.now.iso8601}], |
| 113 | + 'prices' => prices |
| 114 | + } |
| 115 | + end |
| 116 | + end # VirtualServerUpgradeOrder |
| 117 | +end # SoftLayer Module |
0 commit comments