Class: Morpheus::Cli::CertificatesCommand

Inherits:
Object
  • Object
show all
Includes:
CliCommand
Defined in:
lib/morpheus/cli/commands/certificates_command.rb

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

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

#_get(id, params, options) ⇒ Object



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
# File 'lib/morpheus/cli/commands/certificates_command.rb', line 84

def _get(id, params, options)
  certificate = nil
  if id.to_s !~ /\A\d{1,}\Z/
    certificate = find_certificate_by_name_or_id(id)
    return 1, "certificate not found for #{id}" if certificate.nil?
    id = certificate['id']
  end
  @certificates_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @certificates_interface.dry.get(id, params)
    return
  end
  json_response = @certificates_interface.get(id, params)
  certificate = json_response[certificate_object_key]
  render_response(json_response, options, certificate_object_key) do
    print_h1 "Certificate Details", [], options
    print cyan
    show_columns = {
      "ID" => 'id',
      "Name" => 'name',
      "Description" => 'description',
      "Issued To" => lambda {|it|  it['commonName'] },
      "Cert Type" => lambda {|it| it['certType'] },
      "Domain Name" => lambda {|it| it['domainName'] },
      "Wildcard" => lambda {|it| format_boolean(it['wildcard']) },
    }
    print_description_list(show_columns, certificate, options)
    print reset,"\n"
  end
  return 0, nil
end

#_get_type(id, params, options) ⇒ Object



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/morpheus/cli/commands/certificates_command.rb', line 351

def _get_type(id, params, options)
  certificate_type = nil
  if id.to_s !~ /\A\d{1,}\Z/
    certificate_type = find_certificate_type_by_name_or_code(id)
    return 1, "certificate type not found for name or code '#{id}'" if certificate_type.nil?
    id = certificate_type['id']
  end
  # /api/certificate-types does not return optionTypes by default, use ?optionTypes=true
  @certificate_types_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @certificate_types_interface.dry.get(id, params)
    return
  end
  json_response = @certificate_types_interface.get(id, params)
  certificate_type = json_response[certificate_type_object_key]
  render_response(json_response, options, certificate_type_object_key) do
    print_h1 "Certificate Type Details", [], options
    print cyan
    show_columns = certificate_type_column_definitions
    print_description_list(show_columns, certificate_type)

    if certificate_type['optionTypes'] && certificate_type['optionTypes'].size > 0
      print_h2 "Option Types"
      opt_columns = [
        # {"ID" => lambda {|it| it['id'] } },
        {"FIELD NAME" => lambda {|it| (it['fieldContext'] && it['fieldContext'] != 'certificate') ? [it['fieldContext'], it['fieldName']].join('.') : it['fieldName']  } },
        {"FIELD LABEL" => lambda {|it| it['fieldLabel'] } },
        {"TYPE" => lambda {|it| it['type'] } },
        {"DEFAULT" => lambda {|it| it['defaultValue'] } },
        {"REQUIRED" => lambda {|it| format_boolean it['required'] } },
        # {"DESCRIPTION" => lambda {|it| it['description'] }, # do it!
      ]
      print as_pretty_table(certificate_type['optionTypes'], opt_columns)
    else
      # print cyan,"No option types found for this certificate type.","\n",reset
    end

    print reset,"\n"
  end
  return 0, nil
end

#add(args) ⇒ Object



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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/morpheus/cli/commands/certificates_command.rb', line 116

def add(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name] -t CODE [options]")
    # opts.on('-t', '--type CODE', "Certificate Type code, see `#{command_name} list-types` for available type codes") do |val|
    #   options[:options]['type'] = val
    # end
    build_option_type_options(opts, options, add_certificate_option_types)
    build_option_type_options(opts, options, add_certificate_advanced_option_types)
    build_standard_add_options(opts, options)
    opts.footer = <<-EOT
