-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #221 from HewlettPackard/scmb_module
Added SCMB module and CLI command
- Loading branch information
Showing
10 changed files
with
332 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,5 +11,5 @@ | |
|
||
# Gem version defined here | ||
module OneviewSDK | ||
VERSION = '4.2.0'.freeze | ||
VERSION = '4.3.0'.freeze | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.