Sample Ruby Script for Accessing Restful Web Services - Documentation for Truesight Server Automation 21.3
###
# group_name:
#   name: Name of Group
# job_name:
#   name: Name of Job
###
# Flag the script for direct execution
require 'net/http'
require 'net/https'
require 'uri'
require 'rexml/document'
require 'io/console'
# Utility to read in password values without echoing to screen
if STDIN.respond_to?(:noecho)
    def get_password(prompt="Password: ")
        print prompt
        STDIN.noecho(&:gets).chomp
    end
else
    def get_password(prompt="Password: ")
        `read -s -p "#{prompt}" password; echo $password`.chomp
    end
end
# utility to prompt and read value from screen
def get_value(default, prompt)
    print "#{prompt} (#{default}) : "
    result = gets.chomp
    return result.empty? ? default: result
end
# Get values for app server, credentials and job name
host = get_value("bl-appserver", "BladeLogic app server")
username = get_value("BLAdmin", "Username")
password = get_password("Password: ")
role = get_value("BLAdmins", "Role")
authtype = get_value("SRP", "Authorization Type")
group_name = get_value("/Test", "Job Group Name")
job_name = get_value("brpmtest", "Job Name")
# construct authentication string used in URL
auth_string = "?username=#{username}&authType=#{authtype}&password=#{password}&role=#{role}"
# construct URL for execution of the job
url=""
uri = URI.parse(URI.encode(url))
# Read in certificate from blappserver.pem
cert_store = OpenSSL::X509::Store.new
cert_store.add_file 'blappserver.pem'
# create https connection, using the certificate store.
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.cert_store = cert_store
# POST request to BladeLogic
request = Net::HTTP::Post.new(uri.request_uri)
response = http.request(request)
# Create XML Document object with response body
doc = REXML::Document.new response.body
# Search for error in document.
cl = REXML::XPath.first(doc,'//Error')
if cl then
    # If error, show text
    puts cl.text
else
    # If no error, get Operation Result, which provides the value to interrogate the running job
    cl = REXML::XPath.first(doc,'//OperationResult')
    if cl then
        puts cl.attributes["value"]
        url=""value"]}#{auth_string}"
        uri = URI.parse(URI.encode(url))
        status = "unknown"
        # Loop checking Job status every 5 seconds
        until status == "COMPLETE"
            sleep(5)
            # GET request to BladeLogic to get job status
            request = Net::HTTP::Get.new(uri.request_uri)
            response = http.request(request)
            statusd = REXML::Document.new response.body
            # Search for Status in document
            cl = REXML::XPath.first(statusd,'//Status')
            if cl then
                puts cl.attributes["status"]
                status = cl.attributes["status"]
            else
                puts "couldn't find status"
                puts statusd
            end
        end
        # Print results looking at attributes hadErrors and hadWarnings
        puts "The rumours that there were errors are totally " + cl.attributes["hadErrors"]
        puts "The rumours that there were warnings are totally " + cl.attributes["hadWarnings"]
    end
end

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest