Class: ServiceCmd

Inherits:
Object
  • Object
show all
Includes:
MrMurano::Verbose
Defined in:
lib/MrMurano/commands/service.rb

Overview

*** The ‘service` command class.

Constant Summary

Constants included from MrMurano::Verbose

MrMurano::Verbose::TABULARIZE_DATA_FORMAT_ERROR

Instance Method Summary collapse

Methods included from MrMurano::Verbose

ask_yes_no, #ask_yes_no, #assert, assert, cmd_confirm_delete!, #cmd_confirm_delete!, debug, #debug, dump_file_json, dump_file_plain, dump_file_yaml, #dump_output_file, #error, error, #error_file_format!, fancy_ticks, #fancy_ticks, #load_file_json, #load_file_plain, #load_file_yaml, #load_input_file, outf, #outf, #outformat_engine, #pluralize?, pluralize?, #prepare_hash_csv, #read_hashf!, #tabularize, tabularize, verbose, #verbose, warning, #warning, #whirly_interject, whirly_interject, #whirly_linger, whirly_linger, #whirly_msg, whirly_msg, #whirly_pause, whirly_pause, #whirly_start, whirly_start, #whirly_stop, whirly_stop, #whirly_unpause, whirly_unpause

Instance Method Details

#cmd_service_add_blather_result(response, alias_or_id, sol) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/MrMurano/commands/service.rb', line 275

def cmd_service_add_blather_result(response, alias_or_id, sol)
  fancy_alias = fancy_ticks(alias_or_id)
  fancy_name = fancy_ticks(sol.name)
  case response
  when Net::HTTPOK
    verbose %(Added service #{fancy_alias} to #{fancy_name}.)
  when Net::HTTPConflict
    verbose %(Service #{fancy_alias} was already added to #{fancy_name}.)
  when Net::HTTPPaymentRequired
    warning %(Use exchange to add the service to your business first.)
  else
    warning %(Unexpected response: #{response})
  end
end

#cmd_service_add_sol_service(alias_or_id, sol) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/MrMurano/commands/service.rb', line 252

def cmd_service_add_sol_service(alias_or_id, sol)
  svc_cfg = MrMurano::ServiceConfig.new(sol)
  http_req = nil
  response = svc_cfg.create(alias_or_id) do |request, http|
    http_req = request # (lb): This feels like cheating.
    response = http.request(request)
    cmd_service_add_blather_result(response, alias_or_id, sol)
    response
  end

  case response
  when Net::HTTPOK
    true
  when Net::HTTPConflict
    true
  when Net::HTTPPaymentRequired
    false
  else
    svc_cfg.showHttpError(http_req, response)
    false
  end
end

#cmd_service_add_sol_services(alias_or_id, solz) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/MrMurano/commands/service.rb', line 239

def cmd_service_add_sol_services(alias_or_id, solz)
  solz.each do |sol|
    fancy_alias = fancy_ticks(alias_or_id)
    fancy_name = fancy_ticks(sol.name)
    MrMurano::Verbose.whirly_start %(
      Adding #{fancy_alias} serviceconfig to #{fancy_name}...
    ).strip
    suc = cmd_service_add_sol_service(alias_or_id, sol)
    warning %(Unable to add service #{fancy_alias} to #{fancy_name}.) unless suc
  end
  MrMurano::Verbose.whirly_stop
end

#cmd_service_delete_blather_result(response, alias_or_id, sol) ⇒ Object



347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/MrMurano/commands/service.rb', line 347

def cmd_service_delete_blather_result(response, alias_or_id, sol)
  fancy_alias = fancy_ticks(alias_or_id)
  fancy_name = fancy_ticks(sol.name)
  case response
  when Net::HTTPNoContent
    verbose %(Deleted service #{fancy_alias} from #{fancy_name}.)
  when Net::HTTPNotFound
    verbose %(Service #{fancy_alias} was already removed from #{fancy_name}.)
  else
    warning %(Unexpected response: #{response})
  end
end

#cmd_service_delete_sol_service(alias_or_id, sol) ⇒ Object



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/MrMurano/commands/service.rb', line 330

def cmd_service_delete_sol_service(alias_or_id, sol)
  svc_cfg = MrMurano::ServiceConfig.new(sol)
  svc_cfg.remove(alias_or_id) do |request, http|
    response = http.request(request)
    cmd_service_delete_blather_result(response, alias_or_id, sol)
    case response
    when Net::HTTPNoContent
      true
    when Net::HTTPNotFound
      false
    else
      svc_cfg.showHttpError(request, response)
      false
    end
  end
end

#cmd_service_delete_sol_services(alias_or_id, solz) ⇒ Object



317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/MrMurano/commands/service.rb', line 317

def cmd_service_delete_sol_services(alias_or_id, solz)
  solz.each do |sol|
    fancy_alias = fancy_ticks(alias_or_id)
    fancy_name = fancy_ticks(sol.name)
    MrMurano::Verbose.whirly_start %(
      Deleting #{fancy_alias} serviceconfig from #{fancy_name}...
    ).strip
    suc = cmd_service_delete_sol_service(alias_or_id, sol)
    warning %(Service #{fancy_alias} not found for #{fancy_name}.) unless suc
  end
  MrMurano::Verbose.whirly_stop
end

#cmd_service_fetch_solution_services(solz, purchased, options) ⇒ Object

*** ‘service list` command and helpers.



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/MrMurano/commands/service.rb', line 429

