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

#accounts_interface, #find_account_by_id, #find_account_by_name, #find_account_by_name_or_id, #find_account_from_options, #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, #format_role_type, #format_user_role_names, #get_access_string, included, #print_accounts_table, #print_roles_table, #print_users_table, #roles_interface, #users_interface

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, #print, #print_error, #puts, #puts_error, #raise_command_error, #run_command_for_each_arg, #subcommand_aliases, #subcommand_usage, #subcommands, #verify_access_token!

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

#format_activity_display_object(item) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/morpheus/cli/recent_activity_command.rb', line 128

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

#format_activity_severity(severity, return_color = cyan) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/morpheus/cli/recent_activity_command.rb', line 113

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

def list(args)
  options = {}
  optparse = 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).iso8601
    end
    opts.on('--end TIMESTAMP','--end TIMESTAMP', "End timestamp. Default is now.") do |val|
      options[:end] = parse_time(val).iso8601
    end
    build_common_options(opts, options, [:account, :list, :json, :yaml, :csv, :fields, :dry_run, :remote])
  end
  optparse.parse!(args)
  connect(options)
  begin
     = (options)
     =  ? ['id'] : nil
    params = {}
    [:phrase, :offset, :max, :sort, :direction, :start, :end].each do |k|
      params[k] = options[k] unless options[k].nil?
    end
    if options[:dry_run]
      print_dry_run @dashboard_interface.dry.recent_activity(, params)
      return
    end
    json_response = @dashboard_interface.recent_activity(, params)
    if options[:json]
      if options[:include_fields]
        json_response = {"activity" => filter_data(json_response["activity"], options[:include_fields]) }
      end
      print JSON.pretty_generate(json_response)
      print "\n"
      return 0
    end
    if options[:csv]
      puts records_as_csv(json_response["activity"], options)
      return 0
    end
    if options[:yaml]
      if options[:include_fields]
        json_response = {"activity" => filter_data(json_response["activity"], options[:include_fields]) }
      end
      puts as_yaml(json_response, options)
      return 0
    end

    print_h1 "Recent Activity"
    print cyan
    items = json_response["activity"]
    if items.empty?
      puts "No activity found."
      print reset,"\n"
      return 0
    end
    # JD: this api response is funky, no meta and it includes date objects
    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


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/morpheus/cli/recent_activity_command.rb', line 95

def print_recent_activity_table(items, opts={})
  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'] || '' } },
    # {"NAME" => lambda {|item| item['name'] } },
    {"OBJECT" => lambda {|item| format_activity_display_object(item) } },
    {"WHEN" => lambda {|item| format_local_dt(item['ts']) } }
    # {"WHEN" => lambda {|item| "#{format_duration(item['ts'])} ago" } }
  ]
  if opts[:include_fields]
    columns = opts[:include_fields]
  end
  print as_pretty_table(items, columns, opts)
end

#usageObject



22
23
24
# File 'lib/morpheus/cli/recent_activity_command.rb', line 22

def usage
  "Usage: morpheus #{command_name}"
end