Class: Morpheus::Cli::View

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

Overview

require ‘morpheus/routes’

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from CliCommand

#apply_options, #build_common_options, #build_get_options, #build_list_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, #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_subcommand_description, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_bytes_param, #parse_get_options!, #parse_id_list, #parse_list_options, #parse_list_options!, #parse_list_subtitles, #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

#connect(opts) ⇒ Object



10
11
12
# File 'lib/morpheus/cli/commands/view.rb', line 10

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
end

#handle(args) ⇒ Object



14
15
16
17
18
19
20
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/morpheus/cli/commands/view.rb', line 14

def handle(args)
  params = {}
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[path] [id]")
    # debate: should login using /login/ouath-redirect
    opts.on('-L', '--login', "Login with the CLI access token before loading the path." ) do
      options[:login] = true
    end
    opts.on('--absolute', "Absolute path, do not search for a matching route to use") do
      options[:absolute_path] = true
    end
    build_common_options(opts, options, [:dry_run, :remote])
    opts.footer = <<-EOT
View the remote appliance in a web browser.
[path] is optional. This the path or resource type to load. The default is the index page "/".
[id] is optional. This is the resource name or id to be append to the path to load details of a specific object.
The [path] is matched against the #{prog_name} UI site map to find the best matching route.
Route matching is skipped if the path begins with a "/" or --absolute is used.
By default no authentication is not done and the existing web browser session used.
The --login option will authenticate via the CLI access token and create a new browser session.

Examples:
  view --login
  view monitoring
  view user 1
  view user administrator
  view /infrastructure/clouds/2
EOT
  end
  optparse.parse!(args)
  # verify_args!(args:args, optparse:optparse, min: 0, max: 2)
  connect(options)
  # todo: it would actually be cool to use the params and include them on the path..
  # params.merge!(parse_query_options(options))
  path, *ids = args
  # default to index page "/"
  path = path || "/"
  if options[:absolute_path] != true
    if path.start_with?("/")
      # treat like absolute path, no lookup
    else
      # lookup best matching route from sitemap
      # lookup plural routes first, so 'app' finds apps and not approvals
      found_route = Morpheus::Routes.lookup(path)
      if found_route
        # Morpheus::Logging::DarkPrinter.puts "Found matching route: '#{path}' => '#{found_route}'" if Morpheus::Logging.debug?
        path = found_route
      else
        # just use specified path
      end
    end
    # always add a leading slash
    path = path.start_with?("/") ? path : "/#{path}"
    # append id(s) to path if passed
    if ids.size > 0
      # convert names to ids
      # assume the last part of path is the type and use generic finder
      # only lookup names, and allow any id
      ids = ids.collect do |id|
        if id.to_s !~ /\A\d{1,}\Z/
          # assume the last part of path is the type
          record_type = path.split("/").last
          record = find_by_name(record_type, id)
          if record.nil?
            raise_command_error("[id] is invalid. No #{record_type} found for '#{id}'", args, optparse)
          end
          record['id'].to_s
        else
          id
        end
      end
      path = "#{path}/" + ids.join("/")
    end
  end
  # build the link to use, either our path or oauth-redirect to that path
  link = "#{@appliance_url}#{path}"
  if options[:login]
    # uh, this should need CGI::escape(path)
    link = "#{@appliance_url}/login/oauth-redirect?access_token=#{@access_token}\\&redirectUri=#{path}"
  end
  if options[:dry_run]
    print_system_command_dry_run(Morpheus::Util.open_url_command(link), options)
    return 0, nil
  end
  return Morpheus::Util.open_url(link)
end