Class: Morpheus::Cli::DocCommand

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

Overview

This provides commands for authentication This also includes credential management.

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

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

Constructor Details

#initializeDocCommand

Returns a new instance of DocCommand.



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

def initialize()
  # @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
end

Instance Method Details

#connect(options) ⇒ Object



25
26
27
28
# File 'lib/morpheus/cli/doc.rb', line 25

def connect(options)
  @api_client = establish_remote_appliance_connection(options.merge({:no_prompt => true, :skip_verify_access_token => true, :skip_login => true}))
  @doc_interface = @api_client.doc
end

#download_swagger(args) ⇒ Object



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
172
173
174
175
176
177
178
# File 'lib/morpheus/cli/doc.rb', line 115

def download_swagger(args)
  exit_code, err = 0, nil
  params, options = {}, {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[local-file]")
    # build_standard_get_options(opts, options, [], [:csv,:out])
    opts.on(nil, '--yaml', "YAML Output") do
      options[:yaml] = true
      options[:format] = :yaml
    end
    opts.on(nil, "--refresh", "Refresh the document. By default the swagger.yml and swagger.json are cached by the server.") do
      params['refresh'] = true
    end
    opts.on('-g', '--generate', "Alias for --refresh") do
      params['refresh'] = true
    end
    opts.on( '-f', '--force', "Overwrite existing [local-file] if it exists." ) do
      options[:overwrite] = true
    end
    opts.on( '-p', '--mkdir', "Create missing directories for [local-file] if they do not exist." ) do
      options[:mkdir] = true
    end
    build_common_options(opts, options, [:dry_run, :quiet, :remote])
    opts.footer = <<-EOT
Download the Morpheus API Swagger Documentation (openapi).
[local-file] is required. This is the full local filepath for the downloaded file.
The default format is JSON. Supports json or yaml.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:1)
  connect(options)
  # parse args
  outfile = args[0]
  if !validate_outfile(outfile, options)
    return 1, "Failed to validate outfile"
  end
  # construct the api request
  params.merge!(parse_list_options(options))
  if outfile.include?(".yml") || outfile.include?(".yaml")
    options[:yaml] = true
  end
  openapi_format = options[:yaml] ? "yaml" : "json"
  params['format'] = openapi_format
  # execute the api request
  @doc_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @doc_interface.dry.download_swagger(outfile, params)
    return 0, nil
  end
  print cyan + "Downloading swagger.#{openapi_format} to #{outfile} ... " if !options[:quiet]
  http_response = @doc_interface.download_swagger(outfile, params)
  if http_response.code.to_i == 200
    print green + "SUCCESS" + reset + "\n" if !options[:quiet]
    return 0, nil
  else
    print red + "ERROR" + reset + " HTTP #{http_response.code}" + "\n" if !options[:quiet]
    if File.exists?(outfile) && File.file?(outfile)
      Morpheus::Logging::DarkPrinter.puts "Deleting bad file download: #{outfile}" if Morpheus::Logging.debug?
      File.delete(outfile)
    end
    return 1, "HTTP #{http_response.code}"
  end
end

#handle(args) ⇒ Object



21
22
23
# File 'lib/morpheus/cli/doc.rb', line 21

def handle(args)
  handle_subcommand(args)
end

#list(args) ⇒ Object



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

def list(args)
  exit_code, err = 0, nil
  params, options = {}, {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage()
    build_standard_get_options(opts, options)
    opts.footer = <<-EOT
List documentation links.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:0)
  connect(options)
  # construct the api request
  params.merge!(parse_list_options(options))
  # execute the api request
  @doc_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @doc_interface.dry.list(params)
    return 0, nil
  end
  json_response = @doc_interface.list(params)
  render_response(json_response, options, "links") do
    title = "Morpheus Documentation"
    print_h1 title, options
    if json_response['links'].empty?
      print yellow, "No help links found.",reset,"\n"
    else
      columns = {
        "Link Name" => 'name',
        "URL" => 'url',
        "Description" => {display_method:'description', max_width: (options[:wrap] ? nil : 50)}, 
      }
      print as_pretty_table(json_response['links'], columns.upcase_keys!, options)
      # print_results_pagination(json_response)
    end
    print reset,"\n"
  end
  return exit_code, err
end

#swagger(args) ⇒ Object



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
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/morpheus/cli/doc.rb', line 71

def swagger(args)
  exit_code, err = 0, nil
  params, options = {}, {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage()
    opts.on(nil, "--refresh", "Refresh the document. By default the swagger.yml and swagger.json are cached by the server.") do
      params['refresh'] = true
    end
    opts.on('-g', '--generate', "Alias for --refresh") do
      params['refresh'] = true
    end
    build_standard_get_options(opts, options, [], [:csv])
    opts.footer = <<-EOT
Print the Morpheus API Swagger Documentation (openapi).
The default format is JSON. Supports json or yaml.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:0)
  connect(options)
  # construct the api request
  params.merge!(parse_list_options(options))
  # for now, always use .json, and just convert to yaml for display on cli side
  openapi_format = options[:yaml] ? "yaml" : "json"
  # params['format'] = openapi_format
  # execute the api request
  @doc_interface.setopts(options)
  if options[:dry_run]
    params['format'] = openapi_format
    print_dry_run @doc_interface.dry.swagger(params)
    return 0, nil
  end
  json_response = @doc_interface.swagger(params)
  # default format is to print header and json
  render_response(json_response, options) do
    title = "Morpheus API swagger.#{openapi_format}"
    print_h1 title, options
    print cyan
    print as_json(json_response, options)
    print reset,"\n"
  end
  return exit_code, err
end