Oracle Bare Metal Cloud Services Ruby SDK

Version 1.2.3

This topic describes how to install, configure, and use the Oracle Bare Metal Cloud Services Ruby SDK.

SDK Overview

The Ruby SDK supports the following services:

  • Core Services (which includes Networking Service, Compute Service, and Block Volume Service)
  • Identity and Access Management Service
  • Load Balancing Service

Licensing: This SDK and sample is dual licensed under the Universal Permissive License 1.0 and the Apache License.

SDK Attributes

The following table provides details about some of the attributes of the SDK.

Requests API methods expose required parameters as arguments and optional arguments as a hash (opts = {}). Create and update operations take request objects that mirror the properties of those objects.
Responses All API methods return a Response object, which contains an HTTP status (200, 204, etc), headers, and data, and also directly exposes some commonly used response headers such as opc-next_page, and opc-request_id.
Models Get, update, and create operations return first class objects of the corresponding type (such as User, Instance, etc) in Response.data.
Errors Unsuccessful requests will always raise an exception. ServiceError is raised when the service returns an error, and NetworkError is raised for network issues (such as failed host resolution).
Signing Requests made through the API classes are automatically signed, but you can also use the Signer directly to sign your own requests if needed.
Automatic Paging Response objects for lists support page enumeration. See example code later in this file.
Waiters Responses for get requests support waiting for a particular states using wait_until. See example code later in this file.
HTTP Client The Ruby SDK uses Typhoeus for HTTP requests, which in turn calls libcurl. Some behaviors may depend on your installation of libcurl. If needed, arbitrary options may be passed to each Typhoeus request by specifying them in ApiClient.request_option_overrides.

SDK Requirements

To use the Ruby SDK, you must have:

  • An Oracle Bare Metal Cloud Services account.
  • A user created in that account, in a group with a policy that grants the desired permissions. This can be a user for yourself, or another person/system that needs to call the API. For an example of how to set up a new user, group, compartment, and policy, see Adding Users in the Getting Started Guide. For a list of typical policies you may want to use, see Common Policies in the User Guide.
  • A keypair used for signing API requests, with the public key uploaded to Oracle. Only the user calling the API should be in possession of the private key. See the configuration information below.
  • Ruby version 2.2 or later running on Mac or Linux. Windows is not supported at this time.

Downloading and Installing the Gem File

Installing the SDK

Install from RubyGems:

gem install oraclebmc

You can also download the SDK as a zip file containing the gem file, examples, and documentation.

Install the gem with the following command:

gem install oraclebmc-1.2.3.gem

The SDK depends on libcurl, which may need to be installed or upgraded on some operating systems to be compatible with Oracle Bare Metal Cloud Services.

Use one of the following commands for your particular operating system in order to successfully make requests:

  • For Oracle Linux (OL) or Red Hat Enterprise Linux (RHEL): sudo yum upgrade curl
  • For Ubuntu: sudo apt-get install libcurl3 libcurl3-dev

    Troubleshooting an Installation

    If you see "Unable to resolve dependencies”, you can install the dependencies manually:

    gem install typhoeus gem install inifile

Configuring the SDK

To use any of the APIs, you must supply a Config object. You can create the object directly in code, or you can create one in a config file. The configuration includes:

  • Required credentials and settings: See SDK and Tool Configuration in the User Guide.
  • Optional SDK-specific settings: See the Config object for the full list of config options.

Forward Compatibility

Some response fields are enum-typed. In the future, individual services may return values not covered by existing enums for that field. To address this possibility, every enum-type response field has an additional value named "UNKNOWN_ENUM_VALUE". If a service returns a value that is not recognized by your version of the SDK, then the response field will be set to this value. Please ensure that your code handles the "UNKNOWN_ENUM_VALUE" case if you have conditional logic based on an enum-typed field.

Writing Your First Ruby Program with the SDK

require 'oraclebmc'

# This will load the config file at the default location, and will
# use the tenancy from that config as the compartment in the
# call to list_users.
api = OracleBMC::Identity::IdentityClient.new(region: OracleBMC::Regions::REGION_US_PHOENIX_1)
response = api.list_users(OracleBMC.config.tenancy)
response.data.each { |user| puts user.name }

Loading Alternate Configurations

You can also load a config file from a different location, and/or specify a different profile from the config file:

require 'oraclebmc'

my_config = OracleBMC::ConfigFileLoader.load_config(config_file_location:'my_config', profile_name:'USER_TWO')
api = OracleBMC::Identity::IdentityClient.new(config:my_config)
response = api.get_user(my_config.user)
puts 'User Name: ' + response.data.name

Or you can create a Config programmatically. Note that the global value OracleBMC.config will always attempt to load the DEFAULT profile from the default config file location (~/.oraclebmc/config), unless it has been explicitly set to another value.

Service Errors

Any operation resulting in a service error will cause an exception of type OracleBMC::Errors::ServiceError to be thrown by the SDK. For information about common service errors returned by BMCS, see API Errors.

Examples

The example code in this section shows how various parts of the Ruby SDK work. More examples can be found in the SDK download.

Management Operations on a User

The following example runs create, read, update, and delete (CRUD) operations on users.

require 'oraclebmc'

compartment = OracleBMC.config.tenancy

api = OracleBMC::Identity::IdentityClient.new
users = api.list_users(compartment_id = compartment).data
puts "There are currently " + users.length.to_s + " users."

# Create User
request = OracleBMC::Identity::Models::CreateUserDetails.new
request.compartment_id = compartment
request.name = "userA"
request.description = "example user"
response =  api.create_user(request)

puts "Created user " + response.data.name
user_id = response.data.id

users = api.list_users(compartment_id = compartment).data
puts "There are now " + users.length.to_s + " users."

# Get User
response =  api.get_user(user_id = user_id)

# Update User (using a request object)
newDescription = "updated user description"
request = OracleBMC::Identity::Models::UpdateUserDetails.new
request.description = "Updated description"
response = api.update_user(user_id, request)

puts "Updated description to:" + response.data.description

# Update User (using a hash)
newDescription = "updated without a request object"
response = api.update_user(user_id, { description: "Updated again, but using a hash instead of an object." })

# Delete User
api.delete_user(user_id)

users = api.list_users(compartment_id = compartment).data
puts "Back to " + users.length.to_s + " users."

Paging Through Results

The following example shows how to page through results. It also gives an example of supplying optional parameters.

require 'oraclebmc'

api = OracleBMC::Identity::IdentityClient.new
compartment = OracleBMC.config.tenancy

### Automatic paging:
api.list_users(compartment, limit:'3').each { |r| r.data.each { |user| puts user.name }}

### Manual paging:
request_number = 0
next_page = nil

loop do
  response = api.list_users(compartment, {limit: '3', page: next_page})

  puts "Page " + request_number.to_s
  response.data.each { |user| puts user.name }

  break unless response.has_next_page?
  next_page = response.next_page
  request_number += 1
end

Launching an Instance and Waiting for a State

The following example shows how to launch an instance (which assumes that you already have a subnet created), and then wait until the instance is running.

require 'oraclebmc'

ssh_public_key = File.open(File.expand_path(public_key_file), "rb").read

request = OracleBMC::Core::Models::LaunchInstanceDetails.new
request.availability_domain = availability_domain # TODO: Set an availability domain, such as 'kIdk:PHX-AD-2'
request.compartment_id = compartment_id # TODO: set your compartment ID here
request.display_name = 'my_instance'
request.image_id = image_id # TODO: set your image ID here. You can see the available options with list_images.
request.shape = shape # TODO: set your instance shape. You can see the available options with list_shapes.
request.subnet_id = subnet_id # TODO: set your subnet ID here
request. = {'ssh_authorized_keys' => ssh_public_key}

api = OracleBMC::Core::ComputeClient.new
response = api.launch_instance(request)
instance_id = response.data.id
response = api.get_instance(instance_id).wait_until(:lifecycle_state, OracleBMC::Core::Models::Instance::LIFECYCLE_STATE_RUNNING)

Signing a Raw Request

The OracleBMC::Signer can be used to sign arbitrary requests to the Bare Metal Cloud Services. The following example uses Net::HTTP to call the IAM service directly.

require 'oraclebmc'
require 'net/http'

config = OracleBMC::ConfigFileLoader.load_config(config_file_location:my_config_file_location)
endpoint = OracleBMC::Regions.get_service_endpoint(config.region, :IdentityClient)

uri = URI(endpoint + '/20160918/users/' + config.user)
request = Net::HTTP::Get.new(uri)

signer = OracleBMC::Signer.new(config.user, config.fingerprint, config.tenancy, config.key_file, pass_phrase:my_private_key_pass_phrase)
signer.sign(:get, uri.to_s, request, nil)

result = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http|
  http.request(request)
}

puts result.body

Notifications

To be notified when a new version of the Ruby SDK is released, subscribe to the Atom feed.

Questions or Feedback?

Ways to get in touch: