diff --git a/lib/chef/knife/openstack_network_list.rb b/lib/chef/knife/openstack_network_list.rb new file mode 100644 index 00000000..7e009acd --- /dev/null +++ b/lib/chef/knife/openstack_network_list.rb @@ -0,0 +1,34 @@ +# 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 = [ + {:label => 'Name', :key => 'name'}, + {:label => 'ID', :key => 'id'}, + {:label => 'Tenant', :key => 'tenant_id'}, + {:label => 'Shared', :key => 'shared'} + ] + end + + def query_resource + @service.list_networks + end + + end + end + end +end \ No newline at end of file diff --git a/spec/unit/openstack_network_list_spec.rb b/spec/unit/openstack_network_list_spec.rb new file mode 100644 index 00000000..d25b44b7 --- /dev/null +++ b/spec/unit/openstack_network_list_spec.rb @@ -0,0 +1,49 @@ +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 + + 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