Skip to content

Commit

Permalink
Merge pull request #15 from opscode/adamed-OC-7868-google-server-list
Browse files Browse the repository at this point in the history
OC-7868: Knife google plug-in does not read cli switches from knife.rb
  • Loading branch information
Adam Edwards committed May 27, 2013
2 parents 77ea106 + 02e8ce1 commit 7f3a203
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 52 deletions.
1 change: 1 addition & 0 deletions lib/chef/knife/google_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def public_ips(instance)
}
}.flatten.compact
end

end
end
end
13 changes: 9 additions & 4 deletions lib/chef/knife/google_server_create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ class GoogleServerCreate < Knife
option :zone,
:short => "-Z ZONE",
:long => "--google-compute-zone ZONE",
:description => "The Zone for this server",
:required => true
:description => "The Zone for this server"

option :network,
:short => "-n NETWORK",
Expand Down Expand Up @@ -265,23 +264,29 @@ def run
end

begin
zone = client.zones.get(config[:zone]).self_link
zone = client.zones.get(config[:zone] || Chef::Config[:knife][:google_compute_zone]).self_link
rescue Google::Compute::ResourceNotFound
ui.error("Zone '#{config[:zone]}' not found")
ui.error("Zone '#{config[:zone] || Chef::Config[:knife][:google_compute_zone]}' not found")
exit 1
rescue Google::Compute::ParameterValidation
ui.error("Must specify zone in knife config file or in command line as an option. Try --help.")
exit 1
end

begin
machine_type = client.machine_types.get(config[:machine_type]).self_link
rescue Google::Compute::ResourceNotFound
ui.error("MachineType '#{config[:machine_type]}' not found")
exit 1
end

begin
image = client.images.get(:project=>'google', :name=>config[:image]).self_link
rescue Google::Compute::ResourceNotFound
ui.error("Image '#{config[:image]}' not found")
exit 1
end

begin
network = client.networks.get(config[:network]).self_link
rescue Google::Compute::ResourceNotFound
Expand Down
10 changes: 6 additions & 4 deletions lib/chef/knife/google_server_delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ class GoogleServerDelete < Knife
option :zone,
:short => "-Z ZONE",
:long => "--google-compute-zone ZONE",
:description => "The Zone for this server",
:required => true
:description => "The Zone for this server"

option :purge,
:short => "-P",
Expand Down Expand Up @@ -60,9 +59,12 @@ def destroy_item(klass, name, type_name)

def run
begin
zone = client.zones.get(config[:zone]).self_link
zone = client.zones.get(config[:zone] || Chef::Config[:knife][:google_compute_zone]).self_link
rescue Google::Compute::ResourceNotFound
ui.error("Zone '#{config[:zone]}' not found")
ui.error("Zone '#{config[:zone] || Chef::Config[:knife][:google_compute_zone]}' not found")
exit 1
rescue Google::Compute::ParameterValidation
ui.error("Must specify zone in knife config file or in command line as an option. Try --help.")
exit 1
end

Expand Down
30 changes: 15 additions & 15 deletions lib/chef/knife/google_server_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,23 @@ class GoogleServerList < Knife
option :zone,
:short => "-Z ZONE",
:long => "--google-compute-zone ZONE",
:description => "The Zone for this server",
:required => true
:description => "The Zone for this server"

def run
$stdout.sync = true

begin
zone = client.zones.get(config[:zone])
zone = client.zones.get(config[:zone] || Chef::Config[:knife][:google_compute_zone])
rescue Google::Compute::ResourceNotFound
ui.error("Zone '#{config[:zone]}' not found")
ui.error("Zone '#{config[:zone] || Chef::Config[:knife][:google_compute_zone] }' not found.")
exit 1
rescue Google::Compute::ParameterValidation
ui.error("Must specify zone in knife config file or in command line as an option. Try --help.")
exit 1
end

instance_list = [
ui.color("Name", :bold),
ui.color('Type', :bold),
ui.color('Image', :bold),
ui.color('Public IP', :bold),
ui.color('Private IP', :bold),
ui.color('Disks', :bold),
ui.color("Zone", :bold),
ui.color('Status', :bold)].flatten.compact
instance_label = ['Name', 'Type', 'Image', 'Public IP', 'Private IP', 'Disks', 'Zone', 'Status']
instance_list = (instance_label.map {|label| ui.color(label, :bold)}).flatten.compact

output_column_count = instance_list.length

Expand All @@ -70,7 +65,12 @@ def run
end
end
end
puts ui.list(instance_list, :uneven_columns_across, output_column_count)

if instance_list.count > 8 # This condition checks if there are any servers available. The first 8 values are the Labels.
puts ui.list(instance_list, :uneven_columns_across, output_column_count)
else
puts "No servers found in #{zone.name} zone."
end
end
end
end
Expand Down
46 changes: 31 additions & 15 deletions spec/chef/knife/google_server_create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,8 @@

describe Chef::Knife::GoogleServerCreate do

let(:knife_plugin) do
Chef::Knife::GoogleServerCreate.new(["-m"+stored_machine_type.name,
"-I"+stored_image.name, "-n"+stored_network.name,
"-Z"+stored_zone.name, stored_instance.name])
end

