Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Knife-ec2 server list doesn't show instance name #663

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/chef/knife/ec2_server_delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def server_hashes
server_data[id] = i.instances[0].send(id)
end

server_data["name"] = i.instances[0].tags[0].value
server_data["name"] = find_name_tag(i.instances[0].tags)
server_data["az"] = i.instances[0].placement.availability_zone
server_data["iam_instance_profile"] = ( i.instances[0].iam_instance_profile.nil? ? nil : i.instances[0].iam_instance_profile.arn[%r{instance-profile/(.*)}] )
server_data["security_groups"] = i.instances[0].security_groups.map(&:group_name).join(", ")
Expand Down
2 changes: 1 addition & 1 deletion lib/chef/knife/ec2_server_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def server_hashes
tags = extract_tags(i.instances[0].tags)

if config[:name]
server_data["name"] = tags[0]
server_data["name"] = find_name_tag(i.instances[0].tags)
end

if config[:az]
Expand Down
9 changes: 8 additions & 1 deletion lib/chef/knife/helpers/ec2_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def server_hashes(server_obj)
server_data["id"] = server_data["instance_id"]

tags = server_obj.instances[0].tags.map(&:value)
server_data["name"] = tags[0]
server_data["name"] = find_name_tag(server_obj.instances[0].tags)
server_data["placement_group"] = server_obj.instances[0].placement.group_name
server_data["security_groups"] = server_obj.instances[0].security_groups.map(&:group_name)
server_data["security_group_ids"] = server_obj.instances[0].security_groups.map(&:group_id)
Expand Down Expand Up @@ -181,6 +181,13 @@ def ami
@ami ||= fetch_ami(config[:image])
end

# Name tag value return.
# @return [String]
def find_name_tag(tags)
name_tag = tags.find { |tag| tag[:key] == "Name" }
name_tag ? name_tag[:value] : nil
end

# Platform value return for Windows AMIs; otherwise, it is blank.
# @return [Boolean]
def is_image_windows?
Expand Down
57 changes: 43 additions & 14 deletions spec/unit/ec2_server_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,25 @@
let(:knife_ec2_list) { Chef::Knife::Ec2ServerList.new }
let(:ebs) { OpenStruct.new(volume_size: 30) }
let(:block_device_mappings) { OpenStruct.new(ebs: ebs) }
let(:contact_tag) { OpenStruct.new(key: "X-Contact", value: "test-contact") }
let(:created_by_tag) { OpenStruct.new(key: "CreatedBy", value: "test-created-by") }
let(:name_tag) { OpenStruct.new(key: "Name", value: "ec2-test") }
let(:tags) { [contact_tag, created_by_tag, name_tag] }
let(:state) { OpenStruct.new(code: 16, name: "running") }
let(:security_groups) { OpenStruct.new(group_name: "kitchen-fabcfy4np", group_id: "sg-09cfab6b31g7t5285") }
let(:placement) { OpenStruct.new(availability_zone: "us-east-2c") }

let(:instance1) do
OpenStruct.new(
architecture: "x86_64",
image_id: "ami-005bdb005fb00e791",
instance_id: "i-00fe186450a2e8e97",
instance_type: "t2.micro",
platform: "windows",
name: "image-test",
tags: tags,
state: state,
security_groups: [security_groups],
placement: placement,
description: "test windows winrm image",
block_device_mappings: [block_device_mappings]
)
Expand Down Expand Up @@ -59,7 +70,7 @@
end

let(:server_instances) { OpenStruct.new(instances: [instance1, instance2, instance3]) }
let(:ec2_servers) { OpenStruct.new(reservations: server_instances) }
let(:ec2_servers) { OpenStruct.new(reservations: [server_instances]) }
let(:ec2_connection) { Aws::EC2::Client.new(stub_responses: { describe_instances: ec2_servers }) }

before (:each) do
Expand Down Expand Up @@ -91,12 +102,12 @@
end

