class Aeolus::Image::Warehouse::WarehouseModel

Attributes

bucket[RW]
bucket_name[RW]
warehouse[RW]
body[W]

Public Class Methods

all() click to toggle source
# File lib/aeolus_image/model/warehouse/warehouse_model.rb, line 101
def all
  bucket_objects.map do |wh_object|
      self.new(wh_object)
  end
end
bucket_objects() click to toggle source
# File lib/aeolus_image/model/warehouse/warehouse_model.rb, line 81
def bucket_objects
  self.set_warehouse_and_bucket if self.bucket.nil?

  begin
    self.bucket.objects
  rescue RestClient::ResourceNotFound
    []
  end
end
config() click to toggle source
# File lib/aeolus_image/model/warehouse/warehouse_model.rb, line 144
def config
  defined?(@@config) ? @@config : nil
end
config=(conf) click to toggle source
# File lib/aeolus_image/model/warehouse/warehouse_model.rb, line 148
def config=(conf)
  @@config = conf
end
create!(key, body, attributes) click to toggle source
# File lib/aeolus_image/model/warehouse/warehouse_model.rb, line 168
def create!(key, body, attributes)
  self.set_warehouse_and_bucket if self.bucket.nil?
  unless self.warehouse.buckets.include?(self.bucket.name)
    self.bucket = self.warehouse.create_bucket(self.bucket.name)
  end
  obj = self.bucket.create_object(key, body, attributes)
  self.new(obj)
end
delete(uuid) click to toggle source
# File lib/aeolus_image/model/warehouse/warehouse_model.rb, line 131
def delete(uuid)
  self.set_warehouse_and_bucket if self.bucket.nil?
  begin
    if self.bucket.include?(uuid)
      self.bucket.object(uuid).delete!
    else
      false
    end
  rescue RestClient::ResourceNotFound
    false
  end
end
find(uuid) click to toggle source
# File lib/aeolus_image/model/warehouse/warehouse_model.rb, line 107
def find(uuid)
  self.set_warehouse_and_bucket if self.bucket.nil?
  begin
    if self.bucket.include?(uuid)
      self.new(self.bucket.object(uuid))
    else
      nil
    end
  rescue RestClient::ResourceNotFound
    nil
  end
end
first() click to toggle source
# File lib/aeolus_image/model/warehouse/warehouse_model.rb, line 91
def first
  obj = bucket_objects.first
  obj ? self.new(obj) : nil
end
iwhd_url() click to toggle source
# File lib/aeolus_image/model/warehouse/warehouse_model.rb, line 164
def iwhd_url
  config[:iwhd][:url]
end
last() click to toggle source
# File lib/aeolus_image/model/warehouse/warehouse_model.rb, line 96
def last
  obj = bucket_objects.last
  obj ? self.new(obj) : nil
end
new(obj) click to toggle source
# File lib/aeolus_image/model/warehouse/warehouse_model.rb, line 24
def initialize(obj)
  @obj = obj
  @attrs = obj.attrs(obj.attr_list)
  @attrs.each do |k,v|
    self.class.send(:attr_writer, k.to_sym) unless respond_to?(:"#{k}=")
    self.class.send(:attr_reader, k.to_sym) unless respond_to?(k.to_sym)
    send(:"#{k}=", v)
  end
end
oauth_consumer_key() click to toggle source
# File lib/aeolus_image/model/warehouse/warehouse_model.rb, line 156
def oauth_consumer_key
  config[:iwhd][:oauth][:consumer_key] rescue nil
end
oauth_consumer_secret() click to toggle source
# File lib/aeolus_image/model/warehouse/warehouse_model.rb, line 160
def oauth_consumer_secret
  config[:iwhd][:oauth][:consumer_secret] rescue nil
end
set_warehouse_and_bucket() click to toggle source
# File lib/aeolus_image/model/warehouse/warehouse_model.rb, line 71
def set_warehouse_and_bucket
  begin
    @@config ||= load_config
    self.warehouse = Warehouse::Client.new(@@config[:iwhd][:url])
    self.bucket = self.warehouse.bucket(@bucket_name)
  rescue
    raise BucketNotFound
  end
