try again, with puppetforge modules, correctly included now
[mirror/dsa-puppet.git] / 3rdparty / modules / neutron / lib / puppet / provider / neutron_subnet / neutron.rb
1 require File.join(File.dirname(__FILE__), '..','..','..',
2                   'puppet/provider/neutron')
3
4 Puppet::Type.type(:neutron_subnet).provide(
5   :neutron,
6   :parent => Puppet::Provider::Neutron
7 ) do
8   desc <<-EOT
9     Neutron provider to manage neutron_subnet type.
10
11     Assumes that the neutron service is configured on the same host.
12   EOT
13
14   commands :neutron => 'neutron'
15
16   mk_resource_methods
17
18   def self.neutron_type
19     'subnet'
20   end
21
22   def self.instances
23     list_neutron_resources(neutron_type).collect do |id|
24       attrs = get_neutron_resource_attrs(neutron_type, id)
25       new(
26         :ensure                    => :present,
27         :name                      => attrs['name'],
28         :id                        => attrs['id'],
29         :cidr                      => attrs['cidr'],
30         :ip_version                => attrs['ip_version'],
31         :gateway_ip                => parse_gateway_ip(attrs['gateway_ip']),
32         :allocation_pools          => parse_allocation_pool(attrs['allocation_pools']),
33         :host_routes               => parse_host_routes(attrs['host_routes']),
34         :dns_nameservers           => parse_dns_nameservers(attrs['dns_nameservers']),
35         :enable_dhcp               => attrs['enable_dhcp'],
36         :network_id                => attrs['network_id'],
37         :tenant_id                 => attrs['tenant_id']
38       )
39     end
40   end
41
42   def self.prefetch(resources)
43     subnets = instances
44     resources.keys.each do |name|
45       if provider = subnets.find{ |subnet| subnet.name == name }
46         resources[name].provider = provider
47       end
48     end
49   end
50
51   def self.parse_gateway_ip(value)
52     return '' if value.nil?
53     return value
54   end
55
56   def self.parse_allocation_pool(values)
57     allocation_pools = []
58     return [] if values.empty?
59     for value in Array(values)
60       matchdata = /\{\s*"start"\s*:\s*"(.*)"\s*,\s*"end"\s*:\s*"(.*)"\s*\}/.match(value.gsub(/\\"/,'"'))
61       start_ip = matchdata[1]
62       end_ip = matchdata[2]
63       allocation_pools << "start=#{start_ip},end=#{end_ip}"
64     end
65     return allocation_pools
66   end
67
68   def self.parse_host_routes(values)
69     host_routes = []
70     return [] if values.empty?
71     for value in Array(values)
72       matchdata = /\{\s*"destination"\s*:\s*"(.*)"\s*,\s*"nexthop"\s*:\s*"(.*)"\s*\}/.match(value.gsub(/\\"/,'"'))
73       destination = matchdata[1]
74       nexthop = matchdata[2]
75       host_routes << "destination=#{destination},nexthop=#{nexthop}"
76     end
77     return host_routes
78   end
79
80   def self.parse_dns_nameservers(values)
81     # just enforce that this is actually an array
82     return Array(values)
83   end
84
85   def exists?
86     @property_hash[:ensure] == :present
87   end
88
89   def create
90     opts = ["--name=#{@resource[:name]}"]
91
92     if @resource[:ip_version]
93       opts << "--ip-version=#{@resource[:ip_version]}"
94     end
95
96     if @resource[:gateway_ip]
97       if @resource[:gateway_ip] == ''
98         opts << '--no-gateway'
99       else
100         opts << "--gateway-ip=#{@resource[:gateway_ip]}"
101       end
102     end
103
104     if @resource[:enable_dhcp] == 'False'
105       opts << "--disable-dhcp"
106     else
107       opts << "--enable-dhcp"
108     end
109
110     if @resource[:allocation_pools]
111       Array(@resource[:allocation_pools]).each do |allocation_pool|
112         opts << "--allocation-pool=#{allocation_pool}"
113       end
114     end
115
116     if @resource[:dns_nameservers]
117       Array(@resource[:dns_nameservers]).each do |nameserver|
118         opts << "--dns-nameserver=#{nameserver}"
119       end
120     end
121
122     if @resource[:host_routes]
123       Array(@resource[:host_routes]).each do |host_route|
124         opts << "--host-route=#{host_route}"
125       end
126     end
127
128     if @resource[:tenant_name]
129       tenant_id = self.class.get_tenant_id(model.catalog,
130                                            @resource[:tenant_name])
131       opts << "--tenant_id=#{tenant_id}"
132     elsif @resource[:tenant_id]
133       opts << "--tenant_id=#{@resource[:tenant_id]}"
134     end
135
136     if @resource[:network_name]
137       opts << resource[:network_name]
138     elsif @resource[:network_id]
139       opts << resource[:network_id]
140     end
141
142     results = auth_neutron('subnet-create', '--format=shell',
143                            opts, resource[:cidr])
144
145     if results =~ /Created a new subnet:/
146       attrs = self.class.parse_creation_output(results)
147       @property_hash = {
148         :ensure                    => :present,
149         :name                      => resource[:name],
150         :id                        => attrs['id'],
151         :cidr                      => attrs['cidr'],
152         :ip_version                => attrs['ip_version'],
153         :gateway_ip                => self.class.parse_gateway_ip(attrs['gateway_ip']),
154         :allocation_pools          => self.class.parse_allocation_pool(attrs['allocation_pools']),
155         :host_routes               => self.class.parse_host_routes(attrs['host_routes']),
156         :dns_nameservers           => self.class.parse_dns_nameservers(attrs['dns_nameservers']),
157         :enable_dhcp               => attrs['enable_dhcp'],
158         :network_id                => attrs['network_id'],
159         :tenant_id                 => attrs['tenant_id'],
160       }
161     else
162       fail("did not get expected message on subnet creation, got #{results}")
163     end
164   end
165
166   def destroy
167     auth_neutron('subnet-delete', name)
168     @property_hash[:ensure] = :absent
169   end
170
171   def gateway_ip=(value)
172     if value == ''
173       auth_neutron('subnet-update', '--no-gateway', name)
174     else
175       auth_neutron('subnet-update', "--gateway-ip=#{value}", name)
176     end
177   end
178
179   def enable_dhcp=(value)
180     if value == 'False'
181       auth_neutron('subnet-update', "--disable-dhcp", name)
182     else
183       auth_neutron('subnet-update', "--enable-dhcp", name)
184     end
185   end
186
187   def dns_nameservers=(values)
188     unless values.empty?
189       opts = ["#{name}", "--dns-nameservers", "list=true"]
190       for value in values
191         opts << value
192       end
193       auth_neutron('subnet-update', opts)
194     end
195   end
196
197   def host_routes=(values)
198     unless values.empty?
199       opts = ["#{name}", "--host-routes", "type=dict", "list=true"]
200       for value in values
201         opts << value
202       end
203       auth_neutron('subnet-update', opts)
204     end
205   end
206
207   [
208    :cidr,
209    :ip_version,
210    :network_id,
211    :allocation_pools,
212    :tenant_id,
213   ].each do |attr|
214      define_method(attr.to_s + "=") do |value|
215        fail("Property #{attr.to_s} does not support being updated")
216      end
217   end
218
219 end