Skip to content

Commit

Permalink
Added new column description in AMI list also allow user to search an…
Browse files Browse the repository at this point in the history
…y string in description.

Signed-off-by: Piyush <piyush.awasthi@msystechnologies.com>
  • Loading branch information
piyushawasthi committed Feb 17, 2017
1 parent ba1059f commit 13c34b5
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 27 deletions.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -256,6 +256,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.**
Expand Down
52 changes: 36 additions & 16 deletions lib/chef/knife/ec2_ami_list.rb
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
5 changes: 2 additions & 3 deletions lib/chef/knife/ec2_base.rb
Expand Up @@ -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

Expand Down
100 changes: 92 additions & 8 deletions spec/unit/ec2_ami_list_spec.rb
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -133,14 +133,15 @@
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
output_column << (image["platform"] ? image["platform"] : image["name"].split(/\W+/).first)
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
Expand Down Expand Up @@ -192,14 +193,15 @@
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
output_column << (image["platform"] ? image["platform"] : image["name"].split(/\W+/).first)
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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 13c34b5

Please sign in to comment.