Class: Bimbly
- Inherits:
-
Object
- Object
- Bimbly
- Defined in:
- lib/bimbly.rb
Constant Summary collapse
- MANDO_EXCEPT =
{ create_volumes: ['size'] }
Instance Attribute Summary collapse
-
#array ⇒ Object
Returns the value of attribute array.
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#cert ⇒ Object
Returns the value of attribute cert.
-
#data_type ⇒ Object
readonly
Returns the value of attribute data_type.
-
#doc_pointer ⇒ Object
Returns the value of attribute doc_pointer.
-
#error_codes ⇒ Object
readonly
Returns the value of attribute error_codes.
-
#error_names ⇒ Object
readonly
Returns the value of attribute error_names.
-
#file ⇒ Object
Returns the value of attribute file.
-
#file_select ⇒ Object
Returns the value of attribute file_select.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#mando_array ⇒ Object
Returns the value of attribute mando_array.
-
#menu ⇒ Object
Returns the value of attribute menu.
-
#meth_name ⇒ Object
Returns the value of attribute meth_name.
-
#obj_sets ⇒ Object
readonly
Returns the value of attribute obj_sets.
-
#param_pointer ⇒ Object
Returns the value of attribute param_pointer.
-
#password ⇒ Object
Returns the value of attribute password.
-
#pointer ⇒ Object
Returns the value of attribute pointer.
-
#port ⇒ Object
Returns the value of attribute port.
-
#req_pointer ⇒ Object
Returns the value of attribute req_pointer.
-
#saved_array ⇒ Object
Returns the value of attribute saved_array.
-
#user ⇒ Object
Returns the value of attribute user.
-
#verb ⇒ Object
Returns the value of attribute verb.
Instance Method Summary collapse
- #available_methods ⇒ Object
- #build_params(hash) ⇒ Object
- #call(opts = {}) ⇒ Object
- #create_playbook(file) ⇒ Object
- #create_template(file) ⇒ Object
- #data_types(type = nil) ⇒ Object
- #details ⇒ Object
- #doc ⇒ Object
- #error_format(messages) ⇒ Object
- #extract_params ⇒ Object
- #gen_method_hash ⇒ Object
- #gen_methods ⇒ Object
- #gen_uri(opts = {}) ⇒ Object
-
#initialize(opts = {}) ⇒ Bimbly
constructor
A new instance of Bimbly.
- #inspect ⇒ Object
- #new_connection(opts = {}) ⇒ Object
- #parameters ⇒ Object
- #request ⇒ Object
- #required ⇒ Object
- #reset ⇒ Object
- #review ⇒ Object
- #save ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Bimbly
Returns a new instance of Bimbly.
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 |
# File 'lib/bimbly.rb', line 15 def initialize(opts = {}) # Read in setup files if opts[:version] == "3" opts.delete(:version) @error_codes = YAML.load(File.read("#{File.dirname(__FILE__)}/error_codes_v3.yml")) @obj_sets = YAML.load(File.read("#{File.dirname(__FILE__)}/object_sets_v3.yml")) @data_type = YAML.load(File.read("#{File.dirname(__FILE__)}/data_types_v3.yml")) else @error_codes = YAML.load(File.read("#{File.dirname(__FILE__)}/error_codes_v4.yml")) @obj_sets = YAML.load(File.read("#{File.dirname(__FILE__)}/object_sets_v4.yml")) @data_type = YAML.load(File.read("#{File.dirname(__FILE__)}/data_types_v4.yml")) end @meth_name = nil @saved_array = [] @base_url = "NotConnected" @menu = [] @param_pointer = @obj_sets @doc_pointer = @obj_sets @req_pointer = @obj_sets gen_methods new_connection(opts) end |
Instance Attribute Details
#array ⇒ Object
Returns the value of attribute array.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def array @array end |
#base_url ⇒ Object
Returns the value of attribute base_url.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def base_url @base_url end |
#cert ⇒ Object
Returns the value of attribute cert.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def cert @cert end |
#data_type ⇒ Object (readonly)
Returns the value of attribute data_type.
8 9 10 |
# File 'lib/bimbly.rb', line 8 def data_type @data_type end |
#doc_pointer ⇒ Object
Returns the value of attribute doc_pointer.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def doc_pointer @doc_pointer end |
#error_codes ⇒ Object (readonly)
Returns the value of attribute error_codes.
8 9 10 |
# File 'lib/bimbly.rb', line 8 def error_codes @error_codes end |
#error_names ⇒ Object (readonly)
Returns the value of attribute error_names.
8 9 10 |
# File 'lib/bimbly.rb', line 8 def error_names @error_names end |
#file ⇒ Object
Returns the value of attribute file.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def file @file end |
#file_select ⇒ Object
Returns the value of attribute file_select.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def file_select @file_select end |
#headers ⇒ Object
Returns the value of attribute headers.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def headers @headers end |
#mando_array ⇒ Object
Returns the value of attribute mando_array.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def mando_array @mando_array end |
#menu ⇒ Object
Returns the value of attribute menu.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def @menu end |
#meth_name ⇒ Object
Returns the value of attribute meth_name.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def meth_name @meth_name end |
#obj_sets ⇒ Object (readonly)
Returns the value of attribute obj_sets.
8 9 10 |
# File 'lib/bimbly.rb', line 8 def obj_sets @obj_sets end |
#param_pointer ⇒ Object
Returns the value of attribute param_pointer.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def param_pointer @param_pointer end |
#password ⇒ Object
Returns the value of attribute password.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def password @password end |
#pointer ⇒ Object
Returns the value of attribute pointer.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def pointer @pointer end |
#port ⇒ Object
Returns the value of attribute port.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def port @port end |
#req_pointer ⇒ Object
Returns the value of attribute req_pointer.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def req_pointer @req_pointer end |
#saved_array ⇒ Object
Returns the value of attribute saved_array.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def saved_array @saved_array end |
#user ⇒ Object
Returns the value of attribute user.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def user @user end |
#verb ⇒ Object
Returns the value of attribute verb.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def verb @verb end |
Instance Method Details
#available_methods ⇒ Object
198 199 200 |
# File 'lib/bimbly.rb', line 198 def available_methods self.methods - Object.methods end |
#build_params(hash) ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/bimbly.rb', line 288 def build_params(hash) raise ArgumentError, "Please provide a valid hash for parameters" unless hash.instance_of? Hash and hash != {} url_params = "?" size_count = 0 hash.each { |key, value| url_params = "#{url_params}#{key}=#{value}" size_count += 1 url_params = "#{url_params}&" unless size_count == hash.size } url_params end |
#call(opts = {}) ⇒ Object
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 |
# File 'lib/bimbly.rb', line 45 def call(opts = {}) verb = @verb payload = @payload uri = @uri reset raise StandardError, "Instantiate a connection to an array" if @array.nil? raise StandardError, "Method to be used has not been loaded" if verb.nil? # Check if url is valid raise ArgumentError, "Invalid URL: #{uri}" unless uri =~ /\A#{URI::regexp}\z/ # Do some payload stuff to wrap with 'data' and make sure it's usable if not payload.nil? # payload = payload["data"] if payload.keys.size == 1 and payload.keys.include?("data") payload = { data: payload } end payload = payload.to_json if payload.class == Hash begin response = RestClient::Request.execute( method: verb.to_sym, url: uri, ssl_ca_file: @cert, headers: @headers, payload: payload ) rescue RestClient::ExceptionWithResponse => e puts "Response Code: #{e.response.code}" puts "Response Headers:" pp e.response.headers puts "Response Body:" pp error_format(e.response.body) puts "Response Object:" puts e.response.request.inspect end begin JSON.parse(response.body) unless response.nil? || response.body == '' rescue JSON::ParserError => e puts e end end |
#create_playbook(file) ⇒ Object
263 264 265 266 267 268 269 |
# File 'lib/bimbly.rb', line 263 def create_playbook(file) #Method to output saved calls to a yaml formated playbook raise ArgumentError, "Supply #create_playbook with a file name" if file.nil? File.write(file, @saved_array.to_yaml) return nil end |
#create_template(file) ⇒ Object
271 272 273 274 |
# File 'lib/bimbly.rb', line 271 def create_template(file) #Method to take saved calls, scrub data, and create a yml template for them raise ArgumentError, "Supply #create_template with a file name" if file.nil? end |
#data_types(type = nil) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/bimbly.rb', line 202 def data_types(type = nil) if type return @data_type[type] elsif @param_pointer.nil? return @data_type else @param_pointer.each { |key,value| puts "[#{key}]" @data_type[value].each { |key, value| puts "#{key}: #{value}" } puts "" } end return nil end |
#details ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/bimbly.rb', line 169 def details puts "Method Selected: #{@meth_name}" puts "URI: #{@uri}" puts "Verb: #{@verb}" if not @payload.nil? puts "Payload:" pp @payload else puts "Payload: n/a" end return end |
#doc ⇒ Object
182 183 184 |
# File 'lib/bimbly.rb', line 182 def doc puts @doc_pointer.to_yaml end |
#error_format(messages) ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/bimbly.rb', line 92 def error_format() = [] JSON.parse()["messages"].each { || .merge!(@error_codes[["code"]]) .delete("Error_Desc") << } end |
#extract_params ⇒ Object
276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/bimbly.rb', line 276 def extract_params params_hash = {} params = @uri.split("?")[1] params_array = params.split("&") params_array.each do |param| key, value = param.split("=") params_hash[key] = value end params_hash end |
#gen_method_hash ⇒ Object
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/bimbly.rb', line 306 def gen_method_hash method_hash = {} name = "" @obj_sets.each { |obj_key, obj_value| obj_value.each { |op_key, op_value| method_suffix = "" op_value.each { |key, value| next if not key.match(/DELETE|GET|POST|PUT/) if key.match(/id/) method_suffix = "_by_id" elsif key.match(/detail/) method_suffix = "_detailed" end verb, url_suffix = key.split(' ') hash = {} hash[:verb] = verb.downcase.to_sym hash[:url_suffix] = url_suffix hash[:avail_params] = value hash[:request] = @obj_sets[obj_key][op_key]["Request"] hash[:object] = obj_key hash[:op] = op_key name = "#{op_key}_#{obj_key}#{method_suffix}" method_hash[name.to_sym] = hash @menu.push name } } } method_hash end |
#gen_methods ⇒ Object
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/bimbly.rb', line 337 def gen_methods method_hash = gen_method_hash method_hash.each { |method_name, hash| define_singleton_method(method_name) { |opts = {}| @param_pointer = hash[:avail_params] @doc_pointer = @obj_sets[hash[:object]][hash[:op]] @req_pointer = @obj_sets[hash[:object]][hash[:op]]["Request"] @meth_name = method_name raise ArgumentError, "Please provide id" if method_name.match(/id/) and opts[:id].nil? url_suffix = hash[:url_suffix] url_suffix = url_suffix.gsub(/\/id/, "/#{opts[:id]}") if method_name.match(/id/) uri = gen_uri(url_suffix: url_suffix, params: opts[:params]) payload = opts[:payload] # Unwrap the payload data if it is json or wrapped in 'data' so we can inspect the attrs if not payload.nil? payload = JSON.parse(payload) if payload.class == String payload = payload["data"] if payload.keys.include?("data") payload = Hash[payload.map{|(k,v)| [k.to_s,v]}] end @uri = uri @verb = hash[:verb] @payload = payload if not opts[:params].nil? opts[:params].each { |key, value| raise ArgumentError, "Invalid parameter for #{method_name}: #{key}" unless hash[:avail_params].include?(key) } end # Maybe pull this out into its own method # Check for mandatory payload info @mando_array = [] if not hash[:request].nil? hash[:request].each { |key, value| @mando_array << key if value['mandatory'] == 'true' @mando_array << key if not MANDO_EXCEPT[method_name].nil? and MANDO_EXCEPT[method_name].include? key } end raise ArgumentError, "Must supply :payload with attributes #{@mando_array} on #{method_name}" if not @mando_array.empty? and payload.nil? @mando_array.each { |ele| raise ArgumentError, "\'#{ele}\' is a mandatory attribute in the payload for #{method_name}: Please supply these attributes #{@mando_array}" unless payload.keys.to_s.include?(ele) } if not payload.nil? payload.keys.each { |key| raise ArgumentError, "The method #{method_name} does not utilize :payload" if hash[:request].nil? raise ArgumentError, "The attribute \'#{key}\' is not an available attribute for #{method_name}" unless hash[:request].keys.include?(key.to_s) } end self } } end |
#gen_uri(opts = {}) ⇒ Object
301 302 303 304 |
# File 'lib/bimbly.rb', line 301 def gen_uri(opts = {}) url_params = build_params(opts[:params]) if opts[:params] uri = "#{@base_url}/#{opts[:url_suffix]}#{url_params}" end |
#inspect ⇒ Object
41 42 43 |
# File 'lib/bimbly.rb', line 41 def inspect "#<Bimbly:#{object_id}>" end |
#new_connection(opts = {}) ⇒ Object
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 |
# File 'lib/bimbly.rb', line 102 def new_connection(opts = {}) @file = opts[:file] @file_select = opts[:file_select] @array = opts[:array] @cert = opts[:cert] @port = opts[:port] @user = opts[:user] @password = opts[:password] return if opts.empty? if @file conn_data = YAML.load(File.read(File.(@file))) conn_data = conn_data[@file_select] if @file_select @array = conn_data["array"] @cert = conn_data["cert"] @user = conn_data["user"] @password = conn_data["password"] end @port = "5392" if @port.nil? raise ArgumentError, "You must provide an array" if @array.nil? raise ArgumentError, "You must provide a CA cert" if @cert.nil? raise ArgumentError, "You must provide a user" if @user.nil? raise ArgumentError, "You must provide a password" if @password.nil? @base_url = "https://#{array}:#{port}" @uri = "#{@base_url}/v1/tokens" # Get initial connection credentials creds = { data: { username: @user, password: @password } } begin response = RestClient::Request.execute( method: :post, url: @uri, payload: creds.to_json, ssl_ca_file: @cert, ssl_ciphers: 'AESGCM:!aNULL' ) rescue RestClient::ExceptionWithResponse => e puts "Response Code: #{e.response.code}" puts "Response Headers: #{e.response.headers}" puts "Response Body: #{e.response.body}" puts "Response Object: #{e.response.request.inspect}" end token = JSON.parse(response)["data"]["session_token"] @headers = { 'X-Auth-Token' => token } end |
#parameters ⇒ Object
186 187 188 |
# File 'lib/bimbly.rb', line 186 def parameters @param_pointer end |
#request ⇒ Object
194 195 196 |
# File 'lib/bimbly.rb', line 194 def request puts @req_pointer.to_yaml end |
#required ⇒ Object
190 191 192 |
# File 'lib/bimbly.rb', line 190 def required @mando_array end |
#reset ⇒ Object
158 159 160 161 162 163 164 165 166 167 |
# File 'lib/bimbly.rb', line 158 def reset @verb = nil @payload = nil @uri = nil @meth_name = nil @doc_pointer = @obj_sets @param_pointer = @obj_sets return nil end |
#review ⇒ Object
254 255 256 257 258 259 260 261 |
# File 'lib/bimbly.rb', line 254 def review #Method to review calls that are saved @saved_array.each do |ele| pp ele end return nil end |
#save ⇒ Object
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 250 251 252 |
# File 'lib/bimbly.rb', line 220 def save #Method to save call to array method_array = @meth_name.to_s.split("_") operation = method_array.shift if method_array.last == "id" method_array.pop(2) elsif method_array.last == "detailed" method_array.pop end object = method_array.join("_") operations_hash = {} if @meth_name.match(/_by_id/) id = @uri.match(/#{object}\/(.*)/)[1] id = id.split("/")[0] operations_hash['id'] = id unless id.nil? end operations_hash['params'] = extract_params if @uri.match(/\?/) operations_hash['request'] = @payload unless @payload.nil? if operations_hash.empty? @saved_array << { operation => [ object ] } else @saved_array << { operation => [ object => operations_hash ] } end reset end |