Module: Morpheus::Cli::InfrastructureHelper

Overview

Provides common methods for infrastructure management

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



8
9
10
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 8

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

Instance Method Details

#cloud_type_for_id(id) ⇒ Object



128
129
130
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 128

def cloud_type_for_id(id)
  return get_available_cloud_types().find { |z| z['id'].to_i == id.to_i}
end

#cloud_type_for_name(name) ⇒ Object



132
133
134
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 132

def cloud_type_for_name(name)
  return get_available_cloud_types().find { |z| z['name'].downcase == name.downcase || z['code'].downcase == name.downcase}
end

#cloud_type_for_name_or_id(val) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 121

def cloud_type_for_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return cloud_type_for_id(val)
  else
    return cloud_type_for_name(val)
  end
end

#clouds_interfaceObject



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

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

#find_cloud_by_id(id) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 94

def find_cloud_by_id(id)
  json_results = clouds_interface.get(id.to_i)
  if json_results['zone'].empty?
    print_red_alert "Cloud not found by id #{id}"
    exit 1
  end
  cloud = json_results['zone']
  return cloud
end

#find_cloud_by_name(name) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 104

def find_cloud_by_name(name)
  json_results = clouds_interface.list({name: name})
  if json_results['zones'].empty?
    print_red_alert "Cloud not found by name #{name}"
    exit 1
  end
  cloud = json_results['zones'][0]
  return cloud
end

#find_cloud_by_name_or_id(val) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 86

def find_cloud_by_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_cloud_by_id(val)
  else
    return find_cloud_by_name(val)
  end
end

#find_group_by_id(id) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 62

def find_group_by_id(id)
  begin
    json_response = groups_interface.get(id.to_i)
    return json_response['group']
  rescue RestClient::Exception => e
    if e.response && e.response.code == 404
      print_red_alert "Group not found by id #{id}"
      exit 1
    else
      raise e
    end
  end
end

#find_group_by_name(name) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 76

def find_group_by_name(name)
  json_results = groups_interface.list({name: name})
  if json_results['groups'].empty?
    print_red_alert "Group not found by name #{name}"
    exit 1
  end
  group = json_results['groups'][0]
  return group
end

#find_group_by_name_or_id(val) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 54

def find_group_by_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_group_by_id(val)
  else
    return find_group_by_name(val)
  end
end

#find_network_by_id(id) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 147

def find_network_by_id(id)
  begin
    json_response = networks_interface.get(id.to_i)
    return json_response['network']
  rescue RestClient::Exception => e
    if e.response && e.response.code == 404
      print_red_alert "Network not found by id #{id}"
      return nil
    else
      raise e
    end
  end
end

#find_network_by_name(name) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 161

def find_network_by_name(name)
  json_response = networks_interface.list({name: name.to_s})
  networks = json_response['networks']
  if networks.empty?
    print_red_alert "Network not found by name #{name}"
    return nil
  elsif networks.size > 1
    print_red_alert "#{networks.size} networks found by name #{name}"
    rows = networks.collect do |it|
      {id: it['id'], name: it['name']}
    end
    puts as_pretty_table(rows, [:id, :name], {color:red})
    return nil
  else
    network = networks[0]
    # merge in tenants map
    if json_response['tenants'] && json_response['tenants'][network['id']]
      network['tenants'] = json_response['tenants'][network['id']]
    end
    return network
  end
end

#find_network_by_name_or_id(val) ⇒ Object

Networks



139
140
141
142
143
144
145
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 139

def find_network_by_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_network_by_id(val)
  else
    return find_network_by_name(val)
  end
end

#find_network_group_by_id(id) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 312

def find_network_group_by_id(id)
  begin
    json_response = network_groups_interface.get(id.to_i)
    return json_response['networkGroup']
  rescue RestClient::Exception => e
    if e.response && e.response.code == 404
      print_red_alert "Network Group not found by id #{id}"
      return nil
    else
      raise e
    end
  end
end

#find_network_group_by_name(name) ⇒ Object



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 326

def find_network_group_by_name(name)
  json_response = network_groups_interface.list({name: name.to_s})
  network_groups = json_response['networkGroups']
  if network_groups.empty?
    print_red_alert "Network Group not found by name #{name}"
    return nil
  elsif network_groups.size > 1
    print_red_alert "#{network_groups.size} network groups found by name #{name}"
    # print_networks_table(networks, {color: red})
    rows = network_groups.collect do |it|
      {id: it['id'], name: it['name']}
    end
    puts as_pretty_table(rows, [:id, :name], {color:red})
    return nil
  else
    return network_groups[0]
  end
