7be1ace9ddb52374f1f64023c7eb9c273c3ad532
[mirror/dsa-puppet.git] / 3rdparty / modules / glance / lib / puppet / provider / glance_image / glance.rb
1 # Load the Glance provider library to help
2 require File.join(File.dirname(__FILE__), '..','..','..', 'puppet/provider/glance')
3
4 Puppet::Type.type(:glance_image).provide(
5   :glance,
6   :parent => Puppet::Provider::Glance
7 ) do
8   desc <<-EOT
9     Glance provider to manage glance_image type.
10
11     Assumes that the glance-api service is on the same host and is working.
12   EOT
13
14   commands :glance => 'glance'
15
16   mk_resource_methods
17
18   def self.instances
19     list_glance_images.collect do |image|
20       attrs = get_glance_image_attrs(image)
21       new(
22         :ensure           => :present,
23         :name             => attrs['name'],
24         :is_public        => attrs['is_public'],
25         :container_format => attrs['container_format'],
26         :id               => attrs['id'],
27         :disk_format      => attrs['disk_format']
28       )
29     end
30   end
31
32   def self.prefetch(resources)
33     images = instances
34     resources.keys.each do |name|
35       if provider = images.find{ |pkg| pkg.name == name }
36         resources[name].provider = provider
37       end
38     end
39   end
40
41   def exists?
42     @property_hash[:ensure] == :present
43   end
44
45   def create
46     if resource[:source]
47       # copy_from cannot handle file://
48       if resource[:source] =~ /^\// # local file
49         location = "--file=#{resource[:source]}"
50       else
51         location = "--copy-from=#{resource[:source]}"
52       end
53     # location cannot handle file://
54     # location does not import, so no sense in doing anything more than this
55     elsif resource[:location]
56       location = "--location=#{resource[:location]}"
57     else
58       raise(Puppet::Error, "Must specify either source or location")
59     end
60     results = auth_glance('image-create', "--name=#{resource[:name]}", "--is-public=#{resource[:is_public]}", "--container-format=#{resource[:container_format]}", "--disk-format=#{resource[:disk_format]}", location)
61
62     id = nil
63
64     # Check the old behavior of the python-glanceclient
65     if results =~ /Added new image with ID: (\S+)/
66       id = $1
67     else # the new behavior doesn't print the status, so parse the table
68       results_array = parse_table(results)
69       results_array.each do |result|
70         if result["Property"] == "id"
71           id = result["Value"]
72         end
73       end
74     end
75
76     if id
77       @property_hash = {
78         :ensure           => :present,
79         :name             => resource[:name],
80         :is_public        => resource[:is_public],
81         :container_format => resource[:container_format],
82         :disk_format      => resource[:disk_format],
83         :id               => id
84       }
85     else
86         fail("did not get expected message from image creation, got #{results}")
87     end
88   end
89
90   def destroy
91     auth_glance('image-delete', id)
92     @property_hash[:ensure] = :absent
93   end
94
95   def location=(value)
96     auth_glance('image-update', id, "--location=#{value}")
97   end
98
99   def is_public=(value)
100     auth_glance('image-update', id, "--is-public=#{value}")
101   end
102
103   def disk_format=(value)
104     auth_glance('image-update', id, "--disk-format=#{value}")
105   end
106
107   def container_format=(value)
108     auth_glance('image-update', id, "--container-format=#{value}")
109   end
110
111   def id=(id)
112     fail('id is read only')
113   end
114
115 end