Class: SoftLayer::ProductPackage

Inherits:
ModelBase show all
Includes:
DynamicAttribute
Defined in:
lib/softlayer/ProductPackage.rb

Overview

Each SoftLayer ProductPackage provides information about ordering a product or service from SoftLayer.

Product Item Categories

A important companion to Product Packages are ProductItemCategories. Each ProductItemCategory represents a set of options that you may choose from when configuring an attribute of the product or service you are ordering.

ProductItemCategories are identified by categoryCode. Examples of category codes include ‘os’, ‘ram’, and ‘port_speed’.

For example, in a package for ordering a server, the ‘os’ ProductItemCategory contains the available choices for operating systems that may be provisioned on the server.

When you construct an order based on that package, you will make one selection from the ‘os’ category and put it into the order.

Package Configuration

A package also has a Configuration. A Configuration specifies which Categories are valid in an order and, more importantly, which Categories are required in any order that uses the ProductPackage.

When constructing an order, you must provide an option for each of the Categories that the Configuration marks as required (and you must supply a value even if the Category only has one choice)

Instance Attribute Summary

Attributes inherited from ModelBase

#softlayer_client

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DynamicAttribute

included

Methods inherited from ModelBase

#[], #has_sl_property?, #initialize, #refresh_details, sl_attr, #to_ary

Constructor Details

This class inherits a constructor from SoftLayer::ModelBase

Class Method Details

.bare_metal_instance_package(client = nil) ⇒ Object

Returns the ProductPackage of the package used to order Bare Metal Servers with simplified configuration options.

At the time of this writing, the code assumes this package is unique

‘BARE_METAL_CORE’ is a “well known” constant for this purpose



179
180
181
# File 'lib/softlayer/ProductPackage.rb', line 179

def self.bare_metal_instance_package(client = nil)
  packages_with_key_name('BARE_METAL_CORE', client).first
end

.bare_metal_server_packages(client = nil) ⇒ Object

Returns an array of ProductPackages, each of which can be used as the foundation to order a bare metal server.

‘BARE_METAL_CPU’ is a “well known” constant for this purpose



188
189
190
# File 'lib/softlayer/ProductPackage.rb', line 188

def self.bare_metal_server_packages(client = nil)
  packages_with_key_name('BARE_METAL_CPU', client)
end

.package_with_id(package_id, client = nil) ⇒ Object

Requests a list (array) of ProductPackages whose key names match the one passed in.



155
156
157
158
159
160
161
# File 'lib/softlayer/ProductPackage.rb', line 155

def self.package_with_id(package_id, client = nil)
  softlayer_client = client || Client.default_client
  raise "#{__method__} requires a client but none was given and Client::default_client is not set" if !softlayer_client
  
  package_data = softlayer_client['Product_Package'].object_with_id(package_id).object_mask(self.default_object_mask('mask')).getObject
  ProductPackage.new(softlayer_client, package_data)
end

.packages_with_key_name(key_name, client = nil) ⇒ Object

Requests a list (array) of ProductPackages whose key names match the one passed in.



141
142
143
144
145
146
147
148
149
# File 'lib/softlayer/ProductPackage.rb', line 141

def self.packages_with_key_name(key_name, client = nil)
  softlayer_client = client || Client.default_client
  raise "#{__method__} requires a client but none was given and Client::default_client is not set" if !softlayer_client
  
  filter = SoftLayer::ObjectFilter.build('type.keyName', key_name)
  filtered_service = softlayer_client['Product_Package'].object_filter(filter).object_mask(self.default_object_mask('mask'))
  packages_data = filtered_service.getAllObjects
  packages_data.collect { |package_data| ProductPackage.new(softlayer_client, package_data) }
end

.virtual_server_package(client = nil) ⇒ Object

Returns the ProductPackage of the package used to order virtual servers At the time of this writing, the code assumes this package is unique

‘VIRTUAL_SERVER_INSTANCE’ is a “well known” constant for this purpose



168
169
170
# File 'lib/softlayer/ProductPackage.rb', line 168

def self.virtual_server_package(client = nil)
  packages_with_key_name('VIRTUAL_SERVER_INSTANCE', client).first
end

Instance Method Details

#availableLocationsObject

The list of locations where this product package is available.



62
# File 'lib/softlayer/ProductPackage.rb', line 62

sl_attr :availableLocations

#category(category_code) ⇒ Object

Returns the product category with the given category code (or nil if one cannot be found)



117
118
119
# File 'lib/softlayer/ProductPackage.rb', line 117

def category(category_code)
  configuration.find { |category| category.categoryCode == category_code }
end

#configurationObject

The set of product categories needed to make an order for this product package.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/softlayer/ProductPackage.rb', line 67

sl_dynamic_attr :configuration do |resource|
  resource.should_update? do
    # only retrieved once per instance
    @configuration == nil
  end

  resource.to_update do
    #
    # We call +SoftLayer_Product_Package+ to get the configuration for this package.
    #
    # Unfortunately, even though this call includes +SoftLayer_Product_Item_Category+ entities, it does not have the context
    # needed to find the active price items for that category.
    #
    # Instead, we make a second call, this time to +SoftLayer_Product_Package::getCategories+. That method incorporates a complex
    # filtering mechanism on the server side to give us a list of the categories, groups, and prices that are valid for the current
    # account at the current time. We construct the ProductItemCategory objects from the results we get back.
    #
    configuration_data = softlayer_client['Product_Package'].object_with_id(self.id).object_mask("mask[isRequired,itemCategory.categoryCode]").getConfiguration()

    # We sort of invert the information and create a map from category codes to a boolean representing
    # whether or not they are required.
    required_by_category_code = configuration_data.inject({}) do |required_by_category_code, config_category|
      required_by_category_code[config_category['itemCategory']['categoryCode']] = (config_category['isRequired'] != 0)
      required_by_category_code
    end

    # This call to getCategories is the one that does lots of fancy back-end filtering for us
    categories_data = softlayer_client['Product_Package'].object_with_id(self.id).getCategories()

    # Run though the categories and for each one that's in our config, create a SoftLayer::ProductItemCategory object.
    # Conveniently the +keys+ of the required_by_category_code gives us a list of the category codes in the configuration
    config_categories = required_by_category_code.keys
    categories_data.collect do |category_data|
      if config_categories.include? category_data['categoryCode']
        SoftLayer::ProductItemCategory.new(softlayer_client, category_data, required_by_category_code[category_data['categoryCode']])
      else
        nil
      end
    end.compact
  end
end

#datacenter_optionsObject



121
122
123
# File 'lib/softlayer/ProductPackage.rb', line 121

def datacenter_options
  availableLocations.collect { |location_data| location_data["location"]["name"] }
end

#location_id_for_datacenter_name(datacenter_name) ⇒ Object

Given a datacenter name that was returned by datacenter_options, use information in the package to retrieve a location id.



128
129
130
131
# File 'lib/softlayer/ProductPackage.rb', line 128

def location_id_for_datacenter_name(datacenter_name)
  location_data = availableLocations.find { |location_data| location_data["location"]["name"]  == datacenter_name }
  location_data["locationId"]
end

#nameObject

A friendly, readable name for the package



58
# File 'lib/softlayer/ProductPackage.rb', line 58

sl_attr :name

#required_categoriesObject

Returns an array of the required categories in this package



111
112
113
# File 'lib/softlayer/ProductPackage.rb', line 111

def required_categories
  configuration.select { |category| category.required? }
end

#serviceObject



133
134
135
# File 'lib/softlayer/ProductPackage.rb', line 133

def service
  softlayer_client['Product_Package'].object_with_id(self.id)
end