Skip to content

Instantly share code, notes, and snippets.

@hassenius
Last active September 2, 2016 20:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hassenius/10421344 to your computer and use it in GitHub Desktop.
Save hassenius/10421344 to your computer and use it in GitHub Desktop.
Package Options
################################################################################
# get_package_options.rb
# ©Copyright IBM Corporation 2014.
#
# LICENSE: MIT (http://opensource.org/licenses/MIT)
################################################################################
require 'rubygems'
require 'softlayer_api'
require 'optparse'
require 'pp'
$SL_API_USERNAME = " set me "
$SL_API_KEY = " set me "
options = {:package_id => nil, :required_categories_only => false}
parser = OptionParser.new do |opts|
opts.banner = "Usage: get_package_options.rb [options]"
opts.on('-p', '--package packageID', Integer, 'Package id to query') do |package_id|
options[:package_id] = package_id;
end
opts.on('-r', '--required', 'Only displayed required categories') do |required|
options[:required_categories_only] = true;
end
opts.on('-h', '--help', 'Displays Help') do
puts opts
exit
end
end
begin
parser.parse!
mandatory = [:package_id]
missing = mandatory.select{ |param| options[param].nil? }
unless missing.empty?
puts parser
unless options.empty?
puts "\n Missing the following options: #{missing.join(', ')}"
end
exit
end
rescue OptionParser::InvalidOption, OptionParser::MissingArgument, OptionParser::InvalidArgument
puts parser
puts $!.to_s
exit
end
softlayer_product_package = SoftLayer::Service.new("SoftLayer_Product_Package");
softlayer_product_item_price = SoftLayer::Service.new("SoftLayer_Product_Item_Price")
package_id = options[:package_id]
# This creates a proxy of the product package service with the virtual guest package ID already
# "integrated" into it.
package = softlayer_product_package.object_with_id(package_id)
# location_id = 138124
puts "Required Categories:"
#configuration = package.object_mask("isRequired","itemCategory").object_with_id(package_id).getConfiguration()
configuration = package.object_mask("mask.isRequired","mask.itemCategory").getConfiguration()
if options[:required_categories_only] == true then
required_categories = configuration.select { |configuration_entry| 1 == configuration_entry['isRequired'] }
else
required_categories = configuration
end
required_categories.each do |configuration_entry|
puts "\t#{configuration_entry['itemCategory']['id']} -- #{configuration_entry['itemCategory']['name']}"
end
item_prices = softlayer_product_package.object_mask("mask.id", "mask.item.description", "mask.categories.id").object_with_id(package_id).getItemPrices()
required_categories.each do |configuration_entry|
puts "Category \"#{configuration_entry['itemCategory']['name']}\":"
category_prices = item_prices.map do |item|
item["categories"].map do |category|
item if category["id"] == configuration_entry["itemCategory"]["id"]
end if item.include?("categories")
end.flatten.compact
category_prices.each do |category_price|
puts "\t #{category_price['id']} -- #{category_price['item']['description']}"
end
end
@martin2110
Copy link

Thanks for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment