class DTK::Client::Response

TODO: should make higher level class be above whether it is 'rest'

Attributes

print_error_table[RW]

:render_view => symbol specifing type of data to be rendered e.g. :assembly :skip_render => flag that specifies that render is not needed (default: false) :print_error_table => we use it if we want to print 'error legend' for given tables (default: false)

render_view[RW]

:render_view => symbol specifing type of data to be rendered e.g. :assembly :skip_render => flag that specifies that render is not needed (default: false) :print_error_table => we use it if we want to print 'error legend' for given tables (default: false)

skip_render[RW]

:render_view => symbol specifing type of data to be rendered e.g. :assembly :skip_render => flag that specifies that render is not needed (default: false) :print_error_table => we use it if we want to print 'error legend' for given tables (default: false)

Public Class Methods

handle_error_in_wrapper(exception) click to toggle source
# File lib/domain/response.rb, line 73
def self.handle_error_in_wrapper(exception)
  error_hash =  {
    "message"=> exception.message,
    "backtrace" => exception.backtrace,
    "on_client" => true
    }

  if DTK::Configuration.get(:development_mode)
    DtkLogger.instance.error_pp("Error inside wrapper DEV ONLY: #{exception.message}", exception.backtrace)
  end

  Error::Internal.new(error_hash)
end
new(command_class=nil,hash={}) click to toggle source
Calls superclass method
# File lib/domain/response.rb, line 40
def initialize(command_class=nil,hash={})
  super(hash)
  @command_class     = command_class
  @skip_render       = false
  @print_error_table = false
  # default values
  @render_view = RenderView::AUG_SIMPLE_LIST
  @render_data_type = nil
end
wrap_helper_actions(data={}) { |: data)| ... } click to toggle source
# File lib/domain/response.rb, line 54
def self.wrap_helper_actions(data={},&block)
  begin
    results = (block ? yield : data)
    Ok.new(results)

  rescue Git::GitExecuteError => e
    if e.message.include?('Please make sure you have the correct access rights')
      error_msg  = "You do not have git access from this client, please add following SSH key in your git account: \n\n"
      error_msg += SSHUtil.rsa_pub_key_content() + "\n"
      raise DTK::Client::DtkError, error_msg
    end
    handle_error_in_wrapper(e)
  rescue ErrorUsage => e
    Error::Usage.new("message"=> e.to_s)
  rescue => e
    handle_error_in_wrapper(e)
  end
end

Public Instance Methods

array_to_string(array_data) click to toggle source
# File lib/domain/response.rb, line 174
def array_to_string(array_data)
  info = ""
  array_data.each do |a|
    info << "#{a.values.first},"
  end

  "#{info.gsub!(/,$/,'')}"
end
clone_me() click to toggle source
# File lib/domain/response.rb, line 50
def clone_me()
  return Marshal.load(Marshal.dump(self))
end
get_custom_labels(label, type) click to toggle source
# File lib/domain/response.rb, line 160
def get_custom_labels(label, type)
  mappings = {}

  mappings["module"] = {
    "id" => "ID:",
    "display_name" => "NAME:",
    "versions" => "VERSION(S):",
    "remote_repos" => "LINKED REMOTE(S):",
    "dsl_parsed" => "DSL PARSED:"
  }

  mappings[type][label] if mappings[type]
end
get_label_for_column_name(column, type) click to toggle source
# File lib/domain/response.rb, line 87
def get_label_for_column_name(column, type)
  if type.eql?('node')
    mappings = {
      column => column
    }
  else
    mappings = {
      "#{type}_id:" => "ID:",
      "#{type}_name:" => "NAME:",
      "node_type:" => "TYPE:",
      "instance_id:" => "INSTANCE ID:",
      "size:" => "SIZE:",
      "os_type:" => "OS:",
      "op_status:" => "OP STATUS:",
      "dns_name:" => "DNS NAME:",
      "target:" => "TARGET:"
    }
  end

  mappings[column]
end
override_command_class(json_top_type) click to toggle source