end

#find_network_group_by_name_or_id(val) ⇒ Object



304
305
306
307
308
309
310
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 304

def find_network_group_by_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_network_group_by_id(val)
  else
    return find_network_group_by_name(val)
  end
end

#find_network_type_by_id(id) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 192

def find_network_type_by_id(id)
  begin
    json_response = network_types_interface.get(id.to_i)
    return json_response['networkType']
  rescue RestClient::Exception => e
    if e.response && e.response.code == 404
      print_red_alert "Network Type not found by id #{id}"
      return nil
    else
      raise e
    end
  end
end

#find_network_type_by_name(name) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 206

def find_network_type_by_name(name)
  json_response = network_types_interface.list({name: name.to_s})
  network_types = json_response['networkTypes']
  if network_types.empty?
    print_red_alert "Network Type not found by name #{name}"
    return network_types
  elsif network_types.size > 1
    print_red_alert "#{network_types.size} network types found by name #{name}"
    rows = network_types.collect do |it|
      {id: it['id'], name: it['name']}
    end
    puts as_pretty_table(rows, [:id, :name], {color:red})
    return nil
  else
    return network_types[0]
  end
end

#find_network_type_by_name_or_id(val) ⇒ Object



184
185
186
187
188
189
190
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 184

def find_network_type_by_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_network_type_by_id(val)
  else
    return find_network_type_by_name(val)
  end
end

#find_subnet_by_id(id) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 232

def find_subnet_by_id(id)
  begin
    json_response = subnets_interface.get(id.to_i)
    return json_response['subnet']
  rescue RestClient::Exception => e
    if e.response && e.response.code == 404
      print_red_alert "Subnet not found by id #{id}"
      return nil
    else
      raise e
    end
  end
end

#find_subnet_by_name(name) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 246

def find_subnet_by_name(name)
  json_response = subnets_interface.list({name: name.to_s})
  subnets = json_response['subnets']
  if subnets.empty?
    print_red_alert "Subnet not found by name #{name}"
    return nil
  elsif subnets.size > 1
    print_red_alert "#{subnets.size} subnets found by name #{name}"
    rows = subnets.collect do |it|
      {id: it['id'], name: it['name']}
    end
    puts as_pretty_table(rows, [:id, :name], {color:red})
    return nil
  else
    return subnets[0]
  end
end

#find_subnet_by_name_or_id(val) ⇒ Object



224
225
226
227
228
229
230
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 224

def find_subnet_by_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_subnet_by_id(val)
  else
    return find_subnet_by_name(val)
  end
end

#find_subnet_type_by_id(id) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 272

def find_subnet_type_by_id(id)
  begin
    json_response = subnet_types_interface.get(id.to_i)
    return json_response['subnetType']
  rescue RestClient::Exception => e
    if e.response && e.response.code == 404
      print_red_alert "Subnet Type not found by id #{id}"
      return nil
    else
      raise e
    end
  end
end

#find_subnet_type_by_name(name) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 286

def find_subnet_type_by_name(name)
  json_response = subnet_types_interface.list({name: name.to_s})
  subnet_types = json_response['subnetTypes']
  if subnet_types.empty?
    print_red_alert "Subnet Type not found by name #{name}"
    return subnet_types
  elsif subnet_types.size > 1
    print_red_alert "#{subnet_types.size} subnet types found by name #{name}"
    rows = subnet_types.collect do |it|
      {id: it['id'], name: it['name']}
    end
    puts as_pretty_table(rows, [:id, :name], {color:red})
    return nil
  else
    return subnet_types[0]
  end
end

#find_subnet_type_by_name_or_id(val) ⇒ Object



264
265
266
267
268
269
270
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 264

def find_subnet_type_by_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_subnet_type_by_id(val)
  else
    return find_subnet_type_by_name(val)
  end
end

#get_available_cloud_types(refresh = false) ⇒ Object



114
115
116
117
118
119
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 114

def get_available_cloud_types(refresh=false)
  if !@available_cloud_types || refresh
    @available_cloud_types = clouds_interface.cloud_types({max:1000})['zoneTypes']
  end
  return @available_cloud_types
end

#groups_interfaceObject



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

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

#network_groups_interfaceObject



36
37
38
39
40
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 36

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

#network_types_interfaceObject



42
43
44
45
46
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 42

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

#networks_interfaceObject



24
25
26
27
28
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 24

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

