Last active
September 2, 2016 20:52
-
-
Save hassenius/10421344 to your computer and use it in GitHub Desktop.
Package Options
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################################################ | |
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this