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
|
require File.join(File.dirname(__FILE__), '..','..','..',
'puppet/provider/neutron')
Puppet::Type.type(:neutron_port).provide(
:openstack,
:parent => Puppet::Provider::Neutron
) do
desc <<-EOT
Neutron provider to manage neutron_port 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('port', 'list').collect do |attrs|
port = request('port', 'show', attrs[:id])
port[:name] = port[:id] if port[:name].empty?
new(
:ensure => :present,
:name => port[:name],
:id => port[:id],
:status => port[:status],
:project_id => port[:project_id],
:network_id => port[:network_id],
:network_name => get_network_name(port[:network_id]),
:admin_state_up => port[:admin_state_up],
:subnet_id => parse_subnet_id(port[:fixed_ips]),
:subnet_name => get_subnet_name(parse_subnet_id(port[:fixed_ips])),
:ip_address => parse_ip_address(port[:fixed_ips]),
:binding_profile => parse_binding_profile_interface_name(port[:binding_profile]),
:binding_host_id => port[:binding_host_id],
)
end
self.do_not_manage = false
list
end
def self.prefetch(resources)
ports = instances
resources.keys.each do |name|
if provider = ports.find{ |net| net.name == name }
resources[name].provider = provider
end
end
end
def exists?
@property_hash[:ensure] == :present
end
def create
if self.class.do_not_manage
fail("Not managing Neutron_port[#{@resource[:name]}] due to earlier Neutron API failures.")
end
opts = [@resource[:name]]
if @resource[:network_name]
opts << "--network=#{@resource[:network_name]}"
elsif @resource[:network_id]
opts << "--network=#{@resource[:network_id]}"
end
if @resource[:admin_state_up] == 'False'
opts << '--disable'
end
if @resource[:ip_address]
Array(resource[:ip_address]).each do |ip|
opts << "--fixed-ip ip_address=#{ip}"
end
end
if @resource[:subnet_name]
Array(resource[:subnet_name]).each do |subnet|
opts << "--fixed-ip subnet=#{subnet}"
end
end
if @resource[:project_name]
opts << "--project=#{@resource[:project_name]}"
elsif @resource[:project_id]
opts << "--project=#{@resource[:project_id]}"
end
if @resource[:binding_host_id]
opts << "--host=#{@resource[:binding_host_id]}"
end
if @resource[:binding_profile]
@resource[:binding_profile].each do |k,v|
opts << "--binding-profile #{k}=#{v}"
end
end
port = self.class.request('port', 'create', opts)
@property_hash = {
:ensure => :present,
:name => port[:name],
:id => port[:id],
:status => port[:status],
:project_id => port[:project_id],
:network_id => port[:network_id],
:network_name => self.class.get_network_name(port[:network_id]),
:admin_state_up => port[:admin_state_up],
:subnet_id => self.class.parse_subnet_id(port[:fixed_ips]),
:subnet_name => self.class.get_subnet_name(self.class.parse_subnet_id(port[:fixed_ips])),
:ip_address => self.class.parse_ip_address(port[:fixed_ips]),
:binding_profile => self.class.parse_binding_profile_interface_name(port[:binding_profile]),
:binding_host_id => port[:binding_host_id],
}
end
def flush
if !@property_flush.empty?
opts = [@resource[:name]]
if @property_flush.has_key?(:admin_state_up)
if @property_flush[:admin_state_up] == 'False'
opts << '--disable'
else
opts << '--enable'
end
end
if @property_flush.has_key?(:shared)
if @property_flush[:shared] == 'False'
opts << '--no-share'
else
opts << '--share'
end
end
if @property_flush.has_key?(:router_external)
if @property_flush[:router_external] == 'False'
opts << '--internal'
else
opts << '--external'
end
end
if @property_flush.has_key?(:availability_zone_hint)
opts << "--availability-zone-hint=#{@property_flush[:availability_zone_hint]}"
end
self.class.request('port', 'set', opts)
@property_flush.clear
end
end
def destroy
if self.class.do_not_manage
fail("Not managing Neutron_port[#{@resource[:name]}] due to earlier Neutron API failures.")
end
self.class.request('port', 'delete', @resource[:name])
@property_hash.clear
@property_hash[:ensure] = :absent
end
def self.parse_ip_address(value)
fixed_ips = JSON.parse(value.gsub(/\\"/,'"').gsub('\'','"'))
ips = []
fixed_ips.each do |fixed_ip|
ips << fixed_ip['ip_address']
end
if ips.length > 1
ips
else
ips.first
end
end
def self.parse_binding_profile_interface_name(value)
profile = JSON.parse(value.gsub(/\\"/,'"').gsub('\'','"'))
profile['interface_name']
end
[
:admin_state_up,
].each do |attr|
define_method(attr.to_s + "=") do |value|
if self.class.do_not_manage
fail("Not managing Neutron_port[#{@resource[:name]}] due to earlier Neutron API failures.")
end
@property_flush[attr] = value
end
end
[
:network_id,
:subnet_id,
:ip_address,
: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
|