Class: Fog::Image::OpenStack::V2::Image

Inherits:
OpenStack::Model show all
Defined in:
lib/fog/image/openstack/v2/models/image.rb

Instance Attribute Summary

Attributes inherited from OpenStack::Model

#project

Instance Method Summary collapse

Methods inherited from OpenStack::Model

#initialize

Constructor Details

This class inherits a constructor from Fog::OpenStack::Model

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object


44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fog/image/openstack/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(/=$/, '')] = arguments[0]
  else
    super
  end
end

Instance Method Details

#add_member(member_id) ⇒ Object


130
131
132
133
# File 'lib/fog/image/openstack/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) ⇒ Object


160
161
162
163
# File 'lib/fog/image/openstack/v2/models/image.rb', line 160

def add_tag(tag)
  requires :id
  service.add_tag_to_image(id, tag)
end

#add_tags(tags) ⇒ Object


155
156
157
158
# File 'lib/fog/image/openstack/v2/models/image.rb', line 155

def add_tags(tags)
  requires :id
  tags.each { |tag| add_tag tag }
end

#createObject


68
69
70
71
72
# File 'lib/fog/image/openstack/v2/models/image.rb', line 68

def create
  requires :name
  merge_attributes(service.create_image(attributes).body)
  self
end

#deactivateObject


125
126
127
128
# File 'lib/fog/image/openstack/v2/models/image.rb', line 125

def deactivate
  requires :id
  service.deactivate_image(id)
end

#destroyObject


100
101
102
103
104
# File 'lib/fog/image/openstack/v2/models/image.rb', line 100

def destroy
  requires :id
  service.delete_image(id)
  true
end

#download_data(params = {}) ⇒ Object


115
116
117
118
# File 'lib/fog/image/openstack/v2/models/image.rb', line 115

def download_data(params = {})
  requires :id
  service.download_image(id, params[:content_range], params)
end

#member(member_id) ⇒ Object


150
151
152
153
# File 'lib/fog/image/openstack/v2/models/image.rb', line 150

def member(member_id)
  requires :id
  service.get_member_details(id, member_id)
end

#membersObject


145
146
147
148
# File 'lib/fog/image/openstack/v2/models/image.rb', line 145

def members
  requires :id
  service.get_image_members(id).body['members']
end

#reactivateObject


120
121
122
123
# File 'lib/fog/image/openstack/v2/models/image.rb', line 120

def reactivate
  requires :id
  service.reactivate_image(id)
end

#remove_member(member_id) ⇒ Object


135
136
137
138
# File 'lib/fog/image/openstack/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) ⇒ Object


170
171
172
173
# File 'lib/fog/image/openstack/v2/models/image.rb', line 170

def remove_tag(tag)
  requires :id
  service.remove_tag_from_image(id, tag)
end

#remove_tags(tags) ⇒ Object


165
166
167
168
# File 'lib/fog/image/openstack/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) ⇒ Boolean


56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fog/image/openstack/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

#saveObject

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

92
93
94
95
96
97
98
# File 'lib/fog/image/openstack/v2/models/image.rb', line 92

def save
  if @attributes[:self].nil?
    create
  else
    update
  end
end

#update(attr = nil) ⇒ Object

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.

77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/fog/image/openstack/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) ⇒ Object


140
141
142
143
# File 'lib/fog/image/openstack/v2/models/image.rb', line 140

def update_member(member)
  requires :id
  service.update_image_member(id, member)
end

#upload_data(io_obj) ⇒ Object


106
107
108
109
110
111
112
113
# File 'lib/fog/image/openstack/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