Skip to content

Commit

Permalink
Merge pull request #442 from MsysTechnologiesllc/db/flavor_list_suppo…
Browse files Browse the repository at this point in the history
…rt_in_json_format

Adding support for json format output in flavor list
  • Loading branch information
Nimisha Sharad authored Aug 17, 2016
2 parents 074c80a + 3a2c53b commit 5848afa
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 9 deletions.
23 changes: 14 additions & 9 deletions lib/chef/knife/ec2_flavor_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,20 @@ def run
ui.color('Disk', :bold),
ui.color('Cores', :bold)
]
connection.flavors.sort_by(&:id).each do |flavor|
flavor_list << flavor.id.to_s
flavor_list << flavor.name
flavor_list << "#{flavor.bits.to_s}-bit"
flavor_list << "#{flavor.ram.to_s}"
flavor_list << "#{flavor.disk.to_s} GB"
flavor_list << flavor.cores.to_s
end
puts ui.list(flavor_list, :columns_across, 6)
flavors = connection.flavors.sort_by(&:id)
if (config[:format] == 'summary')
flavors.each do |flavor|
flavor_list << flavor.id.to_s
flavor_list << flavor.name
flavor_list << "#{flavor.bits.to_s}-bit"
flavor_list << "#{flavor.ram.to_s}"
flavor_list << "#{flavor.disk.to_s} GB"
flavor_list << flavor.cores.to_s
end
puts ui.list(flavor_list, :columns_across, 6)
else
output(format_for_display(flavors))
end
end
end
end
Expand Down
74 changes: 74 additions & 0 deletions spec/unit/ec2_flavor_list_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# License:: Apache License, Version 2.0
#
# 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.
#

require File.expand_path('../../spec_helper', __FILE__)
require 'fog/aws'
require 'chef/knife/ec2_flavor_list'

describe Chef::Knife::Ec2FlavorList do

describe '#run' do
let(:knife_flavor_list) { Chef::Knife::Ec2FlavorList.new }
let(:ec2_connection) { double(Fog::Compute::AWS) }
before do
allow(knife_flavor_list).to receive(:connection).and_return(ec2_connection)
@flavor1 = double("flavor1", :name => "High-CPU Medium", :architecture => "32-bit-bit", :id => "c1.medium", :bits => "32-bit", :cores => "5", :disk => "1740.8 GB", :ram => "350 GB", :ebs_optimized_available => "false", :instance_store_volumes => "0")

allow(ec2_connection).to receive(:flavors).and_return([@flavor1])

end

it 'invokes validate!' do
ec2_flavors = double(:sort_by => [])

allow(ec2_connection).to receive(:flavors).and_return(ec2_flavors)
allow(knife_flavor_list.ui).to receive(:warn)
expect(knife_flavor_list).to receive(:validate!)
knife_flavor_list.run
end


context '--format option' do
context 'when format=summary' do
before do
@output_s=["ID", "Name", "Architecture", "RAM", "Disk", "Cores", "c1.medium", "High-CPU Medium", "32-bit-bit", "350 GB", "1740.8 GB GB", "5"]
knife_flavor_list.config[:format] = 'summary'
allow(knife_flavor_list.ui).to receive(:warn)
allow(knife_flavor_list).to receive(:validate!)
end

it 'shows the output in summary format' do
expect(knife_flavor_list.ui).to receive(:list).with(@output_s,:columns_across,6)
knife_flavor_list.run
end
end

context 'when format=json' do
before do
knife_flavor_list.config[:format] = 'json'
allow(knife_flavor_list.ui).to receive(:warn)
end

it 'shows the output in json format' do
allow(ec2_connection).to receive(:flavors).and_return([])
allow(knife_flavor_list).to receive(:validate!)
allow(knife_flavor_list).to receive(:format_for_display)
expect(knife_flavor_list).to receive(:output)
knife_flavor_list.run
end
end
end
end
end

0 comments on commit 5848afa

Please sign in to comment.