File: openstack.rb

package info (click to toggle)
puppet-module-neutron 25.0.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,708 kB
  • sloc: ruby: 12,680; python: 38; sh: 15; makefile: 10
file content (111 lines) | stat: -rw-r--r-- 2,859 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
require File.join(File.dirname(__FILE__), '..','..','..',
                  'puppet/provider/neutron')

Puppet::Type.type(:neutron_router_interface).provide(
  :openstack,
  :parent => Puppet::Provider::Neutron
) do
  desc <<-EOT
    Neutron provider to manage neutron_router_interface type.

    Assumes that the neutron service is configured on the same host.

    It is not possible to manage an interface for the subnet used by
    the gateway network, and such an interface will appear in the list
    of resources ('puppet resource [type]').  Attempting to manage the
    gateway interface will result in an error.
  EOT

  @credentials = Puppet::Provider::Openstack::CredentialsV3.new

  mk_resource_methods

  def initialize(value={})
    super(value)
  end

  def self.do_not_manage
    @do_not_manage
  end

  def self.do_not_manage=(value)
    @do_not_manage = value
  end

  def self.instances
    self.do_not_manage = true
    subnet_name_hash = {}
    request('subnet', 'list').each do |subnet|
      subnet_name_hash[subnet[:id]] = subnet[:name]
    end

    instances_ = []
    request('router', 'list').each do |router|
      request('port', 'list', ['--router', router[:id]]).each do |port|
        subnet_id_ = parse_subnet_id(port[:fixed_ip_addresses])
        subnet_name_ = subnet_name_hash[subnet_id_]
        router_name_ = router[:name]
        name_ = "#{router_name_}:#{subnet_name_}"
        instances_ << new(
          :ensure => :present,
          :name   => name_,
          :id     => port[:id],
          :port   => port[:name]
        )
      end
    end
    self.do_not_manage = false
    return instances_
  end

  def self.prefetch(resources)
    interfaces = instances
    resources.keys.each do |name|
      if provider = interfaces.find{ |interface| interface.name == name }
        resources[name].provider = provider
      end
    end
  end

  def exists?
    @property_hash[:ensure] == :present
  end

  def create
    router, subnet = name.split(':', 2)
    port = resource[:port]
    if port
      self.class.request('router', 'add port', [router, port])
    else
      self.class.request('router', 'add subnet', [router, subnet])
    end
    @property_hash = {
      :ensure => :present,
      :name   => resource[:name]
    }
  end

  def destroy
    if self.class.do_not_manage
      fail("Not managing Neutron_router_interface[#{@resource[:name]}] due to earlier Neutron API failures.")
    end
    router, subnet = name.split(':', 2)
    port = resource[:port]
    if port
      self.class.request('router', 'remove port', [router, port])
    else
      self.class.request('router', 'remove subnet', [router, subnet])
    end
    @property_hash.clear
    @property_hash[:ensure] = :absent
  end

  def router_name
    name.split(':', 2).first
  end

  def subnet_name
    name.split(':', 2).last
  end

end