File: image.rb

package info (click to toggle)
ruby-fog-openstack 1.1.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,784 kB
  • sloc: ruby: 47,937; makefile: 5; sh: 4
file content (214 lines) | stat: -rw-r--r-- 6,360 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
require 'fog/openstack/models/model'

module Fog
  module OpenStack
    class Image
      class V2
        class Image < Fog::OpenStack::Model
          identity :id

          attribute :name
          attribute :visibility # public or private

          attribute :tags

          attribute :self
          attribute :size
          attribute :virtual_size
          attribute :disk_format
          attribute :container_format
          attribute :id
          attribute :checksum
          attribute :self
          attribute :file

          # detailed
          attribute :min_disk
          attribute :created_at
          attribute :updated_at
          attribute :protected
          attribute :status # "queued","saving","active","killed","deleted","pending_delete"
          attribute :min_ram
          attribute :owner
          attribute :properties
          attribute :metadata
          attribute :location

          # from snapshot support
          attribute :network_allocated
          attribute :base_image_ref
          attribute :image_type
          attribute :instance_uuid
          attribute :user_id

          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

          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

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

          # 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.
          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

          # 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
          def save
            if @attributes[:self].nil?
              create
            else
              update
            end
          end

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

          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

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

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

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

          def add_member(member_id)
            requires :id
            service.add_member_to_image(id, member_id)
          end

          def remove_member(member_id)
            requires :id
            service.remove_member_from_image(id, member_id)
          end

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

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

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

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

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

          def remove_tags(tags)
            requires :id
            tags.each { |tag| remove_tag tag }
          end

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

          private

          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

          def patch_attributes_to_remove(client_attributes, server_attributes)
            client_attributes.select do |key, value|
              value.nil? && !server_attributes[key].nil?
            end
          end

          def patch_attributes_to_add(client_attributes, server_attributes)
            client_attributes.reject do |key, _|
              server_attributes.key?(key) || client_attributes[key].nil?
            end
          end

          def patch_attributes_to_replace(client_attributes, server_attributes)
            client_attributes.reject do |key, value|
              value.nil? || server_attributes[key] == value
            end
          end

          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
        end
      end
    end
  end
end