Module: Morpheus::Cli::LibraryHelper

Overview

Provides common methods for library management commands

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



7
8
9
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 7

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

Instance Method Details

#api_clientObject



11
12
13
14
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 11

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

#find_instance_type_by_id(id) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 24

def find_instance_type_by_id(id)
  begin
    json_response = @library_instance_types_interface.get(id.to_i)
    return json_response['instanceType']
  rescue RestClient::Exception => e
    if e.response && e.response.code == 404
      print_red_alert "Instance Type not found by id #{id}"
    else
      raise e
    end
  end
end

#find_instance_type_by_name(name) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 37

def find_instance_type_by_name(name)
  json_response = @library_instance_types_interface.list({name: name.to_s})
  instance_types = json_response['instanceTypes']
  if instance_types.empty?
    print_red_alert "Instance Type not found by name #{name}"
    return nil
  elsif instance_types.size > 1
    print_red_alert "#{instance_types.size} instance types found by name #{name}"
    print_instance_types_table(instance_types, {color: red})
    print_red_alert "Try using ID instead"
    print reset,"\n"
    return nil
  else
    return instance_types[0]
  end
end

#find_instance_type_by_name_or_id(val) ⇒ Object



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

def find_instance_type_by_name_or_id(val)
  if val.to_s =~ /\A\d{1,}\Z/
    return find_instance_type_by_id(val)
  else
    return find_instance_type_by_name(val)
  end
end

#format_instance_type_technology(instance_type) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 70

def format_instance_type_technology(instance_type)
  if instance_type
    instance_type['provisionTypeCode'].to_s.capitalize
  else
    ""
  end
end

#load_balance_protocols_dropdownObject



78
79
80
81
82
83
84
85
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 78

def load_balance_protocols_dropdown
  [
    {'name' => 'None', 'value' => ''},
    {'name' => 'HTTP', 'value' => 'HTTP'},
    {'name' => 'HTTPS', 'value' => 'HTTPS'},
    {'name' => 'TCP', 'value' => 'TCP'}
  ]
end


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 54

def print_instance_types_table(instance_types, opts={})
  columns = [
    {"ID" => lambda {|instance_type| instance_type['id'] } },
    {"NAME" => lambda {|instance_type| instance_type['name'] } },
    {"CODE" => lambda {|instance_type| instance_type['code'] } },
    {"TECHNOLOGY" => lambda {|instance_type| format_instance_type_technology(instance_type) } },
    {"CATEGORY" => lambda {|instance_type| instance_type['category'].to_s.capitalize } },
    {"FEATURED" => lambda {|instance_type| format_boolean instance_type['featured'] } },
    {"OWNER" => lambda {|instance_type| instance_type['account'] ? instance_type['account']['name'] : '' } },
  ]
  if opts[:include_fields]
    columns = opts[:include_fields]
  end
  print as_pretty_table(instance_types, columns, opts)
end

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

Prompts user for exposed ports array returns array of port objects



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 89

def prompt_exposed_ports(options={}, api_client=nil, api_params={})
  #puts "Configure ports:"
  no_prompt = (options[:no_prompt] || (options[:options] && options[:options][:no_prompt]))

  ports = []
  port_index = 0
  has_another_port = options[:options] && options[:options]["exposedPort#{port_index}"]
  add_another_port = has_another_port || (!no_prompt && Morpheus::Cli::OptionTypes.confirm("Add an exposed port?"))
  while add_another_port do
    field_context = "exposedPort#{port_index}"

    port = {}
    #port['name'] ||= "Port #{port_index}"
    port_label = port_index == 0 ? "Port" : "Port [#{port_index+1}]"
    v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'name', 'type' => 'text', 'fieldLabel' => "#{port_label} Name", 'required' => false, 'description' => 'Choose a name for this port.', 'defaultValue' => port['name']}], options[:options])
    port['name'] = v_prompt[field_context]['name']

    v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'port', 'type' => 'number', 'fieldLabel' => "#{port_label} Number", 'required' => true, 'description' => 'A port number. eg. 8001', 'defaultValue' => (port['port'] ? port['port'].to_i : nil)}], options[:options])
    port['port'] = v_prompt[field_context]['port']

    v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => field_context, 'fieldName' => 'loadBalanceProtocol', 'type' => 'select', 'fieldLabel' => "#{port_label} LB", 'selectOptions' => load_balance_protocols_dropdown, 'required' => false, 'skipSingleOption' => true, 'description' => 'Choose a load balance protocol.', 'defaultValue' => port['loadBalanceProtocol']}], options[:options])
    port['loadBalanceProtocol'] = v_prompt[field_context]['loadBalanceProtocol']

    ports << port
    port_index += 1
    has_another_port = options[:options] && options[:options]["exposedPort#{port_index}"]
    add_another_port = has_another_port || (!no_prompt && Morpheus::Cli::OptionTypes.confirm("Add another exposed port?"))

  end


  return ports
end