Skip to content

Commit

Permalink
edits 3-Jan-2017
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter McGowan committed Jan 3, 2017
1 parent 15eeeb8 commit 4c2b0aa
Show file tree
Hide file tree
Showing 12 changed files with 894 additions and 0 deletions.
135 changes: 135 additions & 0 deletions automation_request_approval/scripts/AutomationRequest_Pending.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#
# Description: This method is launched from the not_approved method which raises the requst_pending event
# when the provisioning request is NOT auto-approved
#
# Events: request_pending
#
# Model Notes:
# 1. to_email_address - used to specify an email address in the case where the
# requester does not have a valid email address.To specify more than one email
# address separate email address with commas. (I.e. [email protected],[email protected])
# 2. from_email_address - used to specify an email address in the event the
# requester replies to the email
# 3. signature - used to stamp the email with a custom signature
#

###################################
#
# Method: emailrequester
#
# Build email to requester with reason
#
###################################
def emailrequester(miq_request, appliance, msg)
$evm.log('info', "Requester email logic starting")

# Get requester object
requester = miq_request.requester

# Get requester email else set to nil
requester_email = requester.email || nil

# Get Owner Email else set to nil
owner_email = miq_request.options[:owner_email] || nil
$evm.log('info', "Requester email:<#{requester_email}> Owner Email:<#{owner_email}>")

# if to is nil then use requester_email or owner_email
to = nil
to ||= requester_email # || owner_email

# If to is still nil use to_email_address from model
to ||= $evm.object['to_email_address']

# Get from_email_address from model unless specified below
from = nil
from ||= $evm.object['from_email_address']

# Get signature from model unless specified below
signature = nil
signature ||= $evm.object['signature']

# Set email subject
subject = "Request ID #{miq_request.id} - Your Automation Request was not Auto-Approved"

# Build email body
body = "Hello, "
body += "<br>#{msg}."
body += "<br><br>Please review your Request and update or wait for approval from an Administrator."
body += "<br><br>To view this Request go to: "
body += "<a href='https://#{appliance}/miq_request/show/#{miq_request.id}'>https://#{appliance}/miq_request/show/#{miq_request.id}</a>"
body += "<br><br> Thank you,"
body += "<br> #{signature}"

# Send email to requester
$evm.log('info', "Sending email to <#{to}> from <#{from}> subject: <#{subject}>")
$evm.execute(:send_email, to, from, subject, body)
end

###################################
#
# Method: emailapprover
#
# Build email to approver with reason
#
###################################
def emailapprover(miq_request, appliance, msg)
$evm.log('info', "Approver email logic starting")

# Get requester object
requester = miq_request.requester

# Get requester email else set to nil
requester_email = requester.email || nil

# Get Owner Email else set to nil
owner_email = miq_request.options[:owner_email] || nil
$evm.log('info', "Requester email:<#{requester_email}> Owner Email:<#{owner_email}>")

# Override to email address below or get to_email_address from from model
to = nil
to ||= $evm.object['to_email_address']

# Override from_email_address below or get from_email_address from model
from = nil
from ||= $evm.object['from_email_address']

# Get signature from model unless specified below
signature = nil
signature ||= $evm.object['signature']

# Set email subject
subject = "Request ID #{miq_request.id} - Automation request was not approved"


# Build email body
body = "Approver, "
body += "<br>An automation request received from #{requester_email} is pending."
body += "<br><br>#{msg}."
body += "<br><br>For more information you can go to: <a href='https://#{appliance}/miq_request/show/#{miq_request.id}'>https://#{appliance}/miq_request/show/#{miq_request.id}</a>"
body += "<br><br> Thank you,"
body += "<br> #{signature}"

# Send email to approver
$evm.log('info', "Sending email to <#{to}> from <#{from}> subject: <#{subject}>")
$evm.execute(:send_email, to, from, subject, body)
end

# Get miq_request from root
miq_request = $evm.root['miq_request']
raise "miq_request missing" if miq_request.nil?
$evm.log('info', "Detected Request:<#{miq_request.id}> with Approval State:<#{miq_request.approval_state}>")

# Override the default appliance IP Address below
appliance = nil
# appliance ||= 'evmserver.company.com'
appliance ||= $evm.root['miq_server'].ipaddress

# Get incoming message or set it to default if nil
msg = miq_request.resource.message || "Request pending"

# Email Requester
emailrequester(miq_request, appliance, msg)

# Email Approver
emailapprover(miq_request, appliance, msg)

