Module: Morpheus::Cli::LoadBalancersHelper

Overview

Mixin for Morpheus::Cli command classes Provides common methods for load balancer management including load balancers, load balancer types, virtual servers, etc.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



9
10
11
# File 'lib/morpheus/cli/mixins/load_balancers_helper.rb', line 9

def self.included(klass)
  klass.send :include, Morpheus::Cli::PrintHelper
end

Instance Method Details

#find_load_balancer_by_id(id) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/morpheus/cli/mixins/load_balancers_helper.rb', line 65

def find_load_balancer_by_id(id)
  begin
    json_response = load_balancers_interface.get(id.to_i)
    return json_response[load_balancer_object_key]
  rescue RestClient::Exception => e
    if e.response && e.response.code == 404
      print_red_alert "Load Balancer not found by id #{id}"
    else
      raise e
    end
  end
end

#find_load_balancer_by_name(name) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/morpheus/cli/mixins/load_balancers_helper.rb', line 78

def find_load_balancer_by_name(name)
  lbs = load_balancers_interface.list({name: name.to_s})[load_balancer_list_key]
  if lbs.empty?
    print_red_alert "Load Balancer not found by name #{name}"
    return nil
  elsif lbs.size > 1
    print_red_alert "#{lbs.size} load balancers found by name #{name}"
    #print_lbs_table(lbs, {color: red})
    print reset,"\n\n"
    return nil
  else
    return lbs[0]
  end
end

#find_load_balancer_by_name_or_id(val) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/morpheus/cli/mixins/load_balancers_helper.rb', line 57

def find_load_balancer_by_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_load_balancer_by_id(val)
  else
    return find_load_balancer_by_name(val)
  end
end

#find_load_balancer_type_by_id(id) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/morpheus/cli/mixins/load_balancers_helper.rb', line 144

def find_load_balancer_type_by_id(id)
  begin
    json_response = load_balancer_types_interface.get(id.to_i)
    return json_response[load_balancer_type_object_key]
  rescue RestClient::Exception => e
    if e.response && e.response.code == 404
      print_red_alert "Load Balancer Type not found by id #{id}"
      return nil
    else
      raise e
    end
  end
end

#find_load_balancer_type_by_name(name) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/morpheus/cli/mixins/load_balancers_helper.rb', line 158

def find_load_balancer_type_by_name(name)
  json_response = load_balancer_types_interface.list({name: name.to_s})
  load_balancer_types = json_response[load_balancer_type_list_key]
  if load_balancer_types.empty?
    print_red_alert "Load Balancer Type not found by name #{name}"
    return load_balancer_types
  elsif load_balancer_types.size > 1
    print_red_alert "#{load_balancer_types.size} load balancer types found by name #{name}"
    rows = load_balancer_types.collect do |it|
      {id: it['id'], name: it['name']}
    end
    puts as_pretty_table(rows, [:id, :name], {color:red})
    return nil
  else
    return load_balancer_types[0]
  end
end

#find_load_balancer_type_by_name_or_id(val) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/morpheus/cli/mixins/load_balancers_helper.rb', line 136

def find_load_balancer_type_by_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_load_balancer_type_by_id(val)
  else
    return find_load_balancer_type_by_name(val)
  end
end

#get_available_load_balancer_types(refresh = false) ⇒ Object



93
94
95
96
97
98
# File 'lib/morpheus/cli/mixins/load_balancers_helper.rb', line 93

def get_available_load_balancer_types(refresh=false)
  if !@available_load_balancer_types || refresh
    @available_load_balancer_types = load_balancer_types_interface.list({max:1000})[load_balancer_type_list_key]
  end
  return @available_load_balancer_types
end

#load_balancer_labelObject



33
34
35
# File 'lib/morpheus/cli/mixins/load_balancers_helper.rb', line 33

def load_balancer_label
  'Load Balancer'
end

#load_balancer_label_pluralObject



37
38
39
# File 'lib/morpheus/cli/mixins/load_balancers_helper.rb', line 37

def load_balancer_label_plural
  'Load Balancers'
end

#load_balancer_list_keyObject



29
30
31
# File 'lib/morpheus/cli/mixins/load_balancers_helper.rb', line 29

def load_balancer_list_key
  'loadBalancers'
end

#load_balancer_object_keyObject



25
26
27
# File 'lib/morpheus/cli/mixins/load_balancers_helper.rb', line 25

def load_balancer_object_key
  'loadBalancer'
end

#load_balancer_type_for_id(val) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/morpheus/cli/mixins/load_balancers_helper.rb', line 108

def load_balancer_type_for_id(val)
  record = get_available_load_balancer_types().find { |z| z['id'].to_i == val.to_i}
  label = "Load Balancer Type"
  if record.nil?
    print_red_alert "#{label.downcase} not found by id #{val}"
    return nil
  end
  return record
end

#load_balancer_type_for_name(val) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/morpheus/cli/mixins/load_balancers_helper.rb', line 118

def load_balancer_type_for_name(val)
  records = get_available_load_balancer_types().select { |z| z['name'].downcase == val.downcase || z['code'].downcase == val.downcase}
  label = "Load Balancer Type"
  if records.empty?
    print_red_alert "#{label} not found by name '#{val}'"
    return nil
  elsif records.size > 1
    print_red_alert "More than one #{label.downcase} found by name '#{val}'"
    print_error "\n"
    puts_error as_pretty_table(records, [:id, :name], {color:red})
    print_red_alert "Try using ID instead"
    print_error reset,"\n"
    return nil
  else
    return records[0]
  end
end

#load_balancer_type_for_name_or_id(val) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/morpheus/cli/mixins/load_balancers_helper.rb', line 100

def load_balancer_type_for_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return load_balancer_type_for_id(val)
  else
    return load_balancer_type_for_name(val)
  end
end

#load_balancer_type_labelObject



49
50
51
# File 'lib/morpheus/cli/mixins/load_balancers_helper.rb', line 49

def load_balancer_type_label
  'Load Balancer Type'
end

#load_balancer_type_label_pluralObject



53
54
55
# File 'lib/morpheus/cli/mixins/load_balancers_helper.rb', line 53

def load_balancer_type_label_plural
  'Load Balancer Types'
end

#load_balancer_type_list_keyObject



45
46
47
# File 'lib/morpheus/cli/mixins/load_balancers_helper.rb', line 45

def load_balancer_type_list_key
  'loadBalancerTypes'
end

#load_balancer_type_object_keyObject



41
42
43
# File 'lib/morpheus/cli/mixins/load_balancers_helper.rb', line 41

def load_balancer_type_object_key
  'loadBalancerType'
end

#load_balancer_types_interfaceObject



19
20
21
22
23
# File 'lib/morpheus/cli/mixins/load_balancers_helper.rb', line 19

def load_balancer_types_interface
  # @api_client.load_balancer_types
  raise "#{self.class} has not defined @load_balancer_types_interface" if @load_balancer_types_interface.nil?
  @load_balancer_types_interface
end

#load_balancers_interfaceObject



13
14
15
16
17
# File 'lib/morpheus/cli/mixins/load_balancers_helper.rb', line 13

def load_balancers_interface
  # @api_client.load_balancers
  raise "#{self.class} has not defined @load_balancers_interface" if @load_balancers_interface.nil?
  @load_balancers_interface
end