1 require File.join(File.dirname(__FILE__), '..','..','..',
2 'puppet/provider/nova')
4 Puppet::Type.type(:nova_aggregate).provide(
6 :parent => Puppet::Provider::Nova
9 desc "Manage nova aggregations"
11 commands :nova => 'nova'
16 nova_aggregate_resources_ids().collect do |el|
17 attrs = nova_aggregate_resources_attr(el['Id'])
20 :name => attrs['Name'],
22 :availability_zone => attrs['Availability Zone'],
23 :metadata => attrs['Metadata'],
24 :hosts => attrs['Hosts']
29 def self.prefetch(resources)
30 instances_ = instances
31 resources.keys.each do |name|
32 if provider = instances_.find{ |instance| instance.name == name }
33 resources[name].provider = provider
39 @property_hash[:ensure] == :present
44 if not @property_hash[:hosts].nil?
45 @property_hash[:hosts].each do |h|
46 auth_nova("aggregate-remove-host", name, h)
50 auth_nova("aggregate-delete", name)
51 @property_hash[:ensure] = :absent
56 #check for availability zone
57 if not @resource[:availability_zone].nil? and not @resource[:availability_zone].empty?
58 extras << "#{@resource[:availability_zone]}"
61 result = auth_nova("aggregate-create", resource[:name], extras)
64 id = self.class.nova_aggregate_resources_get_name_by_id(resource[:name])
68 :name => resource[:name],
70 :availability_zone => resource[:availability_zone]
74 if not @resource[:metadata].nil? and not @resource[:metadata].empty?
75 @resource[:metadata].each do |key, value|
76 set_metadata_helper(id, key, value)
78 @property_hash[:metadata] = resource[:metadata]
81 #add hosts - This throws an error if the host is already attached to another aggregate!
82 if not @resource[:hosts].nil? and not @resource[:hosts].empty?
83 @resource[:hosts].each do |host|
84 auth_nova("aggregate-add-host", id, "#{host}")
86 @property_hash[:hosts] = resource[:hosts]
92 id = self.class.nova_aggregate_resources_get_name_by_id(name)
93 attrs = self.class.nova_aggregate_resources_attr(id)
94 #remove all hosts which are not in new value list
95 attrs['Hosts'].each do |h|
97 auth_nova("aggregate-remove-host", id, "#{h}")
101 #add hosts from the value list
103 if not attrs['Hosts'].include? h
104 auth_nova("aggregate-add-host", id, "#{h}")
109 def set_metadata_helper(agg_id, key, value)
110 auth_nova("aggregate-set-metadata", agg_id, "#{key}=#{value}")
114 #get current metadata
115 id = self.class.nova_aggregate_resources_get_name_by_id(name)
116 attrs = self.class.nova_aggregate_resources_attr(id)
117 #just ignore the availability_zone. that's handled directly by nova
118 attrs['Metadata'].delete('availability_zone')
119 return attrs['Metadata']
123 #get current metadata
124 id = self.class.nova_aggregate_resources_get_name_by_id(name)
125 attrs = self.class.nova_aggregate_resources_attr(id)
126 #get keys which are in current metadata but not in val. Make sure it has data first!
127 if attrs['Metadata'].length > 0
128 obsolete_keys = attrs['Metadata'].keys - val.keys
130 # clear obsolete keys. If there are any!
132 obsolete_keys.each do |key|
133 if not key.include? 'availability_zone'
134 auth_nova("aggregate-set-metadata", id, "#{key}")
137 #handle keys (with obsolete keys)
138 new_keys = val.keys - obsolete_keys
140 #handle keys (without obsolete keys)
143 #set new metadata if value changed
144 new_keys.each do |key|
145 if val[key] != attrs['Metadata'][key.to_s]
147 set_metadata_helper(id, key, value)
152 def availability_zone=(val)
153 id = self.class.nova_aggregate_resources_get_name_by_id(name)
154 auth_nova("aggregate-set-metadata", id, "availability_zone=#{val}")