it "#run should invoke compute api to create an server" do
zones = mock(Google::Compute::ListableResourceCollection)
before(:each) do
zones = mock(Google::Compute::ListableResourceCollection)
zones.should_receive(:get).with(stored_zone.name).
and_return(stored_zone)

Expand Down Expand Up @@ -59,7 +53,13 @@
:images=>images, :zones=>zones,:machine_types=>machine_types,
:networks=>networks)
Google::Compute::Client.stub!(:from_json).and_return(client)
end


it "#run should invoke compute api to create an server" do
knife_plugin = Chef::Knife::GoogleServerCreate.new(["-m"+stored_machine_type.name,
"-I"+stored_image.name, "-n"+stored_network.name,
"-Z"+stored_zone.name, stored_instance.name])
knife_plugin.config[:disks]=[]
knife_plugin.config[:metadata]=[]
knife_plugin.config[:public_ip]='EPHEMERAL'
Expand All @@ -72,13 +72,29 @@

knife_plugin.run
end
it "should read zone value from knife config file." do
Chef::Config[:knife][:google_compute_zone] = stored_zone.name
knife_plugin = Chef::Knife::GoogleServerCreate.new(["-m"+stored_machine_type.name,
"-I"+stored_image.name, "-n"+stored_network.name,
stored_instance.name])
knife_plugin.config[:disks]=[]
knife_plugin.config[:metadata]=[]
knife_plugin.config[:public_ip]='EPHEMERAL'
knife_plugin.ui.stub!(:info)

describe "without appropriate command line options" do
it "should throw exception when required params are not passed" do
$stdout.stub!(:write) # lets not print those error messages
expect {
Chef::Knife::GoogleServerCreate.new([ "NAME"])
}.to raise_error(SystemExit)
end
knife_plugin.stub!(:wait_for_sshd)
knife_plugin.should_receive(:bootstrap_for_node).
with(stored_instance,'10.100.0.10').
and_return(mock("Chef::Knife::Bootstrap",:run=>true))
knife_plugin.run
end
end

describe "without appropriate command line options" do
it "should throw exception when required params are not passed" do
$stdout.stub!(:write) # lets not print those error messages
expect {
Chef::Knife::GoogleServerCreate.new([ "NAME"])
}.to raise_error(SystemExit)
end
end
22 changes: 22 additions & 0 deletions spec/chef/knife/google_server_delete_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,25 @@
end
end
end

describe Chef::Knife::GoogleServerDelete do
it "should read zone value from knife config file." do
Chef::Config[:knife][:google_compute_zone] = stored_zone.name
knife_plugin = Chef::Knife::GoogleServerDelete.new([stored_instance.name])
zones = mock(Google::Compute::ListableResourceCollection)
zones.should_receive(:get).with(stored_zone.name).and_return(stored_zone)

instances = mock(Google::Compute::DeletableResourceCollection)
instances.should_receive(:get).with(:name=>stored_instance.name, :zone=>stored_zone.name).
and_return(stored_instance)
instances.should_receive(:delete).with(:instance=>stored_instance.name, :zone=>stored_zone.name)

client = mock(Google::Compute::Client, :zones=>zones, :instances=>instances)
Google::Compute::Client.stub!(:from_json).and_return(client)
knife_plugin.config[:yes] = true
knife_plugin.ui.should_receive(:warn).twice
knife_plugin.stub!(:msg_pair)
knife_plugin.run

end
end
27 changes: 13 additions & 14 deletions spec/chef/knife/google_server_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,23 @@
require 'spec_helper'

describe Chef::Knife::GoogleServerList do

let(:knife_plugin) do
Chef::Knife::GoogleServerList.new(["-Z"+stored_zone.name])
end

it "should enlist all the GCE servers when run invoked" do
before(:each) do
zones = mock(Google::Compute::ListableResourceCollection)
zones.should_receive(:get).with(stored_zone.name).
and_return(stored_zone)

zones.should_receive(:get).with(stored_zone.name).and_return(stored_zone)
instances = mock(Google::Compute::DeletableResourceCollection)
instances.should_receive(:list).with(:zone=>stored_zone.name).
and_return([stored_instance])

client = mock(Google::Compute::Client,
:instances=>instances, :zones=>zones)
instances.should_receive(:list).with(:zone=>stored_zone.name).and_return([stored_instance])
client = mock(Google::Compute::Client, :instances=>instances, :zones=>zones)
Google::Compute::Client.stub!(:from_json).and_return(client)
end

it "should enlist all the GCE servers when run invoked" do
knife_plugin = Chef::Knife::GoogleServerList.new(["-Z"+stored_zone.name])
$stdout.should_receive(:write).with(kind_of(String))
knife_plugin.run
end

it "should list all the GCE servers when zone is set in knife.rb" do
knife_plugin = Chef::Knife::GoogleServerList.new([Chef::Config[:knife][:google_compute_zone] = stored_zone.name])
$stdout.should_receive(:write).with(kind_of(String))
knife_plugin.run
end
Expand Down

0 comments on commit 7f3a203

Please sign in to comment.