Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #19954 - Add CSV export on content host index #6814

Merged
merged 1 commit into from
Jul 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions app/controllers/katello/concerns/hosts_controller_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ module Concerns
module HostsControllerExtensions
extend ActiveSupport::Concern
include ForemanTasks::Triggers

included do
alias_method_chain :action_permission, :katello

def destroy
sync_task(::Actions::Katello::Host::Destroy, @host)
process_success(:success_redirect => hosts_path)
Expand All @@ -23,6 +26,36 @@ def puppet_environment_for_content_view
cvpe = Katello::ContentViewPuppetEnvironment.where(:environment_id => environment, :content_view_version_id => version).first
render :json => cvpe.nil? ? nil : {:name => cvpe.puppet_environment.name, :id => cvpe.puppet_environment.id}
end

def content_hosts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having a separate endpoint for 'content host' information is not ideal. We've worked on unifying these concepts at the api and model layer and I think we should continue work towards that. This would mean that the list of attributes for csv would need to be pluggable, but i think thats a better route and could be used for our extensions of hostgroup as well.

respond_to do |format|
format.csv do
@hosts = resource_base_with_search.where(organization_id: params[:organization_id])
.preload(:subscription_facet, :host_statuses, :operatingsystem,
:applicable_rpms, :lifecycle_environment, :content_view)
csv_response(@hosts,
[:name, :subscription_status_label, 'applicable_errata.security.size',
'applicable_errata.bugfix.size', 'applicable_errata.enhancement.size',
'applicable_rpms.size', :operatingsystem, :lifecycle_environment, :content_view,
'subscription_facet.registered_at', 'subscription_facet.last_checkin'],
['Name', 'Subscription Status', 'Installable Updates - Security',
'Installable Updates - Bug Fixes', 'Installable Updates - Enhancements',
'Installable Updates - Package Count', 'OS', 'Environment', 'Content View',
'Registered', 'Last Checkin'])
end
end
end
end

private

def action_permission_with_katello
case params[:action]
when 'content_hosts'
'view'
else
action_permission_without_katello
end
end
end
end
Expand Down
1 change: 1 addition & 0 deletions config/routes/overrides.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def matches?(request)

resources :hosts, :only => [] do
get 'puppet_environment_for_content_view', :on => :collection
get :content_hosts, :on => :collection
end

resources :smart_proxies, :only => [] do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,27 @@
* within the table.
*/
angular.module('Bastion.content-hosts').controller('ContentHostsController',
['$scope', '$q', '$state', '$location', '$window', '$uibModal', 'translate', 'Nutupane', 'Host', 'HostBulkAction', 'GlobalNotification', 'CurrentOrganization', 'ContentHostsHelper',
function ($scope, $q, $state, $location, $window, $uibModal, translate, Nutupane, Host, HostBulkAction, GlobalNotification, CurrentOrganization, ContentHostsHelper) {
var nutupane, params;
['$scope', '$q', '$state', '$location', '$window', '$uibModal', 'translate', 'Nutupane', 'Host', 'HostBulkAction', 'GlobalNotification', 'CurrentOrganization', 'ContentHostsHelper', '$httpParamSerializer',
function ($scope, $q, $state, $location, $window, $uibModal, translate, Nutupane, Host, HostBulkAction, GlobalNotification, CurrentOrganization, ContentHostsHelper, $httpParamSerializer) {
var nutupane, params, query;

$scope.successMessages = [];
$scope.errorMessages = [];

if ($location.search().search) {
query = '"' + $location.search().search + '"';
} else {
query = "";
}

params = {
'organization_id': CurrentOrganization,
'search': $location.search().search || "",
'search': query,
'sort_by': 'name',
'sort_order': 'ASC'
};

$scope.csvQuery = $httpParamSerializer(params);
nutupane = new Nutupane(Host, params);
$scope.controllerName = 'hosts';
nutupane.masterOnly = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ <h2 translate>Content Hosts</h2>
</div>

<div data-block="list-actions">
<a href="/hosts/content_hosts.csv?{{csvQuery}}" class="btn btn-default" target="_self">
<span translate>Export</span>
</a>
<button class="btn btn-default"
type="button"
ng-show="permitted('create_hosts')"
Expand Down
2 changes: 1 addition & 1 deletion katello.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Gem::Specification.new do |gem|

# UI
gem.add_dependency "deface", '>= 1.0.2', '< 2.0.0'
gem.add_dependency "bastion", ">= 5.0.0", "< 6.0.0"
gem.add_dependency "bastion", ">= 5.0.5", "< 6.0.0"

# Testing
gem.add_development_dependency "factory_girl_rails"
Expand Down
1 change: 1 addition & 0 deletions lib/katello/permissions/host_permissions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
]

Foreman::AccessControl.permission(:view_hosts).actions.concat [
'hosts/content_hosts',
'hosts/puppet_environment_for_content_view',
'katello/api/v2/host_autocomplete/auto_complete_search',
'katello/api/v2/host_errata/index',
Expand Down
34 changes: 34 additions & 0 deletions test/controllers/foreman/hosts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,38 @@ def test_puppet_environment_for_content_view

assert_response :success
end

context 'csv' do
setup do
models
@host = FactoryGirl.create(:host, :with_content, :lifecycle_environment => @library,
:content_view => @library_dev_staging_view)
@host2 = FactoryGirl.create(:host, :with_content, :organization_id => @host.organization_id,
:content_view => @library_dev_staging_view,
:lifecycle_environment => @library)
end

def test_csv_export
get :content_hosts, :format => 'csv',
:organization_id => @host.organization_id
assert_equal "text/csv; charset=utf-8", response.headers["Content-Type"]
assert_equal "no-cache", response.headers["Cache-Control"]
assert_equal "attachment; filename=\"hosts-#{Date.today}.csv\"", response.headers["Content-Disposition"]
buf = response.stream.instance_variable_get(:@buf)
assert buf.is_a? Enumerator
assert_equal "Name,Subscription Status,Installable Updates - Security,Installable Updates - Bug \
Fixes,Installable Updates - Enhancements,Installable Updates - Package Count,OS,Environment,\
Content View,Registered,Last Checkin\n",
buf.next
assert_equal 3, buf.count
end

def test_csv_export_search
get :content_hosts, :format => 'csv',
:organization_id => @host.organization_id,
:search => "name = #{@host.name}"
buf = response.stream.instance_variable_get(:@buf)
assert_equal 2, buf.count
end
end
end