diff --git a/README.md b/README.md index 73c413a1..c501800e 100644 --- a/README.md +++ b/README.md @@ -272,6 +272,9 @@ knife ec2 ami list ### `knife ec2 server list` Outputs a list of all servers in the currently configured AWS account. **Note, this shows all instances associated with the account, some of which may not be currently managed by the Chef server.** +### `knife ec2 flavor list` +Outputs a list of all instance types comprising varying combinations of CPU, memory, storage, and architecture capacity of the currently configured AWS account. **Note, this shows all instances type associated with the account.** + ### `knife ec2 server delete` Deletes an existing server in the currently configured AWS account. **By default, this does not delete the associated node and client objects from the Chef server. To do so, add the `--purge` flag** diff --git a/lib/chef/knife/ec2_flavor_list.rb b/lib/chef/knife/ec2_flavor_list.rb index 6e7d4f13..a7754a80 100644 --- a/lib/chef/knife/ec2_flavor_list.rb +++ b/lib/chef/knife/ec2_flavor_list.rb @@ -29,6 +29,7 @@ class Ec2FlavorList < Knife def run validate! + custom_warnings! flavor_list = [ ui.color('ID', :bold), @@ -37,8 +38,16 @@ def run ui.color('RAM', :bold), ui.color('Disk', :bold), ui.color('Cores', :bold) - ] - flavors = connection.flavors.sort_by(&:id) + ].flatten.compact + + output_column_count = flavor_list.length + + begin + flavors = connection.flavors.sort_by(&:id) + rescue Exception => api_error + raise api_error + end + if (config[:format] == 'summary') flavors.each do |flavor| flavor_list << flavor.id.to_s @@ -48,10 +57,10 @@ def run flavor_list << "#{flavor.disk.to_s} GB" flavor_list << flavor.cores.to_s end - puts ui.list(flavor_list, :columns_across, 6) - else + puts ui.list(flavor_list, :uneven_columns_across, output_column_count) + else output(format_for_display(flavors)) - end + end end end end diff --git a/spec/unit/ec2_flavor_list_spec.rb b/spec/unit/ec2_flavor_list_spec.rb index 23c79519..ff699219 100644 --- a/spec/unit/ec2_flavor_list_spec.rb +++ b/spec/unit/ec2_flavor_list_spec.rb @@ -24,7 +24,7 @@ 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") + @flavor1 = double("flavor1", :name => "High-CPU Medium", :architecture => "32", :id => "c1.medium", :bits => "32", :cores => "5", :ram => "1740.8", :disk => "350", :ebs_optimized_available => "false", :instance_store_volumes => "0") allow(ec2_connection).to receive(:flavors).and_return([@flavor1]) @@ -39,18 +39,29 @@ knife_flavor_list.run end + context 'when region is not specified' do + it 'shows warning that default region will be will be used' do + knife_flavor_list.config.delete(:region) + Chef::Config[:knife].delete(:region) + ec2_flavors = double(:sort_by => []) + allow(ec2_connection).to receive(:flavors).and_return(ec2_flavors) + allow(knife_flavor_list).to receive(:validate!) + expect(knife_flavor_list.ui).to receive(:warn).with("No region was specified in knife.rb or as an argument. The default region, us-east-1, will be used:") + knife_flavor_list.run + end + 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"] + @output_s=["ID", "Name", "Architecture", "RAM", "Disk", "Cores", "c1.medium", "High-CPU Medium", "32-bit", "1740.8", "350 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) + expect(knife_flavor_list.ui).to receive(:list).with(@output_s, :uneven_columns_across, 6) knife_flavor_list.run end end