| 1 |
require File.join(File.dirname(__FILE__), '..','..','..', 'puppet/provider/nova') |
| 2 |
|
| 3 |
Puppet::Type.type(:nova_aggregate).provide( |
| 4 |
:openstack, |
| 5 |
:parent => Puppet::Provider::Nova |
| 6 |
) do |
| 7 |
desc <<-EOT |
| 8 |
Provider to manage nova aggregations |
| 9 |
EOT |
| 10 |
|
| 11 |
@credentials = Puppet::Provider::Openstack::CredentialsV3.new |
| 12 |
|
| 13 |
mk_resource_methods |
| 14 |
|
| 15 |
def self.instances |
| 16 |
request('aggregate', 'list').collect do |el| |
| 17 |
attrs = request('aggregate', 'show', el[:name]) |
| 18 |
properties = parse_python_dict(attrs[:properties]) |
| 19 |
new( |
| 20 |
:ensure => :present, |
| 21 |
:name => attrs[:name], |
| 22 |
:id => attrs[:id], |
| 23 |
:availability_zone => attrs[:availability_zone], |
| 24 |
:metadata => properties, |
| 25 |
:hosts => parse_python_list(attrs[:hosts]).sort, |
| 26 |
) |
| 27 |
end |
| 28 |
end |
| 29 |
|
| 30 |
def self.prefetch(resources) |
| 31 |
instances_ = instances |
| 32 |
resources.keys.each do |name| |
| 33 |
if provider = instances_.find{ |instance| instance.name == name } |
| 34 |
resources[name].provider = provider |
| 35 |
end |
| 36 |
end |
| 37 |
end |
| 38 |
|
| 39 |
def self.get_known_hosts |
| 40 |
# get list of hosts known to be active from openstack |
| 41 |
return request('compute service', 'list', ['--service', 'nova-compute']).map{|el| el[:host]} |
| 42 |
end |
| 43 |
|
| 44 |
def exists? |
| 45 |
@property_hash[:ensure] == :present |
| 46 |
end |
| 47 |
|
| 48 |
def destroy |
| 49 |
@property_hash[:hosts].each do |h| |
| 50 |
properties = [@property_hash[:name], h] |
| 51 |
self.class.request('aggregate', 'remove host', properties) |
| 52 |
end |
| 53 |
self.class.request('aggregate', 'delete', @property_hash[:name]) |
| 54 |
@property_hash.clear |
| 55 |
end |
| 56 |
|
| 57 |
def create |
| 58 |
properties = [@resource[:name]] |
| 59 |
if not @resource[:availability_zone].nil? and not @resource[:availability_zone].empty? |
| 60 |
properties << "--zone" << @resource[:availability_zone] |
| 61 |
end |
| 62 |
if not @resource[:metadata].nil? and not @resource[:metadata].empty? |
| 63 |
@resource[:metadata].each do |key, value| |
| 64 |
properties << "--property" << "#{key}=#{value}" |
| 65 |
end |
| 66 |
end |
| 67 |
|
| 68 |
attrs = self.class.request('aggregate', 'create', properties) |
| 69 |
properties = self.class.parse_python_dict(attrs[:properties]) |
| 70 |
@property_hash = { |
| 71 |
:ensure => :present, |
| 72 |
:name => attrs[:name], |
| 73 |
:id => attrs[:id], |
| 74 |
:availability_zone => attrs[:availability_zone], |
| 75 |
:metadata => properties, |
| 76 |
} |
| 77 |
|
| 78 |
if not @resource[:hosts].nil? and not @resource[:hosts].empty? |
| 79 |
# filter host list by known hosts if filter_hosts is set |
| 80 |
if @resource[:filter_hosts] == :true |
| 81 |
@resource[:hosts] = @resource[:hosts] & self.class.get_known_hosts() |
| 82 |
end |
| 83 |
@resource[:hosts].each do |host| |
| 84 |
properties = [@property_hash[:name], host] |
| 85 |
self.class.request('aggregate', 'add host', properties) |
| 86 |
end |
| 87 |
@property_hash[:hosts] = @resource[:hosts].sort |
| 88 |
else |
| 89 |
@property_hash[:hosts] = @resource[:hosts] |
| 90 |
end |
| 91 |
end |
| 92 |
|
| 93 |
def availability_zone=(value) |
| 94 |
self.class.request('aggregate', 'set', [ @resource[:name], '--zone', @resource[:availability_zone] ]) |
| 95 |
end |
| 96 |
|
| 97 |
def metadata=(value) |
| 98 |
# clear obsolete keys |
| 99 |
if @property_hash[:metadata].keys.length > 0 |
| 100 |
properties = [@resource[:name] ] |
| 101 |
(@property_hash[:metadata].keys - @resource[:metadata].keys).each do |key| |
| 102 |
properties << "--property" << "#{key}" |
| 103 |
end |
| 104 |
self.class.request('aggregate', 'unset', properties) |
| 105 |
end |
| 106 |
properties = [@resource[:name] ] |
| 107 |
@resource[:metadata].each do |key, value| |
| 108 |
properties << "--property" << "#{key}=#{value}" |
| 109 |
end |
| 110 |
self.class.request('aggregate', 'set', properties) |
| 111 |
end |
| 112 |
|
| 113 |
def hosts=(value) |
| 114 |
# filter host list by known hosts if filter_hosts is set |
| 115 |
if @resource[:filter_hosts] == :true |
| 116 |
value &= self.class.get_known_hosts() |
| 117 |
end |
| 118 |
if not @property_hash[:hosts].nil? |
| 119 |
# remove hosts that are not present in update |
| 120 |
(@property_hash[:hosts] - value).each do |host| |
| 121 |
self.class.request('aggregate', 'remove host', [@property_hash[:id], host]) |
| 122 |
end |
| 123 |
# add hosts that are not already present |
| 124 |
(value - @property_hash[:hosts]).each do |host| |
| 125 |
self.class.request('aggregate', 'add host', [@property_hash[:id], host]) |
| 126 |
end |
| 127 |
end |
| 128 |
end |
| 129 |
end |