Class: Morpheus::Cli::RecentActivityCommand

Inherits:
Object
  • Object
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

Methods included from AccountsHelper

#account_column_definitions, #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_access_string, #format_role_type, #format_user_role_names, #format_user_status, #get_access_color, #get_access_string, included, #list_account_column_definitions, #list_user_column_definitions, #list_user_group_column_definitions, #role_column_definitions, #roles_interface, #subtenant_role_column_definitions, #user_column_definitions, #user_group_column_definitions, #user_groups_interface, #users_interface

Methods included from CliCommand

#apply_options, #build_common_options, #build_option_type_options, #build_standard_add_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, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #full_command_usage, #get_subcommand_description, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_bytes_param, #parse_id_list, #parse_list_options, #parse_list_subtitles, #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

Constructor Details

#initializeRecentActivityCommand

Returns a new instance of RecentActivityCommand.



12
13
14
# File 'lib/morpheus/cli/recent_activity_command.rb', line 12

def initialize()
  # @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
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

#handle(args) ⇒ Object

def usage

"Usage: morpheus #{command_name}"

end



26
27
28
29
# File 'lib/morpheus/cli/recent_activity_command.rb', line 26

def handle(args)
  print_error yellow,"[DEPRECATED] The command `recent-activity` is deprecated. It has been replaced by `activity list`.",reset,"\n"
  list(args)
end

#list(args) ⇒ Object



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
# File 'lib/morpheus/cli/recent_activity_command.rb', line 31

def list(args)
  params, options = {}, {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = usage
    opts.on( '-u', '--user USER', "Username or ID" ) do |val|
      options[:user] = val
    end
    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_standard_get_options(opts, options)
    opts.footer = <<-EOT
List recent activity.
This command is deprecated. Use `activity` instead.
EOT
  end
  # parse options
  optparse.parse!(args)
  # parse arguments
  verify_args!(args:args, count:0, optparse:optparse)
  # establish connection to @remote_appliance
  connect(options)
  # construct request
  #params.merge!(parse_query_options(options)) # inject -Q PARAMS
  params.merge!(parse_list_options(options)) # inject phrase,sort,max,offset and -Q PARAMS
  # parse my options
  # this api allows filter by params.accountId
  # todo: use OptionSourceHelper parse_tenant_id() instead of AccountsHelper
   = (options)
   =  ? ['id'] : nil
  if 
    params['accountId'] = 
  end
  if options[:start]
    params['start'] = options[:start]
  end
  if options[:end]
    params['end'] = options[:end]
  end

  # parse --user
  if options[:user]
    user_ids = parse_user_id_list(options[:user])
    return 1 if user_ids.nil?
    # userId limited to one right now
    # params['userId'] = user_ids
    params['userId'] = user_ids[0]
  end
  
  # setup interface and check for dry run?
  @dashboard_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @dashboard_interface.dry.recent_activity(params)
    return 0, nil
  end
  
  # make the request
  json_response = @dashboard_interface.recent_activity(params)
  
  # determine exit status
  exit_code, err = 0, nil
  
  # could error if there are no results.
  # if json_response['activity'].empty?
  #   exit_code = 3 
  #   err = "0 results found"
  # end
  
  # render output
  render_response(json_response, options, "activity") do
    title = "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"
    else
      # JD: this api response is funky, no meta and it includes date objects
      # ok then its gone, get good api response data gosh darnit!
      # use /api/activity instead
      items = items.select { |item| item['_id'] || item['name'] }
      columns = [
        # {"ID" => lambda {|item| item['id'] } },
        # {"SEVERITY" => lambda {|item| format_activity_severity(item['severity']) } },
        {"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']) } }
      ]
      print as_pretty_table(items, columns, options)
      print reset,"\n"
    end
    return exit_code, err
  end
end