Class: Morpheus::Cli::RecentActivityCommand
- Inherits:
-
Object
- Object
- Morpheus::Cli::RecentActivityCommand
show all
- Includes:
- AccountsHelper, CliCommand
- Defined in:
- lib/morpheus/cli/recent_activity_command.rb
Instance Attribute Summary
Attributes included from CliCommand
#no_prompt
Instance Method Summary
collapse
#accounts_interface, #find_account_by_id, #find_account_by_name, #find_account_by_name_or_id, #find_account_from_options, #find_all_user_ids, #find_role_by_id, #find_role_by_name, #find_role_by_name_or_id, #find_user_by_id, #find_user_by_username, #find_user_by_username_or_id, #find_user_group_by_id, #find_user_group_by_name, #find_user_group_by_name_or_id, #format_role_type, #format_user_role_names, #get_access_string, included, #print_accounts_table, #print_roles_table, #print_users_table, #roles_interface, #user_groups_interface, #users_interface
Methods included from CliCommand
#build_common_options, #build_option_type_options, #command_name, #default_refresh_interval, #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, #render_with_format, #run_command_for_each_arg, #subcommand_aliases, #subcommand_usage, #subcommands, #verify_access_token!
Constructor Details
Returns a new instance of RecentActivityCommand.
12
13
14
|
# File 'lib/morpheus/cli/recent_activity_command.rb', line 12
def initialize()
end
|
Instance Method Details
#connect(opts) ⇒ Object
16
17
18
19
20
|
# File 'lib/morpheus/cli/recent_activity_command.rb', line 16
def connect(opts)
@api_client = establish_remote_appliance_connection(opts)
@dashboard_interface = @api_client.dashboard
@accounts_interface = @api_client.accounts
end
|
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/morpheus/cli/recent_activity_command.rb', line 135
def format_activity_display_object(item)
out = ""
if item['name']
out << item['name']
end
if item['objectType']
out << " (#{item['objectType']} #{item['objectId']})"
end
if item['deleted']
out << " [deleted]"
end
out
end
|
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/morpheus/cli/recent_activity_command.rb', line 120
def format_activity_severity(severity, return_color=cyan)
out = ""
status_string = severity
if status_string == 'critical'
out << "#{red}#{status_string.capitalize}#{return_color}"
elsif status_string == 'warning'
out << "#{yellow}#{status_string.capitalize}#{return_color}"
elsif status_string == 'info'
out << "#{cyan}#{status_string.capitalize}#{return_color}"
else
out << "#{cyan}#{status_string}#{return_color}"
end
out
end
|
#handle(args) ⇒ Object
26
27
28
|
# File 'lib/morpheus/cli/recent_activity_command.rb', line 26
def handle(args)
list(args)
end
|
#list(args) ⇒ Object
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
|
# File 'lib/morpheus/cli/recent_activity_command.rb', line 29
def list(args)
options = {}
optparse = Morpheus::Cli::OptionParser.new do |opts|
opts.banner = usage
opts.on('--start TIMESTAMP','--start TIMESTAMP', "Start timestamp. Default is 30 days ago.") do |val|
options[:start] = parse_time(val).utc.iso8601
end
opts.on('--end TIMESTAMP','--end TIMESTAMP', "End timestamp. Default is now.") do |val|
options[:end] = parse_time(val).utc.iso8601
end
build_common_options(opts, options, [:account, :query, :list, :json, :yaml, :csv, :fields, :dry_run, :remote])
end
optparse.parse!(args)
connect(options)
begin
account = find_account_from_options(options)
account_id = account ? account['id'] : nil
params = {}
params.merge!(parse_list_options(options))
if options[:start]
params['start'] = options[:start]
end
if options[:end]
params['end'] = options[:end]
end
@dashboard_interface.setopts(options)
if options[:dry_run]
print_dry_run @dashboard_interface.dry.recent_activity(account_id, params)
return
end
json_response = @dashboard_interface.recent_activity(account_id, params)
if options[:json]
puts as_json(json_response, options, "activity")
return 0
elsif options[:yaml]
puts as_yaml(json_response, options, "activity")
return 0
elsif options[:csv]
json_response['activity'] = json_response['activity'].reject {|it| it.keys.size == 1 && it.keys[0] == 'date' }
puts records_as_csv(json_response['activity'], options)
return 0
end
title = "Recent Activity"
subtitles = []
subtitles += parse_list_subtitles(options)
if options[:start]
subtitles << "Start: #{options[:start]}"
end
if options[:end]
subtitles << "End: #{options[:end]}"
end
print_h1 title, subtitles
print cyan
items = json_response["activity"]
if items.empty?
puts "No activity found."
print reset,"\n"
return 0
end
items = items.select { |item| item['_id'] || item['name'] }
print_recent_activity_table(items, options)
print reset,"\n"
return 0
rescue RestClient::Exception => e
print_rest_exception(e, options)
return 1
end
end
|
#print_recent_activity_table(items, opts = {}) ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/morpheus/cli/recent_activity_command.rb', line 102
def print_recent_activity_table(items, opts={})
columns = [
{"TYPE" => lambda {|item| item['activityType'] } },
{"AUTHOR" => lambda {|item| item['userName'] || '' } },
{"MESSAGE" => lambda {|item| item['message'] || '' } },
{"OBJECT" => lambda {|item| format_activity_display_object(item) } },
{"WHEN" => lambda {|item| format_local_dt(item['ts']) } }
]
if opts[:include_fields]
columns = opts[:include_fields]
end
print as_pretty_table(items, columns, opts)
end
|
#usage ⇒ Object
22
23
24
|
# File 'lib/morpheus/cli/recent_activity_command.rb', line 22
def usage
"Usage: morpheus #{command_name}"
end
|