class ServiceAPI

Public Instance Methods

add_param(params, param) click to toggle source
# File lib/ServiceAPI.rb, line 65
def add_param(params, param)
  if (param == nil || param.length() == 0)
    return params
  end
  if (params == nil || params.length() == 0)
    return "?#{param}"
  end
  return "#{params}&#{param}"
end
do_request_by_service(req, state, service) click to toggle source
# File lib/ServiceAPI.rb, line 148
def do_request_by_service(req, state, service)
  service_list = state.get('service_list')
  if (service_list == nil)
    return Result.fatal_error("service_list not available in state!")
  end

  if (service_list[service] == nil)
    return Result.fatal_error("No service of type #{service} available!")
  end

  url = service_list[service][0]
  return do_request_by_url(req, state, url)
end
do_request_by_url(req, state, url) click to toggle source
# File lib/ServiceAPI.rb, line 167
def do_request_by_url(req, state, url)
  show_req(req)
  response = get_http_response(req, state, url)
  show_response(response)

  state.set_in_test('http_code', response.code)
  result = Result.new(response)

  return result
end
get_http_response(req, state, url) click to toggle source
# File lib/ServiceAPI.rb, line 110
def get_http_response(req, state, url)

  # TODO: SSL support

  auth = state.get('auth')
  if (auth == "basic")
    user = state.get('auth_user')
    password = state.get('auth_password')
    req.basic_auth(user, password)
  end

  if (url == nil)
    server = state.get('server')
    port = state.get('server_port')
    die("no server specified") if (server == nil)
    die("no server port specified") if (port == nil)
    url = "http://#{server}:#{port}"
  end

  begin
    urlobj = URI.parse(url)
    http = Net::HTTP.new(urlobj.host, urlobj.port)
    out(1, "Connecting to: #{urlobj.to_s}")
    response = http.request(req)

  rescue Exception => e
    return Result.fatal_error("Failure connecting to server #{server}: #{e.to_s}")
  end

  return response
end
need(symbol, state) click to toggle source
# File lib/ServiceAPI.rb, line 29
def need(symbol, state)
  x = state.get(symbol.to_s)
  if (x == nil)
    return "return Result.fatal_error(\"#{symbol.to_s} required\")"
  end
  return "#{symbol.to_s} = state.get('#{symbol.to_s}')"
end
show_req(request) click to toggle source
# File lib/ServiceAPI.rb, line 78
def show_req(request)
  if ($LOG_LEVEL >= 2)
    puts "---[ Request ]----------------------------------------------------"
    puts "#{request.method} #{request.path}"
    request.each_header { |name,value|
      puts "#{name}: #{value}"
    }
    puts "\n#{request.body}\n"
    puts "------------------------------------------------------------------"
  end
end
show_response(response) click to toggle source
# File lib/ServiceAPI.rb, line 93
def show_response(response)
  if ($LOG_LEVEL >= 2)
    puts "---[ Response ]---------------------------------------------------"
    puts "#{response.code} #{response.message}"
    response.each_header { |name,value|
      puts "#{name}: #{value}"
    }
    puts "\n#{response.body}\n"
    puts "------------------------------------------------------------------"
  end
end
uuid() click to toggle source
# File lib/ServiceAPI.rb, line 42
def uuid
  begin
    require 'securerandom'
    uuid = SecureRandom.uuid()

  rescue Exception => e
    if (File.exist?("/usr/bin/uuidgen")) # Centos e2fsprogs package
      uuid = `/usr/bin/uuidgen`
      return uuid.chomp

    elsif (File.exist?("/usr/bin/uuid")) # Debian uuid package
      uuid = `/usr/bin/uuid`
      return uuid.chomp

    else
      die("Unable to generate UUIDs")
    end
  end
end