From d505801243fdbaef0562ee4efaec126983944221 Mon Sep 17 00:00:00 2001 From: ameyavarade Date: Tue, 27 May 2014 17:57:24 +0530 Subject: [PATCH 1/4] Implemented changes for metadata option for openstack knife cloud --- .../knife/cloud/openstack_server_create_options.rb | 11 ++++++++--- lib/chef/knife/openstack_server_create.rb | 1 + spec/unit/openstack_server_create_spec.rb | 8 ++++++++ 3 files changed, 17 insertions(+), 3 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..4a7971b6 100644 --- a/lib/chef/knife/cloud/openstack_server_create_options.rb +++ b/lib/chef/knife/cloud/openstack_server_create_options.rb @@ -5,8 +5,7 @@ class Chef class Knife class Cloud module OpenstackServerCreateOptions - - def self.included(includer) + def self.included(includer) includer.class_eval do include ServerCreateOptions @@ -51,7 +50,13 @@ def self.included(includer) :boolean => true, :default => true, :description => "Use first available network for bootstrapping if 'public' and 'private' are unavailable." - + + md = {} + option :metadata, + :short => "-M X=1", + :long => "--metadata X=1", + :description => "Metadata information for this server (may pass multiple times)", + :proc => Proc.new { |data| md.merge!({data.split('=')[0]=>data.split('=')[1]}) } end end end diff --git a/lib/chef/knife/openstack_server_create.rb b/lib/chef/knife/openstack_server_create.rb index 41d6688e..afd08984 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), + :metadata => locate_config_value(:metadata), :key_name => locate_config_value(:openstack_ssh_key_id) }, :server_create_timeout => locate_config_value(:server_create_timeout) diff --git a/spec/unit/openstack_server_create_spec.rb b/spec/unit/openstack_server_create_spec.rb index 603deb28..09fa1327 100644 --- a/spec/unit/openstack_server_create_spec.rb +++ b/spec/unit/openstack_server_create_spec.rb @@ -63,6 +63,7 @@ Chef::Config[:knife][:openstack_security_groups] = "openstack_security_groups" Chef::Config[:knife][:server_create_timeout] = "server_create_timeout" Chef::Config[:knife][:openstack_ssh_key_id] = "openstack_ssh_key" + Chef::Config[:knife][:metadata] = "foo=bar" end after(:all) do @@ -71,6 +72,7 @@ Chef::Config[:knife].delete(:openstack_ssh_key_id) Chef::Config[:knife].delete(:openstack_security_groups) Chef::Config[:knife].delete(:server_create_timeout) + Chef::Config[:knife].delete(:metadata) end it "set create_options" do @@ -81,6 +83,7 @@ @instance.create_options[:server_def][:image_ref].should == Chef::Config[:knife][:image] @instance.create_options[:server_def][:security_groups].should == Chef::Config[:knife][:openstack_security_groups] @instance.create_options[:server_def][:flavor_ref].should == Chef::Config[:knife][:flavor] + @instance.create_options[:server_def][:metadata].should == Chef::Config[:knife][:metadata] @instance.create_options[:server_create_timeout].should == Chef::Config[:knife][:server_create_timeout] end @@ -97,6 +100,11 @@ @instance.before_exec_command @instance.create_options[:server_def][:user_data].should == user_data end + + it "ensures default value for metadata" do + options = @instance.options + options[:metadata][:default].should == nil + end end describe "#after_exec_command" do From 10b40822171ed34d7f37014d177beff1fd727a54 Mon Sep 17 00:00:00 2001 From: ameyavarade Date: Thu, 29 May 2014 18:42:14 +0530 Subject: [PATCH 2/4] Implemented review comments related to metadata option --- lib/chef/knife/cloud/openstack_server_create_options.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/chef/knife/cloud/openstack_server_create_options.rb b/lib/chef/knife/cloud/openstack_server_create_options.rb index 4a7971b6..d3384ce5 100644 --- a/lib/chef/knife/cloud/openstack_server_create_options.rb +++ b/lib/chef/knife/cloud/openstack_server_create_options.rb @@ -51,12 +51,11 @@ def self.included(includer) :default => true, :description => "Use first available network for bootstrapping if 'public' and 'private' are unavailable." - md = {} option :metadata, :short => "-M X=1", :long => "--metadata X=1", :description => "Metadata information for this server (may pass multiple times)", - :proc => Proc.new { |data| md.merge!({data.split('=')[0]=>data.split('=')[1]}) } + :proc => Proc.new { |data| Chef::Config[:knife][:metadata] ||= {}; Chef::Config[:knife][:metadata].merge!({data.split('=')[0]=>data.split('=')[1]})} end end end From 7751c4389dbc07805c8704109435ccd9e7d0b16c Mon Sep 17 00:00:00 2001 From: ameyavarade Date: Tue, 27 May 2014 17:57:24 +0530 Subject: [PATCH 3/4] Implemented changes for metadata option for openstack knife cloud --- lib/chef/knife/cloud/openstack_server_create_options.rb | 9 +++++++-- lib/chef/knife/openstack_server_create.rb | 1 + spec/unit/openstack_server_create_spec.rb | 8 ++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/chef/knife/cloud/openstack_server_create_options.rb b/lib/chef/knife/cloud/openstack_server_create_options.rb index f6ac6903..ab31627d 100644 --- a/lib/chef/knife/cloud/openstack_server_create_options.rb +++ b/lib/chef/knife/cloud/openstack_server_create_options.rb @@ -5,8 +5,7 @@ class Chef class Knife class Cloud module OpenstackServerCreateOptions - - def self.included(includer) + def self.included(includer) includer.class_eval do include ServerCreateOptions @@ -63,6 +62,12 @@ def self.included(includer) :description => "The availability zone for this server", :proc => Proc.new { |z| Chef::Config[:knife][:availability_zone] = z } + md = {} + option :metadata, + :short => "-M X=1", + :long => "--metadata X=1", + :description => "Metadata information for this server (may pass multiple times)", + :proc => Proc.new { |data| md.merge!({data.split('=')[0]=>data.split('=')[1]}) } end end end diff --git a/lib/chef/knife/openstack_server_create.rb b/lib/chef/knife/openstack_server_create.rb index 61be8e8b..ebcb34b2 100644 --- a/lib/chef/knife/openstack_server_create.rb +++ b/lib/chef/knife/openstack_server_create.rb @@ -46,6 +46,7 @@ def before_exec_command :flavor_ref => locate_config_value(:flavor), :security_groups => locate_config_value(:openstack_security_groups), :availability_zone => locate_config_value(:availability_zone), + :metadata => locate_config_value(:metadata), :key_name => locate_config_value(:openstack_ssh_key_id) }, :server_create_timeout => locate_config_value(:server_create_timeout) diff --git a/spec/unit/openstack_server_create_spec.rb b/spec/unit/openstack_server_create_spec.rb index 41b6f730..aebab1b6 100644 --- a/spec/unit/openstack_server_create_spec.rb +++ b/spec/unit/openstack_server_create_spec.rb @@ -65,6 +65,7 @@ Chef::Config[:knife][:openstack_ssh_key_id] = "openstack_ssh_key" Chef::Config[:knife][:network_ids] = "test_network_id" Chef::Config[:knife][:network_ids].stub(:map).and_return(Chef::Config[:knife][:network_ids]) + Chef::Config[:knife][:metadata] = "foo=bar" end after(:all) do @@ -73,6 +74,7 @@ Chef::Config[:knife].delete(:openstack_ssh_key_id) Chef::Config[:knife].delete(:openstack_security_groups) Chef::Config[:knife].delete(:server_create_timeout) + Chef::Config[:knife].delete(:metadata) end it "set create_options" do @@ -84,6 +86,7 @@ @instance.create_options[:server_def][:security_groups].should == Chef::Config[:knife][:openstack_security_groups] @instance.create_options[:server_def][:flavor_ref].should == Chef::Config[:knife][:flavor] @instance.create_options[:server_def][:nics].should == Chef::Config[:knife][:network_ids] + @instance.create_options[:server_def][:metadata].should == Chef::Config[:knife][:metadata] @instance.create_options[:server_create_timeout].should == Chef::Config[:knife][:server_create_timeout] end @@ -114,6 +117,11 @@ @instance.create_options[:server_def][:nics].should == ["test_network_id1", "test_network_id2"] end end + + it "ensures default value for metadata" do + options = @instance.options + options[:metadata][:default].should == nil + end end describe "#after_exec_command" do From 8466173411a9a4cce87a8fa2a8d3f4d61d5b7b72 Mon Sep 17 00:00:00 2001 From: ameyavarade Date: Thu, 29 May 2014 18:42:14 +0530 Subject: [PATCH 4/4] Implemented review comments related to metadata option --- lib/chef/knife/cloud/openstack_server_create_options.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/chef/knife/cloud/openstack_server_create_options.rb b/lib/chef/knife/cloud/openstack_server_create_options.rb index ab31627d..4b4a5cd3 100644 --- a/lib/chef/knife/cloud/openstack_server_create_options.rb +++ b/lib/chef/knife/cloud/openstack_server_create_options.rb @@ -62,12 +62,11 @@ def self.included(includer) :description => "The availability zone for this server", :proc => Proc.new { |z| Chef::Config[:knife][:availability_zone] = z } - md = {} option :metadata, :short => "-M X=1", :long => "--metadata X=1", :description => "Metadata information for this server (may pass multiple times)", - :proc => Proc.new { |data| md.merge!({data.split('=')[0]=>data.split('=')[1]}) } + :proc => Proc.new { |data| Chef::Config[:knife][:metadata] ||= {}; Chef::Config[:knife][:metadata].merge!({data.split('=')[0]=>data.split('=')[1]})} end end end