Class: DTK::Client::Response

Inherits:
Common::Response
  • Object
show all
Includes:
ErrorHandlerMixin
Defined in:
lib/domain/response.rb,
lib/domain/response/error_handler.rb

Overview

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

Direct Known Subclasses

Error, NoOp, NotOk, Ok

Defined Under Namespace

Modules: ErrorHandler, ErrorHandlerMixin Classes: Error, NoOp, NotOk, Ok

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ErrorHandlerMixin

#error_info?

Constructor Details

#initialize(command_class = nil, hash = {}) ⇒ Response

Returns a new instance of Response.



40
41
42
43
44
45
46
47
48
# 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

Instance Attribute Details

: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)



33
34
35
# File 'lib/domain/response.rb', line 33

def print_error_table
  @print_error_table
end

#render_viewObject

: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)



33
34
35
# File 'lib/domain/response.rb', line 33

def render_view
  @render_view
end

#skip_renderObject

: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)



33
34
35
# File 'lib/domain/response.rb', line 33

def skip_render
  @skip_render
end

Class Method Details

.handle_error_in_wrapper(exception) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# 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

.wrap_helper_actions(data = {}, &block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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

Instance Method Details

#array_to_string(array_data) ⇒ Object



174
175
176
177
178
179
180
181
# 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_meObject



50
51
52
# File 'lib/domain/response.rb', line 50

def clone_me()
  return Marshal.load(Marshal.dump(self))
end

#get_custom_labels(label, type) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
# 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) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# 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) ⇒ Object

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



36
37
38
# File 'lib/domain/response.rb', line 36

def override_command_class(json_top_type)
  @command_class = json_top_type
end

#render_arg_list!Object



211
212
213
# File 'lib/domain/response.rb', line 211

def render_arg_list!
  @render_view = RenderView::AUG_SIMPLE_LIST
end

#render_custom_info(type) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
# 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) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# 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) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
# 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) ⇒ Object

used just for printing workspace node info



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# 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_datatypeObject



233
234
235
# File 'lib/domain/response.rb', line 233

def response_datatype()
  self["datatype"] && self["datatype"].to_sym
end

#set_datatype(data_type) ⇒ Object



215
216
217
218
# 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) ⇒ Object



241
242
243
# 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