Create a new certificate.
[name] is required. This is the name of the new certificate
Configuration options vary by certificate type.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:0, max:1)
  options[:options]['name'] = args[0] if args[0]
  connect(options)
  payload = {}
  if options[:payload]
    payload = options[:payload]
    payload.deep_merge!({certificate_object_key => parse_passed_options(options)})
  else
    payload.deep_merge!({certificate_object_key => parse_passed_options(options)})
    # Type prompt first
    #params['type'] = Morpheus::Cli::OptionTypes.no_prompt([{'fieldName' => 'type', 'fieldLabel' => 'Type', 'type' => 'select', 'selectOptions' => [{'name' => 'Instance', 'value' => 'instance'}, {'name' => 'Blueprint', 'value' => 'blueprint'}, {'name' => 'Workflow', 'value' => 'workflow'}], 'defaultValue' => 'instance', 'required' => true}], options[:options], @api_client, options[:params])['type']
    v_prompt = Morpheus::Cli::OptionTypes.prompt(add_certificate_option_types(), options[:options], @api_client, options[:params])
    params.deep_merge!(v_prompt)
    advanced_config = Morpheus::Cli::OptionTypes.no_prompt(add_certificate_advanced_option_types, options[:options], @api_client, options[:params])
    advanced_config.deep_compact!
    params.deep_merge!(advanced_config)

    # lookup type by name or code to validate it exists and to prompt for its optionTypes
    # set certificate.type=code because the api expects it that way.
    if params['type'].to_s.empty?
      raise_command_error "missing required option: --type TYPE", args, optparse
    end
    certificate_type = find_certificate_type_by_name_or_code_id(params['type'])
    if certificate_type.nil?
      print_red_alert "certificate type not found for #{params['type']}"
      return 1, "certificate type not found for #{params['type']}"
    end
    params['type'] = certificate_type['code']
    config_option_types = certificate_type['optionTypes']
    if config_option_types.nil?
      config_option_types = @certificate_types_interface.option_types(certificate_type['id'])['optionTypes']
    end
    if config_option_types.nil?
      print yellow,"No option types found for certificate type: #{certificate_type['name']} (#{certificate_type['code']})", reset, "\n"
    end
    if config_option_types && config_option_types.size > 0
      # optionTypes do not need fieldContext: 'certificate'
      config_option_types.each do |opt|
        if opt['fieldContext'] == 'certificate' || opt['fieldContext'] == 'domain'
          opt['fieldContext'] = nil
        end
      end
      # reject hardcoded optionTypes
      config_option_types = config_option_types.reject {|it| it['fieldName'] == 'name' || it['fieldName'] == 'description' || it['fieldName'] == 'domainName' }
      config_prompt = Morpheus::Cli::OptionTypes.prompt(config_option_types, options[:options], @api_client, {certType: certificate_type['id']})
      config_prompt.deep_compact!
      params.deep_merge!(config_prompt)
    end
    # convert checkbox "on" and "off" to true and false
    params.booleanize!
    payload[certificate_object_key].deep_merge!(params)
  end
  @certificates_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @certificates_interface.dry.create(payload)
    return 0, nil
  end
  json_response = @certificates_interface.create(payload)
  certificate = json_response[certificate_object_key]
  render_response(json_response, options, certificate_object_key) do
    print_green_success "Added certificate #{certificate['name']}"
    return _get(certificate["id"], {}, options)
  end
  return 0, nil
end

#connect(opts) ⇒ Object



12
13
14
15
16
# File 'lib/morpheus/cli/commands/certificates_command.rb', line 12

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @certificates_interface = @api_client.certificates
  @certificate_types_interface = @api_client.certificate_types
end

#get(args) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/morpheus/cli/commands/certificates_command.rb', line 63

def get(args)
  params = {}
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[certificate]")
    build_standard_get_options(opts, options)
    opts.footer = <<-EOT
Get details about a specific certificate.
[certificate] is required. This is the name or id of a certificate.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:1)
  connect(options)
  params.merge!(parse_query_options(options))
  id_list = parse_id_list(args)
  return run_command_for_each_arg(id_list) do |arg|
    _get(arg, params, options)
  end
end

#get_type(args) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/morpheus/cli/commands/certificates_command.rb', line 327

def get_type(args)
  params = {'optionTypes' => true}
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[type]")
    opts.on('--optionTypes [true|false]', String, "Include optionTypes in the response. Default is true.") do |val|
      params['optionTypes'] = (val.to_s == '' || val.to_s == 'on' || val.to_s == 'true')
    end
    build_standard_get_options(opts, options)
    opts.footer = <<-EOT
Get details about a specific certificate type.
[type] is required. This is the name or id of a certificate type.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:1)
  connect(options)
  params.merge!(parse_query_options(options))
  id_list = parse_id_list(args)
  return run_command_for_each_arg(id_list) do |arg|
    _get_type(arg, params, options)
  end
end

#handle(args) ⇒ Object



18
19
20
# File 'lib/morpheus/cli/commands/certificates_command.rb', line 18

def handle(args)
  handle_subcommand(args)
end

#list(args) ⇒ Object



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
# File 'lib/morpheus/cli/commands/certificates_command.rb', line 22

