File: images.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 (66 lines) | stat: -rw-r--r-- 1,778 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
require 'fog/openstack/models/collection'
require 'fog/openstack/image/v2/models/image'

module Fog
  module OpenStack
    class Image
      class V2
        class Images < Fog::OpenStack::Collection
          model Fog::OpenStack::Image::V2::Image

          def all(options = {})
            load_response(service.list_images(options), 'images')
          end

          def summary(options = {})
            load_response(service.list_images(options), 'images')
          end

          def find_by_id(id)
            new(service.get_image_by_id(id).body)
          rescue Fog::OpenStack::Image::NotFound
            nil
          end

          alias get find_by_id

          def public
            images = load(service.list_images.body['images'])
            images.delete_if { |image| image.is_public == false }
          end

          def private
            images = load(service.list_images.body['images'])
            images.delete_if(&:is_public)
          end

          def destroy(id)
            image = find_by_id(id)
            image.destroy
          end

          def method_missing(method_sym, *arguments, &block)
            if method_sym.to_s =~ /^find_by_(.*)$/
              load(service.list_images($1.to_sym => arguments.first).body['images'])
            else
              super
            end
          end

          def find_by_size_min(size)
            find_attribute(__method__, size)
          end

          def find_by_size_max(size)
            find_attribute(__method__, size)
          end

          def find_attribute(attribute, value)
            attribute = attribute.to_s.gsub("find_by_", "")
            load(service.list_images(attribute.to_sym => value).body['images'])
          end
        end
      end
    end
  end
end