def cmd_service_fetch_solution_services(solz, purchased, options)
  sol_svcs = []
  MrMurano::Verbose.whirly_start('Fetching solution services...')
  solz.each do |sol|
    MrMurano::Verbose.whirly_start('Fetching solution serviceconfig list...')
    # We could get everything without a query, e.g.,
    #   svc_cfgs = sol.serviceconfig()
    # but maybe it's faster to only ask for what we need.
    svc_cfgs = sol.serviceconfig(
      select: 'id,service,name,alias,status,type,script_key'
    )

    MrMurano::Verbose.whirly_start('Fetching solution service list...')
    svc_defs = sol.service

    MrMurano::Verbose.whirly_start('Assembling solution service list...')
    svc_reducer = ServiceConsolidator.new(options)
    sol_svcs += [
      [
        sol,
        svc_reducer.reconcile_services(
          svc_cfgs[:items],
          svc_defs[:items],
          purchased,
        ),
      ],
    ]
  end
  MrMurano::Verbose.whirly_stop
  sol_svcs
end

#cmd_service_get_sol_info(alias_or_id, sol) ⇒ Object



399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/MrMurano/commands/service.rb', line 399

def cmd_service_get_sol_info(alias_or_id, sol)
  svc_cfg = MrMurano::ServiceConfig.new(sol)
  svc_info = svc_cfg.info(alias_or_id) do |request, http|
    response = http.request(request)
    case response
    when Net::HTTPSuccess
      JSON.parse(response.body)
    when Net::HTTPNotFound
      # Info returns very basic info, e.g., { "usage": {} }
      { 'usage' => '<no such service>' }
    else
      svc_cfg.showHttpError(request, response)
      false
    end
  end
  svc_info
end

#cmd_service_get_sol_info_report(sol_infos, any_sol) ⇒ Object



417
418
419
420
421
422
423
424
425
# File 'lib/MrMurano/commands/service.rb', line 417

def cmd_service_get_sol_info_report(sol_infos, any_sol)
  headers = ['solution id', 'solution name', 'service usage']
  any_sol.outf(sol_infos) do |dd, ios|
    MrMurano::Verbose.tabularize(
      { headers: headers, rows: dd },
      ios
    )
  end
end

#cmd_service_get_sol_infos(alias_or_id, solz) ⇒ Object



384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/MrMurano/commands/service.rb', line 384

