November 23, 2017


# Get a VSI looking for Hostname and Domain
# Important manual pages:
# @license <>
# @author SoftLayer Technologies, Inc. <>
require 'softlayer_api'
require 'pp'

USERNAME = 'set me'
API_KEY = 'set me'

# The virtual machine data
# Set this with the hostname of the machine you want
hostname = 'ProvisionApiTest'
# Set this with the domain of the machine you want
domain = ''

# Creating a filter to get the specific VSI we are seeking.
object_filter = do |filter|
  filter.accept('virtualGuests.hostname').when_it is(hostname)
  filter.accept('virtualGuests.domain').when_it is(domain)

account_service ='SoftLayer_Account', username: USERNAME, api_key: API_KEY)

  # It will return only the virtual machine which have the specified hostname and domain
  virtual_guest = account_service.object_filter(object_filter).getVirtualGuests
  pp virtual_guest[0]['id']
rescue StandardError => exception
  puts "Unable to  get the VSI: #{exception}"


If this article contains any error, or leaves any of your questions unanswered, please help us out by opening up a github issue.
Open an issue