Name: oneview-sdk-ruby
Owner: Hewlett Packard Enterprise
Description: Ruby SDK to Interact with HPE OneView
Created: 2015-10-22 16:48:59.0
Updated: 2018-05-21 16:02:02.0
Pushed: 2018-05-24 10:07:17.0
Size: 7388
Language: Ruby
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
The OneView SDK provides a Ruby library to easily interact with HPE OneView and Image Streamer APIs. The Ruby SDK enables developers to easily build integrations and scalable solutions with HPE OneView and Image Streamer.
Require the gem in your Gemfile:
'oneview-sdk', '~> 5.2'
Then run $ bundle install
Or run the command:
m install oneview-sdk
The OneView client has a few configuration options, which you can pass in during creation:
eate a OneView client object:
ire 'oneview-sdk'
nt = OneviewSDK::Client.new(
l: 'https://oneview.example.com',
er: 'Administrator', # This is the default
ssword: 'secret123',
ken: 'xxxx...', # Set EITHER this or the user & password
l_enabled: true, # This is the default and strongly encouraged
gger: Logger.new(STDOUT), # This is the default
g_level: :info, # This is the default
main: 'LOCAL', # This is the default
i_version: 200 # Defaults to minimum of 200 and appliance's max API version
:lock: Tip: Check the file permissions because the password is stored in clear-text.
The Image Streamer (I3S) client is very similar to the OneView client, but has one key difference: it cannot generate it's own token. However, it uses the same token given to or generated by the OneView client, so if you need to generate a token, create a OneView client using a user & password, then pass the generated token into the Image Streamer client.
eate an Image Streamer client object:
ire 'oneview-sdk'
client = OneviewSDK::ImageStreamer::Client.new(
l: 'https://image-streamer.example.com',
ken: 'xxxx...', # Required. Note that you cannot pass the user & password options
l_enabled: true, # This is the default and strongly encouraged
gger: Logger.new(STDOUT), # This is the default
g_level: :info, # This is the default
i_version: 300 # Defaults to minimum of 300 and appliance's max API version
You can also create the i3s client through the Oneview client instance.
eate an Image Streamer client object through the Oneview client object:
ire 'oneview-sdk'
client = client.new_i3s_client(
l: 'https://image-streamer.example.com',
l_enabled: true, # This is the default and strongly encouraged
gger: Logger.new(STDOUT), # This is the default
g_level: :info, # This is the default
i_version: 300 # Defaults to minimum of 300 and appliance's max API version
You can also set many of the client attributes using environment variables. To set these variables in bash:
eView client options:
rt ONEVIEWSDK_URL='https://oneview.example.com'
rt ONEVIEWSDK_DOMAIN='LOCAL'
u can set the token if you know it, or set the user and password to generate one:
rt ONEVIEWSDK_TOKEN='xxxx...'
rt ONEVIEWSDK_USER='Administrator'
rt ONEVIEWSDK_PASSWORD='secret123'
rt ONEVIEWSDK_SSL_ENABLED=false
TE: Disabling SSL is strongly discouraged. Please see the CLI section for import instructions.
age Streamer (I3S) client options:
rt I3S_URL='https://image-streamer.example.com'
rt I3S_SSL_ENABLED=false
TE: Disabling SSL is strongly discouraged. Please see the CLI section for import instructions.
:lock: Tip: Be sure nobody has access to your environment variables, as the password or token is stored in clear-text.
Then you can leave out these options from your config, enabling you to just do:
ire 'oneview-sdk'
nt = OneviewSDK::Client.new
You can create the i3s client with environment variables in the following ways:
ire 'oneview-sdk'
te: Both options require the I3S_URL environment variable to be set.
is way uses the ONEVIEWSDK_URL, ONEVIEWSDK_USER and ONEVIEWSDK_PASSWORD environment variables to generate a token:
nt = OneviewSDK::Client.new
client = client.new_i3s_client
is way uses the ONEVIEWSDK_TOKEN environment variable directly:
client = OneviewSDK::ImageStreamer::Client.new
NOTE: Run $ oneview-sdk-ruby env
to see a list of available environment variables and their current values.
Configuration files can also be used to define client configuration (json or yaml formats). Here's an example json file:
rl": "https://oneview.example.com",
ser": "Administrator",
assword": "secret123",
pi_version": 200
and load via:
ig = OneviewSDK::Config.load("full_file_path.json")
nt = OneviewSDK::Client.new(config)
:lock: Tip: Check the file permissions if the password or token is stored in clear-text.
The default logger is a standard logger to STDOUT, but if you want to specify your own, you can. However, your logger must implement the following methods:
g(String)
(String)
(String)
r(String)
l=(Symbol, etc.) # The parameter here will be the log_level attribute
:lock: Tip: When the log_level is set to debug, API request options will be logged (including auth tokens and passwords); be careful to protect secret information.
A status of the HPE OneView REST interfaces that have been implemented in this SDK can be found in the endpoints-support.md file.
You may notice resource classes being accessed in a few different ways; for example, OneviewSDK::EthernetNetwork
, OneviewSDK::API300::EthernetNetwork
, and OneviewSDK::API300::C7000::EthernetNetwork
. However, each of these accessors may actually be referring to the same class. This is because in order to keep backwards compatibility and make examples a little more simple, there are module methods in place to redirect/resolve the shorthand accessors to their full namespace identifier. In order to automatically complete the full namespace identifier, there are some defaults in place. Here's some example code that should help clear things up (return values are commented behind the code):
ire 'oneview-sdk'
ow defaults:
iewSDK::SUPPORTED_API_VERSIONS # [200, 300, 500, 600]
iewSDK::DEFAULT_API_VERSION # 200
iewSDK.api_version # 200
iewSDK.api_version_updated? # false
tice the automatic redirection/resolution when we use the shorthand accessor:
iewSDK::EthernetNetwork # OneviewSDK::API200::EthernetNetwork
en this comparison is true:
iewSDK::EthernetNetwork == OneviewSDK::API200::EthernetNetwork # true
w let's set a new API version default:
iewSDK.api_version = 300
iewSDK.api_version # 300
iewSDK.api_version_updated? # true
e API200 module has no variants, but API300, API500 and API600 has 2 (C7000 & Synergy):
iewSDK::API300::SUPPORTED_VARIANTS # ['C7000', 'Synergy']
iewSDK::API300::DEFAULT_VARIANT # 'C7000'
iewSDK::API300.variant # 'C7000'
iewSDK::API300.variant_updated? # false
iewSDK::API500::SUPPORTED_VARIANTS # ['C7000', 'Synergy']
iewSDK::API500::DEFAULT_VARIANT # 'C7000'
iewSDK::API500.variant # 'C7000'
iewSDK::API500.variant_updated? # false
iewSDK::API600::SUPPORTED_VARIANTS # ['C7000', 'Synergy']
iewSDK::API600::DEFAULT_VARIANT # 'C7000'
iewSDK::API600.variant # 'C7000'
iewSDK::API600.variant_updated? # false
erefore, there is 1 more namespace level to the real resource class name
iewSDK::EthernetNetwork # OneviewSDK::API300::C7000::EthernetNetwork
iewSDK::API300::EthernetNetwork # OneviewSDK::API300::C7000::EthernetNetwork
kewise, we can set a new default variant for the API300 module:
iewSDK::API300.variant = 'Synergy'
iewSDK::API300.variant # 'Synergy'
iewSDK::API300.variant_updated? # true
iewSDK::EthernetNetwork # OneviewSDK::API300::Synergy::EthernetNetwork
iewSDK::API300::EthernetNetwork # OneviewSDK::API300::Synergy::EthernetNetwork
We understand that this can be confusing, so to avoid any confusion or unexpected behavior, we recommend specifying the full namespace identifier in your code. At the very least, set defaults explicitly using OneviewSDK.api_version = <ver>
and OneviewSDK::API300.variant = <variant>
, as the defaults may change.
Each OneView and Image Streamer resource is exposed via a Ruby class, enabling CRUD-like functionality (with some exceptions).
Once you instantiate a resource object, you can call intuitive methods such as resource.create
, resource.update
and resource.delete
. In addition, resources respond to helpful methods such as .each
, .eql?(other_resource)
, .like(other_resource)
, .retrieve!
, and many others.
Please see the rubydoc.info documentation for complete usage details and the examples directory for more examples and test-scripts, but here are a few examples to get you started:
rnet = OneviewSDK::EthernetNetwork.new(
ient, { name: 'TestVlan', vlanId: 1001, purpose: 'General', smartLink: false, privateNetwork: false }
rnet.create # Tells OneView to create this resource
rnet['name'] # Returns 'TestVlan'
rnet.data # Returns hash of all data
rnet.each do |key, value|
ts "Attribute #{key} = #{value}"
The resource's data is stored in its @data attribute. However, you can access the data directly using a hash-like syntax on the resource object (recommended). resource['key']
functions a lot like resource.data['key']
. The difference is that when using the data attribute, you must be cautious to use the correct key type (Hash vs Symbol).
The direct hash accessor on the resource converts first-level keys to strings; so resource[:key]
and resource['key']
access the same thing: resource.data['key']
. We recommend using strings exclusively for keys, as the JSON data returned from OneView requests supports strings but not symbols.
Notice that there are a few different ways to do things, so pick your poison!
rnet.set_all(name: 'newName', vlanId: 1002)
rnet['purpose'] = 'General'
rnet['ethernetNetworkType'] = 'Tagged'
rnet.update # Saves current state to OneView
ternatively, you could do this in 1 step with:
rnet.update(name: 'newName', vlanId: 1002, purpose: 'General', ethernetNetworkType: 'Tagged')
You can use the ==
or .eql?
method to compare resource equality, or .like
to compare just a subset of attributes.
rnet2 = OneviewSDK::EthernetNetwork.new(client, { purpose: 'General' })
rnet == ethernet2 # Returns false
rnet.eql?(ethernet2) # Returns false
compare a subset of attributes:
rnet.like?(ethernet2) # Returns true
rnet.like?(name: 'TestVlan', purpose: 'General') # Returns true
rnet = OneviewSDK::EthernetNetwork.new(client, { name: 'OtherVlan' })
rnet.retrieve! # Uses the name attribute to search for the resource on the server and update the data in this object.
ch resource class also has a searching method (NOTE: class method, not instance method)
rnet = OneviewSDK::EthernetNetwork.find_by(client, { name: 'OtherVlan' }).first
iewSDK::EthernetNetwork.find_by(client, { purpose: 'General' }).each do |network|
ts " #{network['name']}"
t all resources:
orks = client.get_all(:EthernetNetwork)
rnet = OneviewSDK::EthernetNetwork.find_by(client, { name: 'OtherVlan' }).first
rnet.delete # Tells OneView to delete this resource
Resources can be saved to files and loaded again very easily using the built-in .to_file
& .from_file
methods.
To save a Resource to a file:
rnet.to_file("full_file_path.json")
To load a resource from a file: (note the class method, not instance method)
rnet4 = OneviewSDK::Resource.from_file(client, "full_file_path.json")
For more examples and test-scripts, see the examples directory and rubydoc.info documentation.
In most cases, interacting with Resource objects is enough, but sometimes you need to make your own custom requests to OneView. This project makes it extremely easy to do with some built-in methods for the Client object. Here are some examples:
t the appliance startup progress:
onse = client.rest_api(:get, '/rest/appliance/progress')
even more simple:
onse = client.rest_get('/rest/appliance/progress')
en we can validate the response and convert the response body into a hash...
= client.response_handler(response)
This example is about as basic as it gets, but you can make any type of OneView request. If a resource does not do what you need, this will allow you to do it. Please refer to the documentation and code for complete list of methods and information about how to use them.
This gem also comes with a command-line interface to make interacting with OneView possible without the need to create a Ruby program or script.
Note: In order to use this, you will need to make sure your Ruby bin
directory is in your path.
Run $ gem environment
to see where the executable paths are for your Ruby installation.
To get started, run $ oneview-sdk-ruby --help
.
To communicate with an appliance, you will need to set up a few environment variables so it knows how to communicate. Run $ oneview-sdk-ruby env
to see the available environment variables.
The CLI does not expose everything in the SDK, but it is great for doing simple tasks such as creating or deleting resources from files, listing resources, and searching. Here are a few examples:
tput a list of ServerProfile names:
eview-sdk-ruby list ServerProfiles
to show in yaml format (json is also supported):
eview-sdk-ruby list ServerProfiles -f yaml
to show specific attributes only:
eview-sdk-ruby list ServerProfiles -a uri,state,bios.overriddenSettings
eview-sdk-ruby show ServerProfile profile-1
to show specific attributes only:
eview-sdk-ruby show ServerProfile profile-1 -a name,uri,enclosureBay
eview-sdk-ruby search ServerProfiles --filter state:Normal affinity:Bay
default, it will just show a list of names of matching resources,
but again, you can show only certain attributes by using the -a option
u can also chain keys together to search in nested hashes:
eview-sdk-ruby search ServerProfiles --filter state:Normal boot.manageBoot:true
to show specific attributes only:
eview-sdk-ruby search ServerProfile --filter state:Normal -a name,uri,enclosureBay
ve resource details to a file (to be used with the create and delete methods below)
eview-sdk-ruby to_file ServerProfile profile-1 /my-server-profile.json
eview-sdk-ruby create_from_file /my-server-profile.json
eview-sdk-ruby delete_from_file /my-server-profile.json
eview-sdk-ruby update FCNetwork FC1 -h linkStabilityTime:20 # Using hash format
eview-sdk-ruby update Volume VOL_01 -j '{"shareable": true}' # Using json format
eview-sdk-ruby rest get rest/fc-networks
eview-sdk-ruby rest PUT rest/enclosures/<id>/configuration
eview-sdk-ruby console
ole Connected to https://oneview.example.com
: The @client object is available to you
Although you can disable SSL validation altogether for the client, this is strongly discouraged. Instead, please import the certificate using the built-in CLI cert command:
eck the certificate first:
eview-sdk-ruby cert check https://oneview.example.com
cking certificate for 'https://oneview.example.com' ...
OR: Certificate Validation Failed!
port the certificate:
eview-sdk-ruby cert import https://oneview.example.com
orting certificate for 'https://oneview.example.com' into '/home/users/user1/.oneview-sdk-ruby/trusted_certs.cer'...
t added to '/home/users/user1/.oneview-sdk-ruby/trusted_certs.cer'
eview-sdk-ruby scmb
eview-sdk-ruby scmb -r 'scmb.ethernet-networks.#'
This project is licensed under the Apache 2.0 license. Please see LICENSE for more info.
Contributing: You know the drill. Fork it, branch it, change it, commit it, and pull-request it. We are passionate about improving this project, and glad to accept help to make it better.
NOTE: We reserve the right to reject changes that we feel do not fit the scope of this project, so for feature additions, please open an issue to discuss your ideas before doing the work.
Feature Requests: If you have a need that is not met by the current implementation, please let us know (via a new issue). This feedback is crucial for us to deliver a useful product. Do not assume we have already thought of everything, because we assure you that is not the case.
First run $ bundle
(requires the bundler gem), then…
$ rake build
.$ rake install
.$ rake rubocop
$ rake spec
$ bundle exec guard
$ rake test:all
to run RuboCop, unit, & integration tests.For the full testing reference please look into TESTING.md file.
See the contributors graph