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 (106 lines) | stat: -rw-r--r-- 3,374 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
require 'fog/openstack/models/collection'
require 'fog/openstack/image/v1/models/image'

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

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

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

          def details(options = {}, deprecated_query = nil)
            Fog::Logger.deprecation("Calling OpenStack[:glance].images.details will be removed, "\
                                    " call .images.all for detailed list.")
            load_response(service.list_public_images_detailed(options, deprecated_query), 'images')
          end

          def find_by_id(id)
            marker          = 'X-Image-Meta-'
            property_marker = 'X-Image-Meta-Property-'
            headers = service.get_image_by_id(id).headers.select { |h, _| h.start_with?(marker) }

            # partioning on the longer prefix, leaving X-Image-Meta
            # headers in the second returned hash.
            custom_properties, params = headers.partition do |k, _|
              k.start_with?(property_marker)
            end.map { |p| Hash[p] }

            params            = remove_prefix_and_convert_type(params, marker)
            custom_properties = remove_prefix_and_convert_type(custom_properties, property_marker)

            params['properties'] = custom_properties
            new(params)
          rescue Fog::OpenStack::Image::NotFound
            nil
          end
          alias get find_by_id

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

          def private
            images = load(service.list_public_images_detailed.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_public_images_detailed($1, 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_public_images_detailed(attribute, value).body['images'])
          end

          private

          def convert_to_type(v)
            case v
            when /^\d+$/
              v.to_i
            when 'True'
              true
            when 'False'
              false
            when /^\d\d\d\d\-\d\d\-\d\dT/
              ::Time.parse(v)
            else
              v
            end
          end

          def remove_prefix_and_convert_type(hash, prefix)
            Hash[hash.map { |k, v| [k.gsub(prefix, '').downcase, convert_to_type(v)] }]
          end
        end
      end
    end
  end
end