it "shows the output without Tags and Availability Zone in summary format" do
output_column = ["Instance ID", "Public IP", "Private IP", "Flavor",
"Image", "SSH Key", "Security Groups", "State"]
output_column = ["Instance ID", "Public IP", "Private IP", "Flavor", "Image", "SSH Key", "Security Groups", "State"]
servers_list = ["i-00fe186450a2e8e97", nil, nil, "t2.micro", "ami-005bdb005fb00e791", nil, "kitchen-fabcfy4np", "running"]
output_column_count = output_column.length
allow(ec2_connection).to receive(:describe_instances).and_return(ec2_servers)
allow(knife_ec2_list).to receive(:validate_aws_config!)
expect(knife_ec2_list.ui).to receive(:list).with(output_column, :uneven_columns_across, output_column_count)
expect(knife_ec2_list.ui).to receive(:list).with(output_column + servers_list, :uneven_columns_across, output_column_count)
knife_ec2_list.run
end
end
Expand Down Expand Up @@ -128,21 +139,21 @@
context "when single tag is passed" do
it "shows single tag field in the output" do
knife_ec2_list.config[:tags] = "tag1"
output_column = ["Instance ID", "Public IP", "Private IP", "Flavor",
"Image", "SSH Key", "Security Groups", "Tag:tag1", "State"]
output_column = ["Instance ID", "Public IP", "Private IP", "Flavor", "Image", "SSH Key", "Security Groups", "Tag:tag1", "State"]
servers_list = ["i-00fe186450a2e8e97", nil, nil, "t2.micro", "ami-005bdb005fb00e791", nil, "kitchen-fabcfy4np", nil, "running"]
output_column_count = output_column.length
expect(knife_ec2_list.ui).to receive(:list).with(output_column, :uneven_columns_across, output_column_count)
expect(knife_ec2_list.ui).to receive(:list).with(output_column + servers_list, :uneven_columns_across, output_column_count)
knife_ec2_list.run
end
end

context "when multiple tags are passed" do
it "shows multiple tags fields in the output" do
knife_ec2_list.config[:tags] = "tag1,tag2"
output_column = ["Instance ID", "Public IP", "Private IP", "Flavor",
"Image", "SSH Key", "Security Groups", "Tag:tag1", "Tag:tag2", "State"]
output_column = ["Instance ID", "Public IP", "Private IP", "Flavor", "Image", "SSH Key", "Security Groups", "Tag:tag1", "Tag:tag2", "State"]
servers_list = ["i-00fe186450a2e8e97", nil, nil, "t2.micro", "ami-005bdb005fb00e791", nil, "kitchen-fabcfy4np", nil, nil, "running"]
output_column_count = output_column.length
expect(knife_ec2_list.ui).to receive(:list).with(output_column, :uneven_columns_across, output_column_count)
expect(knife_ec2_list.ui).to receive(:list).with(output_column + servers_list, :uneven_columns_across, output_column_count)
knife_ec2_list.run
end
end
Expand All @@ -158,10 +169,28 @@

it "shows the availability zones in the output" do
knife_ec2_list.config[:az] = true
output_column = ["Instance ID", "Public IP", "Private IP", "Flavor", "AZ",
"Image", "SSH Key", "Security Groups", "State"]
output_column = ["Instance ID", "Public IP", "Private IP", "Flavor", "AZ", "Image", "SSH Key", "Security Groups", "State"]
servers_list = ["i-00fe186450a2e8e97", nil, nil, "t2.micro", "us-east-2c", "ami-005bdb005fb00e791", nil, "kitchen-fabcfy4np", "running"]
output_column_count = output_column.length
expect(knife_ec2_list.ui).to receive(:list).with(output_column + servers_list, :uneven_columns_across, output_column_count)
knife_ec2_list.run
end
end

context "sets the Name tag by default" do
before do
knife_ec2_list.config[:format] = "summary"
allow(knife_ec2_list.ui).to receive(:warn)
allow(ec2_connection).to receive(:servers).and_return([])
allow(knife_ec2_list).to receive(:validate_aws_config!)
end

it "shows the name in the output" do
knife_ec2_list.config[:name] = true
output_column = ["Instance ID", "Name", "Public IP", "Private IP", "Flavor", "Image", "SSH Key", "Security Groups", "State"]
servers_list = ["i-00fe186450a2e8e97", "ec2-test", nil, nil, "t2.micro", "ami-005bdb005fb00e791", nil, "kitchen-fabcfy4np", "running"]
output_column_count = output_column.length
expect(knife_ec2_list.ui).to receive(:list).with(output_column, :uneven_columns_across, output_column_count)
expect(knife_ec2_list.ui).to receive(:list).with(output_column + servers_list, :uneven_columns_across, output_column_count)
knife_ec2_list.run
end
end
Expand Down