-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from opscode/sid-oc-9533-chef-data
OC-9533 knife CLOUD server list needs to expose Chef data (node names, attributes)
- Loading branch information
Showing
4 changed files
with
187 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# | ||
# Author:: Siddheshwar More (<siddheshwar.more@clogeny.com>) | ||
# Copyright:: Copyright (c) 2013 Opscode, Inc. | ||
# License:: Apache License, Version 2.0 | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
class Chef | ||
class Knife | ||
class Cloud | ||
module ServerListOptions | ||
def self.included(includer) | ||
includer.class_eval do | ||
|
||
option :chef_data, | ||
:long => "--chef-data", | ||
:boolean => true, | ||
:default => false, | ||
:description => "Display chef node data which include chef node name, environment name, fqdn, platform, runlist and tags." | ||
|
||
option :chef_node_attribute, | ||
:long => "--chef-node-attribute CHEF_NODE_ATTRIBUTE_NAME", | ||
:description => "Used with --chef-data option. It display node attributes details by adding new column in server list display.", | ||
:proc => Proc.new { |i| Chef::Config[:knife][:chef_node_attribute] = i } | ||
|
||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Author:: Siddheshwar More (<siddheshwar.more@clogeny.com>) | ||
# Copyright:: Copyright (c) 2013 Opscode, Inc. | ||
|
||
require 'spec_helper' | ||
require 'support/shared_examples_for_command' | ||
require 'chef/knife/cloud/server/list_command' | ||
require 'chef/node' | ||
|
||
describe Chef::Knife::Cloud::ServerListCommand do | ||
it_behaves_like Chef::Knife::Cloud::Command, Chef::Knife::Cloud::ServerListCommand.new | ||
|
||
describe "#before_exec_command" do | ||
it "set chef data columns info on chef data options" do | ||
instance = Chef::Knife::Cloud::ServerListCommand.new | ||
instance.config[:chef_data] = true | ||
Chef::Node.should_receive(:list).with(true) | ||
instance.before_exec_command.should include({:label => "Chef Node Name", :key => "name"}) | ||
end | ||
|
||
it "set chef data columns info on chef-data and chef-node-attribute options" do | ||
chef_node_attribute = "platform_family" | ||
instance = Chef::Knife::Cloud::ServerListCommand.new | ||
instance.config[:chef_data] = true | ||
instance.config[:chef_node_attribute] = chef_node_attribute | ||
Chef::Node.should_receive(:list).with(true) | ||
instance.before_exec_command.should include({:label => chef_node_attribute, :key => chef_node_attribute}) | ||
end | ||
|
||
it "not set chef data columns info if chef-data option is not set" do | ||
instance = Chef::Knife::Cloud::ServerListCommand.new | ||
Chef::Node.should_not_receive(:list).with(true) | ||
instance.before_exec_command.should be(nil) | ||
end | ||
|
||
it "not set chef data columns info on chef-node-attribute option set but chef-data option is not set" do | ||
instance = Chef::Knife::Cloud::ServerListCommand.new | ||
instance.config[:chef_node_attribute] = "platform_family" | ||
Chef::Node.should_not_receive(:list).with(true) | ||
instance.before_exec_command.should be(nil) | ||
end | ||
end | ||
|
||
describe "#get_resource_col_val" do | ||
let (:resources) {[ TestResource.new({:id => "server-1", :name => "server-1", :os => "ubuntu"})]} | ||
before do | ||
class DerivedServerList < Chef::Knife::Cloud::ServerListCommand | ||
attr_accessor :node | ||
def before_exec_command | ||
@columns_with_info = [ { :key => 'id', :label => 'Instance ID' }, {:label => 'Environment', :key => 'chef_environment'}, {:label => 'platform_family', :key => 'platform_family'} ] | ||
@chef_data_col_info = [ {:label => 'Environment', :key => 'chef_environment'}, {:label => 'platform_family', :key => 'platform_family'} ] | ||
@node = TestResource.new({:id => "server-1", :name => "server-1", | ||
:chef_environment => "_default", :platform_family => "debian"}) | ||
@node.define_singleton_method(:attribute?) do |attribute| | ||
end | ||
@node_list = {"server-1" => @node} | ||
end | ||
end | ||
@derived_instance = DerivedServerList.new | ||
@derived_instance.config[:chef_data] = true | ||
@derived_instance.config[:chef_node_attribute] = "platform_family" | ||
@derived_instance.before_exec_command | ||
end | ||
|
||
it "return columns_with_info values" do | ||
@derived_instance.node.should_receive(:attribute?).with("platform_family").and_return(true) | ||
@derived_instance.get_resource_col_val(resources.first).should eq(["server-1", "_default", "debian"]) | ||
end | ||
|
||
it "raise error on invalide chef_node_attribute" do | ||
@derived_instance.ui.stub(:error) | ||
@derived_instance.node.should_receive(:attribute?).with("platform_family").and_return(false) | ||
expect { @derived_instance.get_resource_col_val(resources.first) }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ServerListingError, "The Node does not have a platform_family attribute.") | ||
end | ||
end | ||
end |