def cmd_service_get_sol_infos(alias_or_id, solz)
  sol_infos = []
  solz.each do |sol|
    MrMurano::Verbose.whirly_start %(
      Fetching #{fancy_ticks(alias_or_id)} serviceconfig for #{fancy_ticks(sol.name)}...
    ).strip
    svc_info = cmd_service_get_sol_info(alias_or_id, sol)
    svc_usage = svc_info['usage']
    svc_usage = '<none reported>' if svc_usage.empty?
    sol_infos += [[sol.sid, sol.name, svc_usage]]
  end
  MrMurano::Verbose.whirly_stop
  sol_infos
end

#cmd_service_output_solution_services(sols_and_svcs, options) ⇒ Object



482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'lib/MrMurano/commands/service.rb', line 482

def cmd_service_output_solution_services(sols_and_svcs, options)
  headers = %i[name alias status]
  headers += [:id] if options.show_id
  headers += [:state] if options.show_state
  headers += [:source] if options.show_source
  headers += [:description] if options.show_desc || options.abbrev_desc

  sol_svcs = sols_and_svcs.map { |_sol, svcs| svcs.map { |svc| OpenStruct.new(svc) } }
  width_avail = MrMurano::Pretties.width_last_column(headers, sol_svcs.flatten)
  sols_and_svcs.each do |sol, sol_svc|
    sol.outf(sol_svc) do |dd, ios|
      ios.puts(sol.pretty_desc(add_type: true)) if options.header
      rows = sol_svc_rowify(dd, headers, width_avail)
      sol.tabularize(
        { headers: headers, rows: rows },
        ios
      )
    end
  end
end

#command_service(cmd) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/MrMurano/commands/service.rb', line 197

def command_service(cmd)
  cmd.syntax = %(murano service)
  cmd.summary = %(About service)
  cmd.description = %(
    Commands for working with Solution Services.
  ).strip
  cmd.project_not_required = true
  cmd.must_not_be_managed = true
  cmd.subcmdgrouphelp = true

  cmd.action do |_args, _options|
    ::Commander::UI.enable_paging unless $cfg['tool.no-page']
    say MrMurano::SubCmdGroupHelp.new(cmd).get_help
  end
end

#command_service_add(cmd) ⇒ Object

*** ‘service add` command and helpers.



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/MrMurano/commands/service.rb', line 215

def command_service_add(cmd)
  cmd.syntax = %(murano service add [--options] <service-name>)
  cmd.summary = %(Add a service to a solution)
  cmd.description = %(
    Add a service to a solution.
  ).strip
  cmd.project_not_required = true
  cmd.must_not_be_managed = true

  # FIXME/2018-03-21: See comment under command_service_delete:
  #   We need to add multi-solution support so by default this
  #   command works on a single solution, rather than both the
  #   application and product (unless user specifies sol'n --type).
  cmd_add_solntype_pickers(cmd)

  cmd.action do |args, options|
    cmd.verify_arg_count!(args, 1, ['Missing argument: Service alias or ID'])
    cmd_defaults_solntype_pickers(options)
    alias_or_id = args[0]
    solz = must_fetch_solutions!(options)
    cmd_service_add_sol_services(alias_or_id, solz)
  end
end

#command_service_delete(cmd) ⇒ Object

*** ‘service delete` command and helpers.



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/MrMurano/commands/service.rb', line 292

def command_service_delete(cmd)
  cmd.syntax = %(murano service delete [--options] <service-name>)
  cmd.summary = %(Delete service from solution)
  cmd.description = %(
    Delete service from solution.
  ).strip
  cmd.project_not_required = true
  cmd.must_not_be_managed = true

  # Add flag: --type [application|product|all].
  # FIXME/2018-03-21: The way the CLI works, it chooses all solutions
  #   unless told otherwise; but we probably do not want to delete the
  #   same service from all solutions! So we really need multi-solution
  #   support! I.e., one solution active at a time, not 2!
  cmd_add_solntype_pickers(cmd)

  cmd.action do |args, options|
    cmd.verify_arg_count!(args, 1, ['Missing argument: Service alias or ID'])
    cmd_defaults_solntype_pickers(options)
    alias_or_id = args[0]
    solz = must_fetch_solutions!(options)
    cmd_service_delete_sol_services(alias_or_id, solz)
  end
