From 62ec041fce5f8fbf84ecde90b5ba4de71285ef2c Mon Sep 17 00:00:00 2001 From: prabhu-das Date: Tue, 27 May 2014 18:51:54 +0530 Subject: [PATCH 1/5] Added networkList class. --- lib/chef/knife/openstack_network_list.rb | 29 ++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 lib/chef/knife/openstack_network_list.rb diff --git a/lib/chef/knife/openstack_network_list.rb b/lib/chef/knife/openstack_network_list.rb new file mode 100644 index 00000000..ba77a320 --- /dev/null +++ b/lib/chef/knife/openstack_network_list.rb @@ -0,0 +1,29 @@ +# Author:: Prabhu Das () +# Copyright:: Copyright (c) 2014 Chef Software, Inc. + +require 'chef/knife/cloud/list_resource_command' +require 'chef/knife/openstack_helpers' +require 'chef/knife/cloud/openstack_service_options' + +class Chef + class Knife + class Cloud + class OpenstackNetworkList < ResourceListCommand + include OpenstackHelpers + include OpenstackServiceOptions + + banner "knife openstack network list (options)" + + def before_exec_command + #set columns_with_info map + @columns_with_info = [] + end + + def query_resource + @service.list_networks + end + + end + end + end +end \ No newline at end of file From b82bfe83052c23c68a59d7ce71512038a9310f5e Mon Sep 17 00:00:00 2001 From: prabhu-das Date: Tue, 27 May 2014 19:05:58 +0530 Subject: [PATCH 2/5] Added network list columns options --- lib/chef/knife/openstack_network_list.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/chef/knife/openstack_network_list.rb b/lib/chef/knife/openstack_network_list.rb index ba77a320..30b559ee 100644 --- a/lib/chef/knife/openstack_network_list.rb +++ b/lib/chef/knife/openstack_network_list.rb @@ -16,7 +16,12 @@ class OpenstackNetworkList < ResourceListCommand def before_exec_command #set columns_with_info map - @columns_with_info = [] + @columns_with_info = [ + {:label => 'ID', :key => 'id'}, + {:label => 'Name', :key => 'name'}, + {:label => 'Tenant', :key => 'tenant_id'}, + {:label => 'Shared', :key => 'shared'} + ] end def query_resource From a451ebb799d0348e63f68faa3b09f9c77fb9c693 Mon Sep 17 00:00:00 2001 From: prabhu-das Date: Thu, 29 May 2014 12:29:32 +0530 Subject: [PATCH 3/5] Reordered the network list field sequence. --- lib/chef/knife/openstack_network_list.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/chef/knife/openstack_network_list.rb b/lib/chef/knife/openstack_network_list.rb index 30b559ee..7e009acd 100644 --- a/lib/chef/knife/openstack_network_list.rb +++ b/lib/chef/knife/openstack_network_list.rb @@ -17,8 +17,8 @@ class OpenstackNetworkList < ResourceListCommand def before_exec_command #set columns_with_info map @columns_with_info = [ - {:label => 'ID', :key => 'id'}, {:label => 'Name', :key => 'name'}, + {:label => 'ID', :key => 'id'}, {:label => 'Tenant', :key => 'tenant_id'}, {:label => 'Shared', :key => 'shared'} ] From a80dfbb984cba2010a3ec7f1ba8ebc80064abccd Mon Sep 17 00:00:00 2001 From: prabhu-das Date: Thu, 29 May 2014 15:59:26 +0530 Subject: [PATCH 4/5] Added unit tests for network list. --- spec/unit/openstack_network_list_spec.rb | 41 ++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 spec/unit/openstack_network_list_spec.rb diff --git a/spec/unit/openstack_network_list_spec.rb b/spec/unit/openstack_network_list_spec.rb new file mode 100644 index 00000000..8128fdb4 --- /dev/null +++ b/spec/unit/openstack_network_list_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper' +require 'chef/knife/openstack_network_list' +require 'chef/knife/cloud/openstack_service' +require 'support/shared_examples_for_command' + +describe Chef::Knife::Cloud::OpenstackNetworkList do + it_behaves_like Chef::Knife::Cloud::Command, Chef::Knife::Cloud::OpenstackNetworkList.new + + let (:instance) {Chef::Knife::Cloud::OpenstackNetworkList.new} + + context "#validate!" do + before(:each) do + Chef::Config[:knife][:openstack_username] = "testuser" + Chef::Config[:knife][:openstack_password] = "testpassword" + Chef::Config[:knife][:openstack_auth_url] = "tsturl" + instance.stub(:exit) + end + + it "validate openstack mandatory options" do + expect {instance.validate!}.to_not raise_error + end + + it "raise error on openstack_username missing" do + Chef::Config[:knife].delete(:openstack_username) + instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Username' value.") + expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError) + end + + it "raise error on openstack_password missing" do + Chef::Config[:knife].delete(:openstack_password) + instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Password' value.") + expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError) + end + + it "raise error on openstack_auth_url missing" do + Chef::Config[:knife].delete(:openstack_auth_url) + instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Auth Url' value.") + expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError) + end + end +end From cfa8db1f36429bad4e0f29665521b5ee6b42643c Mon Sep 17 00:00:00 2001 From: prabhu-das Date: Thu, 29 May 2014 16:10:33 +0530 Subject: [PATCH 5/5] Added unit test for query_resource method. --- spec/unit/openstack_network_list_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/unit/openstack_network_list_spec.rb b/spec/unit/openstack_network_list_spec.rb index 8128fdb4..d25b44b7 100644 --- a/spec/unit/openstack_network_list_spec.rb +++ b/spec/unit/openstack_network_list_spec.rb @@ -38,4 +38,12 @@ expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError) end end + + context "query_resource" do + it "returns the networks using the fog service." do + instance.service = double + instance.service.should_receive(:list_networks) + instance.query_resource + end + end end