Class: Morpheus::Cli::ApprovalsCommand

Inherits:
Object
  • Object
show all
Includes:
AccountsHelper, CliCommand
Defined in:
lib/morpheus/cli/approvals_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

Instance Method Details

#_get(approval_id, options = {}) ⇒ Object



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

def _get(approval_id, options = {})
  params = {}
  begin
    @approvals_interface.setopts(options)

    if !(approval_id.to_s =~ /\A\d{1,}\Z/)
      approval = find_approval_by_name_or_id('approval', approval_id)

      if !approval
        print_red_alert "Approval #{approval_id} not found"
        exit 1
      end
      approval_id = approval['id']
    end

    if options[:dry_run]
      print_dry_run @approvals_interface.dry.get(approval_id, params)
      return
    end
    json_response = @approvals_interface.get(approval_id, params)

    render_result = render_with_format(json_response, options, 'approval')
    return 0 if render_result

    title = "Morpheus Approval"
    subtitles = []
    subtitles += parse_list_subtitles(options)
    print_h1 title, subtitles

    approval = json_response['approval']
    print cyan
    description_cols = {
        "ID" => lambda {|it| it['id']},
        "Name" => lambda {|it| it['name'] || (it['accountIntegration'] ? 'Pending' : 'Not Set')},
        "Request Type" => lambda {|it| it['requestType']},
        "External Name" => lambda {|it|it['accountIntegration'] ? it['externalName'] || 'Pending' : 'N/A'},
        "Type" => lambda {|it| it['accountIntegration'] ? it['accountIntegration']['type'] : 'Internal'},
        "Date Created" => lambda {|it| format_local_dt(it['dateCreated'])},
        "Requested By" => lambda {|it| it['requestBy']}
    }
    print_description_list(description_cols, approval)

    print_h2 "Requested Items"
    approval_items = approval['approvalItems']

    if approval_items && !approval_items.empty?
      rows = approval_items.collect do |it|
        {
            id: it['id'],
            name: it['name'] || 'Not Set',
            external_name: it['externalName'] || 'N/A',
            reference: it['reference'] ? it['reference']['displayName'] || it['reference']['name'] : '',
            status: (it['status'] || '').capitalize,
            created: format_local_dt(it['dateCreated']),
            updated: format_local_dt(it['lastUpdated'])
        }
      end
      columns = [
          :id, :name, :external_name, :reference, :status, :created, :updated
      ]
      print as_pretty_table(rows, columns, options)
    else
      print cyan,"No requested items.",reset,"\n"
    end

    print reset,"\n"
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#_update_item(args, action) ⇒ Object



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

def _update_item(args, action)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage( "[id]")
    build_common_options(opts, options, [:json, :dry_run, :remote, :quiet])
    opts.footer = "#{action.capitalize} item.\n[id] is required. Approval item ID"
  end
  optparse.parse!(args)
  connect(options)
  if args.count != 1
    raise_command_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args}\n#{optparse}"
    return 1
  end

  begin
    approval_item = @approvals_interface.get_item(args[0].to_i)['approvalItem']

    if !approval_item
      print_red_alert "Approval item #{args[0]} not found"
      exit 1
    end

    @approvals_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @approvals_interface.dry.update_item(approval_item['id'], action)
      return
    end
    json_response = @approvals_interface.update_item(approval_item['id'], action)

    if options[:json]
      puts as_json(json_response, options)
    elsif !options[:quiet]
      if json_response['success']
        print_green_success  "Approval item #{action} applied"
        _get(approval_item['approval']['id'])
      else
        print_red_alert "Error updating approval item: #{json_response['msg'] || json_response['errors']}"
      end
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#approve(args) ⇒ Object



173
174
175
# File 'lib/morpheus/cli/approvals_command.rb', line 173

def approve(args)
  return _update_item(args, 'approve')
end

#cancel(args) ⇒ Object



181
182
183
# File 'lib/morpheus/cli/approvals_command.rb', line 181

def cancel(args)
  return _update_item(args, 'cancel')
end

#connect(opts) ⇒ Object



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

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @approvals_interface = @api_client.approvals
end

#deny(args) ⇒ Object



177
178
179
# File 'lib/morpheus/cli/approvals_command.rb', line 177

def deny(args)
  return _update_item(args, 'deny')
end

#get(args) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/morpheus/cli/approvals_command.rb', line 84

def get(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[approval]")
    build_common_options(opts, options, [:json, :dry_run, :remote])
    opts.footer = "Get details about a job.\n" +
        "[approval] is required. Approval ID or name"
  end
  optparse.parse!(args)
  if args.count != 1
    raise_command_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args}\n#{optparse}"
  end
  connect(options)
  return _get(args[0], options)
end

#handle(args) ⇒ Object



17
18
19
# File 'lib/morpheus/cli/approvals_command.rb', line 17

def handle(args)
  handle_subcommand(args)
end

#list(args) ⇒ Object



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

def list(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage()
    build_common_options(opts, options, [:list, :query, :json, :yaml, :csv, :fields, :dry_run, :remote])
    opts.footer = "List approvals."
  end
  optparse.parse!(args)
  connect(options)
  if args.count != 0
    raise_command_error "wrong number of arguments, expected 0 and got (#{args.count}) #{args}\n#{optparse}"
    return 1
  end

  begin
    params.merge!(parse_list_options(options))
    @approvals_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @approvals_interface.dry.list(params)
      return
    end
    json_response = @approvals_interface.list(params)

    render_result = render_with_format(json_response, options, 'approvals')
    return 0 if render_result

    title = "Morpheus Approvals"
    subtitles = []
    subtitles += parse_list_subtitles(options)
    print_h1 title, subtitles

    approvals = json_response['approvals']
    if approvals.empty?
      print cyan,"No approvals found.",reset,"\n"
    else
      rows = approvals.collect do |it|
        {
            id: it['id'],
            name: it['name'] || (it['accountIntegration'] ? 'Pending' : 'Not Set'),
            requestType: it['requestType'],
            externalName: it['accountIntegration'] ? it['externalName'] || 'Pending' : 'N/A',
            type: it['accountIntegration'] ? it['accountIntegration']['type'] : 'Internal',
            status: it['status'],
            dateCreated: format_local_dt(it['dateCreated']),
            requestedBy: it['requestBy']
        }
      end
      columns = [
          :id, :name, :requestType, :externalName, :type, :status, :dateCreated, :requestedBy
      ]
      print as_pretty_table(rows, columns, options)
      print_results_pagination(json_response)
      print reset,"\n"
    end
    print reset,"\n"
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end