end

#command_service_get(cmd) ⇒ Object

*** ‘service get` command and helpers.



362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/MrMurano/commands/service.rb', line 362

def command_service_get(cmd)
  cmd.syntax = %(murano service get [--options] <service-name>)
  cmd.summary = %(Get information about an added service)
  cmd.description = %(
    Get information about an added service.
  ).strip
  cmd.project_not_required = true
  cmd.must_not_be_managed = true

  # Add flag: --type [application|product|all].
  cmd_add_solntype_pickers(cmd)

  cmd.action do |args, options|
    cmd.verify_arg_count!(args, 1, ['Missing argument: Service alias or ID'])
    cmd_defaults_solntype_pickers(options)
    alias_or_id = args[0]
    solz = must_fetch_solutions!(options)
    sol_infos = cmd_service_get_sol_infos(alias_or_id, solz)
    cmd_service_get_sol_info_report(sol_infos, solz.first)
  end
end

#command_service_list(cmd) ⇒ Object



503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# File 'lib/MrMurano/commands/service.rb', line 503

def command_service_list(cmd)
  cmd.syntax = %(murano service list)
  cmd.summary = %(List all services previously added to a solution)
  cmd.description = %(
    List all services previously added to a solution.
  ).strip
  cmd.project_not_required = true
  cmd.must_not_be_managed = true

  # Add flag: --type [application|product|all].
  cmd_add_solntype_pickers(cmd)

  cmd.option(
    '--[no-]header', %(Output solution descriptions (default: true))
  )

  cmd.option '--show-all', 'Show all services, including hidden'
  cmd.option '--show-id', 'Show service IDs'
  cmd.option '--show-type', 'Show service types (Core, Product, Application, External)'
  cmd.option '--show-state', 'Show service definition status'
  cmd.option '--show-source', 'Show service source'
  cmd.option '--show-desc', 'Show service descriptions'
  cmd.option '--abbrev-desc', 'Show abbreviated service descriptions'

  cmd.option '--hide-added', 'Show only services available to add to Solution'
  cmd.option '--only-added', 'Show only services already added to Solution'

  cmd.action do |args, options|
    cmd.verify_arg_count!(args)
    options.default(
      all: false,
      header: true,
      show_all: false,
      show_id: false,
      show_type: false,
      show_state: false,
      show_source: false,
      show_desc: false,
      abbrev_desc: false,
      hide_added: false,
      only_added: false,
    )
    cmd_defaults_solntype_pickers(options)

    solz = must_fetch_solutions!(options)
    # Prepare the list of purchased Exchange services.
    biz_exg = MrMurano::Exchange.new
    MrMurano::Verbose.whirly_start('Fetching Exchange purchases list...')
    purchased = biz_exg.fetch_purchased(services_only: true)
    sols_and_svcs = cmd_service_fetch_solution_services(solz, purchased, options)

    cmd_service_output_solution_services(sols_and_svcs, options)
  end
end

#sol_svc_rowify(dd, headers, width_avail) ⇒ Object



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'lib/MrMurano/commands/service.rb', line 461

def sol_svc_rowify(dd, headers, width_avail)
  rows = []
  # Sort by status, then alias. (Yeti orders Exchange elements first.)
  sorted = dd.sort do |lhs, rhs|
    next(lhs[:status] <=> rhs[:status]) unless lhs[:status] == rhs[:status]
    lhs[:alias] <=> rhs[:alias]
  end
  sorted.each do |svc_cfg|
    row = headers.map do |key|
      full = svc_cfg[key]
      next(full) if full.to_s.empty?
      # Apparently descriptions have unpredictable whitespace and HTML tags.
      full = full.tr("\n", ' ')
      next(full) if width_avail.nil? || key != :description
      MrMurano::Pretties.split_text_on_whitespace(full, width_avail)
    end
    rows << row
  end
  rows
end