4637846b519b6afc7cb272b7e0d8c49abdded9bb
[mirror/dsa-puppet.git] / 3rdparty / modules / neutron / lib / puppet / provider / neutron_router_interface / neutron.rb
1 require File.join(File.dirname(__FILE__), '..','..','..',
2                   'puppet/provider/neutron')
3
4 Puppet::Type.type(:neutron_router_interface).provide(
5   :neutron,
6   :parent => Puppet::Provider::Neutron
7 ) do
8   desc <<-EOT
9     Neutron provider to manage neutron_router_interface type.
10
11     Assumes that the neutron service is configured on the same host.
12
13     It is not possible to manage an interface for the subnet used by
14     the gateway network, and such an interface will appear in the list
15     of resources ('puppet resource [type]').  Attempting to manage the
16     gateway interfae will result in an error.
17
18   EOT
19
20   commands :neutron => 'neutron'
21
22   mk_resource_methods
23
24   def self.instances
25     subnet_name_hash = {}
26     Puppet::Type.type('neutron_subnet').instances.each do |instance|
27       subnet_name_hash[instance.provider.id] = instance.provider.name
28     end
29     instances_ = []
30     Puppet::Type.type('neutron_router').instances.each do |instance|
31       list_router_ports(instance.provider.id).each do |port_hash|
32         router_name = instance.provider.name
33         subnet_name = subnet_name_hash[port_hash['subnet_id']]
34         name = "#{router_name}:#{subnet_name}"
35         instances_ << new(
36             :ensure                    => :present,
37             :name                      => name,
38             :id                        => port_hash['id'],
39             :port                      => port_hash['name']
40             )
41       end
42     end
43     return instances_
44   end
45
46   def self.prefetch(resources)
47     instances_ = instances
48     resources.keys.each do |name|
49       if provider = instances_.find{ |instance| instance.name == name }
50         resources[name].provider = provider
51       end
52     end
53   end
54
55   def exists?
56     @property_hash[:ensure] == :present
57   end
58
59   def create
60     router,subnet = resource[:name].split(':', 2)
61     port = resource[:port]
62     args = ["router-interface-add", "--format=shell", router]
63     if port
64       args << "port=#{port}"
65     else
66       args << "subnet=#{subnet}"
67     end
68     results = auth_neutron(args)
69
70     if results =~ /Added interface.* to router/
71       @property_hash = {
72         :ensure => :present,
73         :name   => resource[:name],
74       }
75     else
76       fail("did not get expected message on interface addition, got #{results}")
77     end
78   end
79
80   def router_name
81     name.split(':', 2).first
82   end
83
84   def subnet_name
85     name.split(':', 2).last
86   end
87
88   def destroy
89     auth_neutron('router-interface-delete', router_name, subnet_name)
90     @property_hash[:ensure] = :absent
91   end
92
93 end