Permalink
Browse files

fixes #4423 - allow puppet classes to be imported by environment

  • Loading branch information...
1 parent bfee97b commit 10ac0055a1b3346929c54bf48157048b82402fb2 Neil Miao committed with Dominic Cleal Jan 29, 2014
@@ -6,6 +6,7 @@ module Foreman::Controller::Environments
def import_environments
begin
opts = params[:proxy].blank? ? { } : { :url => SmartProxy.find(params[:proxy]).try(:url) }
+ opts[:env] = params[:env] unless params[:env].blank?
@importer = PuppetClassImporter.new(opts)
@changed = @importer.changes
rescue => e
@@ -34,4 +35,4 @@ def obsolete_and_new
redirect_to :controller => controller_path
end
-end
+end
@@ -12,11 +12,13 @@ def class_update_text pcs, env
end
def import_proxy_select hash
- select_action_button( _('Import'), {},
- SmartProxy.puppet_proxies.map do |proxy|
- display_link_if_authorized(_("Import from %s") % proxy.name, hash.merge(:proxy => proxy), :class=>'btn btn-default')
- end.flatten
- )
+ select_action_button( _('Import'), {}, import_proxy_links(hash, 'btn btn-default'))
+ end
+
+ def import_proxy_links hash, classes=nil
+ SmartProxy.puppet_proxies.map do |proxy|
+ display_link_if_authorized(_("Import from %s") % proxy.name, hash.merge(:proxy => proxy), :class=>classes)
+ end.flatten
end
private
@@ -33,7 +35,6 @@ def pretty_print classes
hash.keys.sort.map do |key|
link_to key,{}, {:remote => true, :rel => "popover", :data => {"content" => hash[key].sort.join('<br>').html_safe, "original-title" => key}}
end.to_sentence.html_safe
-
end
end
@@ -3,6 +3,8 @@ class PuppetClassImporter
def initialize args = { }
@foreman_classes = { }
@proxy_classes = { }
+ @environment = args[:env]
+
if args[:proxy]
@proxy = args[:proxy]
elsif args[:url]
@@ -18,19 +20,29 @@ def initialize args = { }
def changes
changes = { 'new' => { }, 'obsolete' => { }, 'updated' => { } }
- actual_environments.each do |env|
+ if @environment.nil?
+ actual_environments.each do |env|
+ new = new_classes_for(env)
+ old = removed_classes_for(env)
+ updated = updated_classes_for(env)
+ changes['new'][env] = new if new.any?
+ changes['obsolete'][env] = old if old.any?
+ changes['updated'][env] = updated if updated.any?
+ end
+
+ old_environments.each do |env|
+ changes['obsolete'][env] ||= []
+ changes['obsolete'][env] << "_destroy_" unless actual_environments.include?(env)
+ end
+ else
+ env = @environment
new = new_classes_for(env)
old = removed_classes_for(env)
updated = updated_classes_for(env)
changes['new'][env] = new if new.any?
changes['obsolete'][env] = old if old.any?
changes['updated'][env] = updated if updated.any?
end
-
- old_environments.each do |env|
- changes['obsolete'][env] ||= []
- changes['obsolete'][env] << "_destroy_" unless actual_environments.include?(env)
- end
changes
end
@@ -16,7 +16,8 @@
</td>
<td><%= link_to @host_counter[environment.id] || 0, hosts_path(:search => "environment = #{environment}") %>
<td>
- <%= action_buttons(link_to(_('Classes'), puppetclasses_path(:search => "environment = #{environment}")),
+ <%= action_buttons(link_to(_("Classes"), puppetclasses_path(:search => "environment = #{environment}")),
+ import_proxy_links(hash_for_import_environments_environments_path(:env => environment.name)),
display_delete_if_authorized(hash_for_environment_path(:id => environment.name), :confirm => "Delete #{environment.name}?")) %>
</td>
</tr>
View
@@ -59,7 +59,7 @@ namespace :puppet do
namespace :import do
desc "
Update puppet environments and classes. Optional batch flag triggers run with no prompting\nUse proxy=<proxy name> to import from or get the first one by default"
- task :puppet_classes, [:batch] => :environment do | t, args |
+ task :puppet_classes, [:batch, :envname] => :environment do | t, args |
args.batch = args.batch == "true"
proxies = SmartProxy.puppet_proxies
@@ -79,7 +79,7 @@ namespace :puppet do
# the on-disk puppet installation
begin
puts "Evaluating possible changes to your installation" unless args.batch
- importer = PuppetClassImporter.new({ :url => proxy.url })
+ importer = PuppetClassImporter.new({ :url => proxy.url, :env => args.envname })
changes = importer.changes
rescue => e
if args.batch
@@ -3,7 +3,7 @@
class PuppetClassImporterTest < ActiveSupport::TestCase
def setup
- ProxyAPI::Puppet.any_instance.stubs(:environments).returns(["foreman-testing"])
+ ProxyAPI::Puppet.any_instance.stubs(:environments).returns(["foreman-testing","foreman-testing-1"])
ProxyAPI::Puppet.any_instance.stubs(:classes).returns(mocked_classes)
end
@@ -19,6 +19,13 @@ def setup
assert_kind_of ProxyAPI::Puppet, klass.send(:proxy)
end
+ test "should contain only the specified environment in changes" do
+ proxy = smart_proxies(:puppetmaster)
+ importer = PuppetClassImporter.new(:url => proxy.url, :env => 'foreman-testing')
+ assert importer.changes['new'].include?('foreman-testing')
+ assert !importer.changes['new'].include?('foreman-testing-1')
+ end
+
test "should return list of envs" do
assert_kind_of Array, get_an_instance.db_environments
end

0 comments on commit 10ac005

Please sign in to comment.