Skip to content

Commit

Permalink
Merge pull request #221 from HewlettPackard/scmb_module
Browse files Browse the repository at this point in the history
Added SCMB module and CLI command
  • Loading branch information
jsmartt authored Apr 18, 2017
2 parents f44f3c5 + b0d0268 commit 9c8596c
Show file tree
Hide file tree
Showing 10 changed files with 332 additions and 20 deletions.
35 changes: 19 additions & 16 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# Unreleased Changes
## Suggested release: v4.2.1
##### Suggested release: v4.3.0

#### New Features:
- Added SCMB module and CLI command

#### Bug fixes & Enhancements:
- [#222](https://github.com/HewlettPackard/oneview-sdk-ruby/issues/222) Error listing the OS Deployment Plans from OneView

# v4.2.0
## v4.2.0

#### New Resources:
- OS Deployment Plan
Expand All @@ -22,7 +25,7 @@
#### Design changes:
- Architecture for future API500 support. Features for API500 are not yet supported.

# v4.1.0
## v4.1.0

#### New Resources:
Added full support to Image Streamer Rest API version 300:
Expand Down Expand Up @@ -77,7 +80,7 @@ Added full support to Image Streamer Rest API version 300:
- Deployment Plan (unimplemented)
- Golden Image (unimplemented)

# v3.1.0
## v3.1.0
Added full support to OneView Rest API version 300 for the hardware variants C7000 and Synergy to the already existing features:
- Interconnect
- Logical Interconnect
Expand Down Expand Up @@ -133,12 +136,12 @@ Added full support to OneView Rest API version 300 for the hardware variants C70
- Unmanaged devices

# v3.0.0
### Notes
#### Notes
This is the Third major version of the Ruby SDK for HPE OneView. It features a split in the API support, allowing for C7000 and Synergy hardware variants to be used, while maintaining compatibility to older versions. There are some code improvements applied throughout the release, as well as additional endpoints support.
This version of this SDK officially supports OneView appliances version 3.00.00 or higher, using the OneView Rest API version 300.
Support is provided for C7000 and Synergy enclosure types.

### Major changes
#### Major changes
1. Added full support to OneView Rest API version 300 for the hardware variants C7000 and Synergy to the already existing features:
- Connection template
- Datacenter
Expand Down Expand Up @@ -182,23 +185,23 @@ Added full support to OneView Rest API version 300 for the hardware variants C70
- Split features into API modules for each hardware variant
- Fixed/updated/added CLI commands

### v2.2.1
## v2.2.1
- Fixed issue #88 (firmware bundle file size). Uses multipart-post now

### v2.2.0
## v2.2.0
- Added the 'rest' and 'update' commands to the CLI
- Removed the --force option from the create_from_file CLI command

### v2.1.0
## v2.1.0
- Fixed issue with the :resource_named method for OneViewSDK::Resource in Ruby 2.3

# v2.0.0
### Notes
#### Notes
This is the second version of the Ruby SDK for HPE OneView. It was given support to the major features of OneView, refactor in some of the already existing code, and also a full set of exceptions to make some common exceptions more explicit in the debugging process.
This version of this SDK officially supports OneView appliances version 2.00.00 or higher, using the OneView Rest API version 200.
For now only C7000 enclosure types are being supported.

### Major changes
#### Major changes
1. Added full support to the already existing features:
- Server Profile
- Server Profile Template
Expand All @@ -219,7 +222,7 @@ Added full support to OneView Rest API version 300 for the hardware variants C70
- Server hardware types
3. New exceptions to address the most common issues (Check them in *lib/oneview-sdk/resource/exceptions.rb*)

### Breaking changes
#### Breaking changes
1. Refactored some method names that may cause incompatibility with older SDK versions. Due to the nature of OneView, the `create` and `delete` methods did not fit the physical infrastructure elements like Enclosures, or Switches, so they now have `add` and `remove` methods that act the same as before, but now it leaves no margin to misunderstand that OneView could actually create these resources. They are:
- Datacenters
- Enclosure
Expand All @@ -235,7 +238,7 @@ Added full support to OneView Rest API version 300 for the hardware variants C70
- Switches
- Unmanaged devices

### Features supported
#### Features supported
- Ethernet network
- FC network
- FCOE network
Expand Down Expand Up @@ -271,13 +274,13 @@ Added full support to OneView Rest API version 300 for the hardware variants C70
- Unmanaged devices

# v1.0.0
### Notes
#### Notes
This is the first release of the OneView SDK in Ruby and it adds full support to some core features listed bellow, with some execeptions that are explicit.
This version of this SDK supports OneView appliances version 2.00.00 or higher, using the OneView Rest API version 200.
For now it only supports C7000 enclosure types.


### Features supported
#### Features supported
- Ethernet Network
- FC Network
- FCoE Network
Expand All @@ -298,7 +301,7 @@ Added full support to OneView Rest API version 300 for the hardware variants C70
- Server Profile Template (CRUD supported)
- Server Hardware (CRUD Supported)

### Known issues
#### Known issues
The integration tests may warn about 3 issues:

1. OneviewSDK::LogicalInterconnect Firmware Updates perform the actions Stage
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The OneView SDK provides a Ruby library to easily interact with HPE OneView and
- Require the gem in your Gemfile:

```ruby
gem 'oneview-sdk', '~> 4.2'
gem 'oneview-sdk', '~> 4.3'
```

Then run `$ bundle install`
Expand Down Expand Up @@ -410,6 +410,13 @@ $ oneview-sdk-ruby cert import https://oneview.example.com
Cert added to '/home/users/user1/.oneview-sdk-ruby/trusted_certs.cer'
```

##### Subscribe to the OneView State Change Message Bus (SCMB):

```bash
$ oneview-sdk-ruby scmb
$ oneview-sdk-ruby scmb -r 'scmb.ethernet-networks.#'
```

## License
This project is licensed under the Apache 2.0 license. Please see [LICENSE](LICENSE) for more info.

Expand Down
45 changes: 45 additions & 0 deletions examples/scmb.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# (c) Copyright 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 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
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

require_relative '_client' # Gives access to @client

# You can create a connection to the SCMB very easily; your client object has everything
# the ::new_connection method needs to connect:
connection = OneviewSDK::SCMB.new_connection(@client)

# The ::new_connection method above does some setup tasks for you if needed, including
# creating a keypair for the default RabbitMQ user on OneView (rabitmq_readonly)

# Now you can create a queue that will be used to subscribe to messages. By default it
# will use the least specific routing key possible, so it will respond to ALL events:
q = OneviewSDK::SCMB.new_queue(connection)
puts "Created queue: #{q.name}"

# More likely, you'll want to subscribe to a smaller subset of events. For example, to
# subscribe to only messages posted when an ethernet network is created:
eth_create_q = OneviewSDK::SCMB.new_queue(connection, 'scmb.ethernet-networks.Created.#')
puts "Created queue: #{eth_create_q.name}\n\n"

# Here are some other routing key options for the ethernet network resource:
# 'scmb.ethernet-networks.#' -> Any ethernet network event
# 'scmb.ethernet-networks.Updated.<resource_uri>' -> Only when a specific network is updated

puts 'Subscribing to OneView messages. To exit, press Ctrl + c'

# Then when you're ready to start listening, subscribe to a queue. Here we'll just print
# out the message, but you'll insert your own logic in the block below. Also, see
# http://rubybunny.info/articles/queues.html for more details & options.
q.subscribe(block: true) do |_delivery_info, _properties, payload|
data = JSON.parse(payload) rescue payload
puts 'Received message with payload:'
pp data # Pretty print
puts "\n#{'=' * 50}\n"
end
3 changes: 2 additions & 1 deletion lib/oneview-sdk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
require_relative 'oneview-sdk/client'
require_relative 'oneview-sdk/resource'
Dir[File.dirname(__FILE__) + '/oneview-sdk/resource/*.rb'].each { |file| require file }
require_relative 'oneview-sdk/cli'
require_relative 'oneview-sdk/scmb'
require_relative 'oneview-sdk/image_streamer'
require_relative 'oneview-sdk/cli'

# Module for interacting with the HPE OneView API
module OneviewSDK
Expand Down
29 changes: 28 additions & 1 deletion lib/oneview-sdk/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def execute!

class_option :ssl_verify,
type: :boolean,
desc: 'Enable/Disable SSL verification for requests. Can also use ENV[\'ONEVIEWSDK_SSL_ENABLED\']',
desc: 'Enable/Disable SSL verification for requests. Uses ENV[\'ONEVIEWSDK_SSL_ENABLED\']',
default: nil

class_option :url,
Expand Down Expand Up @@ -428,6 +428,33 @@ def cert(type, url = ENV['ONEVIEWSDK_URL'])
fail_nice e.message
end

method_option :route,
desc: 'Routing key to filter messages',
type: :string,
aliases: '-r',
default: OneviewSDK::SCMB::DEFAULT_ROUTING_KEY
method_option :format,
desc: 'Output format',
aliases: '-f',
enum: %w(json yaml raw),
default: 'json'
scmb_examples = "\n oneview-sdk-ruby scmb -r 'scmb.ethernet-networks.#'"
scmb_examples << "\n oneview-sdk-ruby scmb -r 'scmb.ethernet-networks.Created'"
scmb_examples << "\n oneview-sdk-ruby scmb -r 'scmb.ethernet-networks.Updated.<resource_uri>'"
desc 'scmb', "Subscribe to the OneView State Change Message Bus. Examples:#{scmb_examples}"
# Subscribe to the OneView SCMB
def scmb
client_setup
connection = OneviewSDK::SCMB.new_connection(@client)
q = OneviewSDK::SCMB.new_queue(connection, @options['route'])
puts 'Subscribing to OneView messages. To exit, press Ctrl + c'
q.subscribe(block: true) do |_delivery_info, _properties, payload|
data = JSON.parse(payload) rescue payload
puts "\n#{'=' * 50}\n\nReceived message with payload:"
@options['format'] == 'raw' ? puts(payload) : output(data)
end
end

private

def fail_nice(msg = nil, exit_code = 1)
Expand Down
71 changes: 71 additions & 0 deletions lib/oneview-sdk/scmb.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# (c) Copyright 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 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
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

require_relative 'client'
require 'bunny'

module OneviewSDK
# State Schange Message Bus (SCMB) helper
module SCMB
DEFAULT_ROUTING_KEY = 'scmb.#'.freeze

# Create a new connection to the message bus
# @param [OneviewSDK::Client] client The client object for the OneView appliance
# @param [Hash] opts Connection options (passed to Bunny.new). Defaults:
# port: 5671
# auth_mechanism: 'EXTERNAL'
# tls: true
# verify_peer: client.ssl_enabled
# tls_cert: (retrieved automatically from OneView)
# tls_key: (retrieved automatically from OneView)
# tls_ca_certificates: System default CA (unless verify_peer is false)
# See http://rubybunny.info/articles/connecting.html for more details & options
# @return [Bunny::Session] Connection to the message bus
def self.new_connection(client, opts = {})
con_opts = {
port: 5671,
auth_mechanism: 'EXTERNAL',
tls: true,
verify_peer: client.ssl_enabled,
logger: client.logger
}
con_opts.merge!(opts)
con_opts[:host] = URI.parse(client.url).host
unless con_opts[:tls_cert] && con_opts[:tls_key]
kp = get_or_create_keypair(client)
con_opts[:tls_cert] = kp['base64SSLCertData']
con_opts[:tls_key] = kp['base64SSLKeyData']
end
Bunny.new(con_opts).start
end

# Retrieve or create the default RabbitMQ keypair
# @param [OneviewSDK::Client] client The client object for the OneView appliance
# @return [Hash] Keypair details
def self.get_or_create_keypair(client)
client.response_handler(client.rest_get('/rest/certificates/client/rabbitmq/keypair/default'))
rescue OneviewSDK::NotFound # Create the keypair if it doesn't exist
client.logger.info('RabbitMQ default keypair not found. Creating it now.')
opts = { commonName: 'default', type: 'RabbitMqClientCertV2' }
client.response_handler(client.rest_post('/rest/certificates/client/rabbitmq', body: opts))
# Retrieve the created key
client.response_handler(client.rest_get('/rest/certificates/client/rabbitmq/keypair/default'))
end

# @param [Bunny::Session] Connection to the message bus. See ::new_connection
# @return [Bunny::Queue] Queue listening to the specified routing key
def self.new_queue(connection, routing_key = DEFAULT_ROUTING_KEY)
ch = connection.create_channel
q = ch.queue('')
q.bind('scmb', routing_key: routing_key)
end
end
end
2 changes: 1 addition & 1 deletion lib/oneview-sdk/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@

# Gem version defined here
module OneviewSDK
VERSION = '4.2.0'.freeze
VERSION = '4.3.0'.freeze
end
1 change: 1 addition & 0 deletions oneview-sdk.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency 'highline'
spec.add_runtime_dependency 'pry'
spec.add_runtime_dependency 'multipart-post'
spec.add_runtime_dependency 'bunny'

spec.add_development_dependency 'coveralls'
spec.add_development_dependency 'bundler'
Expand Down
Loading

0 comments on commit 9c8596c

Please sign in to comment.