Skip to content

Commit

Permalink
Merge pull request #57 from ClogenyTechnologies/prabhu-OC-10611-custo…
Browse files Browse the repository at this point in the history
…m-arguments

OC-10611 implementation for custom_arguments passed to Fog
  • Loading branch information
Mukta Aphale committed Apr 29, 2014
2 parents c0c13ed + eb26f4e commit 881cec7
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/chef/knife/cloud/fog/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ def self.included(includer)
:long => "--api-endpoint ENDPOINT",
:description => "Your API endpoint. Eg, for Eucalyptus it can be 'http://ecc.eucalyptus.com:8773/services/Eucalyptus'",
:proc => Proc.new { |endpoint| Chef::Config[:knife][:api_endpoint] = endpoint }

end
end

end
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/chef/knife/cloud/fog/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def connection
# cloud server specific implementation methods for commands.
def create_server(options = {})
begin
add_custom_attributes(options[:server_def])
server = connection.servers.create(options[:server_def])
rescue Excon::Errors::BadRequest => e
response = Chef::JSONCompat.from_json(e.response.body)
Expand Down Expand Up @@ -127,7 +128,7 @@ def list_resource_configurations
raise CloudExceptions::CloudAPIException, error_message
end
end

def delete_server_on_failure(server = nil)
server.destroy if ! server.nil?
end
Expand Down Expand Up @@ -172,6 +173,7 @@ def is_image_windows?(image)
image_info = connection.images.get(image)
!image_info.nil? ? image_info.platform == 'windows' : false
end

end
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/chef/knife/cloud/server/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ def self.included(includer)
:short => "-N NAME",
:long => "--node-name NAME",
:description => "The name of the node and client to delete, if it differs from the server name. Only has meaning when used with the '--purge' option."

option :custom_attributes,
:long => "--custom-attributes CUSTOM_ATTRIBUTES",
:description => "Custom attributes to be passed to Fog.",
:proc => Proc.new {|args| Chef::Config[:knife][:custom_attributes] = args.split(';').map{|keys| keys.split('=')}.map{|j| Hash[*j.map{|k| k.strip}]}}
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/chef/knife/cloud/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ def get_server(server_name)
def server_summary(server, columns_with_info = [])
raise Chef::Exceptions::Override, "You must override server_summary in #{self.to_s}"
end

def add_custom_attributes(server_def)
Chef::Config[:knife][:custom_attributes].map{|args| args.map{|k,v| server_def.merge!(k.to_sym => v)}} unless Chef::Config[:knife][:custom_attributes].nil?
end

end # class service
end
end
Expand Down
17 changes: 16 additions & 1 deletion spec/unit/fog_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Copyright:: Copyright (c) 2013 Opscode, Inc.
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'chef/knife/cloud/fog/service'

require 'support/shared_examples_for_service'

describe Chef::Knife::Cloud::FogService do
Expand All @@ -23,4 +22,20 @@
end

end

context "add_custom_attributes" do
before(:each) do
Chef::Config[:knife][:custom_attributes] = [{"state"=>"Inactive"}]
@server_def = {:name=>"vm-1", :image_ref=>"123",:flavor_ref=>"2", :key_name=>"key"}
instance.add_custom_attributes(@server_def)
end

it "adds the custom attributes provided to server_def" do
expect(@server_def.include?(:state)).to be true
end

it "sets the provided attributes with supplied values" do
expect(@server_def[:state] == "Inactive").to be true
end
end
end

0 comments on commit 881cec7

Please sign in to comment.