class OcpClient

Attributes

response[R]
url[R]

Public Class Methods

new() click to toggle source
# File lib/client/OcpClient.rb, line 33
    def initialize
            @method = nil
            @url = nil
            @token = nil
            @noverifyssl = false
@pretty = false
@clientcert = nil
@clientkey = nil
@clientca = nil
@clientcafile = nil
@debug = nil
@response = nil
    end

Public Instance Methods

delete(location) click to toggle source
# File lib/client/OcpClient.rb, line 123
def delete(location)
  @method = :delete
  return call(location,nil,:delete)
end
get(location) click to toggle source
# File lib/client/OcpClient.rb, line 108
def get(location)
  @method = :get
        return call(location,nil,:get)
end
post(location, body) click to toggle source
# File lib/client/OcpClient.rb, line 113
def post(location, body)
  @method = :post
  return call(location,body,:post)
end
put(location, body) click to toggle source
# File lib/client/OcpClient.rb, line 118
def put(location, body)
  @method = :put
  return call(location,body,:put)
end
setup(url, noverifyssl, pretty, debug, token, clientcertfile, clientkeyfile, clientcafile) click to toggle source
# File lib/client/OcpClient.rb, line 47
def setup(url, noverifyssl, pretty, debug, token,
              clientcertfile, clientkeyfile, clientcafile)
  @noverifyssl = noverifyssl
  @pretty = pretty
  @debug = debug

  @url = url

  unless clientcertfile.nil? or clientkeyfile.nil?
    @clientcertfile = clientcertfile
    @clientkeyfile = clientkeyfile
  end

  unless clientcafile.nil?
    @clientcafile = clientcafile
  end

  unless @clientcertfile.nil? or @clientkeyfile.nil?
    @clientcert = OpenSSL::X509::Certificate.new File.read @clientcertfile
    @clientkey = OpenSSL::PKey::RSA.new File.read @clientkeyfile
  end

  unless @clientcafile.nil?
    @clientca = OpenSSL::X509::Certificate.new File.read @clientcafile
  end

  unless token.nil?
    @token = token
  end

  validate_options

end
setup_by_config_file(configfile, pretty, debug) click to toggle source
# File lib/client/OcpClient.rb, line 81
def setup_by_config_file(configfile, pretty, debug)
  config = read_config(configfile)
  @debug=debug
  @pretty=pretty

  unless config.nil?
    @token = config["connection"]["token"]
    @url = config["connection"]["master"]
    @noverifyssl = config["connection"]["no_verify_ssl"]
    @clientcertfile = config["connection"]["client-certificate-file"]
    @clientkeyfile = config["connection"]["client-key-file"]
    @clientcafile = config["connection"]["client-ca-file"]
  end

  unless @clientcertfile.nil? or @clientkeyfile.nil?
    @clientcert = OpenSSL::X509::Certificate.new File.read @clientcertfile
    @clientkey = OpenSSL::PKey::RSA.new File.read @clientkeyfile
  end

  unless @clientcafile.nil?
    @clientca = OpenSSL::X509::Certificate.new File.read @clientcafile
  end

  validate_options
end

Private Instance Methods

call(location, body, method) click to toggle source
# File lib/client/OcpClient.rb, line 130
def call(location, body, method)
  if !@clientcert.nil? and !@clientkey.nil?
    if @debug
      puts "Calling by Certificate"
    end
    return callbycert(location, body, method)
  else
    if @debug
      puts "Calling by Token"
    end
    return callbytoken(location, body, method)
  end
end
callbycert(location, body=nil, method) click to toggle source
# File lib/client/OcpClient.rb, line 195
def callbycert(location, body=nil, method)

  if @debug
    puts "Client Cert - #{@clientcert}"
    puts "Client Key - #{@clientkey}"
    puts "CA File - #{@clientcafile}"
    puts "URL - #{@url}#{location}"
    puts "Method - #{method}"
    unless body.nil?
      puts "Body  - #{JSON.pretty_generate(body)}"
    end
  end

  response = nil
  begin
    if body.nil?
      response = RestClient::Request.new(
          :method => method,
          :url => @url+location+"?"+query_params,
          :verify_ssl => !@noverifyssl,
          :ssl_client_cert => @clientcert,
          :ssl_client_key => @clientkey,
          :ssl_ca_file => @clientcafile,
          :headers => { :accept => :json,
          :content_type => :json}
      ).execute
    else
      response = RestClient::Request.new(
          :method => method,
          :url => @url+location+"?"+query_params,
          :verify_ssl => !@noverifyssl,
          :ssl_client_cert => @clientcert,
          :ssl_client_key => @clientkey,
          :ssl_ca_file => @clientcafile,
          :headers => { :accept => :json,
          :content_type => :json},
          :payload => JSON.generate(body)
      ).execute
    end
  rescue => exception
    if @debug and !exception.nil?
      puts "Exception: " << exception.to_s
    end
    @response = exception.to_s
  end

  if !response.nil? and @pretty
    results = JSON.pretty_generate(JSON.parse(response.to_str))
    @response = results
  elsif !response.nil?
    @response = response
  end

  return @response

end
callbytoken(location, body=nil, method) click to toggle source
# File lib/client/OcpClient.rb, line 144
def callbytoken(location, body=nil, method)

  if @debug
    puts "Client Token - #{@token}"
    puts "URL - #{@url}#{location}"
    puts "Method - #{method}"
    unless body.nil?
      puts "Body  - #{JSON.pretty_generate(body)}"
    end
  end

  response = nil
  begin
    if body.nil?
      response = RestClient::Request.new(
          :method => method,
          :url => @url+location+"?"+query_params,
          :verify_ssl => !@noverifyssl,
          :headers => { :accept => :json,
          :content_type => :json,
          :Authorization => "Bearer #{@token}"}
      ).execute
    else
      response = RestClient::Request.new(
          :method => method,
          :url => @url+location+"?"+query_params,
          :verify_ssl => !@noverifyssl,
          :headers => { :accept => :json,
          :content_type => :json,
          :Authorization => "Bearer #{@token}"},
          :payload => JSON.generate(body)
      ).execute
    end
  rescue => exception
    if @debug and !exception.nil?
      puts "Exception: " << exception.to_s
    end
    @response = exception.to_s
  end

  if !response.nil? and @pretty
    results = JSON.pretty_generate(JSON.parse(response.to_str))
    @response = results
  elsif !response.nil?
    @response = response
  end

  return @response

end
query_params() click to toggle source
# File lib/client/OcpClient.rb, line 252
def query_params
  return "pretty=#{@pretty}"
end
read_config(configfile) click to toggle source
# File lib/client/OcpClient.rb, line 265
def read_config(configfile)
    begin
      unless configfile.nil?
        return YAML.load_file(configfile)
      else
        return YAML.load_file("config.yaml")
      end
    rescue => exception
      puts exception.message
      puts "Unable to read config file #{configfile}.  Proceeding"
    end
end
validate_options() click to toggle source
# File lib/client/OcpClient.rb, line 256
def validate_options

  if @token.nil? and (@clientcertfile.nil? or @clientkeyfile.nil?)
    raise MissingRequiredOptionException.new("token OR clientcertfile AND clientkeyfile")
  elsif @url.nil?
    raise MissingRequiredOptionException.new("master")
  end
end