diff --git a/README.md b/README.md index 9cbb658b..73c413a1 100644 --- a/README.md +++ b/README.md @@ -260,6 +260,14 @@ knife ec2 ami list ``` :Allowed platform windows, ubuntu, debian, centos, fedora, rhel, nginx, turnkey, jumpbox, coreos, cisco, amazon, nessus ``` +- **Search:** + User can search any string into the description column by using -s or --search: + + **command:** knife ec2 ami list -s (search_keyword) + + ``` + :search_keyword Any String or number + ``` ### `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.** diff --git a/lib/chef/knife/ec2_ami_list.rb b/lib/chef/knife/ec2_ami_list.rb index 1899d1ab..548f5faf 100644 --- a/lib/chef/knife/ec2_ami_list.rb +++ b/lib/chef/knife/ec2_ami_list.rb @@ -46,13 +46,18 @@ class Ec2AmiList < Knife option :platform, :short => "-p PLATFORM", :long => "--platform PLATFORM", - :description => "Platform of the server. Allowed values are ubuntu, debian, centos, fedora, rhel, nginx, turnkey, jumpbox, coreos, cisco, amazon, nessus" + :description => "Platform of the server. Allowed values are windows, ubuntu, debian, centos, fedora, rhel, nginx, turnkey, jumpbox, coreos, cisco, amazon, nessus" - option :owner, + option :owner, :short => "-o OWNER", :long => "--owner OWNER", :description => "The server owner (self, aws-marketplace, microsoft). Default is aws-marketplace" + option :search, + :short => "-s SEARCH", + :long => "--search SEARCH", + :description => "Filter AMIs list as per search keywords." + def run $stdout.sync = true @@ -64,7 +69,8 @@ def run ui.color("Platform", :bold), ui.color("Architecture", :bold), ui.color("Size", :bold), - ui.color("Name", :bold) + ui.color("Name", :bold), + ui.color("Description", :bold) ].flatten.compact output_column_count = server_list.length @@ -74,28 +80,42 @@ def run rescue Exception => api_error raise api_error end + servers.body["imagesSet"].each do |server| - server_name = server["name"] - server["platform"] = find_server_platform(server_name) unless server["platform"] + server["platform"] = find_server_platform(server["name"]) unless server["platform"] - if locate_config_value(:platform) + if (locate_config_value(:platform) && locate_config_value(:search)) + locate_config_value(:search).downcase! + if (server["description"] && server["platform"] == locate_config_value(:platform) && server["description"].downcase.include?(locate_config_value(:search))) + server_list += get_server_list(server) + end + elsif locate_config_value(:platform) if server["platform"] == locate_config_value(:platform) - server_list << server["imageId"] - server_list << server["platform"] - server_list << server["architecture"] - server_list << server["blockDeviceMapping"].first["volumeSize"].to_s - server_list << server_name.split(/\W+/).first + server_list += get_server_list(server) + end + elsif locate_config_value(:search) + locate_config_value(:search).downcase! + if (server["description"] && server["description"].downcase.include?(locate_config_value(:search))) + server_list += get_server_list(server) end else - server_list << server["imageId"] - server_list << server["platform"] - server_list << server["architecture"] - server_list << server["blockDeviceMapping"].first["volumeSize"].to_s - server_list << server_name.split(/\W+/).first + server_list += get_server_list(server) end end puts ui.list(server_list, :uneven_columns_across, output_column_count) end + + private + + def get_server_list(server) + server_list = [] + server_list << server["imageId"] + server_list << server["platform"] + server_list << server["architecture"] + server_list << server["blockDeviceMapping"].first["volumeSize"].to_s + server_list << server["name"].split(/\W+/).first + server_list << server["description"] + end end end end diff --git a/lib/chef/knife/ec2_base.rb b/lib/chef/knife/ec2_base.rb index 5232c233..c352424e 100644 --- a/lib/chef/knife/ec2_base.rb +++ b/lib/chef/knife/ec2_base.rb @@ -222,13 +222,12 @@ def ini_parse(file) # All valid platforms def valid_platforms - ["ubuntu", "debian", "centos", "fedora", "rhel", "nginx", "turnkey", "jumpbox", "coreos", "cisco", "amazon", "nessus"] + ["windows", "ubuntu", "debian", "centos", "fedora", "rhel", "nginx", "turnkey", "jumpbox", "coreos", "cisco", "amazon", "nessus"] end # Get the platform from server name def find_server_platform(server_name) - available_platforms = valid_platforms - get_platform = available_platforms.select { |name| server_name.downcase.include?(name) } + get_platform = valid_platforms.select { |name| server_name.downcase.include?(name) } return get_platform.first end diff --git a/spec/unit/ec2_ami_list_spec.rb b/spec/unit/ec2_ami_list_spec.rb index 5174cf82..feb7b6a9 100644 --- a/spec/unit/ec2_ami_list_spec.rb +++ b/spec/unit/ec2_ami_list_spec.rb @@ -32,7 +32,7 @@ "deleteOnTermination"=>"true", "volumeType"=>"standard", "encrypted"=>"false"}], - 'description' => "DC for Quan", + 'description' => "window winrm", 'hypervisor' => "xen", 'imageId' => "ami-4ace6d23", 'imageLocation' => "microsoft/Windows_Server-2008-R2-SP1-English-64Bit-WebMatrix_Hosting-2012.06.12", @@ -80,7 +80,7 @@ "deleteOnTermination"=>"true", "volumeType"=>"standard", "encrypted"=>"false"}], - 'description' => "DC for Quan", + 'description' => "ubuntu 14.04", 'hypervisor' => "xen", 'imageId' => "ami-4ace6d29", 'imageOwnerAlias' => "aws-marketplace", @@ -133,7 +133,7 @@ allow(ec2_connection).to receive(:describe_images).and_return(@describe_images_format) expect(knife_ec2_ami_list).to receive(:validate!) images = ec2_connection.describe_images.body['imagesSet'] - output_column = ["AMI ID", "Platform", "Architecture", "Size", "Name"] + output_column = ["AMI ID", "Platform", "Architecture", "Size", "Name", "Description"] output_column_count = output_column.length images.each do |image| output_column << image["imageId"].to_s @@ -141,6 +141,7 @@ output_column << image["architecture"].to_s output_column << image["blockDeviceMapping"].first["volumeSize"].to_s output_column << image["name"].split(/\W+/).first + output_column << image["description"] end expect(knife_ec2_ami_list.ui).to receive(:list).with(output_column,:uneven_columns_across, output_column_count) knife_ec2_ami_list.run @@ -192,7 +193,7 @@ allow(ec2_connection).to receive(:describe_images).and_return(@describe_images_format) images = ec2_connection.describe_images.body['imagesSet'] expect(knife_ec2_ami_list).to receive(:validate!) - output_column = ["AMI ID", "Platform", "Architecture", "Size", "Name"] + output_column = ["AMI ID", "Platform", "Architecture", "Size", "Name", "Description"] output_column_count = output_column.length images.each do |image| output_column << image["imageId"].to_s @@ -200,6 +201,7 @@ output_column << image["architecture"].to_s output_column << image["blockDeviceMapping"].first["volumeSize"].to_s output_column << image["name"].split(/\W+/).first + output_column << image["description"] end expect(knife_ec2_ami_list.ui).to receive(:list).with(output_column,:uneven_columns_across, output_column_count) knife_ec2_ami_list.run @@ -212,13 +214,14 @@ allow(ec2_connection).to receive(:describe_images).and_return(@describe_images_format) window_image = ec2_connection.describe_images.body['imagesSet'].first expect(knife_ec2_ami_list).to receive(:validate!) - output_column = ["AMI ID", "Platform", "Architecture", "Size", "Name"] + output_column = ["AMI ID", "Platform", "Architecture", "Size", "Name", "Description"] output_column_count = output_column.length output_column << window_image["imageId"] output_column << window_image["platform"] output_column << window_image["architecture"] output_column << window_image["blockDeviceMapping"].first["volumeSize"].to_s output_column << window_image["name"].split(/\W+/).first + output_column << window_image["description"] expect(knife_ec2_ami_list.ui).to receive(:list).with(output_column,:uneven_columns_across, output_column_count) knife_ec2_ami_list.run end @@ -230,13 +233,14 @@ allow(ec2_connection).to receive(:describe_images).and_return(@describe_images_format) ubuntu_image = ec2_connection.describe_images.body['imagesSet'][1] expect(knife_ec2_ami_list).to receive(:validate!) - output_column = ["AMI ID", "Platform", "Architecture", "Size", "Name"] + output_column = ["AMI ID", "Platform", "Architecture", "Size", "Name", "Description"] output_column_count = output_column.length output_column << ubuntu_image["imageId"] output_column << ubuntu_image["name"].split(/\W+/).first output_column << ubuntu_image["architecture"] output_column << ubuntu_image["blockDeviceMapping"].first["volumeSize"].to_s output_column << ubuntu_image["name"].split(/\W+/).first + output_column << ubuntu_image["description"] expect(knife_ec2_ami_list.ui).to receive(:list).with(output_column,:uneven_columns_across, output_column_count) knife_ec2_ami_list.run end @@ -248,13 +252,14 @@ allow(ec2_connection).to receive(:describe_images).and_return(@describe_images_format) expect(knife_ec2_ami_list).to receive(:validate!) fedora_image = ec2_connection.describe_images.body['imagesSet'].last - output_column = ["AMI ID", "Platform", "Architecture", "Size", "Name"] + output_column = ["AMI ID", "Platform", "Architecture", "Size", "Name", "Description"] output_column_count = output_column.length output_column << fedora_image["imageId"] output_column << fedora_image["name"].split(/\W+/).first output_column << fedora_image["architecture"] output_column << fedora_image["blockDeviceMapping"].first["volumeSize"].to_s output_column << fedora_image["name"].split(/\W+/).first + output_column << fedora_image["description"] expect(knife_ec2_ami_list.ui).to receive(:list).with(output_column,:uneven_columns_across, output_column_count) knife_ec2_ami_list.run end @@ -265,7 +270,86 @@ knife_ec2_ami_list.config[:platform] = 'xyz' knife_ec2_ami_list.config[:use_iam_profile] = true knife_ec2_ami_list.config[:owner] = true - expect{ knife_ec2_ami_list.validate! }.to raise_error "Invalid platform: #{knife_ec2_ami_list.config[:platform]}. Allowed platforms are: ubuntu, debian, centos, fedora, rhel, nginx, turnkey, jumpbox, coreos, cisco, amazon, nessus." + expect{ knife_ec2_ami_list.validate! }.to raise_error "Invalid platform: #{knife_ec2_ami_list.config[:platform]}. Allowed platforms are: windows, ubuntu, debian, centos, fedora, rhel, nginx, turnkey, jumpbox, coreos, cisco, amazon, nessus." + end + end + end + + context 'when --search is passed' do + before do + allow(knife_ec2_ami_list.ui).to receive(:warn) + allow(knife_ec2_ami_list).to receive(:custom_warnings!) + end + + context 'When search key word is present in description' do + it 'shows only AMIs List that have 14.04 in description' do + knife_ec2_ami_list.config[:search] = '14.04' + allow(ec2_connection).to receive(:describe_images).and_return(@describe_images_format) + image = ec2_connection.describe_images.body['imagesSet'][2] + expect(knife_ec2_ami_list).to receive(:validate!) + output_column = ["AMI ID", "Platform", "Architecture", "Size", "Name", "Description"] + output_column_count = output_column.length + output_column << image["imageId"] + output_column << image["name"].split(/\W+/).first + output_column << image["architecture"] + output_column << image["blockDeviceMapping"].first["volumeSize"].to_s + output_column << image["name"].split(/\W+/).first + output_column << image["description"] + expect(knife_ec2_ami_list.ui).to receive(:list).with(output_column,:uneven_columns_across, output_column_count) + knife_ec2_ami_list.run + end + end + + context 'When user pass platform and search keyword' do + it 'shows only AMIs List that have 14.04 in description and platform is ubuntu' do + knife_ec2_ami_list.config[:platform] = 'ubuntu' + knife_ec2_ami_list.config[:search] = 'Quan' + allow(ec2_connection).to receive(:describe_images).and_return(@describe_images_format) + ubuntu_image = ec2_connection.describe_images.body['imagesSet'][1] + expect(knife_ec2_ami_list).to receive(:validate!) + output_column = ["AMI ID", "Platform", "Architecture", "Size", "Name", "Description"] + output_column_count = output_column.length + output_column << ubuntu_image["imageId"] + output_column << ubuntu_image["name"].split(/\W+/).first + output_column << ubuntu_image["architecture"] + output_column << ubuntu_image["blockDeviceMapping"].first["volumeSize"].to_s + output_column << ubuntu_image["name"].split(/\W+/).first + output_column << ubuntu_image["description"] + expect(knife_ec2_ami_list.ui).to receive(:list).with(output_column,:uneven_columns_across, output_column_count) + knife_ec2_ami_list.run + end + end + + context 'When user pass owner, platform and search keyword' do + it 'shows only AMIs List that owner microsoft platform windows and search keyword is winrm' do + knife_ec2_ami_list.config[:owner] = 'microsoft' + knife_ec2_ami_list.config[:platform] = 'windows' + knife_ec2_ami_list.config[:search] = 'winrm' + allow(ec2_connection).to receive(:describe_images).and_return(@describe_images_format) + ubuntu_image = ec2_connection.describe_images.body['imagesSet'].first + expect(knife_ec2_ami_list).to receive(:validate!) + output_column = ["AMI ID", "Platform", "Architecture", "Size", "Name", "Description"] + output_column_count = output_column.length + output_column << ubuntu_image["imageId"] + output_column << ubuntu_image["platform"] + output_column << ubuntu_image["architecture"] + output_column << ubuntu_image["blockDeviceMapping"].first["volumeSize"].to_s + output_column << ubuntu_image["name"].split(/\W+/).first + output_column << ubuntu_image["description"] + expect(knife_ec2_ami_list.ui).to receive(:list).with(output_column,:uneven_columns_across, output_column_count) + knife_ec2_ami_list.run + end + end + + context 'When search key word is not present in description' do + it 'Fetch no AMI' do + knife_ec2_ami_list.config[:search] = 'Not present' + allow(ec2_connection).to receive(:describe_images).and_return(@describe_images_format) + expect(knife_ec2_ami_list).to receive(:validate!) + output_column = ["AMI ID", "Platform", "Architecture", "Size", "Name", "Description"] + output_column_count = output_column.length + expect(knife_ec2_ami_list.ui).to receive(:list).with(output_column,:uneven_columns_across, output_column_count) + knife_ec2_ami_list.run end end end