Skip to content

Commit

Permalink
Merge pull request #179 from HewlettPackard/client_updates
Browse files Browse the repository at this point in the history
Added client domain attribute and destroy_session method
  • Loading branch information
jsmartt committed Feb 23, 2017
2 parents 4e179aa + bb8bee9 commit 5c57e2a
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Added full support to Image Streamer Rest API version 300:
- [#135](https://github.com/HewlettPackard/oneview-sdk-ruby/issues/135) Firmware Bundle timeout does not give proper instructions for user post failure
- [#146](https://github.com/HewlettPackard/oneview-sdk-ruby/issues/146) Why is Switch the only resource that directly implements #set_scope_uris?
- [#166](https://github.com/HewlettPackard/oneview-sdk-ruby/issues/166) I3S - Simplify login to i3s through oneview client
- [#178](https://github.com/HewlettPackard/oneview-sdk-ruby/issues/178) Add client destroy_session method and domain attribute
- Give custom exception classes a data attribute for more error context and default message
- [#174](https://github.com/HewlettPackard/oneview-sdk-ruby/issues/174) I3S - Integration test for Build Plan failing
- [#183](https://github.com/HewlettPackard/oneview-sdk-ruby/issues/183) Image Streamer Client cannot be created with the OneView environment variables
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ client = OneviewSDK::Client.new(
ssl_enabled: true, # This is the default and strongly encouraged
logger: Logger.new(STDOUT), # This is the default
log_level: :info, # This is the default
domain: 'LOCAL', # This is the default
api_version: 200 # Defaults to minimum of 200 and appliance's max API version
)
```
Expand Down Expand Up @@ -84,6 +85,7 @@ You can also set many of the client attributes using environment variables. To s
```bash
# OneView client options:
export ONEVIEWSDK_URL='https://oneview.example.com'
export ONEVIEWSDK_DOMAIN='LOCAL'
# You can set the token if you know it, or set the user and password to generate one:
export ONEVIEWSDK_TOKEN='xxxx...'
export ONEVIEWSDK_USER='Administrator'
Expand Down
2 changes: 1 addition & 1 deletion lib/oneview-sdk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

# Module for interacting with the HPE OneView API
module OneviewSDK
env_sdk = %w(ONEVIEWSDK_URL ONEVIEWSDK_USER ONEVIEWSDK_PASSWORD ONEVIEWSDK_TOKEN)
env_sdk = %w(ONEVIEWSDK_URL ONEVIEWSDK_USER ONEVIEWSDK_PASSWORD ONEVIEWSDK_TOKEN ONEVIEWSDK_DOMAIN)
env_sdk.concat %w(ONEVIEWSDK_SSL_ENABLED ONEVIEWSDK_API_VERSION ONEVIEWSDK_VARIANT)
env_i3s = %w(I3S_URL I3S_SSL_ENABLED)
ENV_VARS = env_sdk.concat(env_i3s).freeze
Expand Down
26 changes: 18 additions & 8 deletions lib/oneview-sdk/client.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# (C) Copyright 2016 Hewlett Packard Enterprise Development LP
# (c) Copyright 2016-2017 Hewlett Packard Enterprise Development LP
#
# Licensed under the Apache License, Version 2.0 (the "License");
# You may not use this file except in compliance with the License.
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed
Expand All @@ -18,7 +18,7 @@ module OneviewSDK
# The client defines the connection to the OneView server and handles communication with it.
class Client
attr_reader :max_api_version
attr_accessor :url, :user, :token, :password, :ssl_enabled, :api_version, \
attr_accessor :url, :user, :token, :password, :domain, :ssl_enabled, :api_version, \
:logger, :log_level, :cert_store, :print_wait_dots, :timeout

include Rest
Expand All @@ -32,6 +32,7 @@ class Client
# @option options [String] :url URL of OneView appliance
# @option options [String] :user ('Administrator') The username to use for authentication with the OneView appliance
# @option options [String] :password (ENV['ONEVIEWSDK_PASSWORD']) The password to use for authentication with OneView appliance
# @option options [String] :domain ('LOCAL') The name of the domain directory used for authentication
# @option options [String] :token (ENV['ONEVIEWSDK_TOKEN']) The token to use for authentication with OneView appliance
# Use the token or the username and password (not both). The token has precedence.
# @option options [Integer] :api_version (200) This is the API version to use by default for requests
Expand Down Expand Up @@ -65,12 +66,12 @@ def initialize(options = {})
@timeout = options[:timeout] unless options[:timeout].nil?
@cert_store = OneviewSDK::SSLHelper.load_trusted_certs if @ssl_enabled
@token = options[:token] || ENV['ONEVIEWSDK_TOKEN']
return if @token
@logger.warn 'User option not set. Using default (Administrator)' unless options[:user] || ENV['ONEVIEWSDK_USER']
@logger.warn 'User option not set. Using default (Administrator)' unless @token || options[:user] || ENV['ONEVIEWSDK_USER']
@user = options[:user] || ENV['ONEVIEWSDK_USER'] || 'Administrator'
@password = options[:password] || ENV['ONEVIEWSDK_PASSWORD']
raise InvalidClient, 'Must set user & password options or token option' unless @password
@token = login
raise InvalidClient, 'Must set user & password options or token option' unless @token || @password
@domain = options[:domain] || ENV['ONEVIEWSDK_DOMAIN'] || 'LOCAL'
@token ||= login
end

def log_level=(level)
Expand Down Expand Up @@ -157,6 +158,15 @@ def refresh_login
self
end

# Delete the session on the appliance, invalidating the client's token.
# To generate a new token after calling this method, use the refresh_login method.
# Call this after a token expires or the user and/or password is updated on the client object.
# @return [OneviewSDK::Client] self
def destroy_session
response_handler(rest_delete('/rest/login-sessions'))
self
end

# Creates the image streamer client object.
# @param [Hash] options the options to configure the client
# @option options [Logger] :logger (Logger.new(STDOUT)) Logger object to use.
Expand Down Expand Up @@ -194,7 +204,7 @@ def login(retries = 2)
'body' => {
'userName' => @user,
'password' => @password,
'authLoginDomain' => 'LOCAL'
'authLoginDomain' => @domain
}
}
response = rest_post('/rest/login-sessions', options)
Expand Down
3 changes: 3 additions & 0 deletions lib/oneview-sdk/image-streamer/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ class Client < OneviewSDK::Client
undef :user=
undef :password
undef :password=
undef :domain
undef :domain=
undef :refresh_login
undef :destroy_session

# Creates client object, establish connection, and set up logging and api version.
# @param [Hash] options the options to configure the client
Expand Down
5 changes: 5 additions & 0 deletions spec/unit/cli/env_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
expect { command }.to output(/ONEVIEWSDK_PASSWORD\s+=\s'secret123'/).to_stdout_from_any_process
end

it 'shows ONEVIEWSDK_DOMAIN' do
ENV['ONEVIEWSDK_DOMAIN'] = 'Other'
expect { command }.to output(/ONEVIEWSDK_DOMAIN\s+=\s'Other'/).to_stdout_from_any_process
end

it 'shows ONEVIEWSDK_TOKEN' do
expect { command }.to output(/ONEVIEWSDK_TOKEN\s+=\s'secret456'/).to_stdout_from_any_process
end
Expand Down
21 changes: 21 additions & 0 deletions spec/unit/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
expect(client.user).to eq('Administrator')
end

it 'sets the domain to "LOCAL" by default' do
options = { url: 'https://oneview.example.com', token: 'secret123' }
client = OneviewSDK::Client.new(options)
expect(client.domain).to eq('LOCAL')
end

it 'respects credential environment variables' do
ENV['ONEVIEWSDK_USER'] = 'Admin'
ENV['ONEVIEWSDK_PASSWORD'] = 'secret456'
Expand All @@ -45,6 +51,12 @@
expect(client.token).to eq('secret456')
end

it 'respects the domain environment variable' do
ENV['ONEVIEWSDK_DOMAIN'] = 'Other'
client = OneviewSDK::Client.new(url: 'https://oneview.example.com', token: 'secret123')
expect(client.domain).to eq('Other')
end

it 'respects the ssl environment variable' do
ENV['ONEVIEWSDK_SSL_ENABLED'] = 'false'
client = OneviewSDK::Client.new(url: 'https://oneview.example.com', token: 'secret123')
Expand Down Expand Up @@ -274,6 +286,15 @@
end
end

describe '#destroy_session' do
include_context 'shared context'

it 'makes a REST call to destroy the session' do
expect(@client).to receive(:rest_delete).with('/rest/login-sessions').and_return(FakeResponse.new)
expect(@client.destroy_session).to eq(@client)
end
end

describe '#wait_for' do
include_context 'shared context'

Expand Down

0 comments on commit 5c57e2a

Please sign in to comment.