From 595f59891d806028c19a87b3ecd00c9349ccaa30 Mon Sep 17 00:00:00 2001
From: Alexander Olofsson <alexander.olofsson@liu.se>
Date: Mon, 10 Aug 2020 16:03:52 +0200
Subject: [PATCH] Add CSR render endpoint, to ease use in unattend

---
 .../unattended_controller_extensions.rb       | 55 +++++++++++++------
 1 file changed, 38 insertions(+), 17 deletions(-)

diff --git a/app/controllers/concerns/foreman_wds/unattended_controller_extensions.rb b/app/controllers/concerns/foreman_wds/unattended_controller_extensions.rb
index 2f45e3a..c90138e 100644
--- a/app/controllers/concerns/foreman_wds/unattended_controller_extensions.rb
+++ b/app/controllers/concerns/foreman_wds/unattended_controller_extensions.rb
@@ -1,23 +1,44 @@
 module ForemanWds
   module UnattendedControllerExtensions
     def host_template
-      return super unless params[:kind] == 'wds_localboot'
-
-      begin
-        iface = @host.provision_interface
-
-        # Deploy regular DHCP and local boot TFTP
-        @host.provision_method = 'build'
-        @host.build = false
-        iface.send :rebuild_tftp
-        iface.send :rebuild_dhcp
-
-        render inline: "Success. Local boot template was deployed successfully.\n"
-      rescue StandardError => e
-        message = format('Failed to set local boot template: %{error}', error: e)
-        logger.error message
-        render text: message, status: :error, content_type: 'text/plain'
-      end
+      return wds_render_csr if params[:kind] == 'csr_attributes.yaml'
+      return wds_deploy_localboot if params[:kind] == 'wds_localboot'
+      super
+    end
+
+    def load_template_vars
+      super unless params[:kind] == 'wds_localboot'
+    end
+
+    private
+
+    def wds_render_csr
+      return render(:plain => 'Host not in build mode') unless @host and @host.build?
+
+      template = ProvisioningTemplate.find_by_name('csr_attributes.yaml')
+
+      content = @host.render_template template: template
+      raise Foreman::Exception.new(N_("Template '%s' didn't render correctly"), template.name) unless content
+
+      render plain: content
+    end
+
+    def wds_deploy_localboot
+      return render(:plain => 'Host not in build mode') unless @host and @host.build?
+
+      iface = @host.provision_interface
+
+      # Deploy regular DHCP and local boot TFTP
+      @host.provision_method = 'build'
+      @host.build = false
+      iface.send :rebuild_tftp
+      iface.send :rebuild_dhcp
+
+      render inline: "Success. Local boot template was deployed successfully.\n"
+    rescue StandardError => e
+      message = format('Failed to set local boot template: %{error}', error: e)
+      logger.error message
+      render text: message, status: :error, content_type: 'text/plain'
     end
   end
 end
-- 
GitLab