#prompt_for_network(network_id, options = {}, required = true, field_name = 'network', field_label = 'Network') ⇒ Object



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 345

def prompt_for_network(network_id, options={}, required=true, field_name='network', field_label='Network')
  # Prompt for a Network, text input that searches by name or id
  network = nil
  still_prompting = true
  while still_prompting do
    v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => field_name, 'type' => 'text', 'fieldLabel' => field_label, 'required' => required, 'description' => 'Network name or ID.'}], network_id ? {(field_name) => network_id} : {})
    network_id = v_prompt['network']
    begin
      network = find_network_by_name_or_id(network_id)
    rescue SystemExit => cmdexit
    end
    if options[:no_prompt]
      still_prompting = false
    else
      still_prompting = network ? false : true
    end
    if still_prompting
      network_id = nil
    end
  end
  return {success:!!network, network: network}
end

#prompt_for_networks(params, options = {}, api_client = nil, api_params = {}) ⇒ Object



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 368

def prompt_for_networks(params, options={}, api_client=nil, api_params={})
  # Networks
  network_list = nil
  network_ids = nil
  still_prompting = true
  if params['networks'].nil?
    still_prompting = true
    while still_prompting do
      v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'networks', 'type' => 'text', 'fieldLabel' => 'Networks', 'required' => false, 'description' => 'Networks to include, comma separated list of names or IDs.'}], options[:options])
      unless v_prompt['networks'].to_s.empty?
        network_list = v_prompt['networks'].split(",").collect {|it| it.to_s.strip.empty? ? nil : it.to_s.strip }.compact.uniq
      end
      network_ids = []
      bad_ids = []
      if network_list && network_list.size > 0
        network_list.each do |it|
          found_network = nil
          begin
            found_network = find_network_by_name_or_id(it)
          rescue SystemExit => cmdexit
          end
          if found_network
            network_ids << found_network['id']
          else
            bad_ids << it
          end
        end
      end
      still_prompting = bad_ids.empty? ? false : true
    end
  else
    network_list = params['networks']
    still_prompting = false
    network_ids = []
    bad_ids = []
    if network_list && network_list.size > 0
      network_list.each do |it|
        found_network = nil
        begin
          found_network = find_network_by_name_or_id(it)
        rescue SystemExit => cmdexit
        end
        if found_network
          network_ids << found_network['id']
        else
          bad_ids << it
        end
      end
    end
    if !bad_ids.empty?
      return {success:false, msg:"Networks not found: #{bad_ids}"}
    end
  end
  return {success:true, data: network_ids}
end

#prompt_for_subnets(params, options = {}, api_client = nil, api_params = {}) ⇒ Object



424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 424

def prompt_for_subnets(params, options={}, api_client=nil, api_params={})
  # todo: make this a generic method now please.
  # Subnets
  record_list = nil
  record_ids = nil
  still_prompting = true
  if params['subnets'].nil?
    still_prompting = true
    while still_prompting do
      v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'subnets', 'type' => 'text', 'fieldLabel' => 'Subnets', 'required' => false, 'description' => 'Subnets to include, comma separated list of names or IDs.'}], options[:options])
      unless v_prompt['subnets'].to_s.empty?
        record_list = v_prompt['subnets'].split(",").collect {|it| it.to_s.strip.empty? ? nil : it.to_s.strip }.compact.uniq
      end
      record_ids = []
      bad_ids = []
      if record_list && record_list.size > 0
        record_list.each do |it|
          found_record = nil
          begin
            found_record = find_subnet_by_name_or_id(it)
          rescue SystemExit => cmdexit
          end
          if found_record
            record_ids << found_record['id']
          else
            bad_ids << it
          end
        end
      end
      still_prompting = bad_ids.empty? ? false : true
    end
  else
    record_list = params['subnets']
    still_prompting = false
    record_ids = []
    bad_ids = []
    if record_list && record_list.size > 0
      record_list.each do |it|
        found_subnet = nil
        begin
          found_subnet = find_subnet_by_name_or_id(it)
        rescue SystemExit => cmdexit
        end
        if found_subnet
          record_ids << found_subnet['id']
        else
          bad_ids << it
        end
      end
    end
    if !bad_ids.empty?
      return {success:false, msg:"Subnets not found: #{bad_ids}"}
    end
  end
  return {success:true, data: record_ids}
end

#subnet_types_interfaceObject



48
49
50
51
52
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 48

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

#subnets_interfaceObject



30
31
32
33
34
# File 'lib/morpheus/cli/mixins/infrastructure_helper.rb', line 30

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