Project

General

Profile

« Previous | Next » 

Revision 4c091cd8

Added by Ohad Levy over 12 years ago

  • ID 4c091cd8bfb4efbdc116743109c0a30de2f97d90

fixes #1201 - Import Subnets from DHCP server

View differences:

app/controllers/subnets_controller.rb
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
app/models/subnet.rb
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
app/views/smart_proxies/index.html.erb
<% 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>
</tr>
app/views/subnets/_fields.html.erb
<%= f.error_messages(:object_name => "Subnet") %>
<p>
<%= f.label :name, nil, :class => "span-3" %>
<%= f.text_field :name, :class => "last" %>
</p>
<p>
<%= f.label :domain, nil, :class => "span-3" %>
<%= f.collection_select :domain_id, Domain.all, :id, :name, {:include_blank => true}, {:class => "last"} %>
</p>
<p>
<%= f.label :network, nil, :class => "span-3" %>
<%= f.text_field :network, :class => "last" %>
</p>
<p>
<%= f.label :mask, nil, :class => "span-3" %>
<%= f.text_field :mask, :title => "netmask for this subnet", :class => "last" %>
</p>
<p>
<%= 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" %>
</p>
<p title="DHCP Proxy to use within this subnet">
<%= 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"} %>
</p>
<p title="TFTP Proxy to use within this subnet">
<%= 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"} %>
</p>
<p>
<%= f.label :vlanid, nil, :class => "span-3" %>
<%= f.text_field :vlanid, :class => "last" %>
</p>
<p>
<%= f.label :priority, nil, :class => "span-3"%>
<%= f.text_field :priority, :class => "last"%>
</p>
app/views/subnets/_form.html.erb
<% form_for @subnet do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name, nil, :class => "span-3" %>
<%= f.text_field :name, :class => "last" %>
</p>
<p>
<%= f.label :domain, nil, :class => "span-3" %>
<%= f.collection_select :domain_id, Domain.all, :id, :name, {:include_blank => true}, {:class => "last"} %>
</p>
<p>
<%= f.label :network, nil, :class => "span-3" %>
<%= f.text_field :network, :class => "last" %>
</p>
<p>
<%= f.label :mask, nil, :class => "span-3" %>
<%= f.text_field :mask, :title => "netmask for this subnet", :class => "last" %>
</p>
<p>
<%= 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" %>
</p>
<p title="DHCP Proxy to use within this subnet">
<%= 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"} %>
</p>
<p title="TFTP Proxy to use within this subnet">
<%= 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"} %>
</p>
<p>
<%= f.label :vlanid, nil, :class => "span-3" %>
<%= f.text_field :vlanid, :class => "last" %>
</p>
<p>
<%= f.label :priority, nil, :class => "span-3"%>
<%= f.text_field :priority, :class => "last"%>
</p>
<%= render 'fields', :f => f %>
<p><%= f.submit %></p>
<% end %>
app/views/subnets/import.html.erb
<% title "Import subnets" %>
<% form_tag create_multiple_subnets_path do -%>
<% @subnets.each do |subnet| -%>
<h3><%= subnet -%></h3>
<hr>
<% fields_for "subnets[]", subnet do |f| -%>
<%= render 'fields', :f => f %>
<% end -%>
<% end -%>
<%= submit_tag %>
<% end -%>
config/routes.rb
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

Also available in: Unified diff