Class: Aeolus::Image::Warehouse::WarehouseModel

Inherits:
Object
  • Object
show all
Defined in:
lib/aeolus_image/model/warehouse/warehouse_model.rb

Direct Known Subclasses

Icicle, Image, ImageBuild, ProviderImage, TargetImage, Template

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ WarehouseModel

Returns a new instance of WarehouseModel.



24
25
26
27
28
29
30
31
32
# 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

Class Attribute Details

.bucketObject

Returns the value of attribute bucket.



69
70
71
# File 'lib/aeolus_image/model/warehouse/warehouse_model.rb', line 69

def bucket
  @bucket
end

.bucket_nameObject

Returns the value of attribute bucket_name.



69
70
71
# File 'lib/aeolus_image/model/warehouse/warehouse_model.rb', line 69

def bucket_name
  @bucket_name
end

.warehouseObject

Returns the value of attribute warehouse.



69
70
71
# File 'lib/aeolus_image/model/warehouse/warehouse_model.rb', line 69

def warehouse
  @warehouse
end

Instance Attribute Details

#bodyObject



34
35
36
# File 'lib/aeolus_image/model/warehouse/warehouse_model.rb', line 34

def body
  @obj.body
end

Class Method Details

.allObject



101
102
103
104
105
# 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_objectsObject



81
82
83
84
85
86
87
88
89
# 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

.configObject



144
145
146
# File 'lib/aeolus_image/model/warehouse/warehouse_model.rb', line 144

def config
  defined?(@@config) ? @@config : nil
end

.config=(conf) ⇒ Object



148
149
150
# File 'lib/aeolus_image/model/warehouse/warehouse_model.rb', line 148

def config=(conf)
  @@config = conf
end

.create!(key, body, attributes) ⇒ Object



168
169
170
171
172
173
174
175
# 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) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
# 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) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
# 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

.firstObject



91
92
93
94
# File 'lib/aeolus_image/model/warehouse/warehouse_model.rb', line 91

def first
  obj = bucket_objects.first
  obj ? self.new(obj) : nil
end

.iwhd_urlObject



164
165
166
# File 'lib/aeolus_image/model/warehouse/warehouse_model.rb', line 164

def iwhd_url
  config[:iwhd][:url]
end

.lastObject



96
97
98
99
# File 'lib/aeolus_image/model/warehouse/warehouse_model.rb', line 96

def last
  obj = bucket_objects.last
  obj ? self.new(obj) : nil
end

.oauth_consumer_keyObject



156
157
158
# 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_secretObject



160
161
162
# 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_bucketObject



71
72
73
74
75
76
77
78
79
# 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?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/aeolus_image/model/warehouse/warehouse_model.rb', line 152

def use_oauth?
  !!oauth_consumer_key && !!oauth_consumer_secret
end

.where(query_string) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# 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

Instance Method Details

#==(other_obj) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# 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

#bucket_objectObject

Returns the bucket object represending this object



56
57
58
# File 'lib/aeolus_image/model/warehouse/warehouse_model.rb', line 56

def bucket_object
  @obj
end

#idObject



51
52
53
# File 'lib/aeolus_image/model/warehouse/warehouse_model.rb', line 51

def id
  uuid
end

#set_attr(key, value) ⇒ Object

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



63
64
65
# File 'lib/aeolus_image/model/warehouse/warehouse_model.rb', line 63

def set_attr(key, value)
  bucket_object.set_attr(key, value)
end