Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #14158 - Add support for tailoring files
- Loading branch information
Ondrej Prazak
committed
Jan 2, 2017
1 parent
a726ad0
commit a8a8b15
Showing
44 changed files
with
856 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
app/controllers/api/v2/compliance/tailoring_files_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
module Api::V2 | ||
module Compliance | ||
class TailoringFilesController < ::Api::V2::BaseController | ||
include Foreman::Controller::Parameters::TailoringFile | ||
before_filter :find_resource, :except => %w(index create) | ||
|
||
def resource_name | ||
'::ForemanOpenscap::TailoringFile' | ||
end | ||
|
||
def get_resource | ||
instance_variable_get :"@tailoring_file" or fail 'no resource loaded' | ||
end | ||
|
||
api :GET, '/compliance/tailoring_files', N_('List Tailoring files') | ||
param_group :search_and_pagination, ::Api::V2::BaseController | ||
|
||
def index | ||
@tailoring_files = resource_scope_for_index(:permission => :view_tailoring_files) | ||
end | ||
|
||
api :GET, '/compliance/tailoring_files/:id/xml', N_('Show a Tailoring file as XML') | ||
param :id, :identifier, :required => true | ||
|
||
def xml | ||
send_data @tailoring_file.scap_file, | ||
:type => 'application/xml', | ||
:filename => @tailoring_file.original_filename || "#{@tailoring_file.name}.xml" | ||
end | ||
|
||
api :GET, '/compliance/tailoring_files/:id', N_('Show a Tailoring file') | ||
param :id, :identifier, :required => true | ||
def show | ||
end | ||
|
||
def_param_group :tailoring_file do | ||
param :tailoring_file, Hash, :required => true, :action_aware => true do | ||
param :name, String, :required => true, :desc => N_('Tailoring file name') | ||
param :scap_file, String, :required => true, :desc => N_('XML containing tailoring file') | ||
param :original_filename, String, :desc => N_('Original file name of the XML file') | ||
param_group :taxonomies, ::Api::V2::BaseController | ||
end | ||
end | ||
|
||
api :POST, '/compliance/tailoring_files', N_('Create a Tailoring file') | ||
param_group :tailoring_file, :as => :create | ||
|
||
def create | ||
@tailoring_file = ForemanOpenscap::TailoringFile.new(tailoring_file_params) | ||
process_response @tailoring_file.save | ||
end | ||
|
||
api :PUT, '/compliance/tailoring_files/:id', N_('Update a Tailoring file') | ||
param :id, :identifier, :required => true | ||
param_group :tailoring_file | ||
|
||
def update | ||
process_response @tailoring_file.update_attributes(tailoring_file_params) | ||
end | ||
|
||
api :DELETE, '/compliance/tailoring_files/:id', N_('Deletes a Tailoring file') | ||
param :id, :identifier, :required => true | ||
|
||
def destroy | ||
process_response @tailoring_file.destroy | ||
end | ||
|
||
private | ||
def find_resource | ||
not_found and return if params[:id].blank? | ||
instance_variable_set("@tailoring_file", resource_scope.find(params[:id])) | ||
end | ||
|
||
def action_permission | ||
case params[:action] | ||
when 'xml' | ||
:view | ||
else | ||
super | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
app/controllers/concerns/foreman/controller/parameters/tailoring_file.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Foreman::Controller::Parameters::TailoringFile | ||
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
def tailoring_file_params_filter | ||
Foreman::ParameterFilter.new(::ForemanOpenscap::TailoringFile).tap do |filter| | ||
filter.permit :name, :scap_file, :original_filename, :location_ids => [], :organization_ids => [] | ||
end | ||
end | ||
end | ||
|
||
def tailoring_file_params | ||
self.class.tailoring_file_params_filter.filter_params(params, parameter_filter_context) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
class TailoringFilesController < ApplicationController | ||
include Foreman::Controller::AutoCompleteSearch | ||
include Foreman::Controller::Parameters::TailoringFile | ||
|
||
before_filter :find_tailoring_file, :only => [:destroy, :update, :edit, :xml] | ||
before_filter :handle_file_upload, :only => [:create, :update] | ||
|
||
def model_of_controller | ||
::ForemanOpenscap::TailoringFile | ||
end | ||
|
||
def index | ||
@tailoring_files = resource_base.search_for(params[:search], :order => params[:order]) | ||
.paginate(:page => params[:page], :per_page => params[:per_page]) | ||
end | ||
|
||
def new | ||
@tailoring_file = ::ForemanOpenscap::TailoringFile.new | ||
end | ||
|
||
def create | ||
@tailoring_file = ForemanOpenscap::TailoringFile.new(tailoring_file_params) | ||
if @tailoring_file.save | ||
process_success | ||
else | ||
process_error | ||
end | ||
end | ||
|
||
def edit | ||
end | ||
|
||
def update | ||
if @tailoring_file.update_attributes(tailoring_file_params) | ||
process_success | ||
else | ||
process_error | ||
end | ||
end | ||
|
||
def destroy | ||
if @tailoring_file.destroy | ||
process_success | ||
else | ||
process_error :object => @tailoring_file | ||
end | ||
end | ||
|
||
def xml | ||
send_data @tailoring_file.scap_file, | ||
:type => 'application/xml', | ||
:filename => @tailoring_file.original_filename || "#{@tailoring_file.name}.xml" | ||
end | ||
|
||
private | ||
|
||
def find_tailoring_file | ||
@tailoring_file = resource_base.find(params[:id]) | ||
end | ||
|
||
def handle_file_upload | ||
return unless params[:tailoring_file] && raw_file = params[:tailoring_file][:scap_file] | ||
params[:tailoring_file][:original_filename] = raw_file.original_filename | ||
params[:tailoring_file][:scap_file] = raw_file.tempfile.read if raw_file.respond_to?(:tempfile) && raw_file.tempfile.respond_to?(:read) | ||
end | ||
|
||
def action_permission | ||
case params[:action] | ||
when 'xml' | ||
:view | ||
else | ||
super | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.