[PATCH/foreman 1/1] fixes #1201 - Import Subnets from DHCP server

Signed-off-by: Ohad Levy <ohadlevy@gmail.com>

··· --- app/controllers/subnets_controller.rb | 17 ++++++++++++ app/models/subnet.rb | 11 ++++++++ app/views/smart_proxies/index.html.erb | 5 +++- app/views/subnets/_fields.html.erb | 44 +++++++++++++++++++++++++++++++ app/views/subnets/_form.html.erb | 45 +------------------------------- app/views/subnets/import.html.erb | 12 ++++++++ config/routes.rb | 2 +- 7 files changed, 90 insertions(+), 46 deletions(-) create mode 100644 app/views/subnets/_fields.html.erb create mode 100644 app/views/subnets/import.html.erb

diff --git a/app/controllers/subnets_controller.rb b/app/controllers/subnets_controller.rb
index 81f52a5…29572c8 100644
— a/app/controllers/subnets_controller.rb
+++ b/app/controllers/subnets_controller.rb
@@ -69,4 +69,21 @@ class SubnetsController < ApplicationController
head :status => 500
end

  • def import
  • proxy = SmartProxy.find(params[:smart_proxy_id])
  • @subnets = Subnet.import(proxy)
  • if @subnets.empty?
  •  redirect_to :subnets, :notice => "No new subnets found"
    
  • end
  • end
  • def create_multiple
  • @subnets = Subnet.create(params[:subnets]).reject { |s| s.errors.empty? }
  • if @subnets.empty?
  •  process_success(:object => @subnets, :success_msg => "Imported Subnets")
    
  • else
  •  render :action => "import"
    
  • end
  • end

end
diff --git a/app/models/subnet.rb b/app/models/subnet.rb
index 6edfd4e…d97383c 100644
— a/app/models/subnet.rb
+++ b/app/models/subnet.rb
@@ -74,4 +74,15 @@ class Subnet < ActiveRecord::Base
nil
end

  • imports subnets from a dhcp smart proxy

  • def self.import proxy
  • return unless proxy.features.include?(Feature.find_by_name(“DHCP”))
  • ProxyAPI::DHCP.new(:url => proxy.url).subnets.map do |s|
  •  # do not import existing networks.
    
  •  attrs = { :network => s["network"], :mask => s["netmask"] }
    
  •  next if first(:conditions => attrs)
    
  •  new(attrs.update(:dhcp => proxy))
    
  • end.compact
  • end

end
diff --git a/app/views/smart_proxies/index.html.erb b/app/views/smart_proxies/index.html.erb
index ccebf82…b1ca374 100644
— a/app/views/smart_proxies/index.html.erb
+++ b/app/views/smart_proxies/index.html.erb
@@ -19,7 +19,10 @@
<% if proxy.features.include? Feature.find_by_name(“Puppet CA”) -%>
<%= display_link_if_authorized “Certificates”, hash_for_smart_proxy_puppetca_index_path(:smart_proxy_id => proxy) %>
<%= display_link_if_authorized “Autosign”, hash_for_smart_proxy_autosign_index_path(:smart_proxy_id => proxy) %>

  •      <% end -%>
    
  •    <% end -%>
    
  •    <% if proxy.features.include? Feature.find_by_name("DHCP") -%>
    
  •      <%= display_link_if_authorized "Import Subnets", hash_for_import_subnets_path(:smart_proxy_id => proxy) %>
    
  •    <% end -%>
       <%= display_link_if_authorized "Destroy", hash_for_smart_proxy_path(:id => proxy, :auth_action => :destroy), :confirm => "Destroy #{proxy.name}?", :method => :delete %>
     </td>
    

diff --git a/app/views/subnets/_fields.html.erb b/app/views/subnets/_fields.html.erb
new file mode 100644
index 0000000…da77d05
— /dev/null
+++ b/app/views/subnets/_fields.html.erb
@@ -0,0 +1,44 @@
+<%= f.error_messages(:object_name => “Subnet”) %>
+

  • <%= f.label :name, nil, :class => “span-3” %>
  • <%= f.text_field :name, :class => “last” %>
    +

+

  • <%= f.label :domain, nil, :class => “span-3” %>
  • <%= f.collection_select :domain_id, Domain.all, :id, :name, {:include_blank => true}, {:class => “last”} %>
    +

+

  • <%= f.label :network, nil, :class => “span-3” %>
  • <%= f.text_field :network, :class => “last” %>
    +

+

  • <%= f.label :mask, nil, :class => “span-3” %>
  • <%= f.text_field :mask, :title => “netmask for this subnet”, :class => “last” %>
    +

