Class: Morpheus::Cli::PowerSchedulesCommand

Inherits:
Object
  • Object
show all
Includes:
CliCommand
Defined in:
lib/morpheus/cli/commands/power_schedules_command.rb

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from CliCommand

#add_query_parameter, #apply_options, #build_common_options, #build_get_options, #build_list_options, #build_option_type_options, #build_standard_add_many_options, #build_standard_add_options, #build_standard_api_options, #build_standard_delete_options, #build_standard_get_options, #build_standard_list_options, #build_standard_post_options, #build_standard_put_options, #build_standard_remove_options, #build_standard_update_options, #command_description, #command_name, #confirm, #confirm!, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #execute_api, #execute_api_payload, #execute_api_request, #find_all, #find_all_json, #find_by_id, #find_by_name, #find_by_name_or_id, #find_record, #find_record_json, #full_command_usage, #get_interface, #get_list_key, #get_object_key, #get_subcommand_description, #handle_each_payload, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_array, #parse_bytes_param, #parse_get_options!, #parse_id_list, #parse_labels, #parse_list_options, #parse_list_options!, #parse_list_subtitles, #parse_options, #parse_parameter_as_resource_id!, #parse_passed_options, #parse_payload, #parse_query_options, #print, #print_error, #println, #prog_name, #puts, #puts_error, #raise_args_error, #raise_command_error, #render_response, #run_command_for_each_arg, #subcommand_aliases, #subcommand_description, #subcommand_usage, #subcommands, #usage, #validate_outfile, #verify_args!, #visible_subcommands

Instance Method Details

#_get(id, params, options) ⇒ Object



84
85
86
87
88
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
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/morpheus/cli/commands/power_schedules_command.rb', line 84

def _get(id, params, options)
  options ||= {}
  options[:max_servers] ||= 10
  options[:max_instances] ||= 10
  
  schedule = find_schedule_by_name_or_id(id)
  if schedule.nil?
    return 1
  end
  @power_schedules_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @power_schedules_interface.dry.get(schedule['id'], params)
    return
  end
  json_response = @power_schedules_interface.get(schedule['id'], params)
  render_response(json_response, options, power_schedule_object_key) do
    schedule = json_response[power_schedule_object_key]
    instances = json_response['instances'] || []
    servers = json_response['servers'] || []
    
    print_h1 "Power Schedule Details"
    print cyan
    print_description_list(power_schedule_column_definitions(options), schedule, options)

    ## Instances
    if instances.size == 0
      # print cyan,"No instances",reset,"\n"
    else
      print_h2 "Instances (#{instances.size})"
      instance_rows = instances.first(options[:max_instances])
      print as_pretty_table(instance_rows, [:id, :name])
      print_results_pagination({'meta'=>{'total'=>instances.size,'size'=>instance_rows.size,'max'=>options[:max_servers],'offset'=>0}}, {:label => "instance in schedule", :n_label => "instances in schedule"})
    end

    ## Hosts
    if servers.size == 0
      # print cyan,"No hosts",reset,"\n"
    else
      options[:max_servers] ||= 10
      print_h2 "Hosts (#{servers.size})"
      server_rows = servers.first(options[:max_servers])
      print as_pretty_table(server_rows, [:id, :name])
      print_results_pagination({'meta'=>{'total'=>servers.size,'size'=>server_rows.size,'max'=>options[:max_servers],'offset'=>0}}, {:label => "host in schedule", :n_label => "hosts in schedule"})
    end

    print reset,"\n"
  end
  return 0, nil
end

#add(args) ⇒ Object



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
# File 'lib/morpheus/cli/commands/power_schedules_command.rb', line 134

def add(args)
  options = {}
  params = {'scheduleType' => 'power'}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name]")
    build_option_type_options(opts, options, add_power_schedule_option_types)
    build_standard_add_options(opts, options)
    opts.footer = "Create a new power schedule." + "\n" +
                  "[name] is required and can be passed as --name instead."
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:0, max:1)
  # support [name] as first argument
  if args[0]
    options[:options]['name'] = args[0]
  end
  connect(options)
  begin
    # construct payload
    payload = {}
    if options[:payload]
      payload = options[:payload]
      payload.deep_merge!({'schedule' => parse_passed_options(options)})
    else
      # merge -O options into normally parsed options
      payload.deep_merge!({'schedule' => parse_passed_options(options)})
      # prompt
      schedule_payload = Morpheus::Cli::OptionTypes.prompt(add_power_schedule_option_types, options[:options], @api_client, options[:params])
      payload.deep_merge!({'schedule' => schedule_payload})
      payload.booleanize!
    end
    @power_schedules_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @power_schedules_interface.dry.create(payload)
      return
    end
    json_response = @power_schedules_interface.create(payload)
    if options[:json]
      puts as_json(json_response, options)
    elsif !options[:quiet]
      schedule = json_response['schedule']
      print_green_success "Added power schedule #{schedule['name']}"
      _get(schedule['id'], {}, options)
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#add_hosts(args) ⇒ Object



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
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
# File 'lib/morpheus/cli/commands/power_schedules_command.rb', line 392

def add_hosts(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[schedule] [host]")
    build_standard_update_options(opts, options)
    opts.footer = "Assign hosts to a power schedule.\n" +
                  "[schedule] is required. This is the name or id of a power schedule.\n" +
                  "[host] is required. This is the name or id of a host. More than one can be passed."
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:2)
  connect(options)
  begin
    schedule = find_schedule_by_name_or_id(args[0])
    if schedule.nil?
      return 1
    end

    # construct payload
    payload = {}
    if options[:payload]
      payload = options[:payload]
      payload.deep_merge!(parse_passed_options(options))
    else
      payload.deep_merge!(parse_passed_options(options))
      server_ids = args[1..-1]
      servers = []
      server_ids.each do |server_id|
        server = find_server_by_name_or_id(server_id)
        return 1 if server.nil?
        servers << server
      end
      payload.deep_merge!({'servers' => servers.collect {|it| it['id'] } })
    end
    @power_schedules_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @power_schedules_interface.dry.add_servers(schedule["id"], payload)
      return 0
    end
    json_response = @power_schedules_interface.add_servers(schedule["id"], payload)
    if options[:json]
      puts as_json(json_response, options)
    elsif !options[:quiet]
      if servers.size == 1
          print_green_success  "Added host #{servers[0]['name']} to power schedule #{schedule['name']}"
        else
          print_green_success "Added #{servers.size} hosts to power schedule #{schedule['name']}"
        end
      _get(schedule['id'], {}, options)
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#add_instances(args) ⇒ Object



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
# File 'lib/morpheus/cli/commands/power_schedules_command.rb', line 276

def add_instances(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[schedule] [instance]")
    build_standard_update_options(opts, options)
    opts.footer = "Assign instances to a power schedule.\n" +
                  "[schedule] is required. This is the name or id of a power schedule.\n" +
                  "[instance] is required. This is the name or id of an instance. More than one can be passed."
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:2)
  connect(options)
  begin
    schedule = find_schedule_by_name_or_id(args[0])
    if schedule.nil?
      return 1
    end

    # construct payload
    payload = {}
    if options[:payload]
      payload = options[:payload]
      payload.deep_merge!(parse_passed_options(options))
    else
      payload.deep_merge!(parse_passed_options(options))
      instance_ids = args[1..-1]
      instances = []
      instance_ids.each do |instance_id|
        instance = find_instance_by_name_or_id(instance_id)
        return 1 if instance.nil?
        instances << instance
      end
      payload.deep_merge!({'instances' => instances.collect {|it| it['id'] } })
    end
    @power_schedules_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @power_schedules_interface.dry.add_instances(schedule["id"], payload)
      return 0
    end
    json_response = @power_schedules_interface.add_instances(schedule["id"], payload)
    if options[:json]
      puts as_json(json_response, options)
    elsif !options[:quiet]
      if instances.size == 1
          print_green_success  "Added #{instances[0]['name']} to power schedule #{schedule['name']}"
        else
          print_green_success "Added #{instances.size} instances to power schedule #{schedule['name']}"
        end
      _get(schedule['id'], {}, options)
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#connect(opts) ⇒ Object



14
15
16
17
18
19
# File 'lib/morpheus/cli/commands/power_schedules_command.rb', line 14

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @power_schedules_interface = @api_client.power_schedules
  @instances_interface = @api_client.instances
  @servers_interface = @api_client.servers
end

#get(args) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/morpheus/cli/commands/power_schedules_command.rb', line 59

def get(args)
  params = {}
  options = {}
  options[:max_instances] = 10
  options[:max_servers] = 10
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[schedule]")
    opts.on('--max-instances VALUE', String, "Display a limited number of instances in schedule. Default is 10") do |val|
      options[:max_instances] = val.to_i
    end
    opts.on('--max-hosts VALUE', String, "Display a limited number of hosts in schedule. Default is 10") do |val|
      options[:max_servers] = val.to_i
    end
    build_standard_get_options(opts, options)
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:1)
  connect(options)
  params.merge!(parse_query_options(options))
  id_list = parse_id_list(args)
  return run_command_for_each_arg(id_list) do |arg|
    _get(arg, params, options)
  end
end

#handle(args) ⇒ Object



21
22
23
# File 'lib/morpheus/cli/commands/power_schedules_command.rb', line 21

def handle(args)
  handle_subcommand(args)
end

#list(args) ⇒ Object



25
26
27
28
29
30
31
32
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
# File 'lib/morpheus/cli/commands/power_schedules_command.rb', line 25

def list(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[search]")
    build_standard_list_options(opts, options)
    opts.footer = "List power schedules."
  end
  optparse.parse!(args)
  connect(options)
  if args.count > 0
    options[:phrase] = args.join(" ")
  end
  params.merge!(parse_list_options(options))
  @power_schedules_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @power_schedules_interface.dry.list(params)
    return
  end
  json_response = @power_schedules_interface.list(params)
  render_response(json_response, options, power_schedule_list_key) do
    power_schedules = json_response[power_schedule_list_key]
    print_h1 "Morpheus Power Schedules", parse_list_subtitles(options), options
    if power_schedules.empty?
      print cyan,"No power schedules found.",reset,"\n"
    else
      print as_pretty_table(power_schedules, power_schedule_list_column_definitions(options).upcase_keys!, options)
      print_results_pagination(json_response)
    end
    print reset,"\n"
  end
  return 0, nil
end

#remove(args) ⇒ 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
# File 'lib/morpheus/cli/commands/power_schedules_command.rb', line 236

def remove(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[schedule]")
    build_standard_remove_options(opts, options)
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:1)
  connect(options)

  begin
    schedule = find_schedule_by_name_or_id(args[0])
    if schedule.nil?
      return 1
    end

    unless options[:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to delete power schedule '#{schedule['name']}'?", options)
      return false
    end

    @power_schedules_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @power_schedules_interface.dry.destroy(schedule["id"])
      return
    end

    json_response = @power_schedules_interface.destroy(schedule["id"])
    if options[:json]
      puts as_json(json_response, options)
    elsif !options[:quiet]
      print_green_success "Deleted power schedule #{schedule['name']}"
    end
    return 0, nil
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#remove_hosts(args) ⇒ Object



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
# File 'lib/morpheus/cli/commands/power_schedules_command.rb', line 450

def remove_hosts(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[schedule] [host]")
    build_standard_update_options(opts, options)
    opts.footer = "Remove hosts from a power schedule.\n" +
                  "[schedule] is required. This is the name or id of a power schedule.\n" +
                  "[host] is required. This is the name or id of a host. More than one can be passed."
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:2)
  connect(options)
  begin
    schedule = find_schedule_by_name_or_id(args[0])
    if schedule.nil?
      return 1
    end

    # construct payload
    payload = {}
    if options[:payload]
      payload = options[:payload]
      payload.deep_merge!(parse_passed_options(options))
    else
      payload.deep_merge!(parse_passed_options(options))
      server_ids = args[1..-1]
      servers = []
      server_ids.each do |server_id|
        server = find_server_by_name_or_id(server_id)
        return 1 if server.nil?
        servers << server
      end
      payload.deep_merge!({'servers' => servers.collect {|it| it['id'] } })
    end
    @power_schedules_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @power_schedules_interface.dry.remove_servers(schedule["id"], payload)
      return 0
    end
    json_response = @power_schedules_interface.remove_servers(schedule["id"], payload)
    if options[:json]
      puts as_json(json_response, options)
    elsif !options[:quiet]
      if servers.size == 1
          print_green_success  "Removed host #{servers[0]['name']} from power schedule #{schedule['name']}"
        else
          print_green_success "Removed #{servers.size} hosts from power schedule #{schedule['name']}"
        end
      _get(schedule['id'], {}, options)
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#remove_instances(args) ⇒ Object



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
# File 'lib/morpheus/cli/commands/power_schedules_command.rb', line 334

def remove_instances(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[schedule] [instance]")
    build_standard_update_options(opts, options)
    opts.footer = "Remove instances from a power schedule.\n" +
                  "[schedule] is required. This is the name or id of a power schedule.\n" +
                  "[instance] is required. This is the name or id of an instance. More than one can be passed."
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:2)
  connect(options)
  begin
    schedule = find_schedule_by_name_or_id(args[0])
    if schedule.nil?
      return 1
    end

    # construct payload
    payload = {}
    if options[:payload]
      payload = options[:payload]
      payload.deep_merge!(parse_passed_options(options))
    else
      payload.deep_merge!(parse_passed_options(options))
      instance_ids = args[1..-1]
      instances = []
      instance_ids.each do |instance_id|
        instance = find_instance_by_name_or_id(instance_id)
        return 1 if instance.nil?
        instances << instance
      end
      payload.deep_merge!({'instances' => instances.collect {|it| it['id'] } })
    end
    @power_schedules_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @power_schedules_interface.dry.remove_instances(schedule["id"], payload)
      return 0
    end
    json_response = @power_schedules_interface.remove_instances(schedule["id"], payload)
    if options[:json]
      puts as_json(json_response, options)
    elsif !options[:quiet]
      if instances.size == 1
          print_green_success  "Removed instance #{instances[0]['name']} from power schedule #{schedule['name']}"
        else
          print_green_success "Removed #{instances.size} instances from power schedule #{schedule['name']}"
        end
      _get(schedule['id'], {}, options)
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#update(args) ⇒ Object



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
# File 'lib/morpheus/cli/commands/power_schedules_command.rb', line 186

def update(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[schedule]")
    build_option_type_options(opts, options, update_power_schedule_option_types)
    build_standard_add_options(opts, options)
    opts.footer = "Update a power schedule." + "\n" +
                  "[schedule] is required. This is the name or id of a power schedule."
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:1)
  connect(options)
  begin
    schedule = find_schedule_by_name_or_id(args[0])
    if schedule.nil?
      return 1
    end
    # construct payload
    payload = {}
    if options[:payload]
      payload = options[:payload]
      payload.deep_merge!({'schedule' => parse_passed_options(options)})
    else
      # merge -O options into normally parsed options
      payload.deep_merge!({'schedule' => parse_passed_options(options)})
      # prompt
      schedule_payload = Morpheus::Cli::OptionTypes.no_prompt(update_power_schedule_option_types, options[:options], @api_client, options[:params])
      payload.deep_merge!({'schedule' => schedule_payload})
      payload.booleanize!
    end
    @power_schedules_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @power_schedules_interface.dry.update(schedule["id"], payload)
      return
    end
    json_response = @power_schedules_interface.update(schedule["id"], payload)
    if options[:json]
      puts as_json(json_response, options)
    elsif !options[:quiet]
      print_green_success "Updated power schedule #{schedule['name']}"
      _get(schedule['id'], {}, options)
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end