6 changes: 6 additions & 0 deletions automation_request_approval/scripts/approve_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#
# Description: This method is executed when the automation request is auto-approved
#
# Auto-Approve request
$evm.log("info", "AUTO-APPROVING automation request")
$evm.root["miq_request"].approve("admin", "Auto-Approved")
9 changes: 9 additions & 0 deletions automation_request_approval/scripts/pending_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# Description: This method is executed when the automation request is NOT auto-approved
#
# Get objects
msg = $evm.object['reason']
$evm.log('info', "#{msg}")

# Raise automation event: request_pending
$evm.root["miq_request"].pending
15 changes: 15 additions & 0 deletions automation_request_approval/scripts/validate_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
request = $evm.root['miq_request']
resource = request.resource
raise "Automation Request not found" if request.nil? || resource.nil?

$evm.log("info", "Checking for auto_approval")
approval_type = $evm.object['approval_type'].downcase
if approval_type == 'auto'
$evm.root["miq_request"].approve("admin", "Auto-Approved")
$evm.root['ae_result'] = 'ok'
else
msg = "Request was not auto-approved"
resource.set_message(msg)
$evm.root['ae_result'] = 'error'
$evm.object['reason'] = msg
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#!/usr/bin/env ruby
#
# service_via_api
#
# Author: Peter McGowan ([email protected])
# Copyright 2016 Peter McGowan, Red Hat
#
# Revision History
#
require 'rest-client'
require 'json'
require 'optparse'

begin
options = {
:server => nil,
:username => nil,
:password => nil,
:request => nil,
:reason => nil,
:action => nil,
}
parser = OptionParser.new do|opts|
opts.banner = "Usage: service_via_api.rb [options]"
opts.on('-s', '--server server', 'CloudForms server to connect to') do |server|
options[:server] = server
end
opts.on('-u', '--username username', 'Username to connect as') do |username|
options[:username] = username
end
opts.on('-p', '--password password', 'Password') do |password|
options[:password] = password
end
opts.on('-i', '--request id', 'Request ID to approve or deny') do |request|
options[:request] = request
end
opts.on('-a', '--action action', '"approve" or "deny"') do |action|
options[:action] = action
end
opts.on('-r', '--reason reason', 'Reason for approving or denying') do |reason|
options[:reason] = reason
end
opts.on('-h', '--help', 'Displays Help') do
puts opts
exit!
end
end
parser.parse!

if options[:server].nil?
server = "myserver"
else
server = options[:server]
end
if options[:username].nil?
username = "admin"
else
username = options[:username]
end
if options[:password].nil?
password = "smartvm"
else
password = options[:password]
end
if options[:request].nil?
puts "Missing request ID (--request option)"
exit!
end
unless ["approve", "deny"].include?(options[:action])
puts "Must specify either 'approve' or 'deny' for --action"
exit!
end
if options[:reason].nil?
puts "Missing reason (--reason option)"
exit!
end

api_uri = "https://#{server}/api"

#
# Get an authentication token
#
url = URI.encode(api_uri + '/auth')
rest_response = RestClient::Request.execute(method: :get,
url: url,
:user => username,
:password => password,
:headers => {:accept => :json},
verify_ssl: false)
auth_token = JSON.parse(rest_response)['auth_token']
raise "Couldn't get an authentication token" if auth_token.nil?

url = URI.encode(api_uri + "/automation_requests/#{options[:request]}")
#
# Get the current approval status
#
rest_response = RestClient::Request.execute(method: :get,
url: url,
:headers => {:accept => :json,
'x-auth-token' => auth_token},
verify_ssl: false)
result = JSON.parse(rest_response)
approval_state = result['approval_state']
puts "Current approval state for request #{options[:request]} is #{approval_state}"
#
# Issue the approve/deny
#
post_params = {
"action" => options[:action],
"reason" => options[:reason]
}.to_json
rest_response = RestClient::Request.execute(method: :post,
url: url,
:headers => {:accept => :json,
'x-auth-token' => auth_token},
:payload => post_params,
verify_ssl: false)
result = JSON.parse(rest_response)
puts "#{result['message']}"

rescue RestClient::Exception => err
unless err.response.nil?
error = err.response
puts "The REST request failed with code: #{error.code}"
puts "The response body was:"
puts JSON.pretty_generate JSON.parse(error.body)
end
exit!
rescue => err
puts "[#{err}]\n#{err.backtrace.join("\n")}"
exit!
end
Loading

0 comments on commit 4c2b0aa

Please sign in to comment.