From 9cf6f84a8a983eb41ea1f445387ff0f9f343e2d1 Mon Sep 17 00:00:00 2001 From: nimisha Date: Fri, 19 Jun 2015 18:25:31 +0530 Subject: [PATCH 1/2] Support --format option for group list command --- lib/chef/knife/openstack_group_list.rb | 41 +++++++++++++++----------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/lib/chef/knife/openstack_group_list.rb b/lib/chef/knife/openstack_group_list.rb index d606519f..9d67a79c 100644 --- a/lib/chef/knife/openstack_group_list.rb +++ b/lib/chef/knife/openstack_group_list.rb @@ -22,27 +22,32 @@ def query_resource end def list(security_groups) - group_list = [ - ui.color('Name', :bold), - ui.color('Protocol', :bold), - ui.color('From', :bold), - ui.color('To', :bold), - ui.color('CIDR', :bold), - ui.color('Description', :bold), - ] - security_groups.sort_by(&:name).each do |group| - group.security_group_rules.each do |rule| - unless rule.ip_protocol.nil? - group_list << group.name - group_list << rule.ip_protocol - group_list << rule.from_port.to_s - group_list << rule.to_port.to_s - group_list << rule.ip_range['cidr'] - group_list << group.description + if(config[:format] == "summary") + group_list = [ + ui.color('Name', :bold), + ui.color('Protocol', :bold), + ui.color('From', :bold), + ui.color('To', :bold), + ui.color('CIDR', :bold), + ui.color('Description', :bold), + ] + security_groups.sort_by(&:name).each do |group| + group.security_group_rules.each do |rule| + unless rule.ip_protocol.nil? + group_list << group.name + group_list << rule.ip_protocol + group_list << rule.from_port.to_s + group_list << rule.to_port.to_s + group_list << rule.ip_range['cidr'] + group_list << group.description + end end end + + puts ui.list(group_list, :uneven_columns_across, 6) + else + output(format_for_display(security_groups)) end - puts ui.list(group_list, :uneven_columns_across, 6) end end end From 3683cbfe7ee7053a263e2ccb20dc13a5cb6f1ad5 Mon Sep 17 00:00:00 2001 From: nimisha Date: Mon, 22 Jun 2015 16:18:46 +0530 Subject: [PATCH 2/2] fixed specs --- spec/functional/flavor_list_func_spec.rb | 1 + spec/functional/group_list_func_spec.rb | 1 + spec/functional/image_list_func_spec.rb | 1 + spec/functional/network_list_func_spec.rb | 1 + spec/functional/server_list_func_spec.rb | 1 + spec/unit/openstack_group_list_spec.rb | 1 + 6 files changed, 6 insertions(+) diff --git a/spec/functional/flavor_list_func_spec.rb b/spec/functional/flavor_list_func_spec.rb index 8ffd3c1b..2c5432cc 100644 --- a/spec/functional/flavor_list_func_spec.rb +++ b/spec/functional/flavor_list_func_spec.rb @@ -33,6 +33,7 @@ allow(instance).to receive(:puts) allow(instance).to receive(:create_service_instance).and_return(Chef::Knife::Cloud::Service.new) allow(instance).to receive(:validate!) + instance.config[:format] = "summary" end it "lists formatted list of resources" do diff --git a/spec/functional/group_list_func_spec.rb b/spec/functional/group_list_func_spec.rb index 7a321946..f11c9cae 100644 --- a/spec/functional/group_list_func_spec.rb +++ b/spec/functional/group_list_func_spec.rb @@ -55,6 +55,7 @@ allow(instance).to receive(:puts) allow(instance).to receive(:create_service_instance).and_return(Chef::Knife::Cloud::Service.new) allow(instance).to receive(:validate!) + instance.config[:format] = "summary" end it "lists formatted list of resources" do diff --git a/spec/functional/image_list_func_spec.rb b/spec/functional/image_list_func_spec.rb index 1e80983e..c9d9ab4f 100644 --- a/spec/functional/image_list_func_spec.rb +++ b/spec/functional/image_list_func_spec.rb @@ -32,6 +32,7 @@ allow(instance).to receive(:puts) allow(instance).to receive(:create_service_instance).and_return(Chef::Knife::Cloud::Service.new) allow(instance).to receive(:validate!) + instance.config[:format] = "summary" end it "displays formatted list of images, filtered by default" do diff --git a/spec/functional/network_list_func_spec.rb b/spec/functional/network_list_func_spec.rb index 73e9ec47..ee32252f 100644 --- a/spec/functional/network_list_func_spec.rb +++ b/spec/functional/network_list_func_spec.rb @@ -32,6 +32,7 @@ allow(instance).to receive(:puts) allow(instance).to receive(:create_service_instance).and_return(Chef::Knife::Cloud::Service.new) allow(instance).to receive(:validate!) + instance.config[:format] = "summary" end it "lists formatted list of network resources" do diff --git a/spec/functional/server_list_func_spec.rb b/spec/functional/server_list_func_spec.rb index 260ad619..126dbe28 100644 --- a/spec/functional/server_list_func_spec.rb +++ b/spec/functional/server_list_func_spec.rb @@ -35,6 +35,7 @@ allow(instance).to receive(:puts) allow(instance).to receive(:create_service_instance).and_return(Chef::Knife::Cloud::FogService.new) allow(instance).to receive(:validate!) + instance.config[:format] = "summary" end it "lists formatted list of resources" do diff --git a/spec/unit/openstack_group_list_spec.rb b/spec/unit/openstack_group_list_spec.rb index e22ee7cc..b5d89a25 100644 --- a/spec/unit/openstack_group_list_spec.rb +++ b/spec/unit/openstack_group_list_spec.rb @@ -32,6 +32,7 @@ context "#list" do before(:each) do @security_groups = [TestResource.new({ "name" => "Unrestricted","description" => "testdescription", "security_group_rules" => [TestResource.new({"from_port"=>636, "group"=>{}, "ip_protocol"=>"tcp", "to_port"=>636, "parent_group_id"=>14, "ip_range"=>{"cidr"=>"0.0.0.0/0"}, "id"=>183})]})] + instance.config[:format] = "summary" end it "returns group list" do