Class: Api::Rhsm::CandlepinProxiesController

Inherits:
V2::ApiController
  • Object
show all
Includes:
Katello::Authentication::ClientAuthentication
Defined in:
app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb

Instance Method Summary collapse

Methods included from Katello::Authentication::ClientAuthentication

#add_candlepin_version_header, #authenticate_client, #cert_from_request, #cert_present?, #set_client_user

Instance Method Details

#available_releasesObject



140
141
142
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 140

def available_releases
  render :json => @host.content_facet.try(:available_releases) || []
end

#consumer_activateObject

used for registering with activation keys api :POST, “/consumers”, N_(“Register a system with activation key (compatibility)”) param :activation_keys, String, :required => true



216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 216

def consumer_activate
  # Activation keys are userless by definition so use the internal generic user
  # Set it before calling find_activation_keys to allow communication with candlepin
  User.current = User.anonymous_admin
  activation_keys = find_activation_keys
  host = Katello::Host::SubscriptionFacet.find_or_create_host(params[:facts]['network.hostname'],
                                activation_keys.first.organization, rhsm_params)

  sync_task(::Actions::Katello::Host::Register, host, System.new, rhsm_params, nil, activation_keys)
  host.reload

  render :json => Resources::Candlepin::Consumer.get(host.subscription_facet.uuid)
end

#consumer_checkinObject

api :PUT, “/consumers/:id/checkin/”, N_(“Update consumer check-in time”) param :date, String, :desc => N_(“check-in time”)



124
125
126
127
128
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 124

def consumer_checkin
  @host.update_attributes(:last_checkin => params[:date])
  Candlepin::Consumer.new(@host.subscription_facet.uuid).checkin(params[:date])
  render :json => Resources::Candlepin::Consumer.get(@host.subscription_facet.uuid)
end

#consumer_createObject

api :POST, “/environments/:environment_id/consumers”, N_(“Register a consumer in environment”)



193
194
195
196
197
198
199
200
201
202
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 193

def consumer_create
  content_view_environment = find_content_view_environment
  host = Katello::Host::SubscriptionFacet.find_or_create_host(params[:facts]['network.hostname'],
             content_view_environment.environment.organization, rhsm_params)

  sync_task(::Actions::Katello::Host::Register, host, System.new, rhsm_params, content_view_environment)
  host.reload

  render :json => Resources::Candlepin::Consumer.get(host.subscription_facet.uuid)
end

#consumer_destroyObject

api :DELETE, “/consumers/:id”, N_(“Unregister a consumer”) param :id, String, :desc => N_(“UUID of the consumer”), :required => true



206
207
208
209
210
211
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 206

def consumer_destroy
  User.as_anonymous_admin do
    sync_task(::Actions::Katello::Host::Unregister, @host)
  end
  render :text => _("Deleted consumer '%s'") % params[:id], :status => 204
end

#consumer_showObject

api :GET, “/consumers/:id”, N_(“Show a system”) param :id, String, :desc => N_(“UUID of the consumer”), :required => true



92
93
94
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 92

def consumer_show
  render :json => Resources::Candlepin::Consumer.get(params[:id])
end

#deleteObject



72
73
74
75
76
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 72

def delete
  r = Resources::Candlepin::Proxy.delete(@request_path, @request_body.read)
  logger.debug r
  render :json => r
end

#drop_api_namespace(original_request_path) ⇒ Object



61
62
63
64
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 61

def drop_api_namespace(original_request_path)
  prefix = "/rhsm"
  original_request_path.gsub(prefix, '')
end

#enabled_reposObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 170

def enabled_repos
  repos_params = params['enabled_repos'] rescue raise(HttpErrors::BadRequest, _("Expected attribute is missing:") + " enabled_repos")
  repos_params = repos_params['repos'] || []

  paths = repos_params.map do |repo|
    if !repo['baseurl'].blank?
      URI(repo['baseurl'].first).path
    else
      logger.warn("System #{@host.name} (#{@host.id}) attempted to bind to unspecific repo (#{repo}).")
      nil
    end
  end

  result = nil
  User.as_anonymous_admin do
    @host.content_host.save_bound_repos_by_path!(paths.compact)
    result = @host.content_facet.update_repositories_by_paths(paths.compact)
  end

  respond_for_show :resource => result
end

#factsObject



244
245
246
247
248
249
250
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 244

def facts
  User.as_anonymous_admin do
    sync_task(::Actions::Katello::Host::Update, @host, rhsm_params)
    Katello::Host::SubscriptionFacet.update_facts(@host, rhsm_params[:facts]) unless rhsm_params[:facts].nil?
  end
  render :json => {:content => _("Facts successfully updated.")}, :status => 200