end
use_oauth?() click to toggle source
# File lib/aeolus_image/model/warehouse/warehouse_model.rb, line 152
def use_oauth?
  !!oauth_consumer_key && !!oauth_consumer_secret
end
where(query_string) click to toggle source
# File lib/aeolus_image/model/warehouse/warehouse_model.rb, line 120
def where(query_string)
  begin
    self.set_warehouse_and_bucket if self.bucket.nil?
    self.warehouse.query(@bucket_name, query_string).xpath('/objects/object').map do |obj|
      self.new(self.bucket.object(obj.at_xpath('./key/text()').to_s))
    end
  rescue RestClient::ResourceNotFound
    []
  end
end

Protected Class Methods

is_file?(path) click to toggle source
# File lib/aeolus_image/model/warehouse/warehouse_model.rb, line 223
def is_file?(path)
  full_path = File.expand_path(path)
  if File.exist?(full_path) && !File.directory?(full_path)
    return true
  end
  false
end
load_config() click to toggle source

Copy over entirely too much code to load the config file

# File lib/aeolus_image/model/warehouse/warehouse_model.rb, line 180
def load_config
  # TODO - Is this always the case? We should probably have /etc/aeolus-cli or something too?
  # Or allow Rails to override this
  @config_location ||= "~/.aeolus-cli"
  begin
    file_str = read_file(@config_location)
    if is_file?(@config_location) && !file_str.include?(":url")
      lines = File.readlines(File.expand_path(@config_location)).map do |line|
        "#" + line
      end
      File.open(File.expand_path(@config_location), 'w') do |file|
        file.puts lines
      end
      write_file
    end
    write_file unless is_file?(@config_location)
    YAML::load(File.open(File.expand_path(@config_location)))
  rescue Errno::ENOENT
    #TODO: Create a custom exception to wrap CLI Exceptions
    raise "Unable to locate or write configuration file: \"" + @config_location + "\""
  end
end
read_file(path) click to toggle source
# File lib/aeolus_image/model/warehouse/warehouse_model.rb, line 210
def read_file(path)
  begin
    full_path = File.expand_path(path)
    if is_file?(path)
      File.read(full_path)
    else
      return nil
    end
  rescue
    nil
  end
end
write_file() click to toggle source
# File lib/aeolus_image/model/warehouse/warehouse_model.rb, line 203
def write_file
  example = File.read(File.expand_path(File.dirname(__FILE__) + "/../../examples/aeolus-cli"))
  File.open(File.expand_path(@config_location), 'a+') do |f|
    f.write(example)
  end
end

Public Instance Methods

==(other_obj) click to toggle source
# File lib/aeolus_image/model/warehouse/warehouse_model.rb, line 38
def ==(other_obj)
  # If the objects have different instance variables defined, they're definitely not ==
  return false unless instance_variables.sort == other_obj.instance_variables.sort
  # Otherwise, ensure that they're all the same
  instance_variables.each do |iv|
    next if iv == "@obj" || iv == :@obj
    return false unless other_obj.instance_variable_get(iv) == instance_variable_get(iv)
    return false unless other_obj.body == body
  end
  # They have the same instance variables and values, so they're equal
  true
end
body() click to toggle source
# File lib/aeolus_image/model/warehouse/warehouse_model.rb, line 34
def body
  @obj.body
end
bucket_object() click to toggle source

Returns the bucket object represending this object

# File lib/aeolus_image/model/warehouse/warehouse_model.rb, line 56
def bucket_object
  @obj
end
id() click to toggle source
# File lib/aeolus_image/model/warehouse/warehouse_model.rb, line 51
def id
  uuid
end
set_attr(key, value) click to toggle source

Set (and immediately update) an attribute on the object TODO: It might be nicer to offer a .save! that iterates over each attribute and calls this, to better match ActiveResource

# File lib/aeolus_image/model/warehouse/warehouse_model.rb, line 63
def set_attr(key, value)
  bucket_object.set_attr(key, value)
end