def list(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[search]")
    build_standard_list_options(opts, options)
    opts.footer = "List certificates."
  end
  optparse.parse!(args)
  if args.count > 0
    options[:phrase] = args.join(" ")
  end
  connect(options)
  params.merge!(parse_list_options(options))
  @certificates_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @certificates_interface.dry.list(params)
    return
  end
  json_response = @certificates_interface.list(params)
  render_response(json_response, options, certificate_list_key) do
    certificates = json_response[certificate_list_key]
    print_h1 "Morpheus Certificates", parse_list_subtitles(options), options
    if certificates.empty?
      print cyan,"No certificates found.",reset,"\n"
    else
      list_columns = {
        "ID" => 'id',
        "Name" => 'name',
        "Issued To" => lambda {|it|  it['commonName'] },
        "Cert Type" => lambda {|it| it['certType'] },
        "Domain Name" => lambda {|it| it['domainName'] },
      }.upcase_keys!
      print as_pretty_table(certificates, list_columns, options)
      print_results_pagination(json_response)
    end
    print reset,"\n"
  end
  return 0, nil
end

#list_types(args) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/morpheus/cli/commands/certificates_command.rb', line 288

def list_types(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[search]")
    opts.on('--optionTypes [true|false]', String, "Include optionTypes in the response. Default is false.") do |val|
      params['optionTypes'] = (val.to_s == '' || val.to_s == 'on' || val.to_s == 'true')
    end
    build_standard_list_options(opts, options)
    opts.footer = "List certificate types."
  end
  optparse.parse!(args)
  connect(options)
  # verify_args!(args:args, optparse:optparse, count:0)
  if args.count > 0
    options[:phrase] = args.join(" ")
  end
  params.merge!(parse_list_options(options))
  @certificate_types_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @certificate_types_interface.dry.list(params)
    return
  end
  json_response = @certificate_types_interface.list(params)
  render_response(json_response, options, certificate_type_list_key) do
    certificate_types = json_response[certificate_type_list_key]
    print_h1 "Morpheus Certificate Types", parse_list_subtitles(options), options
    if certificate_types.empty?
      print cyan,"No certificate types found.",reset,"\n"
    else
      list_columns = certificate_type_column_definitions.upcase_keys!
      print as_pretty_table(certificate_types, list_columns, options)
      print_results_pagination(json_response)
    end
    print reset,"\n"
  end
  return 0, nil
end

#remove(args) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/morpheus/cli/commands/certificates_command.rb', line 256

def remove(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[certificate] [options]")
    build_standard_remove_options(opts, options)
    opts.footer = <<-EOT
Delete a certificate.
[certificate] is required. This is the name or id of a certificate.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:1)
  connect(options)
  certificate = find_certificate_by_name_or_id(args[0])
  return 1 if certificate.nil?
  @certificates_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @certificates_interface.dry.destroy(certificate['id'], params)
    return
  end
  unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the certificate #{certificate['name']}?")
    return 9, "aborted command"
  end
  json_response = @certificates_interface.destroy(certificate['id'], params)
  render_response(json_response, options) do
    print_green_success "Removed certificate #{certificate['name']}"
  end
  return 0, nil
end

#update(args) ⇒ Object



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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/morpheus/cli/commands/certificates_command.rb', line 200

def update(args)
  options = {}
  params = {}
  payload = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[certificate] [options]")
    build_option_type_options(opts, options, update_certificate_option_types)
    build_option_type_options(opts, options, update_certificate_advanced_option_types)
    opts.on(nil, '--no-refresh', "Skip refresh on update.") do
      payload['refresh'] = 'false'
    end
    build_standard_update_options(opts, options)
    opts.footer = <<-EOT
Update a certificate.
[certificate] is required. This is the name or id of a certificate.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:1)
  connect(options)
  certificate = find_certificate_by_name_or_id(args[0])
  return 1 if certificate.nil?
  payload = {}
  if options[:payload]
    payload = options[:payload]
    payload.deep_merge!({certificate_object_key => parse_passed_options(options)})
  else
    payload.deep_merge!({certificate_object_key => parse_passed_options(options)})
    # do not prompt on update
    v_prompt = Morpheus::Cli::OptionTypes.no_prompt(update_certificate_option_types, options[:options], @api_client, options[:params])
    v_prompt.deep_compact!
    params.deep_merge!(v_prompt)
    advanced_config = Morpheus::Cli::OptionTypes.no_prompt(update_certificate_advanced_option_types, options[:options], @api_client, options[:params])
    advanced_config.deep_compact!
    params.deep_merge!(advanced_config)
    # convert checkbox "on" and "off" to true and false
    params.booleanize!
    payload.deep_merge!({certificate_object_key => params})
    if payload[certificate_object_key].empty? # || options[:no_prompt]
      raise_command_error "Specify at least one option to update.\n#{optparse}"
    end
  end
  @certificates_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @certificates_interface.dry.update(certificate['id'], payload)
    return
  end
  json_response = @certificates_interface.update(certificate['id'], payload)
  certificate = json_response[certificate_object_key]
  render_response(json_response, options, certificate_object_key) do
    print_green_success "Updated certificate #{certificate['name']}"
    return _get(certificate["id"], {}, options)
  end
  return 0, nil
end