Skip to content

Instantly share code, notes, and snippets.

@richm
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richm/66b6e2e0388284dee7d3 to your computer and use it in GitHub Desktop.
Save richm/66b6e2e0388284dee7d3 to your computer and use it in GitHub Desktop.
diff --git a/lib/puppet/provider/openstack.rb b/lib/puppet/provider/openstack.rb
index 4fcd377..2be4d5b 100644
--- a/lib/puppet/provider/openstack.rb
+++ b/lib/puppet/provider/openstack.rb
@@ -1,4 +1,5 @@
require 'puppet'
+require 'csv'
class Puppet::Error::OpenstackRequestError < Puppet::Error
end
@@ -40,17 +41,20 @@ class Puppet::Provider::Openstack < Puppet::Provider
def self.authenticate_request(service, action, *args)
if(action == 'list')
response = openstack(service, action, '--long', '--quiet', '--format', 'csv', args)
- response = response.to_s.gsub(/"/,'').split("\n")
- keys = response.delete_at(0) # ID,Name,Description,Enabled
- keys = keys.split(',')
- response.collect do |line|
+ # returns a list of hashes e.g.
+ # [{"ID"=>"ababa", "Name"=>"cinder"}, {"ID"=>"bababa", "Name"=>"glance"}]
+ resplist = []
+ headerrow = []
+ CSV.parse(response) do |row|
hash = {}
- line = line.split(',')
- keys.each_index do |index|
- hash[keys[index].downcase.to_sym] = line[index]
+ if headerrow.empty?
+ headerrow = row
+ else
+ headerrow.zip(row) {|arr| hash[arr[0]] = arr[1]}
+ resplist << hash
end
- hash
end
+ resplist
else
openstack(service, action, args)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment