Module: Collins::CLI::Mixins

Included in:
Find, IPAM, Log, Modify, Power, Provision
Defined in:
lib/collins/cli/mixins.rb

Constant Summary collapse

COLORS =
{
  'EMERGENCY'     => {:color => :red, :background => :light_blue},
  'ALERT'         => {:color => :red},
  'CRITICAL'      => {:color => :black, :background => :red},
  'ERROR'         => {:color => :red},
  'WARNING'       => {:color => :yellow},
  'NOTICE'        => {},
  'INFORMATIONAL' => {:color => :green},
  'SUCCESS'       => {:color => :green},
  'DEBUG'         => {:color => :blue},
  'NOTE'          => {:color => :light_cyan},
}
SUCCESS =
"SUCCESS".colorize(COLORS['SUCCESS'])
ERROR =
"ERROR".colorize(COLORS['ERROR'])

Instance Method Summary collapse

Instance Method Details

#api_call(desc, method, tag, *varargs, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/collins/cli/mixins.rb', line 27

def api_call desc, method, tag, *varargs, &block
  printf "%s %s... " % [tag, desc]
  result,message = begin
    [collins.send(method,tag,*varargs),nil]
  rescue => e
    [false,e.message]
  end
  if result && block_given?
    # if the call was a success, let the caller format the response
    formatted_result = yield result
  end
  str = "#{result ? SUCCESS : ERROR}#{formatted_result.nil? ? '' : " (#{formatted_result})"}#{message.nil? ? nil : " (%s)" % e.message}"
  puts str
  result
end

#as_query?(attrs) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/collins/cli/mixins.rb', line 43

def as_query?(attrs)
  attrs.any?{|k,v| v.is_a? Array}
end

#collinsObject



19
20
21
22
23
24
25
# File 'lib/collins/cli/mixins.rb', line 19

def collins
  begin
    @collins ||= Collins::Authenticator.setup_client timeout: @options[:timeout], config_file: @options[:config], prompt: true
  rescue => e
    raise "Unable to set up Collins client! #{e.message}"
  end
end

#convert_to_query(op, attrs, options) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/collins/cli/mixins.rb', line 47

def convert_to_query(op, attrs, options)
  # we want to support being able to query -Smaintenance:noop,:running,:provisioning_problem
  # and not have the states ored together. Handle status/state pairs separately
  basic_query = attrs.reject {|k,v| [:status,:state].include?(k)}.map do |k,v|
    next if v.nil?
    if v.is_a? Array
      "(" + v.map{|x| "#{k} = #{x}"}.join(' OR ') + ")"
    else
      "#{k} = #{v}"
    end
  end.compact.join(" #{op} ")
  # because they are provided in pairs, lets handle them together
  # create the (( STATUS = maintenance AND STATE = noop) OR (STATE = provisioning_problem)) query
  if options[:status_state]
    status_query = options[:status_state].flat_map do |ss|
      h = {}
      h[:status], h[:state] = ss.split(':')
      h[:status] = nil if h[:status].nil? or h[:status].empty?
      h[:state] = nil if h[:state].nil? or h[:state].empty?
      "( " + h.map {|k,v| v.nil? ? nil : "#{k.to_s.upcase} = #{v}"}.compact.join(" AND ") + " )"
    end.compact.join(' OR ')
    status_query = "( #{status_query} )"
  end
  [basic_query,status_query].reject {|q| q.nil? or q.empty?}.join(" #{op} ")
end