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 (285 lines) | stat: -rw-r--r-- 8,329 bytes parent folder | download
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
require File.join(File.dirname(__FILE__), '..','..','..',
                  'puppet/provider/neutron')

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

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

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

  mk_resource_methods

  def initialize(value={})
    super(value)
    @property_flush = {}
  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
    list = request('subnet', 'list').collect do |attrs|
      subnet = request('subnet', 'show', attrs[:id])
      new(
        :ensure            => :present,
        :name              => attrs[:name],
        :id                => attrs[:id],
        :cidr              => subnet[:cidr],
        :ip_version        => subnet[:ip_version],
        :ipv6_ra_mode      => subnet[:ipv6_ra_mode],
        :ipv6_address_mode => subnet[:ipv6_address_mode],
        :gateway_ip        => parse_gateway_ip(subnet[:gateway_ip]),
        :allocation_pools  => parse_allocation_pool(subnet[:allocation_pools]),
        :host_routes       => parse_host_routes(subnet[:host_routes]),
        :dns_nameservers   => parse_dns_nameservers(subnet[:dns_nameservers]),
        :enable_dhcp       => subnet[:enable_dhcp],
        :network_id        => subnet[:network_id],
        :network_name      => get_network_name(subnet[:network_id]),
        :project_id        => subnet[:project_id],
      )
    end
    self.do_not_manage = false
    list
  end

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

  def self.parse_gateway_ip(value)
    return '' if value.nil?
    return value
  end

  def self.parse_allocation_pool(values)
    allocation_pools = []
    return [] if values.empty? or values == '[]'
    values = values.gsub('[', '').gsub(']', '')
    for value in Array(values)
      allocation_pool = JSON.parse(value.gsub(/\\"/,'"').gsub('\'','"'))
      start_ip = allocation_pool['start']
      end_ip = allocation_pool['end']
      allocation_pools << "start=#{start_ip},end=#{end_ip}"
    end
    return allocation_pools
  end

  def self.parse_host_routes(values)
    host_routes = []
    return [] if values.empty? or values == '[]'
    values = values.gsub('[', '').gsub(']', '')
    for value in Array(values)
      host_route = JSON.parse(value.gsub(/\\"/,'"').gsub('\'','"'))
      nexthop = host_route['nexthop']
      destination = host_route['destination']
      host_routes << "destination=#{destination},nexthop=#{nexthop}"
    end
    return host_routes
  end

  def self.parse_dns_nameservers(values)
    if values.is_a? String
        values = values.gsub('\'','').gsub('[', '').gsub(']', '')
                       .gsub(',', '').split(' ')
    end
    # just enforce that this is actually an array
    return Array(values)
  end

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

  def create
    if self.class.do_not_manage
      fail("Not managing Neutron_subnet[#{@resource[:name]}] due to earlier Neutron API failures.")
    end

    opts = [@resource[:name]]

    if @resource[:ip_version]
      opts << "--ip-version=#{@resource[:ip_version]}"
    end

    if @resource[:ipv6_ra_mode]
      opts << "--ipv6-ra-mode=#{@resource[:ipv6_ra_mode]}"
    end

    if @resource[:ipv6_address_mode]
      opts << "--ipv6-address-mode=#{@resource[:ipv6_address_mode]}"
    end

    if @resource[:gateway_ip]
      if @resource[:gateway_ip] == ''
        opts << '--gateway=none'
      else
        opts << "--gateway=#{@resource[:gateway_ip]}"
      end
    end

    if @resource[:enable_dhcp] == 'False'
      opts << "--no-dhcp"
    else
      opts << "--dhcp"
    end

    if @resource[:allocation_pools]
      Array(@resource[:allocation_pools]).each do |allocation_pool|
        opts << "--allocation-pool=#{allocation_pool}"
      end
    end

    if @resource[:dns_nameservers]
      Array(@resource[:dns_nameservers]).each do |nameserver|
        opts << "--dns-nameserver=#{nameserver}"
      end
    end

    if @resource[:host_routes]
      Array(@resource[:host_routes]).each do |host_route|
        opts << "--host-route=#{host_route}"
      end
    end

    if @resource[:project_name]
      opts << "--project=#{@resource[:project_name]}"
    elsif @resource[:project_id]
      opts << "--project=#{@resource[:project_id]}"
    end

    if @resource[:network_name]
      opts << "--network=#{@resource[:network_name]}"
    elsif @resource[:network_id]
      opts << "--network=#{@resource[:network_id]}"
    end

    opts << "--subnet-range=#{@resource[:cidr]}"

    subnet = self.class.request('subnet', 'create', opts)
    @property_hash = {
      :ensure            => :present,
      :name              => subnet[:name],
      :id                => subnet[:id],
      :cidr              => subnet[:cidr],
      :ip_version        => subnet[:ip_version],
      :ipv6_ra_mode      => subnet[:ipv6_ra_mode],
      :ipv6_address_mode => subnet[:ipv6_address_mode],
      :gateway_ip        => self.class.parse_gateway_ip(subnet[:gateway_ip]),
      :allocation_pools  => self.class.parse_allocation_pool(subnet[:allocation_pools]),
      :host_routes       => self.class.parse_host_routes(subnet[:host_routes]),
      :dns_nameservers   => self.class.parse_dns_nameservers(subnet[:dns_nameservers]),
      :enable_dhcp       => subnet[:enable_dhcp],
      :network_id        => subnet[:network_id],
      :network_name      => self.class.get_network_name(subnet[:network_id]),
      :project_id        => subnet[:project_id],
    }
  end

  def flush
    if !@property_flush.empty?
      opts = [@resource[:name]]
      clear_opts = [@resource[:name]]

      if @property_flush.has_key?(:gateway_ip)
        if @property_flush[:gateway_ip] == ''
          opts << '--gateway=none'
        else
          opts << "--gateway=#{@property_flush[:gateway_ip]}"
        end
      end

      if @property_flush.has_key?(:enable_dhcp)
        if @property_flush[:enable_dhcp] == 'False'
          opts << '--no-dhcp'
        else
          opts << '--dhcp'
        end
      end

      if @property_flush.has_key?(:allocation_pools)
        clear_opts << '--no-allocation-pool'
        Array(@property_flush[:allocation_pools]).each do |allocation_pool|
          opts << "--allocation-pool=#{allocation_pool}"
        end
      end

      if @property_flush.has_key?(:dns_nameservers)
        clear_opts << '--no-dns-nameservers'
        Array(@property_flush[:dns_nameservers]).each do |nameserver|
          opts << "--dns-nameserver=#{nameserver}"
        end
      end

      if @property_flush.has_key?(:host_routes)
        clear_opts << '--no-host-route'
        Array(@property_flush[:host_routes]).each do |host_route|
          opts << "--host-route=#{host_route}"
        end
      end

      if clear_opts.length > 1
        self.class.request('subnet', 'set', clear_opts)
      end
      if opts.length > 1
        self.class.request('subnet', 'set', opts)
      end
      @property_flush.clear
    end
  end

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

  [
    :gateway_ip,
    :enable_dhcp,
    :allocation_pools,
    :dns_nameservers,
    :host_routes,
  ].each do |attr|
    define_method(attr.to_s + "=") do |value|
      if self.class.do_not_manage
        fail("Not managing Neutron_network[#{@resource[:name]}] due to earlier Neutron API failures.")
      end
      @property_flush[attr] = value
    end
  end

  [
   :cidr,
   :ip_version,
   :ipv6_ra_mode,
   :ipv6_address_mode,
   :network_id,
   :project_id,
   :project_name,
  ].each do |attr|
    define_method(attr.to_s + "=") do |value|
      fail("Property #{attr.to_s} does not support being updated")
    end
  end

end