Class: Puppet::Provider::Neutron
- Inherits:
-
Openstack
- Object
- Openstack
- Puppet::Provider::Neutron
- Extended by:
- Openstack::Auth
- Defined in:
- lib/puppet/provider/neutron.rb
Class Method Summary collapse
- .auth_endpoint ⇒ Object
- .auth_neutron(*args) ⇒ Object
- .cleanup_csv(text) ⇒ Object
- .cleanup_csv_with_id(text) ⇒ Object
- .conf_filename ⇒ Object
- .get_auth_endpoint ⇒ Object
- .get_neutron_credentials ⇒ Object
- .get_neutron_resource_attrs(type, id) ⇒ Object
- .get_tenant_id(catalog, name, domain = 'Default') ⇒ Object
- .list_neutron_resources(type) ⇒ Object
- .list_router_ports(router_name_or_id) ⇒ Object
- .neutron_conf ⇒ Object
- .neutron_credentials ⇒ Object
- .neutron_request(service, action, error, properties = nil) ⇒ Object
- .parse_creation_output(data) ⇒ Object
- .request(service, action, properties = nil) ⇒ Object
- .reset ⇒ Object
- .withenv(hash, &block) ⇒ Object
Instance Method Summary collapse
Class Method Details
.auth_endpoint ⇒ Object
112 113 114 |
# File 'lib/puppet/provider/neutron.rb', line 112 def self.auth_endpoint @auth_endpoint ||= get_auth_endpoint end |
.auth_neutron(*args) ⇒ Object
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 |
# File 'lib/puppet/provider/neutron.rb', line 116 def self.auth_neutron(*args) q = neutron_credentials authenv = { :OS_AUTH_URL => self.auth_endpoint, :OS_USERNAME => q['username'], :OS_PROJECT_NAME => q['project_name'], :OS_PASSWORD => q['password'], :OS_PROJECT_DOMAIN_NAME => q['project_domain_name'], :OS_USER_DOMAIN_NAME => q['user_domain_name'] } if q.key?('region_name') authenv[:OS_REGION_NAME] = q['region_name'] end rv = nil timeout = 10 end_time = Time.now.to_i + timeout loop do begin withenv authenv do rv = neutron(args) end break rescue Puppet::ExecutionFailure => e if ! e. =~ /(\(HTTP\s+400\))| (400-\{\'message\'\:\s+\'\'\})| (\[Errno 111\]\s+Connection\s+refused)| (503\s+Service\s+Unavailable)| (504\s+Gateway\s+Time-out)| (\:\s+Maximum\s+attempts\s+reached)| (Unauthorized\:\s+bad\s+credentials)| (Max\s+retries\s+exceeded)/ raise(e) end current_time = Time.now.to_i if current_time > end_time break else wait = end_time - current_time notice("Unable to complete neutron request due to non-fatal error: \"#{e.}\". Retrying for #{wait} sec.") end sleep(2) # Note(xarses): Don't remove, we know that there is one of the # Recoverable erros above, So we will retry a few more times end end return rv end |
.cleanup_csv(text) ⇒ Object
264 265 266 267 268 269 |
# File 'lib/puppet/provider/neutron.rb', line 264 def self.cleanup_csv(text) # Ignore warnings - assume legitimate output starts with a double quoted # string. Errors will be caught and raised prior to this text = text.split("\n").drop_while { |line| line !~ /^\".*\"/ }.join("\n") "#{text}\n" end |
.cleanup_csv_with_id(text) ⇒ Object
271 272 273 274 275 |
# File 'lib/puppet/provider/neutron.rb', line 271 def self.cleanup_csv_with_id(text) return nil if text.nil? text = text.split("\n").drop_while { |line| line !~ /^\s*id$/ }.join("\n") "#{text}\n" end |
.conf_filename ⇒ Object
41 42 43 |
# File 'lib/puppet/provider/neutron.rb', line 41 def self.conf_filename '/etc/neutron/neutron.conf' end |
.get_auth_endpoint ⇒ Object
107 108 109 110 |
# File 'lib/puppet/provider/neutron.rb', line 107 def self.get_auth_endpoint q = neutron_credentials "#{q['auth_url']}" end |
.get_neutron_credentials ⇒ Object
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 |
# File 'lib/puppet/provider/neutron.rb', line 74 def self.get_neutron_credentials #needed keys for authentication auth_keys = ['auth_url', 'project_name', 'username', 'password'] conf = neutron_conf if conf and conf['keystone_authtoken'] and auth_keys.all?{|k| !conf['keystone_authtoken'][k].nil?} creds = Hash[ auth_keys.map \ { |k| [k, conf['keystone_authtoken'][k].strip] } ] if !conf['keystone_authtoken']['region_name'].nil? creds['region_name'] = conf['keystone_authtoken']['region_name'].strip end if !conf['keystone_authtoken']['project_domain_name'].nil? creds['project_domain_name'] = conf['keystone_authtoken']['project_domain_name'].strip else creds['project_domain_name'] = 'Default' end if !conf['keystone_authtoken']['user_domain_name'].nil? creds['user_domain_name'] = conf['keystone_authtoken']['user_domain_name'].strip else creds['user_domain_name'] = 'Default' end return creds else raise(Puppet::Error, "File: #{conf_filename} does not contain all " + "required sections. Neutron types will not work if neutron is not " + "correctly configured.") end end |
.get_neutron_resource_attrs(type, id) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/puppet/provider/neutron.rb', line 187 def self.get_neutron_resource_attrs(type, id) attrs = {} net = auth_neutron("#{type}-show", '--format=shell', id) if net.nil? raise(Puppet::ExecutionFailure, "Can't retrieve #{type}-show because Neutron or Keystone API is not available.") end last_key = nil (net.split("\n") || []).compact.collect do |line| if line.include? '=' k, v = line.split('=', 2) attrs[k] = v.gsub(/\A"|"\Z/, '') last_key = k else # Handle the case of a list of values v = line.gsub(/\A"|"\Z/, '') attrs[last_key] = [attrs[last_key], v].flatten end end return attrs end |
.get_tenant_id(catalog, name, domain = 'Default') ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/puppet/provider/neutron.rb', line 234 def self.get_tenant_id(catalog, name, domain='Default') instance_type = 'keystone_tenant' instance = catalog.resource("#{instance_type.capitalize!}[#{name}]") if ! instance instance = Puppet::Type.type(instance_type).instances.find do |i| # We need to check against the Default domain name because of # https://review.opendev.org/#/c/226919/ which changed the naming # format for the tenant to include <Domain name>. This should be # removed when we drop the resource without a domain name. # TODO(aschultz): remove ::domain lookup as part of M-cycle i.provider.name == name || i.provider.name == "#{name}::#{domain}" end end if instance return instance.provider.id else fail("Unable to find #{instance_type} for name #{name}") end end |
.list_neutron_resources(type) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/puppet/provider/neutron.rb', line 173 def self.list_neutron_resources(type) ids = [] list = cleanup_csv_with_id(auth_neutron("#{type}-list", '--format=csv', '--column=id', '--quote=none')) if list.nil? raise(Puppet::ExecutionFailure, "Can't retrieve #{type}-list because Neutron or Keystone API is not available.") end (list.split("\n")[1..-1] || []).compact.collect do |line| ids << line.strip end return ids end |
.list_router_ports(router_name_or_id) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/puppet/provider/neutron.rb', line 209 def self.list_router_ports(router_name_or_id) results = [] cmd_output = auth_neutron("router-port-list", '--format=csv', router_name_or_id) if ! cmd_output return results end headers = nil CSV.parse(cleanup_csv(cmd_output)) do |row| if headers == nil headers = row else result = Hash[*headers.zip(row).flatten] match_data = /.*"subnet_id": "(.*)", .*/.match(result['fixed_ips']) if match_data result['subnet_id'] = match_data[1] end results << result end end return results end |
.neutron_conf ⇒ Object
59 60 61 62 63 64 |
# File 'lib/puppet/provider/neutron.rb', line 59 def self.neutron_conf return @neutron_conf if @neutron_conf @neutron_conf = Puppet::Util::IniConfig::File.new @neutron_conf.read(conf_filename) @neutron_conf end |
.neutron_credentials ⇒ Object
66 67 68 |
# File 'lib/puppet/provider/neutron.rb', line 66 def self.neutron_credentials @neutron_credentials ||= get_neutron_credentials end |
.neutron_request(service, action, error, properties = nil) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/puppet/provider/neutron.rb', line 26 def self.neutron_request(service, action, error, properties=nil) properties ||= [] @credentials.username = neutron_credentials['username'] @credentials.password = neutron_credentials['password'] @credentials.project_name = neutron_credentials['project_name'] @credentials.auth_url = auth_endpoint @credentials.user_domain_name = neutron_credentials['user_domain_name'] @credentials.project_domain_name = neutron_credentials['project_domain_name'] if neutron_credentials['region_name'] @credentials.region_name = neutron_credentials['region_name'] end raise error unless @credentials.set? Puppet::Provider::Openstack.request(service, action, properties, @credentials) end |
.parse_creation_output(data) ⇒ Object
254 255 256 257 258 259 260 261 262 |
# File 'lib/puppet/provider/neutron.rb', line 254 def self.parse_creation_output(data) hash = {} data.split("\n").compact.each do |line| if line.include? '=' hash[line.split('=').first] = line.split('=', 2)[1].gsub(/\A"|"\Z/, '') end end hash end |
.request(service, action, properties = nil) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/puppet/provider/neutron.rb', line 18 def self.request(service, action, properties=nil) begin super rescue Puppet::Error::OpenstackAuthInputError => error neutron_request(service, action, error, properties) end end |
.reset ⇒ Object
168 169 170 171 |
# File 'lib/puppet/provider/neutron.rb', line 168 def self.reset @neutron_conf = nil @neutron_credentials = nil end |
.withenv(hash, &block) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/puppet/provider/neutron.rb', line 45 def self.withenv(hash, &block) saved = ENV.to_hash hash.each do |name, val| ENV[name.to_s] = val end yield ensure ENV.clear saved.each do |name, val| ENV[name] = val end end |
Instance Method Details
#auth_neutron(*args) ⇒ Object
164 165 166 |
# File 'lib/puppet/provider/neutron.rb', line 164 def auth_neutron(*args) self.class.auth_neutron(args) end |
#neutron_credentials ⇒ Object
70 71 72 |
# File 'lib/puppet/provider/neutron.rb', line 70 def neutron_credentials self.class.neutron_credentials end |