2014-01-28 11:27:54 +00:00
|
|
|
# encoding: UTF-8
|
|
|
|
|
2013-05-20 14:39:57 -04:00
|
|
|
#
|
|
|
|
# Cookbook Name:: openstack-common
|
|
|
|
# library:: parse
|
|
|
|
#
|
|
|
|
# Copyright 2013, Craig Tracey <craigtracey@gmail.com>
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2015-05-28 12:11:23 -05:00
|
|
|
# Parse methods
|
|
|
|
module ::Openstack
|
2013-05-20 14:39:57 -04:00
|
|
|
# The current state of (at least some) OpenStack CLI tools do not provide a
|
|
|
|
# mechanism for outputting data in formats other than PrettyTable output.
|
|
|
|
# Therefore this function is intended to parse PrettyTable output into a
|
|
|
|
# usable array of hashes. Similarly, it will flatten Property/Value tables
|
|
|
|
# into a single element array.
|
|
|
|
# table - the raw PrettyTable output of the CLI command
|
|
|
|
# output - array of hashes representing the data.
|
2014-01-28 11:27:54 +00:00
|
|
|
def prettytable_to_array(table) # rubocop:disable MethodLength
|
2013-05-20 14:39:57 -04:00
|
|
|
ret = []
|
2014-01-28 11:27:54 +00:00
|
|
|
return ret if table.nil?
|
2013-05-20 14:39:57 -04:00
|
|
|
indicies = []
|
2014-01-28 11:27:54 +00:00
|
|
|
(table.split(/$/).map { |x| x.strip }).each do |line|
|
|
|
|
unless line.start_with?('+--') || line.empty?
|
|
|
|
cols = line.split('|').map { |x| x.strip }
|
2013-05-20 14:39:57 -04:00
|
|
|
cols.shift
|
|
|
|
if indicies == []
|
|
|
|
indicies = cols
|
|
|
|
next
|
|
|
|
end
|
|
|
|
newobj = {}
|
2014-01-28 11:27:54 +00:00
|
|
|
cols.each { |val| newobj[indicies[newobj.length]] = val }
|
2013-05-20 14:39:57 -04:00
|
|
|
ret.push(newobj)
|
|
|
|
end
|
2014-01-28 11:27:54 +00:00
|
|
|
end
|
2013-05-20 14:39:57 -04:00
|
|
|
|
|
|
|
# this kinda sucks, but some prettytable data comes
|
|
|
|
# as Property Value pairs. If this is the case, then
|
|
|
|
# flatten it as expected.
|
|
|
|
newobj = {}
|
|
|
|
if indicies == ['Property', 'Value']
|
2014-01-28 11:27:54 +00:00
|
|
|
ret.each { |x| newobj[x['Property']] = x['Value'] }
|
2013-05-20 14:39:57 -04:00
|
|
|
[newobj]
|
|
|
|
else
|
|
|
|
ret
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|