Module: RightScale::Api::BaseExtend

Instance Method Summary collapse

Methods included from BaseConnection

#connection

Instance Method Details

#[](*args) ⇒ Object



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
# File 'lib/rest_connection/rightscale/rightscale_api_base.rb', line 269

def [](*args)
  ret = []
  args.each { |arg|
    temp = []
    begin
      if arg.is_a?(Hash)
        if arg.keys.first.to_s == "cloud_id"
          temp << find_by_cloud_id(arg.values.first.to_i)
        else
          temp << find_with_filter(arg)
        end
      elsif arg.is_a?(Regexp)
        temp << find_by(:nickname) { |n| n =~ arg }
      else
        temp << find(arg)
      end
    rescue
    end
    temp.flatten!
    if temp.empty?
      all = find_all
      if arg.is_a?(Hash)
        temp << all.select { |v| v.__send__(arg.keys.first.to_sym) =~ /#{arg.values.first}/ }
      elsif arg.is_a?(Regexp)
        temp += all.select { |n| n.name =~ arg }
        temp += all.select { |n| n.nickname =~ arg } if temp.empty?
      else
        temp += all.select { |n| n.name =~ /#{arg}/ }
        temp += all.select { |n| n.nickname =~ /#{arg}/ } if temp.empty?
      end
    end
    ret += temp
  }
  return (args.empty? ? find_all : ret.flatten.uniq)
end

#create(opts) ⇒ Object



237
238
239
240
241
242
# File 'lib/rest_connection/rightscale/rightscale_api_base.rb', line 237

def create(opts)
  location = connection.post(self.resource_plural_name, self.resource_singular_name.to_sym => opts)
  newrecord = self.new('href' => location)
  newrecord.reload
  newrecord
end

#deny_methods(*symbols) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/rest_connection/rightscale/rightscale_api_base.rb', line 305

def deny_methods(*symbols)
  symbols.map! { |sym| sym.to_sym }
  if symbols.delete(:index)
    symbols |= [:find_all, :find_by, :find_by_cloud_id, :find_by_nickname, :find_by_nickname_speed, :find_with_filter]
  end
  if symbols.delete(:show)
    symbols |= [:show, :reload, :find, :find_by_id]
  end
  if symbols.delete(:update)
    symbols |= [:save, :update]
  end
  symbols.each do |sym|
    sym = sym.to_sym
    eval_str = "undef #{sym.inspect}"
    if self.respond_to?(sym)
      instance_eval(eval_str)
    elsif self.new.respond_to?(sym)
      class_eval(eval_str)
    end
  end
end

#filtersObject



265
266
267
# File 'lib/rest_connection/rightscale/rightscale_api_base.rb', line 265

def filters()
  []
end

#find(href, additional_params = {}, &block) ⇒ Object

Retrieves one or more resources of the same type.

Parameters:

  • href (Integer|Symbol|String)

    should be one of the following: resource id, :all, :first, :last, resource href

  • additional_params (Hash) (defaults to: {})

    if href is an integer, will be part of retrieve request

  • block (Block)

    if href is a symbol, will be used inside select block to refine results



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/rest_connection/rightscale/rightscale_api_base.rb', line 211

def find(href, additional_params={}, &block)
  if href.is_a?(Integer)
    return self.new(connection.get(self.resource_plural_name + "/#{href}", additional_params))
  elsif href.is_a?(Symbol)
    results = self.find_all
    if block_given?
      results = results.select { |s| yield(s) }
    end
    if href == :all
      return results
    elsif href == :first
      return results.first
    elsif href == :last
      return results.last
    end
  elsif uri = URI.parse(href)
    return self.new(connection.get(uri.path))
  end
  nil
end

#find_allObject



185
186
187
188
189
190
191
# File 'lib/rest_connection/rightscale/rightscale_api_base.rb', line 185

def find_all
  a = Array.new
  connection.get(self.resource_plural_name).each do |object|
    a << self.new(object)
  end
  return a
end

#find_by(attrib, &block) ⇒ Object

matches using result of block match expression ex: Server.find_by(:nickname) { |n| n =~ /production/ }



175
176
177
178
179
180
181
182
183
# File 'lib/rest_connection/rightscale/rightscale_api_base.rb', line 175

def find_by(attrib, &block)
  attrib = attrib.to_sym
  if self.filters.include?(attrib)
    connection.logger("#{self} includes the filter '#{attrib}', you might be able to speed up this API call")
  end
  self.find_all.select do |s|
    yield(s[attrib.to_s])
  end
end

#find_by_cloud_id(cloud_id) ⇒ Object



193
194
195
196
197
198
199
# File 'lib/rest_connection/rightscale/rightscale_api_base.rb', line 193

def find_by_cloud_id(cloud_id)
  a = Array.new
  connection.get(self.resource_plural_name, "cloud_id" => cloud_id).each do |object|
    a << self.new(object)
  end
  return a
end

#find_by_id(id) ⇒ Object



232
233
234
235
# File 'lib/rest_connection/rightscale/rightscale_api_base.rb', line 232

def find_by_id(id)
  connection.logger("DEPRECATION WARNING: use of find_by_id is deprecated, please use find(id) ")
  self.find(id)
end

#find_by_nickname(nickname) ⇒ Object



201
202
203
204
# File 'lib/rest_connection/rightscale/rightscale_api_base.rb', line 201

def find_by_nickname(nickname)
  connection.logger("DEPRECATION WARNING: use of find_by_nickname is deprecated, please use find_by(:nickname) { |n| n == '#{nickname}' } ")
  self.find_by(:nickname) { |n| n == nickname }
end

#find_by_nickname_speed(nickname) ⇒ Object

filter is only implemented on some api endpoints



245
246
247
# File 'lib/rest_connection/rightscale/rightscale_api_base.rb', line 245

def find_by_nickname_speed(nickname)
  self.find_with_filter('nickname' => nickname)
end

#find_with_filter(filter = {}) ⇒ Object

filter is only implemented on some api endpoints



250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/rest_connection/rightscale/rightscale_api_base.rb', line 250

def find_with_filter(filter = {})
  filter_params = []
  filter.each { |key,val|
    unless self.filters.include?(key.to_sym)
      raise ArgumentError.new("#{key} is not a valid filter for resource #{self.resource_singular_name}")
    end
    filter_params << "#{key}=#{val}"
  }
  a = Array.new
  connection.get(self.resource_plural_name, :filter => filter_params).each do |object|
    a << self.new(object)
  end
  return a
end

#resource_plural_nameObject



165
166
167
# File 'lib/rest_connection/rightscale/rightscale_api_base.rb', line 165

def resource_plural_name
  self.to_s.underscore.pluralize
end

#resource_singular_nameObject



169
170
171
# File 'lib/rest_connection/rightscale/rightscale_api_base.rb', line 169

def resource_singular_name
  self.to_s.underscore
end