class Fog::OpenStack::Image::V2::Image

Public Instance Methods

add_member(member_id) click to toggle source
# File lib/fog/openstack/image/v2/models/image.rb, line 130
def add_member(member_id)
  requires :id
  service.add_member_to_image(id, member_id)
end
add_tag(tag) click to toggle source
# File lib/fog/openstack/image/v2/models/image.rb, line 160
def add_tag(tag)
  requires :id
  service.add_tag_to_image(id, tag)
end
add_tags(tags) click to toggle source
# File lib/fog/openstack/image/v2/models/image.rb, line 155
def add_tags(tags)
  requires :id
  tags.each { |tag| add_tag tag }
end
create() click to toggle source
# File lib/fog/openstack/image/v2/models/image.rb, line 68
def create
  requires :name
  merge_attributes(service.create_image(attributes).body)
  self
end
deactivate() click to toggle source
# File lib/fog/openstack/image/v2/models/image.rb, line 125
def deactivate
  requires :id
  service.deactivate_image(id)
end
destroy() click to toggle source
# File lib/fog/openstack/image/v2/models/image.rb, line 100
def destroy
  requires :id
  service.delete_image(id)
  true
end
download_data(params = {}) click to toggle source
# File lib/fog/openstack/image/v2/models/image.rb, line 115
def download_data(params = {})
  requires :id
  service.download_image(id, params[:content_range], params)
end
member(member_id) click to toggle source
# File lib/fog/openstack/image/v2/models/image.rb, line 150
def member(member_id)
  requires :id
  service.get_member_details(id, member_id)
end
members() click to toggle source
# File lib/fog/openstack/image/v2/models/image.rb, line 145
def members
  requires :id
  service.get_image_members(id).body['members']
end
method_missing(method_sym, *arguments, &block) click to toggle source
Calls superclass method
# File lib/fog/openstack/image/v2/models/image.rb, line 44
def method_missing(method_sym, *arguments, &block)
  if attributes.key?(method_sym)
    attributes[method_sym]
  elsif attributes.key?(method_sym.to_s)
    attributes[method_sym.to_s]
  elsif method_sym.to_s.end_with?('=')
    attributes[method_sym.to_s.gsub(/=$/, '').to_sym] = arguments[0]
  else
    super
  end
end
reactivate() click to toggle source
# File lib/fog/openstack/image/v2/models/image.rb, line 120
def reactivate
  requires :id
  service.reactivate_image(id)
end
remove_member(member_id) click to toggle source
# File lib/fog/openstack/image/v2/models/image.rb, line 135
def remove_member(member_id)
  requires :id
  service.remove_member_from_image(id, member_id)
end
remove_tag(tag) click to toggle source
# File lib/fog/openstack/image/v2/models/image.rb, line 170
def remove_tag(tag)
  requires :id
  service.remove_tag_from_image(id, tag)
end
remove_tags(tags) click to toggle source
# File lib/fog/openstack/image/v2/models/image.rb, line 165
def remove_tags(tags)
  requires :id
  tags.each { |tag| remove_tag tag }
end
respond_to?(method_sym, include_all = false) click to toggle source
Calls superclass method
# File lib/fog/openstack/image/v2/models/image.rb, line 56
def respond_to?(method_sym, include_all = false)
  if attributes.key?(method_sym)
    true
  elsif attributes.key?(method_sym.to_s)
    true
  elsif method_sym.to_s.end_with?('=')
    true
  else
    super
  end
end
save() click to toggle source

This overrides the behaviour of Fog::OpenStack::Model::save() which tries to be clever and

assumes save=update if an ID is present - but Image V2 allows ID to be specified on creation
# File lib/fog/openstack/image/v2/models/image.rb, line 92
def save
  if @attributes[:self].nil?
    create
  else
    update
  end
end
update(attr = nil) click to toggle source

Here we convert 'attributes' into a form suitable for Glance's usage of JSON Patch (RFC6902).

We fetch the existing attributes from the server to compute the delta (difference)
Setting value to nil will delete that attribute from the server.
# File lib/fog/openstack/image/v2/models/image.rb, line 77
def update(attr = nil)
  requires :id
  client_attributes = attr || @attributes
  server_attributes = service.images.get(id).attributes

  json_patch = build_update_json_patch(client_attributes, server_attributes)

  merge_attributes(
    service.update_image(id, json_patch).body
  )
  self
end
update_member(member) click to toggle source
# File lib/fog/openstack/image/v2/models/image.rb, line 140
def update_member(member)
  requires :id
  service.update_image_member(id, member)
end
upload_data(io_obj) click to toggle source
# File lib/fog/openstack/image/v2/models/image.rb, line 106
def upload_data(io_obj)
  requires :id
  if io_obj.kind_of? Hash
    service.upload_image(id, nil, io_obj)
  else
    service.upload_image(id, io_obj)
  end
end

Private Instance Methods

build_patch_operation(op_name, attributes) click to toggle source
# File lib/fog/openstack/image/v2/models/image.rb, line 203
def build_patch_operation(op_name, attributes)
  json_patch = []
  attributes.each do |key, value|
    json_patch << {:op => op_name, :path => "/#{key}", :value => value}
  end
  json_patch
end
build_update_json_patch(client_attributes, server_attributes) click to toggle source
# File lib/fog/openstack/image/v2/models/image.rb, line 177
def build_update_json_patch(client_attributes, server_attributes)
  [
    build_patch_operation('remove', patch_attributes_to_remove(client_attributes, server_attributes)),
    build_patch_operation('add', patch_attributes_to_add(client_attributes, server_attributes)),
    build_patch_operation('replace', patch_attributes_to_replace(client_attributes, server_attributes)),
  ].flatten
end
patch_attributes_to_add(client_attributes, server_attributes) click to toggle source
# File lib/fog/openstack/image/v2/models/image.rb, line 191
def patch_attributes_to_add(client_attributes, server_attributes)
  client_attributes.reject do |key, _|
    server_attributes.key?(key) || client_attributes[key].nil?
  end
end
patch_attributes_to_remove(client_attributes, server_attributes) click to toggle source
# File lib/fog/openstack/image/v2/models/image.rb, line 185
def patch_attributes_to_remove(client_attributes, server_attributes)
  client_attributes.select do |key, value|
    value.nil? && !server_attributes[key].nil?
  end
end
patch_attributes_to_replace(client_attributes, server_attributes) click to toggle source
# File lib/fog/openstack/image/v2/models/image.rb, line 197
def patch_attributes_to_replace(client_attributes, server_attributes)
  client_attributes.reject do |key, value|
    value.nil? || server_attributes[key] == value
  end
end