Module: Morpheus::Cli::LibraryHelper
- Included in:
- LibraryClusterLayoutsCommand, LibraryContainerTemplatesCommand, LibraryContainerTypesCommand, LibraryInstanceTypesCommand, LibraryLayoutsCommand, LibraryOptionListsCommand, LibraryOptionTypesCommand, LibrarySpecTemplatesCommand, LibraryUpgradesCommand, PackagesCommand
- Defined in:
- lib/morpheus/cli/mixins/library_helper.rb
Overview
Provides common methods for library management commands
Class Method Summary collapse
Instance Method Summary collapse
- #api_client ⇒ Object
- #find_instance_type_by_id(id) ⇒ Object
- #find_instance_type_by_name(name) ⇒ Object
- #find_instance_type_by_name_or_id(val) ⇒ Object
- #find_spec_template_by_id(id) ⇒ Object
- #find_spec_template_by_name(name) ⇒ Object
-
#find_spec_template_by_name_or_id(val) ⇒ Object
Spec Template helper methods.
- #find_spec_template_type_by_id(id) ⇒ Object
-
#find_spec_template_type_by_name_or_code(name) ⇒ Object
def find_spec_template_type_by_name(name) json_response = @spec_template_types_interface.list(name.to_s) spec_template_types = json_response if spec_template_types.empty? print_red_alert "Network Type not found by name #name" return spec_template_types elsif spec_template_types.size > 1 print_red_alert "#spec_template_typesspec_template_types.size network types found by name #name" rows = spec_template_types.collect do |it| it['id'], name: it['name'] end puts as_pretty_table(rows, [:id, :name], color:red) return nil else return spec_template_types end end.
- #find_spec_template_type_by_name_or_code_id(val) ⇒ Object
- #format_instance_type_technology(instance_type) ⇒ Object
- #get_all_spec_template_types ⇒ Object
- #load_balance_protocols_dropdown ⇒ Object
- #print_instance_types_table(instance_types, opts = {}) ⇒ Object
- #print_resource_specs_table(resource_specs, opts = {}) ⇒ Object
-
#prompt_exposed_ports(options = {}, api_client = nil, api_params = {}) ⇒ Object
Prompts user for exposed ports array returns array of port objects.
- #prompt_for_spec_templates(params, options = {}, api_client = nil, api_params = {}) ⇒ Object
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_client ⇒ Object
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 |
#find_spec_template_by_id(id) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 133 def find_spec_template_by_id(id) begin json_response = @spec_templates_interface.get(id.to_i) return json_response['specTemplate'] rescue RestClient::Exception => e if e.response && e.response.code == 404 print_red_alert "Spec Template not found by id #{id}" else raise e end end end |
#find_spec_template_by_name(name) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 146 def find_spec_template_by_name(name) resource_specs = @spec_templates_interface.list({name: name.to_s})['specTemplates'] if resource_specs.empty? print_red_alert "Spec Template not found by name #{name}" return nil elsif resource_specs.size > 1 print_red_alert "#{resource_specs.size} spec templates found by name #{name}" print_resource_specs_table(resource_specs, {color: red}) print_red_alert "Try using ID instead" print reset,"\n" return nil else return resource_specs[0] end end |
#find_spec_template_by_name_or_id(val) ⇒ Object
Spec Template helper methods
125 126 127 128 129 130 131 |
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 125 def find_spec_template_by_name_or_id(val) if val.to_s =~ /\A\d{1,}\Z/ return find_spec_template_by_id(val) else return find_spec_template_by_name(val) end end |
#find_spec_template_type_by_id(id) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 174 def find_spec_template_type_by_id(id) begin json_response = @spec_template_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_spec_template_type_by_name_or_code(name) ⇒ Object
def find_spec_template_type_by_name(name) json_response = @spec_template_types_interface.list(name.to_s) spec_template_types = json_response if spec_template_types.empty? print_red_alert "Network Type not found by name #name" return spec_template_types elsif spec_template_types.size > 1 print_red_alert "#Morpheus::Cli::LibraryHelper.spec_template_typesspec_template_types.size network types found by name #name" rows = spec_template_types.collect do |it| it['id'], name: it['name'] end puts as_pretty_table(rows, [:id, :name], color:red) return nil else return spec_template_types end end
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 206 def find_spec_template_type_by_name_or_code(name) spec_template_types = get_all_spec_template_types().select { |it| name && it['code'] == name || it['name'] == name } if spec_template_types.empty? print_red_alert "Spec Template Type not found by code #{name}" return nil elsif spec_template_types.size > 1 print_red_alert "#{spec_template_types.size} spec template types found by code #{name}" rows = spec_template_types.collect do |it| {id: it['id'], code: it['code'], name: it['name']} end print "\n" puts as_pretty_table(rows, [:id, :code, :name], {color:red}) return nil else return spec_template_types[0] end end |
#find_spec_template_type_by_name_or_code_id(val) ⇒ Object
224 225 226 227 228 229 230 |
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 224 def find_spec_template_type_by_name_or_code_id(val) if val.to_s =~ /\A\d{1,}\Z/ return find_subnet_by_id(val) else return find_spec_template_type_by_name_or_code(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 |
#get_all_spec_template_types ⇒ Object
232 233 234 |
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 232 def get_all_spec_template_types @all_spec_template_types ||= @spec_template_types_interface.list({max: 1000})['specTemplateTypes'] || [] end |
#load_balance_protocols_dropdown ⇒ Object
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 |
#print_instance_types_table(instance_types, opts = {}) ⇒ Object
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 |
#print_resource_specs_table(resource_specs, opts = {}) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 162 def print_resource_specs_table(resource_specs, opts={}) columns = [ {"ID" => lambda {|resource_spec| resource_spec['id'] } }, {"NAME" => lambda {|resource_spec| resource_spec['name'] } }, #{"OWNER" => lambda {|resource_spec| resource_spec['account'] ? resource_spec['account']['name'] : '' } }, ] if opts[:include_fields] columns = opts[:include_fields] end print as_pretty_table(resource_specs, 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(={}, api_client=nil, api_params={}) #puts "Configure ports:" no_prompt = ([:no_prompt] || ([:options] && [:options][:no_prompt])) ports = [] port_index = 0 has_another_port = [: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]) 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]) 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]) port['loadBalanceProtocol'] = v_prompt[field_context]['loadBalanceProtocol'] ports << port port_index += 1 has_another_port = [: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 |
#prompt_for_spec_templates(params, options = {}, api_client = nil, api_params = {}) ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/morpheus/cli/mixins/library_helper.rb', line 236 def prompt_for_spec_templates(params, ={}, api_client=nil, api_params={}) # spec_templates spec_template_list = nil spec_template_ids = nil still_prompting = true if params['specTemplates'].nil? still_prompting = true while still_prompting do v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'specTemplates', 'type' => 'text', 'fieldLabel' => 'Spec Templates', 'required' => false, 'description' => 'Spec Templates to include, comma separated list of names or IDs.'}], [:options]) unless v_prompt['specTemplates'].to_s.empty? spec_template_list = v_prompt['specTemplates'].split(",").collect {|it| it.to_s.strip.empty? ? nil : it.to_s.strip }.compact.uniq end spec_template_ids = [] bad_ids = [] if spec_template_list && spec_template_list.size > 0 spec_template_list.each do |it| found_spec_template = nil begin found_spec_template = find_spec_template_by_name_or_id(it) rescue SystemExit => cmdexit end if found_spec_template spec_template_ids << found_spec_template['id'] else bad_ids << it end end end still_prompting = bad_ids.empty? ? false : true end else spec_template_list = params['specTemplates'] still_prompting = false spec_template_ids = [] bad_ids = [] if spec_template_list && spec_template_list.size > 0 spec_template_list.each do |it| found_spec_template = nil begin found_spec_template = find_spec_template_by_name_or_id(it) rescue SystemExit => cmdexit end if found_spec_template spec_template_ids << found_spec_template['id'] else bad_ids << it end end end if !bad_ids.empty? return {success:false, msg:"Spec Templates not found: #{bad_ids}"} end end return {success:true, data: spec_template_ids} end |