From a9fd8be9302d48b6d0d83ef320c4e0991e0999e4 Mon Sep 17 00:00:00 2001 From: siddheshwar-more Date: Fri, 23 May 2014 16:42:59 +0530 Subject: [PATCH] Added availability-zone support for openstack knife-cloud --- .../cloud/openstack_server_create_options.rb | 6 +++++ lib/chef/knife/openstack_server_create.rb | 4 +++- lib/chef/knife/openstack_server_list.rb | 3 ++- lib/chef/knife/openstack_server_show.rb | 3 ++- spec/functional/server_list_func_spec.rb | 24 +++++++++---------- 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/lib/chef/knife/cloud/openstack_server_create_options.rb b/lib/chef/knife/cloud/openstack_server_create_options.rb index b3fcb796..b1baf076 100644 --- a/lib/chef/knife/cloud/openstack_server_create_options.rb +++ b/lib/chef/knife/cloud/openstack_server_create_options.rb @@ -52,6 +52,12 @@ def self.included(includer) :default => true, :description => "Use first available network for bootstrapping if 'public' and 'private' are unavailable." + option :availability_zone, + :short => "-Z ZONE_NAME", + :long => "--availability-zone ZONE_NAME", + :description => "The availability zone for this server", + :proc => Proc.new { |z| Chef::Config[:knife][:availability_zone] = z } + end end end diff --git a/lib/chef/knife/openstack_server_create.rb b/lib/chef/knife/openstack_server_create.rb index 41d6688e..6b5b6647 100644 --- a/lib/chef/knife/openstack_server_create.rb +++ b/lib/chef/knife/openstack_server_create.rb @@ -45,6 +45,7 @@ def before_exec_command :image_ref => locate_config_value(:image), :flavor_ref => locate_config_value(:flavor), :security_groups => locate_config_value(:openstack_security_groups), + :availability_zone => locate_config_value(:availability_zone), :key_name => locate_config_value(:openstack_ssh_key_id) }, :server_create_timeout => locate_config_value(:server_create_timeout) @@ -62,7 +63,8 @@ def before_exec_command {:label => 'Flavor', :key => 'flavor', :value_callback => method(:get_id)}, {:label => 'Image', :key => 'image', :value_callback => method(:get_id)}, {:label => 'Keypair', :key => 'key_name'}, - {:label => 'State', :key => 'state'} + {:label => 'State', :key => 'state'}, + {:label => 'Availability Zone', :key => 'availability_zone'} ] super end diff --git a/lib/chef/knife/openstack_server_list.rb b/lib/chef/knife/openstack_server_list.rb index 9855dfd7..9effaa7a 100644 --- a/lib/chef/knife/openstack_server_list.rb +++ b/lib/chef/knife/openstack_server_list.rb @@ -43,7 +43,8 @@ def before_exec_command {:label => 'Flavor', :key => 'flavor', :value_callback => method(:get_id)}, {:label => 'Image', :key => 'image', :value_callback => method(:get_id)}, {:label => 'Keypair', :key => 'key_name'}, - {:label => 'State', :key => 'state'} + {:label => 'State', :key => 'state'}, + {:label => 'Availability Zone', :key => 'availability_zone'} ] super end diff --git a/lib/chef/knife/openstack_server_show.rb b/lib/chef/knife/openstack_server_show.rb index 7f1bb820..5f9d0cd0 100644 --- a/lib/chef/knife/openstack_server_show.rb +++ b/lib/chef/knife/openstack_server_show.rb @@ -42,7 +42,8 @@ def before_exec_command {:label => 'Flavor', :key => 'flavor', :value_callback => method(:get_id)}, {:label => 'Image', :key => 'image', :value_callback => method(:get_id)}, {:label => 'Keypair', :key => 'key_name'}, - {:label => 'State', :key => 'state'} + {:label => 'State', :key => 'state'}, + {:label => 'Availability Zone', :key => 'availability_zone'} ] super end diff --git a/spec/functional/server_list_func_spec.rb b/spec/functional/server_list_func_spec.rb index 14fcd0a3..046fd80d 100644 --- a/spec/functional/server_list_func_spec.rb +++ b/spec/functional/server_list_func_spec.rb @@ -10,9 +10,9 @@ context "functionality" do before do - @resources = [ TestResource.new({:id => "resource-1", :name => "ubuntu01", :addresses => {"public"=>[{"version"=>4, "addr"=>"172.31.6.132"}], "private"=>[{"version"=>4, "addr"=>"172.31.6.133"}]}, :flavor => {"id" => "1"}, :image => {"id" => "image1"}, :key_name => "keypair", :state => "ACTIVE"}), - TestResource.new({:id => "resource-2", :name => "windows2008", :addresses => {"public"=>[{"version"=>4, "addr"=>"172.31.6.132"}]}, :flavor => {"id" => "id2"}, :image => {"id" => "image2"}, :key_name => "keypair", :state => "ACTIVE"}), - TestResource.new({:id => "resource-3-err", :name => "windows2008", :addresses => {"public"=>[], "private"=>[]}, :flavor => {"id" => "id2"}, :image => {"id" => "image2"}, :key_name => "keypair", :state => "ERROR"}) + @resources = [ TestResource.new({:id => "resource-1", :name => "ubuntu01", :availability_zone => "test zone", :addresses => {"public"=>[{"version"=>4, "addr"=>"172.31.6.132"}], "private"=>[{"version"=>4, "addr"=>"172.31.6.133"}]}, :flavor => {"id" => "1"}, :image => {"id" => "image1"}, :key_name => "keypair", :state => "ACTIVE"}), + TestResource.new({:id => "resource-2", :name => "windows2008",:availability_zone => "test zone", :addresses => {"public"=>[{"version"=>4, "addr"=>"172.31.6.132"}]}, :flavor => {"id" => "id2"}, :image => {"id" => "image2"}, :key_name => "keypair", :state => "ACTIVE"}), + TestResource.new({:id => "resource-3-err", :name => "windows2008", :availability_zone => "test zone", :addresses => {"public"=>[], "private"=>[]}, :flavor => {"id" => "id2"}, :image => {"id" => "image2"}, :key_name => "keypair", :state => "ERROR"}) ] instance.stub(:query_resource).and_return(@resources) instance.stub(:puts) @@ -21,30 +21,30 @@ end it "lists formatted list of resources" do - instance.ui.should_receive(:list).with(["Instance ID", "Name", "Public IP", "Private IP", "Flavor", "Image", "Keypair", "State", - "resource-1", "ubuntu01", "172.31.6.132", "172.31.6.133", "1", "image1","keypair", "ACTIVE", - "resource-2", "windows2008", "172.31.6.132", nil, "id2", "image2", "keypair", "ACTIVE", - "resource-3-err", "windows2008", nil, nil, "id2", "image2", "keypair", "ERROR"], :uneven_columns_across, 8) + instance.ui.should_receive(:list).with(["Instance ID", "Name", "Public IP", "Private IP", "Flavor", "Image", "Keypair", "State", "Availability Zone", + "resource-1", "ubuntu01", "172.31.6.132", "172.31.6.133", "1", "image1","keypair", "ACTIVE", "test zone", + "resource-2", "windows2008", "172.31.6.132", nil, "id2", "image2", "keypair", "ACTIVE", "test zone", + "resource-3-err", "windows2008", nil, nil, "id2", "image2", "keypair", "ERROR", "test zone"], :uneven_columns_across, 9) instance.run end context "when chef-data and chef-node-attribute set" do before(:each) do - @resources.push(TestResource.new({:id => "server-4", :name => "server-4", :addresses => {"public"=>[{"version"=>4, "addr"=>"172.31.6.132"}], "private"=>[{"version"=>4, "addr"=>"172.31.6.133"}]}, :flavor => {"id" => "1"}, :image => {"id" => "image1"}, :key_name => "keypair", :state => "ACTIVE"})) + @resources.push(TestResource.new({:id => "server-4", :name => "server-4", :availability_zone => "test zone", :addresses => {"public"=>[{"version"=>4, "addr"=>"172.31.6.132"}], "private"=>[{"version"=>4, "addr"=>"172.31.6.133"}]}, :flavor => {"id" => "1"}, :image => {"id" => "image1"}, :key_name => "keypair", :state => "ACTIVE"})) @node = TestResource.new({:id => "server-4", :name => "server-4", :chef_environment => "_default", :fqdn => "testfqdnnode.us", :run_list => [], :tags => [], :platform => "ubuntu", :platform_family => "debian"}) Chef::Node.stub(:list).and_return({"server-4" => @node}) instance.config[:chef_data] = true end it "lists formatted list of resources on chef data option set" do - instance.ui.should_receive(:list).with(["Instance ID", "Name", "Public IP", "Private IP", "Flavor", "Image", "Keypair", "State", "Chef Node Name", "Environment", "FQDN", "Runlist", "Tags", "Platform", "resource-1", "ubuntu01", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "", "", "", "", "", "", "resource-2", "windows2008", "172.31.6.132", nil, "id2", "image2", "keypair", "ACTIVE", "", "", "", "", "", "", "resource-3-err", "windows2008", nil, nil, "id2", "image2", "keypair", "ERROR", "", "", "", "", "", "","server-4", "server-4", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "server-4", "_default", "testfqdnnode.us", "[]", "[]", "ubuntu"], :uneven_columns_across, 14) + instance.ui.should_receive(:list).with(["Instance ID", "Name", "Public IP", "Private IP", "Flavor", "Image", "Keypair", "State", "Availability Zone", "Chef Node Name", "Environment", "FQDN", "Runlist", "Tags", "Platform", "resource-1", "ubuntu01", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "test zone", "", "", "", "", "", "", "resource-2", "windows2008", "172.31.6.132", nil, "id2", "image2", "keypair", "ACTIVE", "test zone", "", "", "", "", "", "", "resource-3-err", "windows2008", nil, nil, "id2", "image2", "keypair", "ERROR", "test zone", "", "", "", "", "", "", "server-4", "server-4", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "test zone", "server-4", "_default", "testfqdnnode.us", "[]", "[]", "ubuntu"], :uneven_columns_across, 15) instance.run end it "lists formatted list of resources on chef-data and chef-node-attribute option set" do instance.config[:chef_node_attribute] = "platform_family" @node.should_receive(:attribute?).with("platform_family").and_return(true) - instance.ui.should_receive(:list).with(["Instance ID", "Name", "Public IP", "Private IP", "Flavor", "Image", "Keypair", "State", "Chef Node Name", "Environment", "FQDN", "Runlist", "Tags", "Platform", "platform_family", "resource-1", "ubuntu01", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "", "", "", "", "", "", "", "resource-2", "windows2008", "172.31.6.132", nil, "id2", "image2", "keypair", "ACTIVE", "", "", "", "", "", "", "", "resource-3-err", "windows2008", nil, nil, "id2", "image2", "keypair", "ERROR", "", "", "","","","","","server-4", "server-4", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "server-4", "_default", "testfqdnnode.us", "[]", "[]", "ubuntu", "debian"], :uneven_columns_across, 15) + instance.ui.should_receive(:list).with(["Instance ID", "Name", "Public IP", "Private IP", "Flavor", "Image", "Keypair", "State", "Availability Zone", "Chef Node Name", "Environment", "FQDN", "Runlist", "Tags", "Platform", "platform_family", "resource-1", "ubuntu01", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "test zone", "", "", "", "", "", "", "", "resource-2", "windows2008", "172.31.6.132", nil, "id2", "image2", "keypair", "ACTIVE", "test zone", "", "", "", "", "", "", "", "resource-3-err", "windows2008", nil, nil, "id2", "image2", "keypair", "ERROR", "test zone", "", "", "", "", "", "", "", "server-4", "server-4", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "test zone", "server-4", "_default", "testfqdnnode.us", "[]", "[]", "ubuntu", "debian"], :uneven_columns_across, 16) instance.run end @@ -59,8 +59,8 @@ it "not display chef-data on chef-node-attribute set but chef-data option missing" do instance.config[:chef_data] = false instance.config[:chef_node_attribute] = "platform_family" - instance.ui.should_not_receive(:list).with(["Instance ID", "Name", "Public IP", "Private IP", "Flavor", "Image", "Keypair", "State", "Chef Node Name", "Environment", "FQDN", "Runlist", "Tags", "Platform", "resource-1", "ubuntu01", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "", "", "", "", "", "", "resource-2", "windows2008", "172.31.6.132", nil, "id2", "image2", "keypair", "ACTIVE", "", "", "", "", "", "", "server-4", "server-4", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "resource-3-err", "windows2008", nil, nil, "id2", "image2", "keypair", "ERROR", "", "", "", "", "", "","server-4", "_default", "testfqdnnode.us", "[]", "[]", "ubuntu"], :uneven_columns_across, 14) - instance.ui.should_receive(:list).with(["Instance ID", "Name", "Public IP", "Private IP", "Flavor", "Image", "Keypair", "State", "resource-1", "ubuntu01", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "resource-2", "windows2008", "172.31.6.132", nil, "id2", "image2", "keypair", "ACTIVE", "resource-3-err", "windows2008", nil, nil, "id2", "image2", "keypair", "ERROR","server-4", "server-4", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE"], :uneven_columns_across, 8) + instance.ui.should_not_receive(:list).with(["Instance ID", "Name", "Public IP", "Private IP", "Flavor", "Image", "Keypair", "State", "Availability Zone", "Chef Node Name", "Environment", "FQDN", "Runlist", "Tags", "Platform", "resource-1", "ubuntu01", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "test zone", "", "", "", "", "", "", "resource-2", "windows2008", "172.31.6.132", nil, "id2", "image2", "keypair", "ACTIVE", "test zone", "", "", "", "", "", "", "resource-3-err", "windows2008", nil, nil, "id2", "image2", "keypair", "ERROR", "test zone", "", "", "", "", "", "", "server-4", "server-4", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "test zone", "server-4", "_default", "testfqdnnode.us", "[]", "[]", "ubuntu"], :uneven_columns_across, 15) + instance.ui.should_receive(:list).with(["Instance ID", "Name", "Public IP", "Private IP", "Flavor", "Image", "Keypair", "State", "Availability Zone", "resource-1", "ubuntu01", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "test zone", "resource-2", "windows2008", "172.31.6.132", nil, "id2", "image2", "keypair", "ACTIVE", "test zone", "resource-3-err", "windows2008", nil, nil, "id2", "image2", "keypair", "ERROR", "test zone", "server-4", "server-4", "172.31.6.132", "172.31.6.133", "1", "image1", "keypair", "ACTIVE", "test zone"], :uneven_columns_across, 9) instance.run end end