class CloudstackCli::Base
Attributes
config[R]
Public Class Methods
exit_on_failure?()
click to toggle source
exit with return code 1 in case of a error
# File lib/cloudstack-cli/base.rb 36 def self.exit_on_failure? 37 true 38 end
start(given_args=ARGV, config={})
click to toggle source
rescue error globally
Calls superclass method
# File lib/cloudstack-cli/base.rb 16 def self.start(given_args=ARGV, config={}) 17 super 18 rescue => e 19 error_class = e.class.name.split('::') 20 if error_class.size == 2 && error_class.first == "CloudstackClient" 21 puts "\e[31mERROR\e[0m: #{error_class.last} - #{e.message}" 22 puts e.backtrace if ARGV.include? "--debug" 23 else 24 raise 25 end 26 end
Public Instance Methods
add_filters_to_options(command)
click to toggle source
# File lib/cloudstack-cli/base.rb 85 def add_filters_to_options(command) 86 options[:filter].each do |filter_key, filter_value| 87 if client.api.params(command).find {|param| param["name"] == filter_key.downcase } 88 options[filter_key.downcase] = filter_value.gsub(/[^\w\s\.-]/, '') 89 options[:filter].delete(filter_key) 90 end 91 end 92 end
client()
click to toggle source
# File lib/cloudstack-cli/base.rb 41 def client 42 @config ||= load_configuration 43 @client ||= CloudstackClient::Client.new( 44 @config[:url], 45 @config[:api_key], 46 @config[:secret_key] 47 ) 48 @client.debug = true if options[:debug] 49 @client 50 end
filter_by(objects, key, value)
click to toggle source
# File lib/cloudstack-cli/base.rb 60 def filter_by(objects, key, value) 61 if objects.size == 0 62 return objects 63 elsif !(keys = objects.map{|i| i.keys}.flatten.uniq).include?(key) 64 say "WARNING: Filter invalid, no key \"#{key}\" found.", :yellow 65 say("DEBUG: Supported keys are, #{keys.join(', ')}.", :magenta) if options[:debug] 66 return objects 67 end 68 objects.select do |object| 69 object[key.to_s].to_s =~ /#{value}/i 70 end 71 rescue RegexpError => e 72 say "ERROR: ", :red 73 say "Invalid regular expression in filter - #{e.message}" 74 exit 1 75 end
filter_objects(objects, filter = options[:filter])
click to toggle source
# File lib/cloudstack-cli/base.rb 77 def filter_objects(objects, filter = options[:filter]) 78 filter.each do |key, value| 79 objects = filter_by(objects, key, value) 80 return objects if objects.size == 0 81 end 82 objects 83 end
load_configuration()
click to toggle source
# File lib/cloudstack-cli/base.rb 52 def load_configuration 53 CloudstackClient::Configuration.load(options) 54 rescue CloudstackClient::ConfigurationError => e 55 say "Error: ", :red 56 say e.message 57 exit 1 58 end
parse_file(file, extensions = %w(.json .yaml .yml))
click to toggle source
# File lib/cloudstack-cli/base.rb 94 def parse_file(file, extensions = %w(.json .yaml .yml)) 95 handler = case File.extname(file) 96 when ".json" 97 Object.const_get "JSON" 98 when ".yaml", ".yml" 99 Object.const_get "YAML" 100 else 101 say "ERROR: ", :red 102 say "File extension #{File.extname(file)} not supported. Supported extensions are #{extensions.join(', ')}" 103 exit 104 end 105 begin 106 return handler.load open(file){|f| f.read} 107 rescue SystemCallError 108 say "ERROR: ", :red 109 say "Can't find the file '#{file}'." 110 exit 1 111 rescue => e 112 say "ERROR: ", :red 113 say "Can't parse file '#{file}': #{e.message}" 114 exit 1 115 end 116 end