Class: Api::V2::DiscoveredHostsController
- Inherits:
-
BaseController
- Object
- BaseController
- Api::V2::DiscoveredHostsController
- Includes:
- Foreman::Controller::DiscoveredExtensions
- Defined in:
- app/controllers/api/v2/discovered_hosts_controller.rb
Instance Method Summary collapse
- #auto_provision ⇒ Object
- #auto_provision_all ⇒ Object
- #create ⇒ Object
- #destroy ⇒ Object
- #facts ⇒ Object
- #index ⇒ Object
- #reboot ⇒ Object
- #refresh_facts ⇒ Object
- #show ⇒ Object
- #update ⇒ Object
Instance Method Details
#auto_provision ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'app/controllers/api/v2/discovered_hosts_controller.rb', line 119 def auto_provision @discovered_host.transaction do if rule = find_discovery_rule(@discovered_host) msg = _("Host %{host} was provisioned with rule %{rule}") % {:host => @discovered_host.name, :rule => rule.name} process_response perform_auto_provision(@discovered_host, rule), msg else process_success _("No rule found for host %s") % @discovered_host.name end end rescue ::Foreman::Exception => e render :json => {'message' => e.to_s}, :status => :unprocessable_entity end |
#auto_provision_all ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'app/controllers/api/v2/discovered_hosts_controller.rb', line 134 def auto_provision_all result = true = _("Errors during auto provisioning: %s") if Host::Discovered.count == 0 = _("No discovered hosts to provision") result = false end Host.transaction do overall_errors = "" Host::Discovered.all.each do |discovered_host| if rule = find_discovery_rule(discovered_host) result &= perform_auto_provision(discovered_host, rule) unless discovered_host.errors.empty? errors = discovered_host.errors..join(' ') logger.warn "Failed to auto provision host %s: %s" % [discovered_host.name, errors] overall_errors << "#{discovered_host.name}: #{errors} " end else logger.warn "No rule found for host %s" % discovered_host.name end end if result process_success _("Discovered hosts are provisioning now") else render_error :custom_error, :status => :unprocessable_entity, :locals => { :message => % overall_errors } end end end |
#create ⇒ Object
42 43 44 45 |
# File 'app/controllers/api/v2/discovered_hosts_controller.rb', line 42 def create @discovered_host = Host::Discovered.new(params[:discovered_host]) process_response @discovered_host.save end |
#destroy ⇒ Object
90 91 92 |
# File 'app/controllers/api/v2/discovered_hosts_controller.rb', line 90 def destroy process_response @discovered_host.destroy end |
#facts ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'app/controllers/api/v2/discovered_hosts_controller.rb', line 97 def facts state = true Host.transaction do @discovered_host, state = Host::Discovered.import_host_and_facts(params[:facts]) end if Setting['discovery_auto'] @discovered_host.transaction do if state && rule = find_discovery_rule(@discovered_host) state = perform_auto_provision(@discovered_host, rule) else Rails.logger.warn "Discovered facts import unsuccessful, skipping auto provisioning" end end end process_response state rescue ::Foreman::Exception => e render :json => {'message'=>e.to_s}, :status => :unprocessable_entity end |
#index ⇒ Object
21 22 23 |
# File 'app/controllers/api/v2/discovered_hosts_controller.rb', line 21 def index @discovered_hosts = resource_scope.search_for(*).paginate() end |
#reboot ⇒ Object
182 183 184 185 186 |
# File 'app/controllers/api/v2/discovered_hosts_controller.rb', line 182 def reboot process_response @discovered_host.reboot rescue ::Foreman::Exception => e render :json => {'message'=>e.to_s} end |
#refresh_facts ⇒ Object
173 174 175 176 177 |
# File 'app/controllers/api/v2/discovered_hosts_controller.rb', line 173 def refresh_facts process_response @discovered_host.refresh_facts rescue ::Foreman::Exception => e render :json => { 'message' => e.to_s } end |
#show ⇒ Object
28 29 |
# File 'app/controllers/api/v2/discovered_hosts_controller.rb', line 28 def show end |
#update ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'app/controllers/api/v2/discovered_hosts_controller.rb', line 78 def update Host.transaction do @host = ::ForemanDiscovery::HostConverter.to_managed(@discovered_host) forward_request_url update_response = @host.update_attributes(params[:discovered_host]) process_response update_response end end |