Amar: had to add this in order to get json meta for PP that's not for domain class

# File lib/domain/response.rb, line 36
def override_command_class(json_top_type)
  @command_class = json_top_type
end
render_arg_list!() click to toggle source
# File lib/domain/response.rb, line 211
def render_arg_list!
  @render_view = RenderView::AUG_SIMPLE_LIST
end
render_custom_info(type) click to toggle source
# File lib/domain/response.rb, line 146
def render_custom_info(type)
  puts "--- \n"

  unless data.empty?
    data.each do |k,v|
      label = get_custom_labels(k, type)
      v = array_to_string(v) if v.is_a?(Array)
      STDOUT << " #{label} #{v}\n" if label
    end
  end

  puts "\n"
end
render_data(print_error_table=false) click to toggle source
# File lib/domain/response.rb, line 184
def render_data(print_error_table=false)
  unless @skip_render
    if ok?()

      @print_error_table ||= print_error_table

      # if response is empty, response status is ok but no data is passed back
      if data.empty? or (data.is_a?(Array) ? data.first.nil? : data.nil?)
        @render_view = RenderView::SIMPLE_LIST
        if data.kind_of?(Array)
          set_data('Message' => "List is empty.")
        else #data.kind_of?(Hash)
          set_data('Status' => 'OK')
        end
      end

      # sending raw data from response
      rendered_data = ViewProcessor.render(@command_class, data, @render_view, @render_data_type, nil, @print_error_table)

      puts "\n" unless rendered_data
      return rendered_data
    else
      hash_part()
    end
  end
end
render_table(default_data_type=nil, use_default=false) click to toggle source
# File lib/domain/response.rb, line 220
def render_table(default_data_type=nil, use_default=false)
  unless ok?
    return self
  end
  unless data_type = (use_default ? default_data_type : (response_datatype() || default_data_type))
    raise DTK::Client::DtkError, "Server did not return datatype."
  end

  @render_data_type = symbol_to_data_type_upcase(data_type)
  @render_view = RenderView::TABLE
  self
end
render_workspace_node_info(type) click to toggle source

used just for printing workspace node info

# File lib/domain/response.rb, line 110
def render_workspace_node_info(type)
  info_list = ""
  if type.eql?("component")
    info_list = ["component_name","component_id","basic_type","description"]
  else
    info_list = ["type", "node_id", "node_name","os_type", "instance_id", "admin_op_status", "size", "target", "dns_name", "image_id", "ec2_public_address", "privete_dns_name", "keypair", "security_groups", "security_group", "security_group_set"]
  end

  columns = []
  puts "--- \n"
  if data.kind_of?(String)
    data.each_line do |l|
      print = "#{l.gsub(/\s+\-*\s+/,'')}"
      print.gsub!(/-\s+/,"")
      info_list.each do |i|
        if match = print.to_s.match(/^(#{i}:)(.+)/)
          label = get_label_for_column_name(match[1], type)
          columns << " #{label}#{match[2]}\n"
        end
      end
    end
  end

  if type.eql?('node')
    # move target from first place
    columns.rotate!
  else
    columns.sort!()
  end

  columns.each do |column|
    STDOUT << column
  end
  puts "\n"
end
response_datatype() click to toggle source
# File lib/domain/response.rb, line 233
def response_datatype()
  self["datatype"] && self["datatype"].to_sym
end
set_datatype(data_type) click to toggle source
# File lib/domain/response.rb, line 215
def set_datatype(data_type)
  @render_data_type = symbol_to_data_type_upcase(data_type)
  self
end
symbol_to_data_type_upcase(data_type) click to toggle source
# File lib/domain/response.rb, line 241
def symbol_to_data_type_upcase(data_type)
  return data_type.nil? ? nil : data_type.to_s.upcase
end

Private Instance Methods

hash_part() click to toggle source
# File lib/domain/response.rb, line 237
def hash_part()
  keys.inject(Hash.new){|h,k|h.merge(k => self[k])}
end