+

  • <%= f.label :ranges, nil, :class => “span-3” %>
  • <%= f.text_field :ranges, :title => “A list of comma separated single IPs or start-end couples.”, :class => “last” %>
    +

+

  • <%= f.label :dhcp_id, “DHCP Proxy”, :class => “span-3” %>
  • <%= f.collection_select :dhcp_id, Feature.find_by_name(“DHCP”).smart_proxies, :id, :name, {:include_blank => “None”}, {:class => “last”} %>
    +
    +

  • <%= f.label :tftp_id, “TFTP Proxy”, :class => “span-3” %>
  • <%= f.collection_select :tftp_id, Feature.find_by_name(“TFTP”).smart_proxies, :id, :name, {:include_blank => “None”}, {:class => “last”} %>
    +

+

  • <%= f.label :vlanid, nil, :class => “span-3” %>
  • <%= f.text_field :vlanid, :class => “last” %>
    +

+

  • <%= f.label :priority, nil, :class => “span-3”%>
  • <%= f.text_field :priority, :class => “last”%>
    +
    diff --git a/app/views/subnets/_form.html.erb b/app/views/subnets/_form.html.erb
    index 4de358e…1af4440 100644
    — a/app/views/subnets/_form.html.erb
    +++ b/app/views/subnets/_form.html.erb
    @@ -1,47 +1,4 @@
    <% form_for @subnet do |f| %>
  • <%= f.error_messages %>
  • <%= f.label :name, nil, :class => “span-3” %>
  • <%= f.text_field :name, :class => “last” %>
  • <%= f.label :domain, nil, :class => “span-3” %>
  • <%= f.collection_select :domain_id, Domain.all, :id, :name, {:include_blank => true}, {:class => “last”} %>
  • <%= f.label :network, nil, :class => “span-3” %>
  • <%= f.text_field :network, :class => “last” %>
  • <%= f.label :mask, nil, :class => “span-3” %>
  • <%= f.text_field :mask, :title => “netmask for this subnet”, :class => “last” %>
  • <%= f.label :ranges, nil, :class => “span-3” %>
  • <%= f.text_field :ranges, :title => “A list of comma separated single IPs or start-end couples.”, :class => “last” %>
  • <%= f.label :dhcp_id, “DHCP Proxy”, :class => “span-3” %>
  • <%= f.collection_select :dhcp_id, Feature.find_by_name(“DHCP”).smart_proxies, :id, :name, {:include_blank => “None”}, {:class => “last”} %>
  • <%= f.label :tftp_id, “TFTP Proxy”, :class => “span-3” %>
  • <%= f.collection_select :tftp_id, Feature.find_by_name(“TFTP”).smart_proxies, :id, :name, {:include_blank => “None”}, {:class => “last”} %>
  • <%= f.label :vlanid, nil, :class => “span-3” %>
  • <%= f.text_field :vlanid, :class => “last” %>
  • <%= f.label :priority, nil, :class => “span-3”%>
  • <%= f.text_field :priority, :class => “last”%>
  • <%= render ‘fields’, :f => f %>

    <%= f.submit %>

<% end %>
diff --git a/app/views/subnets/import.html.erb b/app/views/subnets/import.html.erb
new file mode 100644
index 0000000…a44dcbd
— /dev/null
+++ b/app/views/subnets/import.html.erb
@@ -0,0 +1,12 @@
+<% title “Import subnets” %>
+
+<% form_tag create_multiple_subnets_path do -%>

  • <% @subnets.each do |subnet| -%>
  • <%= subnet -%>


  • <% fields_for “subnets[]”, subnet do |f| -%>
  •  <%= render 'fields', :f => f %>
    
  • <% end -%>
  • <% end -%>
  • <%= submit_tag %>
    +<% end -%>
    diff --git a/config/routes.rb b/config/routes.rb
    index e2ae8d5…9a1d95b 100644
    — a/config/routes.rb
    +++ b/config/routes.rb
    @@ -41,7 +41,7 @@ ActionController::Routing::Routes.draw do |map|
    map.resources :architectures, :collection => {:auto_complete_search => :get}
    map.resources :ptables, :collection => {:auto_complete_search => :get}
    map.resources :config_templates, :except => [:show], :collection => { :auto_complete_search => :get }, :requirements => { :id => /[^/]+/ }
  • map.resources :subnets, :except => [:show], :collection => {:auto_complete_search => :get}
  • map.resources :subnets, :except => [:show], :collection => {:auto_complete_search => :get, :import => :get, :create_multiple => :post}
    map.connect ‘unattended/template/:id/:hostgroup’, :controller => “unattended”, :action => "template"
    end


1.7.6.4