Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial implementation for custom_arguments passed to Fog. #7

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 3 additions & 0 deletions lib/chef/knife/cloud/fog/service.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#
# Author:: Kaustubh Deorukhkar (<[email protected]>)
# Author:: Prabhu Das (<[email protected]>)
# Copyright:: Copyright (c) 2013 Opscode, Inc.
#

Expand Down Expand Up @@ -46,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 @@ -121,6 +123,7 @@ def delete_server_on_failure(server = nil)
def add_api_endpoint
raise Chef::Exceptions::Override, "You must override add_api_endpoint in #{self.to_s} to add endpoint in auth_params for connection"
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
4 changes: 4 additions & 0 deletions lib/chef/knife/cloud/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ def list_images(image_filters)
raise Chef::Exceptions::Override, "You must override list_images 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