Class: Morpheus::Cli::LibraryLayoutsCommand
- Inherits:
-
Object
- Object
- Morpheus::Cli::LibraryLayoutsCommand
- Includes:
- CliCommand, LibraryHelper
- Defined in:
- lib/morpheus/cli/library_layouts_command.rb
Instance Attribute Summary
Attributes included from CliCommand
Instance Method Summary collapse
- #_get(id, options) ⇒ Object
- #add(args) ⇒ Object
- #connect(opts) ⇒ Object
- #get(args) ⇒ Object
- #handle(args) ⇒ Object
-
#initialize ⇒ LibraryLayoutsCommand
constructor
A new instance of LibraryLayoutsCommand.
- #list(args) ⇒ Object
- #remove(args) ⇒ Object
- #update(args) ⇒ Object
Methods included from LibraryHelper
#api_client, #find_instance_type_by_id, #find_instance_type_by_name, #find_instance_type_by_name_or_id, #format_instance_type_technology, included, #load_balance_protocols_dropdown, #print_instance_types_table, #prompt_exposed_ports
Methods included from CliCommand
#build_common_options, #build_option_type_options, #command_name, #default_subcommand, #establish_remote_appliance_connection, #full_command_usage, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_id_list, #parse_list_options, #parse_list_subtitles, #print, #print_error, #puts, #puts_error, #raise_command_error, #run_command_for_each_arg, #subcommand_aliases, #subcommand_usage, #subcommands, #usage, #verify_access_token!
Constructor Details
#initialize ⇒ LibraryLayoutsCommand
Returns a new instance of LibraryLayoutsCommand.
15 16 17 |
# File 'lib/morpheus/cli/library_layouts_command.rb', line 15 def initialize() # @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance end |
Instance Method Details
#_get(id, options) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/morpheus/cli/library_layouts_command.rb', line 123 def _get(id, ) instance_type_id = nil begin if [:dry_run] if id.to_s =~ /\A\d{1,}\Z/ print_dry_run @library_layouts_interface.dry.get(instance_type_id, id.to_i) else print_dry_run @library_layouts_interface.dry.list(instance_type_id, {name:id}) end return end layout = find_layout_by_name_or_id(instance_type_id, id) if layout.nil? return 1 end # skip redundant request #json_response = @library_layouts_interface.get(instance_type_id, layout['id']) json_response = {'instanceTypeLayout' => layout} #layout = json_response['instanceTypeLayout'] if [:json] puts as_json(json_response, , "instanceTypeLayout") return 0 elsif [:yaml] puts as_yaml(json_response, , "instanceTypeLayout") return 0 elsif [:csv] puts records_as_csv([json_response['instanceTypeLayout']], ) return 0 end print_h1 "Layout Details" print cyan description_cols = { "ID" => lambda {|it| it['id'] }, "Name" => lambda {|it| it['name'] }, #"Code" => lambda {|it| it['code'] }, "Version" => lambda {|it| it['instanceVersion'] }, "Description" => lambda {|it| it['description'] }, "Technology" => lambda {|it| format_layout_technology(it) }, "Min Memory" => lambda {|it| if it['memoryRequirement'].to_i != 0 (it['memoryRequirement'].to_i / (1024*1024)).to_s + " MB" else "" end }, "Workflow" => lambda {|it| if it['taskSets'] it['taskSets'][0]['name'] rescue "" else "" end }, # "Category" => lambda {|it| it['category'].to_s.capitalize }, # # "Logo" => lambda {|it| it['logo'].to_s }, # "Visiblity" => lambda {|it| it['visibility'].to_s.capitalize }, # "Environment Prefix" => lambda {|it| it['environmentPrefix'] }, # "Enable Settings" => lambda {|it| format_boolean it['hasSettings'] }, # "Enable Scaling" => lambda {|it| format_boolean it['hasAutoScale'] }, # "Supports Deployments" => lambda {|it| format_boolean it['hasDeployment'] }, # "Featured" => lambda {|it| format_boolean it['featured'] }, # "Owner" => lambda {|it| it['account'] ? it['account']['name'] : '' }, # "Active" => lambda {|it| format_boolean it['active'] }, # "Created" => lambda {|it| format_local_dt(it['dateCreated']) }, # "Updated" => lambda {|it| format_local_dt(it['lastUpdated']) } } print_description_list(description_cols, layout) layout_evars = layout['environmentVariables'] if layout_evars && layout_evars.size > 0 print_h2 "Environment Variables" evar_columns = [ {"NAME" => lambda {|it| it['name'] } }, {"VALUE" => lambda {|it| it['defaultValue'] } }, {"TYPE" => lambda {|it| it['valueType'].to_s.capitalize } }, {"EXPORT" => lambda {|it| format_boolean it['export'] } }, {"MASKED" => lambda {|it| format_boolean it['mask'] } }, ] print as_pretty_table(layout_evars, evar_columns) else # print yellow,"No environment variables found for this instance type.","\n",reset end layout_option_types = layout['optionTypes'] if layout_option_types && layout_option_types.size > 0 print_h2 "Option Types" columns = [ {"ID" => lambda {|it| it['id'] } }, {"NAME" => lambda {|it| it['name'] } }, {"TYPE" => lambda {|it| it['type'] } }, {"FIELD NAME" => lambda {|it| it['fieldName'] } }, {"FIELD LABEL" => lambda {|it| it['fieldLabel'] } }, {"DEFAULT" => lambda {|it| it['defaultValue'] } }, {"REQUIRED" => lambda {|it| format_boolean it['required'] } }, ] print as_pretty_table(layout_option_types, columns) else # print yellow,"No option types found for this layout.","\n",reset end print_h2 "Node Types" layout_node_types = layout['containerTypes'] if layout_node_types && layout_node_types.size > 0 # match UI sorting [version desc, name asc] # or use something simpler like one of these layout_node_types = layout_node_types.sort { |a,b| a['name'] <=> b['name'] } # layout_node_types = layout_node_types.sort { |a,b| a['sortOrder'] <=> b['sortOrder'] } node_type_columns = [ {"ID" => lambda {|it| it['id'] } }, {"NAME" => lambda {|it| it['name'] } }, {"SHORT NAME" => lambda {|it| it['shortName'] } }, {"VERSION" => lambda {|it| it['containerVersion'] } }, {"TECHNOLOGY" => lambda {|it| it['provisionType'] ? it['provisionType']['name'] : '' } }, {"CATEGORY" => lambda {|it| it['category'] } }, ] print as_pretty_table(layout_node_types, node_type_columns) else print yellow,"No node types for this layout.","\n",reset end print reset,"\n" rescue RestClient::Exception => e print_rest_exception(e, ) return 1 end end |
#add(args) ⇒ Object
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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 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 |
# File 'lib/morpheus/cli/library_layouts_command.rb', line 253 def add(args) = {} params = {} instance_type_id = nil option_type_ids = nil node_type_ids = nil evars = nil optparse = Morpheus::Cli::OptionParser.new do|opts| opts. = subcommand_usage("[instance-type]") opts.on('--instance-type ID', String, "Instance Type") do |val| instance_type_id = val end opts.on('--name VALUE', String, "Name for this layout") do |val| params['name'] = val end opts.on('--version VALUE', String, "Version") do |val| params['instanceVersion'] = val end opts.on('--description VALUE', String, "Description") do |val| params['description'] = val end opts.on('--technology CODE', String, "Technology") do |val| params['provisionTypeCode'] = val end opts.on('--min-memory VALUE', String, "Minimum Memory (MB)") do |val| params['memoryRequirement'] = val end opts.on('--workflow ID', String, "Workflow") do |val| params['taskSetId'] = val.to_i end opts.on('--option-types x,y,z', Array, "List of Option Type IDs") do |val| option_type_ids = val #.collect {|it| it.to_i } end opts.on('--node-types x,y,z', Array, "List of Node Type IDs") do |val| node_type_ids = val #.collect {|it| it.to_i } end #build_option_type_options(opts, options, add_layout_option_types()) (opts, , [:options, :payload, :json, :dry_run, :remote]) opts. = "Create a new layout." + "\n" + "[instance-type] is required and can be passed as --instance-type instead." end optparse.parse!(args) connect() if instance_type_id.nil? instance_type_id = args[0] end if !instance_type_id puts optparse exit 1 end begin instance_type = find_instance_type_by_name_or_id(instance_type_id) exit 1 if instance_type.nil? instance_type_id = instance_type['id'] # construct payload payload = nil if [:payload] payload = [:payload] else # v_prompt = Morpheus::Cli::OptionTypes.prompt(add_layout_option_types, options[:options], @api_client, options[:params]) # params.deep_merge!(v_prompt) params.deep_merge!([:options].reject {|k,v| k.is_a?(Symbol) }) if [:options] if !params['name'] v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'name', 'type' => 'text', 'fieldLabel' => 'Name', 'required' => true}], [:options]) params['name'] = v_prompt['name'] end if !params['instanceVersion'] v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'instanceVersion', 'type' => 'text', 'fieldLabel' => 'Version', 'required' => true}], [:options]) params['instanceVersion'] = v_prompt['instanceVersion'] end if !params['description'] v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'description', 'type' => 'text', 'fieldLabel' => 'Description', 'required' => false}], [:options]) params['description'] = v_prompt['description'] if v_prompt['description'] end provision_types = @provision_types_interface.get({customSupported: true})['provisionTypes'] if provision_types.empty? print_red_alert "No available provision types found!" exit 1 end = provision_types.collect {|it| { 'name' => it['name'], 'value' => it['code']} } if !params['provisionTypeCode'] v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'provisionTypeCode', 'type' => 'select', 'selectOptions' => , 'fieldLabel' => 'Technology', 'required' => true, 'description' => 'The type of container technology.'}], [:options]) params['provisionTypeCode'] = v_prompt['provisionTypeCode'] else end provision_type = provision_types.find {|it| it['code'] == params['provisionTypeCode'] } if !params['memoryRequirement'] v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'memoryRequirement', 'type' => 'text', 'fieldLabel' => 'Min Memory (MB)', 'required' => false, 'description' => 'This will override any memory requirement set on the virtual image'}], [:options]) params['memoryRequirement'] = v_prompt['memoryRequirement'] if v_prompt['memoryRequirement'] end if !params['taskSetId'] v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'taskSetId', 'type' => 'text', 'fieldLabel' => 'Workflow ID', 'required' => false, 'description' => 'Worflow ID'}], [:options]) params['taskSetId'] = v_prompt['taskSetId'].to_i if v_prompt['taskSetId'] end # ENVIRONMENT VARIABLES if evars else # prompt end # OPTION TYPES if option_type_ids params['optionTypes'] = option_type_ids.collect {|it| it.to_i }.select { |it| it != 0 } else # prompt end # NODE TYPES if node_type_ids params['containerTypes'] = node_type_ids.collect {|it| it.to_i }.select { |it| it != 0 } else # prompt end payload = {'instanceTypeLayout' => params} end if [:dry_run] print_dry_run @library_layouts_interface.dry.create(instance_type_id, payload) return end json_response = @library_layouts_interface.create(instance_type_id, payload) if [:json] print JSON.pretty_generate(json_response), "\n" return end print_green_success "Added Layout #{params['name']}" #get([json_response['instanceTypeLayout']['id']]) rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |
#connect(opts) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/morpheus/cli/library_layouts_command.rb', line 19 def connect(opts) @api_client = establish_remote_appliance_connection(opts) @library_layouts_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).library_layouts @library_instance_types_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).library_instance_types @provision_types_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).provision_types @option_types_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).option_types @option_type_lists_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).option_type_lists end |
#get(args) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/morpheus/cli/library_layouts_command.rb', line 105 def get(args) = {} optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage("[name]") (opts, , [:json, :yaml, :csv, :fields, :dry_run, :remote]) end optparse.parse!(args) if args.count < 1 puts optparse return 1 end connect() id_list = parse_id_list(args) return run_command_for_each_arg(id_list) do |arg| _get(arg, ) end end |
#handle(args) ⇒ Object
28 29 30 |
# File 'lib/morpheus/cli/library_layouts_command.rb', line 28 def handle(args) handle_subcommand(args) end |
#list(args) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/morpheus/cli/library_layouts_command.rb', line 33 def list(args) = {} params = {} instance_type = nil instance_type_id = nil optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage() opts.on('--instance-type ID', String, "Filter by Instance Type") do |val| instance_type_id = val end opts.on('--category VALUE', String, "Filter by category") do |val| params['category'] = val end opts.on('--code VALUE', String, "Filter by code") do |val| params['code'] = val end opts.on('--technology VALUE', String, "Filter by technology") do |val| params['provisionType'] = val end (opts, , [:list, :query, :json, :yaml, :csv, :fields, :dry_run, :remote]) opts. = "List layouts." end optparse.parse!(args) connect() begin # construct payload if instance_type_id instance_type = find_instance_type_by_name_or_id(instance_type_id) return 1 if instance_type.nil? instance_type_id = instance_type['id'] end params.merge!(()) if [:dry_run] print_dry_run @library_layouts_interface.dry.list(instance_type_id, params) return end json_response = @library_layouts_interface.list(instance_type_id, params) if [:json] puts as_json(json_response, , "instanceTypeLayouts") return 0 elsif [:csv] puts records_as_csv(json_response['instanceTypeLayouts'], ) return 0 elsif [:yaml] puts as_yaml(json_response, , "instanceTypeLayouts") return 0 end layouts = json_response['instanceTypeLayouts'] title = "Morpheus Library - Layouts" subtitles = [] if instance_type subtitles << "Instance Type: #{instance_type['name']}".strip end subtitles += parse_list_subtitles() print_h1 title, subtitles if layouts.empty? print cyan,"No layouts found.",reset,"\n" else print_layouts_table(layouts, ) print_results_pagination(json_response, {:label => "layout", :n_label => "layouts"}) # print_results_pagination(json_response) end print reset,"\n" rescue RestClient::Exception => e print_rest_exception(e, ) return 1 end end |
#remove(args) ⇒ Object
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 |
# File 'lib/morpheus/cli/library_layouts_command.rb', line 510 def remove(args) = {} optparse = Morpheus::Cli::OptionParser.new do|opts| opts. = subcommand_usage("[name]") (opts, , [:auto_confirm, :json, :dry_run, :remote]) opts. = "Delete a layout." end optparse.parse!(args) if args.count < 1 puts optparse exit 1 end connect() begin layout = find_layout_by_name_or_id(nil, args[0]) exit 1 if layout.nil? unless Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the instance type #{layout['name']}?", ) exit end if [:dry_run] print_dry_run @library_layouts_interface.dry.destroy(nil, layout['id']) return end json_response = @library_layouts_interface.destroy(nil, layout['id']) if [:json] print JSON.pretty_generate(json_response), "\n" return end print_green_success "Removed Layout #{layout['name']}" #list([]) rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |
#update(args) ⇒ Object
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 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 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 |
# File 'lib/morpheus/cli/library_layouts_command.rb', line 406 def update(args) = {} params = {} instance_type_id = nil option_type_ids = nil node_type_ids = nil evars = nil optparse = Morpheus::Cli::OptionParser.new do|opts| opts. = subcommand_usage("[name] [options]") opts.on('--name VALUE', String, "Name for this layout") do |val| params['name'] = val end opts.on('--version VALUE', String, "Version") do |val| params['instanceVersion'] = val end opts.on('--description VALUE', String, "Description") do |val| params['description'] = val end # opts.on('--technology CODE', String, "Technology") do |val| # params['provisionTypeCode'] = val # end opts.on('--min-memory VALUE', String, "Minimum Memory (MB)") do |val| params['memoryRequirement'] = val end opts.on('--workflow ID', String, "Workflow") do |val| params['taskSetId'] = val.to_i end opts.on('--option-types x,y,z', Array, "List of Option Type IDs") do |val| option_type_ids = val #.collect {|it| it.to_i } end opts.on('--node-types x,y,z', Array, "List of Node Type IDs") do |val| node_type_ids = val #.collect {|it| it.to_i } end #build_option_type_options(opts, options, update_layout_option_types()) (opts, , [:options, :json, :dry_run, :remote]) opts. = "Update a layout." end optparse.parse!(args) if args.count < 1 puts optparse exit 1 end connect() begin layout = find_layout_by_name_or_id(nil, args[0]) exit 1 if layout.nil? payload = nil if [:payload] payload = [:payload] else # option_types = update_layout_option_types(instance_type) # params = Morpheus::Cli::OptionTypes.prompt(option_types, options[:options], @api_client, options[:params]) params.deep_merge!([:options].reject {|k,v| k.is_a?(Symbol) }) if [:options] # ENVIRONMENT VARIABLES if evars else # prompt end # OPTION TYPES if option_type_ids params['optionTypes'] = option_type_ids.collect {|it| it.to_i }.select { |it| it != 0 } else # prompt end # NODE TYPES if node_type_ids params['containerTypes'] = node_type_ids.collect {|it| it.to_i }.select { |it| it != 0 } else # prompt end if params.empty? puts optparse exit 1 end payload = {'instanceTypeLayout' => params} end if [:dry_run] print_dry_run @library_layouts_interface.dry.update(nil, layout['id'], payload) return end json_response = @library_layouts_interface.update(nil, layout['id'], payload) if [:json] print JSON.pretty_generate(json_response), "\n" return end print_green_success "Updated Layout #{params['name'] || layout['name']}" #list([]) rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |