Class: Morpheus::Cli::LibraryOptionListsCommand
- Inherits:
-
Object
- Object
- Morpheus::Cli::LibraryOptionListsCommand
- Includes:
- CliCommand, LibraryHelper
- Defined in:
- lib/morpheus/cli/library_option_lists_command.rb
Instance Attribute Summary
Attributes included from CliCommand
Instance Method Summary collapse
- #add(args) ⇒ Object
- #connect(opts) ⇒ Object
- #get(args) ⇒ Object
- #handle(args) ⇒ Object
-
#initialize ⇒ LibraryOptionListsCommand
constructor
A new instance of LibraryOptionListsCommand.
- #list(args) ⇒ Object
- #remove(args) ⇒ Object
- #update(args) ⇒ Object
Methods included from LibraryHelper
#api_client, included, #load_balance_protocols_dropdown, #prompt_exposed_ports
Methods included from CliCommand
#build_common_options, #build_option_type_options, #command_name, #default_refresh_interval, #default_subcommand, #establish_remote_appliance_connection, #full_command_usage, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_id_list, #parse_list_options, #parse_list_subtitles, #print, #print_error, #puts, #puts_error, #raise_command_error, #render_with_format, #run_command_for_each_arg, #subcommand_aliases, #subcommand_usage, #subcommands, #usage, #verify_access_token!
Constructor Details
#initialize ⇒ LibraryOptionListsCommand
Returns a new instance of LibraryOptionListsCommand.
14 15 16 |
# File 'lib/morpheus/cli/library_option_lists_command.rb', line 14 def initialize() # @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance end |
Instance Method Details
#add(args) ⇒ Object
178 179 180 181 182 183 184 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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/morpheus/cli/library_option_lists_command.rb', line 178 def add(args) # JD: this is annoying because our option_types (for prompting and help) # are the same type of object being managed here.., options options options = {} my_option_types = nil list_type = nil optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage("[type] [options]") opts.on( '-t', '--type TYPE', "Option List Type. (rest, manual)" ) do |val| list_type = val # options[:options] ||= {} # options[:options]['type'] = val end (opts, , new_option_type_list_option_types()) (opts, , [:options, :payload, :json, :dry_run, :remote]) end optparse.parse!(args) connect() begin = [:options].reject {|k,v| k.is_a?(Symbol) } payload = nil if [:payload] payload = [:payload] # support -O OPTION switch on top of --payload if !.empty? payload['optionTypeList'] ||= {} payload['optionTypeList'].deep_merge!() end else if !list_type v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'type', 'fieldLabel' => 'Type', 'type' => 'select', 'selectOptions' => get_available_option_list_types, 'defaultValue' => 'rest', 'required' => true}], [:options], @api_client, {}) list_type = v_prompt['type'] end params = v_prompt = Morpheus::Cli::OptionTypes.prompt(new_option_type_list_option_types(list_type), [:options], @api_client, [:params]) params.deep_merge!(v_prompt) params['type'] = list_type if params['type'] == 'rest' # prompt for Source Headers source_headers = prompt_source_headers() if !source_headers.empty? params['config'] ||= {} params['config']['sourceHeaders'] = source_headers end end if params.key?('required') params['required'] = ['on','true'].include?(params['required'].to_s) end list_payload = params payload = {'optionTypeList' => list_payload} end @option_type_lists_interface.setopts() if [:dry_run] print_dry_run @option_type_lists_interface.dry.create(payload) return end json_response = @option_type_lists_interface.create(payload) if [:json] print JSON.pretty_generate(json_response), "\n" return end option_type_list = json_response['optionTypeList'] print_green_success "Added Option List #{option_type_list['name']}" get([option_type_list['id']] + ([:remote] ? ["-r",[:remote]] : [])) return 0 rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |
#connect(opts) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/morpheus/cli/library_option_lists_command.rb', line 18 def connect(opts) @api_client = establish_remote_appliance_connection(opts) @library_instance_types_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).library_instance_types @provision_types_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).provision_types @option_types_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).option_types @option_type_lists_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).option_type_lists end |
#get(args) ⇒ Object
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 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 |
# File 'lib/morpheus/cli/library_option_lists_command.rb', line 88 def get(args) = {} optparse = Morpheus::Cli::OptionParser.new do|opts| opts. = subcommand_usage("[name]") (opts, , [:json, :dry_run, :remote]) opts. = "This outputs details about a particular Option List." end optparse.parse!(args) if args.count < 1 puts optparse exit 1 end connect() begin @option_type_lists_interface.setopts() if [:dry_run] if args[0].to_s =~ /\A\d{1,}\Z/ print_dry_run @option_type_lists_interface.dry.get(args[0].to_i) else print_dry_run @option_type_lists_interface.dry.list({name: args[0]}) end return end option_type_list = find_option_type_list_by_name_or_id(args[0]) exit 1 if option_type_list.nil? if [:json] print JSON.pretty_generate({optionTypeList: option_type_list}), "\n" return end print_h1 "Option List Details", print cyan if option_type_list['type'] == 'manual' print_description_list({ "ID" => 'id', "Name" => 'name', "Description" => 'description', "Type" => lambda {|it| it['type'].to_s.capitalize }, }, option_type_list) # print_h2 "Initial Dataset" # print bright_black,"#{option_type_list['initialDataset']}","\n",reset else option_list_columns = { "ID" => 'id', "Name" => 'name', "Description" => 'description', "Type" => lambda {|it| it['type'].to_s.capitalize }, "Source URL" => 'sourceUrl', "Real Time" => lambda {|it| format_boolean it['realTime'] }, "Ignore SSL Errors" => lambda {|it| format_boolean it['ignoreSSLErrors'] }, "Source Method" => lambda {|it| it['sourceMethod'].to_s.upcase } } source_headers = [] if option_type_list['config'] && option_type_list['config']['sourceHeaders'] source_headers = option_type_list['config']['sourceHeaders'].collect do |header| {name: header['name'], value: header['value'], masked: format_boolean(header['masked'])} end #option_list_columns["Source Headers"] = lambda {|it| source_headers.collect {|it| "#{it[:name]} #{it[:value]}"}.join("\n") } end print_description_list(option_list_columns, option_type_list) if source_headers && !source_headers.empty? print cyan print_h2 "Source Headers" print as_pretty_table(source_headers, [:name, :value, :masked], ) end if !option_type_list['initialDataset'].empty? print_h2 "Initial Dataset" print bright_black,"#{option_type_list['initialDataset']}","\n",reset end if !option_type_list['translationScript'].empty? print_h2 "Translation Script" print bright_black,"#{option_type_list['translationScript']}","\n",reset end end print_h2 "List Items" if option_type_list['listItems'] print as_pretty_table(option_type_list['listItems'], ['name', 'value'], ) else puts "No data" end print reset,"\n" rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |
#handle(args) ⇒ Object
26 27 28 |
# File 'lib/morpheus/cli/library_option_lists_command.rb', line 26 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/morpheus/cli/library_option_lists_command.rb', line 30 def list(args) = {} optparse = Morpheus::Cli::OptionParser.new do|opts| opts. = subcommand_usage() (opts, , [:list, :query, :dry_run, :json, :remote]) opts. = "This outputs a list of custom Option List records." end optparse.parse!(args) connect() begin params = {} params.merge!(()) @option_type_lists_interface.setopts() if [:dry_run] print_dry_run @option_type_lists_interface.dry.list(params) return end json_response = @option_type_lists_interface.list(params) if [:json] print JSON.pretty_generate(json_response), "\n" return end option_type_lists = json_response['optionTypeLists'] subtitles = [] subtitles += parse_list_subtitles() print_h1 "Morpheus Option Lists", subtitles, if option_type_lists.empty? print cyan,"No option lists found.",reset,"\n" else rows = option_type_lists.collect do |option_type_list| { id: option_type_list['id'], name: option_type_list['name'], description: option_type_list['description'], type: option_type_list['type'], size: option_type_list['listItems'] ? option_type_list['listItems'].size : '' } end end columns = [ :id, :name, :description, :type, :size ] print cyan print as_pretty_table(rows, columns, ) print reset,"\n" rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |
#remove(args) ⇒ Object
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/morpheus/cli/library_option_lists_command.rb', line 319 def remove(args) = {} optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage("[name]") (opts, , [:auto_confirm, :json, :dry_run, :remote]) end optparse.parse!(args) if args.count < 1 puts optparse exit 1 end connect() begin option_type_list = find_option_type_list_by_name_or_id(args[0]) exit 1 if option_type_list.nil? unless Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the option type #{option_type_list['name']}?", ) exit end @option_type_lists_interface.setopts() if [:dry_run] print_dry_run @option_type_lists_interface.dry.destroy(option_type_list['id']) return end json_response = @option_type_lists_interface.destroy(option_type_list['id']) if [:json] print JSON.pretty_generate(json_response), "\n" return end print_green_success "Removed Option List #{option_type_list['name']}" #list([]) rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |
#update(args) ⇒ Object
251 252 253 254 255 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 286 287 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 |
# File 'lib/morpheus/cli/library_option_lists_command.rb', line 251 def update(args) # JD: this is annoying because our option_types (for prompting and help) # are the same type of object being managed here.., options options options = {} optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage("[name] [options]") (opts, , update_option_type_list_option_types()) (opts, , [:options, :payload, :json, :dry_run, :remote]) end optparse.parse!(args) connect() begin option_type_list = find_option_type_list_by_name_or_id(args[0]) exit 1 if option_type_list.nil? = [:options].reject {|k,v| k.is_a?(Symbol) } payload = nil if [:payload] payload = [:payload] # support -O OPTION switch on top of --payload if !.empty? payload['optionTypeList'] ||= {} payload['optionTypeList'].deep_merge!() end else list_type = option_type_list['type'] = update_option_type_list_option_types(list_type) params = v_prompt = Morpheus::Cli::OptionTypes.no_prompt(, [:options], @api_client, [:params]) params.deep_merge!(v_prompt) if list_type == 'rest' # parse Source Headers source_headers = prompt_source_headers(.merge({no_prompt: true})) if !source_headers.empty? #params['config'] ||= option_type_list['config'] || {} params['config'] ||= {} params['config']['sourceHeaders'] = source_headers end end if params.empty? print_red_alert "Specify at least one option to update" puts optparse exit 1 end if params.key?('required') params['required'] = ['on','true'].include?(params['required'].to_s) end list_payload = params payload = {'optionTypeList' => list_payload} end @option_type_lists_interface.setopts() if [:dry_run] print_dry_run @option_type_lists_interface.dry.update(option_type_list['id'], payload) return end json_response = @option_type_lists_interface.update(option_type_list['id'], payload) if [:json] print JSON.pretty_generate(json_response), "\n" return end print_green_success "Updated Option List #{list_payload['name']}" get([option_type_list['id']] + ([:remote] ? ["-r",[:remote]] : [])) rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |