Class: Katello::Api::V2::HostSubscriptionsController

Inherits:
ApiController
  • Object
show all
Defined in:
app/controllers/katello/api/v2/host_subscriptions_controller.rb

Instance Method Summary collapse

Methods inherited from ApiController

#full_result_response, #resource_class, #scoped_search

Methods included from Rendering

#respond_for_async, #respond_for_bulk_async, #respond_for_create, #respond_for_destroy, #respond_for_index, #respond_for_show, #respond_for_status, #respond_for_update, #respond_with_template, #respond_with_template_collection, #respond_with_template_resource, #try_specific_collection_template, #try_specific_resource_template

Methods included from Katello::Api::Version2

#api_version

Instance Method Details

#add_subscriptionsObject



114
115
116
117
118
119
120
121
# File 'app/controllers/katello/api/v2/host_subscriptions_controller.rb', line 114

def add_subscriptions
  pools_with_quantities = params[:subscriptions].map do |sub_params|
    PoolWithQuantities.new(Pool.find(sub_params['id']), sub_params['quantity'])
  end

  sync_task(::Actions::Katello::Host::AttachSubscriptions, @host, pools_with_quantities)
  respond_for_index(:collection => index_response, :template => "index")
end

#auto_attachObject



27
28
29
30
# File 'app/controllers/katello/api/v2/host_subscriptions_controller.rb', line 27

def auto_attach
  sync_task(::Actions::Katello::Host::AutoAttachSubscriptions, @host)
  respond_for_index(:collection => index_response, :template => "index")
end

#content_overrideObject



127
128
129
130
131
132
# File 'app/controllers/katello/api/v2/host_subscriptions_controller.rb', line 127

def content_override
  content_override = validate_content_overrides(params)
  @host.subscription_facet.candlepin_consumer.set_content_override(content_override[:content_label], 'enabled', content_override[:value])

  product_content
end

#createObject



61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/katello/api/v2/host_subscriptions_controller.rb', line 61

def create
  rhsm_params = params_to_rhsm_params
  name = rhsm_params[:facts]['network.hostname']

  host = Katello::Host::SubscriptionFacet.find_or_create_host(name, @content_view_environment.environment.organization, rhsm_params)
  sync_task(::Actions::Katello::Host::Register, host, System.new, rhsm_params, @content_view_environment)
  host.reload
  ::Katello::Host::SubscriptionFacet.update_facts(host, rhsm_params[:facts]) unless rhsm_params[:facts].blank?

  respond_for_show(:resource => host, :template => '../../../api/v2/hosts/show')
end

#destroyObject



41
42
43
44
45
# File 'app/controllers/katello/api/v2/host_subscriptions_controller.rb', line 41

def destroy
  sync_task(::Actions::Katello::Host::Unregister, @host)
  @host.reload
  respond_for_destroy(:resource => @host)
end

#eventsObject



34
35
36
37
# File 'app/controllers/katello/api/v2/host_subscriptions_controller.rb', line 34

def events
  collection = full_result_response(@host.subscription_facet.candlepin_consumer.events.map { |e| OpenStruct.new(e) })
  respond_for_index :collection => collection
end

#indexObject



14
15
16
17
# File 'app/controllers/katello/api/v2/host_subscriptions_controller.rb', line 14

def index
  @collection = index_response
  respond_for_index :collection => @collection
end

#index_responseObject



19
20
21
22
23
# File 'app/controllers/katello/api/v2/host_subscriptions_controller.rb', line 19

def index_response
  entitlements = @host.subscription_facet.candlepin_consumer.entitlements
  subscriptions = entitlements.map { |entitlement| ::Katello::HostSubscriptionPresenter.new(entitlement) }
  full_result_response(subscriptions)
end

#params_to_rhsm_paramsObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/controllers/katello/api/v2/host_subscriptions_controller.rb', line 73

def params_to_rhsm_params
  rhsm_params = params.slice(:facts, :uuid, :name)
  rhsm_params[:releaseVer] = params['release_version'] if params['release_version']
  rhsm_params[:serviceLevel] = params['service_level'] if params['service_level']
  rhsm_params[:guestIds] = params['hypervisor_guest_uuids'] if params[:hypervisor_guest_uuids]
  rhsm_params[:type] = Katello::Candlepin::Consumer::SYSTEM
  rhsm_params[:facts] ||= {}
  rhsm_params[:facts]['network.hostname'] ||= rhsm_params[:name]

  if params['installed_products']
    rhsm_params[:installedProducts] = params['installed_products'].map do |product|
      { :productId => product['product_id'], :productName => product['product_name'] }
    end
  end
  rhsm_params
end

#product_contentObject



136
137
138
139
140
141
142
# File 'app/controllers/katello/api/v2/host_subscriptions_controller.rb', line 136

def product_content
  content = @host.subscription_facet.candlepin_consumer.available_product_content
  overrides = @host.subscription_facet.candlepin_consumer.content_overrides
  results = content.map { |product_content| Katello::ProductContentPresenter.new(product_content, overrides) }

  respond_for_index(:collection => full_result_response(results))
end

#remove_subscriptionsObject



96
97
98
99
100
101
102
103
104
105
106
# File 'app/controllers/katello/api/v2/host_subscriptions_controller.rb', line 96

def remove_subscriptions
  #combine the quantities for duplicate pools into PoolWithQuantities objects
  pool_id_quantities = params[:subscriptions].inject({}) do |new_hash, subscription|
    new_hash[subscription['id']] ||= PoolWithQuantities.new(Pool.find(subscription['id']))
    new_hash[subscription['id']].quantities << subscription['quantity']
    new_hash
  end

  @host.subscription_facet.remove_subscriptions(pool_id_quantities.values)
  respond_for_index(:collection => index_response, :template => "index")
end