end

#getObject



66
67
68
69
70
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 66

def get
  r = Resources::Candlepin::Proxy.get(@request_path)
  logger.debug r
  render :json => r
end

#hypervisors_updateObject

api :POST, “/hypervisors”, N_(“Update the hypervisors information for environment”) desc ‘See virt-who tool for more details.’



113
114
115
116
117
118
119
120
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 113

def hypervisors_update
   = User.consumer? ? User.anonymous_api_admin. : User.current.
  task = User.as() do
    sync_task(::Actions::Katello::Host::Hypervisors, @environment, @content_view,
                        params.except(:controller, :action, :format))
  end
  render :json => task.output[:results]
end

#list_ownersObject



144
145
146
147
148
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 144

def list_owners
  orgs = User.current.allowed_organizations
  # rhsm expects owner (Candlepin format)
  respond_for_index :collection => orgs.map { |o| { :key => o.label, :displayName => o.name } }
end

#postObject



78
79
80
81
82
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 78

def post
  r = Resources::Candlepin::Proxy.post(@request_path, @request_body.read)
  logger.debug r
  render :json => r
end

#proxy_request_bodyObject



57
58
59
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 57

def proxy_request_body
  @request_body = @_request.body
end

#proxy_request_pathObject



53
54
55
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 53

def proxy_request_path
  @request_path = drop_api_namespace(@_request.fullpath)
end

#putObject



84
85
86
87
88
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 84

def put
  r = Resources::Candlepin::Proxy.put(@request_path, @request_body.read)
  logger.debug r
  render :json => r
end

#regenerate_identity_certificatesObject

api :POST, “/consumers/:id”, N_(“Regenerate consumer identity”) param :id, String, :desc => N_(“UUID of the consumer”) desc ‘Schedules the consumer identity certificate regeneration’



153
154
155
156
157
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 153

def regenerate_identity_certificates
  uuid = @host.subscription_facet.uuid
  Candlepin::Consumer.new(uuid).regenerate_identity_certificates
  render :json => Resources::Candlepin::Consumer.get(uuid)
end

#repackage_messageObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 26

def repackage_message
  yield
ensure
  if response.status >= 400
    begin
      body_json = JSON.parse(response.body)
      if body_json['message'] && body_json['displayMessage'].nil?
        body_json['displayMessage'] = body_json['message']
      end
      response.body = body_json.to_s

    # rubocop:disable HandleExceptions
    rescue JSON::ParserError
      # Not a json response, leave as-is
    end
  end
end

#rhsm_indexObject

api :GET, “/owners/:organization_id/environments”, N_(“List environments for RHSM”)



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 97

def rhsm_index
  organization = find_organization
  @all_environments = get_content_view_environments(params[:name], organization).collect do |env|
    {
      :id  => env.cp_id,
      :name => env.label,
      :display_name => env.name,
      :description => env.content_view.description
    }
  end

  respond_for_index :collection => @all_environments
end

#serialsObject



252
253
254
255
256
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 252

def serials
  @host.subscription_facet.last_checkin = DateTime.now
  @host.subscription_facet.save!
  render :json => Katello::Resources::Candlepin::Consumer.serials(@host.subscription_facet.uuid)
end

#server_statusObject

api :GET, “/status”, N_(“Shows version information”) description N_(“This service is available for unauthenticated users”)



232
233
234
235
236
237
238
239
240
241
242
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 232

def server_status
  status = { :managerCapabilities => Resources::Candlepin::CandlepinPing.ping['managerCapabilities'],
             :result => Resources::Candlepin::CandlepinPing.ping['result'],
             :rulesSource => Resources::Candlepin::CandlepinPing.ping['rulesSource'],
             :rulesVersion => Resources::Candlepin::CandlepinPing.ping['rulesVersion'],
             :standalone => Resources::Candlepin::CandlepinPing.ping['standalone'],
             :timeUTC => Time.now.getutc,
             :version => Katello::VERSION }

  render :json => status
end

#upload_package_profileObject

api :PUT, “/consumers/:id/packages”, N_(“Update installed packages”) api :PUT, “/consumers/:id/profile”, N_(“Update installed packages”) param :id, String, :desc => N_(“UUID of the consumer”), :required => true



133
134
135
136
137
138
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 133

def upload_package_profile
  User.as_anonymous_admin do
    sync_task(::Actions::Katello::Host::UploadPackageProfile, @host, params[:_json])
  end
  render :json => Resources::Candlepin::Consumer.get(@host.subscription_facet.uuid)
end