Skip to content

Instantly share code, notes, and snippets.

@Juice10
Created January 29, 2016 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Juice10/f4f170389d2f121793df to your computer and use it in GitHub Desktop.
Save Juice10/f4f170389d2f121793df to your computer and use it in GitHub Desktop.
Billing Examples in Ruby
# please substitute your username and api key below,
# SoftLayer library will look to see if these global variables are set when making a connection
# more information here: https://github.com/softlayer/softlayer-ruby/blob/master/lib/softlayer/Config.rb#L11-L58
SL_API_USERNAME = "<your username here>"
SL_API_KEY = "<your apikey here>"
require 'softlayer_api' # Requires softlayer_api >= 3.2
require 'pp' # used to display results
# Create a client
client = SoftLayer::Client.new
# Uncomment the examples below you would like to try out
# ### How do I find out what my next bill will be?
#
# total_amount = client['Account'].getNextInvoiceTotalAmount
# puts "Next invoice total amount: #{total_amount}"
# ### How much has an hourly device cost me month-to-date?
#
# virtual_server_id = FILL_IN_YOUR_SERVERS_ID # add your server id, can be found with the following script: https://softlayer.github.io/ruby/list_instances/
#
# item = client[:Virtual_Guest].object_mask("mask[billingItem[createDate,hoursUsed,hourlyRecurringFee,currentHourlyCharge]]").object_with_id(virtual_server_id).getObject
# puts "Billing Item for Virtual Server:"
# pp item['billingItem']
# ### How do I find a billing item from a provisioned product?
#
# network_storage_id = FILL_IN_YOUR_STORAGE_ID # add your network storage id, it can be found by doing this call: http://sldn.softlayer.com/reference/services/SoftLayer_Account/getNasNetworkStorage
#
# item = client[:Network_Storage].object_mask("mask[billingItem[createDate,hoursUsed,hourlyRecurringFee,currentHourlyCharge]]").object_with_id(network_storage_id).getObject
# pp item['billingItem']
# ### How do I cancel an item?
#
# # First get the Billing Item ID:
# id_of_virtual_server_to_cancel = FILL_IN_YOUR_SERVERS_ID # add your servers' id, can be found with the following script: https://softlayer.github.io/ruby/list_instances/
#
# begin
# # Substitute :Virtual_Guest for :Hardware_Server if you want to cancel a bare metal server
# item = client[:Virtual_Guest].object_mask("mask[billingItem[id]]").object_with_id(id_of_virtual_server_to_cancel).getObject
# rescue XMLRPC::FaultException # error will be triggered if item doesn't exist
# item = false
# end
#
# # Then invoke [cancelService](http://sldn.softlayer.com/reference/services/SoftLayer_Billing_Item/cancelService) or [cancelItem](http://sldn.softlayer.com/reference/services/SoftLayer_Billing_Item/cancelItem)
# if item and item['billingItem'] and client[:Billing_Item].object_with_id(item['billingItem']['id']).cancelService()
# puts "Cancelled service of #{id_of_virtual_server_to_cancel}"
# else
# puts "#{id_of_virtual_server_to_cancel} Doesn't exsist or is already cancelled"
# end
# ### How do I find a provisioned product from a billing item?
#
# id = FILL_IN_YOUR_SERVERS_ID # add your servers' id, can be found with the following script: https://softlayer.github.io/ruby/list_instances/
# item = client[:Virtual_Guest].object_mask("mask[billingItem[id]]").object_with_id(id).getObject
# billing_item_id = item['billingItem']['id']
#
# # If you need to associate a Billing ID with the service or item it corresponds to you can use [getAssociatedBillingItem](http://sldn.softlayer.com/reference/services/SoftLayer_Billing_Item/getAssociatedBillingItem) or [getAssociatedParent](http://sldn.softlayer.com/reference/services/SoftLayer_Billing_Item/getAssociatedParent) along with an Object Mask.
# puts "Associated parent (currently not working...)"
# pp client[:Billing_Item].object_mask("mask[id]").object_with_id(billing_item_id).getAssociatedParent # this doesn't actually work...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment