Class: Morpheus::Cli::ActivityCommand
- Inherits:
-
Object
- Object
- Morpheus::Cli::ActivityCommand
- Includes:
- CliCommand, OperationsHelper, OptionSourceHelper
- Defined in:
- lib/morpheus/cli/commands/activity_command.rb
Instance Attribute Summary
Attributes included from CliCommand
Instance Method Summary collapse
Methods included from OptionSourceHelper
#find_available_user_option, #find_cloud_option, #find_group_option, #find_tenant_option, #get_available_user_options, #get_cloud_options, #get_group_options, #get_tenant_options, included, #load_option_source_data, #options_interface, #parse_cloud_id_list, #parse_group_id_list, #parse_option_source_id_list, #parse_project_id_list, #parse_tenant_id_list, #parse_user_id_list
Methods included from OperationsHelper
#format_activity_display_object, #format_activity_severity, included
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
#connect(opts) ⇒ Object
11 12 13 14 |
# File 'lib/morpheus/cli/commands/activity_command.rb', line 11 def connect(opts) @api_client = establish_remote_appliance_connection(opts) @activity_interface = @api_client.activity end |
#handle(args) ⇒ Object
16 17 18 |
# File 'lib/morpheus/cli/commands/activity_command.rb', line 16 def handle(args) handle_subcommand(args) end |
#list(args) ⇒ Object
20 21 22 23 24 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 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 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 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/morpheus/cli/commands/activity_command.rb', line 20 def list(args) exit_code, err = 0, nil params, = {}, {} optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage() opts.on('-a', '--details', "Display more details object id, full date and time, etc." ) do [:details] = true end opts.on('-t','--type TYPE', "Activity Type eg. Provisioning, Admin") do |val| [:type] ||= [] [:type] << val end opts.on('--timeframe TIMEFRAME', String, "Timeframe, eg. hour,day,today,yesterday,week,month,3months. Default is month") do |val| [:timeframe] = val end opts.on('--start TIMESTAMP','--start TIMESTAMP', "Start date to search for activity, can be used instead of --timeframe. Default is a month ago.") do |val| [:start] = parse_time(val) #.utc.iso8601 end opts.on('--end TIMESTAMP','--end TIMESTAMP', "Start date to search for activity. Default is the current time.") do |val| [:end] = parse_time(val) #.utc.iso8601 end opts.on('-u', '--user USER', "User Name or ID" ) do |val| [:user] = val end opts.on( '--tenant TENANT', String, "Tenant Name or ID" ) do |val| [:tenant] = val end (opts, ) opts. = <<-EOT List activity. The default timeframe a month ago up until now, with the most recent activity seen first. The option --timeframe or --start and --end can be used to customized the date period EOT end # parse options optparse.parse!(args) # parse arguments verify_args!(args:args, count:0, optparse:optparse) # establish connection to @remote_appliance connect() # construct request # inject -Q PARAMS and standard list options phrase,max,sort,search params.merge!(()) # --type if [:type] params['type'] = [[:type]].flatten.collect {|it| it.to_s.strip.split(",") }.flatten.collect {|it| it.to_s.strip } end # --timeframe if [:timeframe] params['timeframe'] = [:timeframe] end # --start if [:start] params['start'] = [:start] end # --end if [:end] params['end'] = [:end] end # --user if [:user] user_ids = parse_user_id_list([:user]) return 1 if user_ids.nil? params['userId'] = user_ids end # --tenant if [:tenant] tenant_ids = parse_tenant_id_list([:tenant]) return 1 if tenant_ids.nil? params['tenantId'] = tenant_ids[0] end # execute the api request @activity_interface.setopts() if [:dry_run] print_dry_run @activity_interface.dry.list(params) return 0, nil end json_response = @activity_interface.list(params) activity = json_response["activity"] render_response(json_response, , "activity") do title = "Morpheus Activity" subtitles = [] subtitles += parse_list_subtitles() if json_response["meta"] && json_response["meta"]["startDate"] subtitles << "#{format_local_dt(json_response["meta"]["startDate"])} - #{format_local_dt(json_response["meta"]["endDate"])}" end if [:start] subtitles << "Start: #{[:start]}" end if [:end] subtitles << "End: #{[:end]}" end print_h1 title, subtitles, if activity.empty? print yellow, "No activity found.",reset,"\n" else columns = [ # {"SEVERITY" => lambda {|record| format_activity_severity(record['severity']) } }, {"TYPE" => lambda {|record| record['activityType'] } }, {"NAME" => lambda {|record| record['name'] } }, [:details] ? {"RESOURCE" => lambda {|record| "#{record['objectType']} #{record['objectId']}" } } : nil, {"MESSAGE" => lambda {|record| record['message'] || '' } }, {"USER" => lambda {|record| record['user'] ? record['user']['username'] : record['userName'] } }, #{"DATE" => lambda {|record| "#{format_duration_ago(record['ts'] || record['timestamp'])}" } }, {"DATE" => lambda {|record| # show full time if searching for custom timerange or --details, otherwise the default is to show relative time if params['start'] || params['end'] || params['timeframe'] || [:details] "#{format_local_dt(record['ts'] || record['timestamp'])}" else "#{format_duration_ago(record['ts'] || record['timestamp'])}" end } }, ].compact print as_pretty_table(activity, columns, ) print_results_pagination(json_response) end print reset,"\n" end if activity.empty? return 1, "0 activity found" else return 0, nil end end |