def test_auto_attach(self): self.mock_identity.is_valid.return_value = True self.mock_identity.uuid = "id" self.mock_cp.bind.return_value = CONTENT_JSON result = attach.AttachService( self.mock_cp).attach_auto('service_level') self.assertEqual(CONTENT_JSON, result) expected_update_calls = [ mock.call('id', service_level='service_level') ] self.assertEqual(expected_update_calls, self.mock_cp.updateConsumer.call_args_list) expected_bind_calls = [ mock.call('id'), ] self.assertEqual(expected_bind_calls, self.mock_cp.bind.call_args_list) expected_plugin_calls = [ mock.call('pre_auto_attach', consumer_uuid='id'), mock.call('post_auto_attach', consumer_uuid='id', entitlement_data=CONTENT_JSON) ] self.assertEqual(expected_plugin_calls, self.mock_pm.run.call_args_list)
def test_pool_attach(self): self.mock_identity.is_valid.return_value = True self.mock_identity.uuid = "id" self.mock_cp.bindByEntitlementPool.return_value = CONTENT_JSON result = attach.AttachService(self.mock_cp).attach_pool('x', 1) self.assertEqual(CONTENT_JSON, result) expected_bind_calls = [ mock.call('id', 'x', 1), ] self.assertEqual(expected_bind_calls, self.mock_cp.bindByEntitlementPool.call_args_list) expected_plugin_calls = [ mock.call('pre_subscribe', consumer_uuid='id', pool_id='x', quantity=1), mock.call('post_subscribe', consumer_uuid='id', entitlement_data=CONTENT_JSON) ] self.assertEqual(expected_plugin_calls, self.mock_pm.run.call_args_list)
def _do_auto_attach(self, consumer): """ Try to do auto-attach, when it was requested using --auto-attach CLI option :return: None """ # Do not try to do auto-attach, when simple content access mode is used # Only print info message to stdout if is_simple_content_access(uep=self.cp, identity=self.identity): self._print_ignore_auto_attach_mesage() return if "serviceLevel" not in consumer and self.options.service_level: system_exit( os.EX_UNAVAILABLE, _( "Error: The --servicelevel option is not supported " "by the server. Did not complete your request." ), ) try: # We don't call auto_attach with self.option.service_level, because it has been already # set during service.register() call attach.AttachService(self.cp).attach_auto(service_level=None) except connection.RestlibException as rest_lib_err: mapped_message: str = ExceptionMapper().get_message(rest_lib_err) print_error(mapped_message) except Exception: log.exception("Auto-attach failed") raise
def _run_bind(self, pool, quantity, bind_callback, cert_callback, except_callback): try: attach.AttachService(self.cp_provider.get_consumer_auth_cp()).attach_pool(pool['id'], quantity) if bind_callback: ga_GObject.idle_add(bind_callback) fetch_certificates(self.certlib) if cert_callback: ga_GObject.idle_add(cert_callback) except Exception: ga_GObject.idle_add(except_callback, sys.exc_info())
def _do_command(self): """ Executes the command. """ self.assert_should_be_registered() self._validate_options() # --pool or --file turns off default auto attach if self.options.pool or self.options.file: self.auto_attach = False # Do not try to do auto-attach, when simple content access mode is used # BZ: https://bugzilla.redhat.com/show_bug.cgi?id=1826300 # if is_simple_content_access(uep=self.cp, identity=self.identity): if self.auto_attach is True: self._print_ignore_auto_attach_mesage() else: self._print_ignore_attach_message() return 0 installed_products_num = 0 return_code = 0 report = None # TODO: change to if self.auto_attach: else: pool/file stuff try: cert_action_client = ActionClient(skips=[PackageProfileActionInvoker]) cert_action_client.update() cert_update = True attach_service = attach.AttachService(self.cp) if self.options.pool: subscribed = False for pool in self.options.pool: # odd html strings will cause issues, reject them here. if pool.find("#") >= 0: system_exit(os.EX_USAGE, _("Please enter a valid numeric pool ID.")) try: ents = attach_service.attach_pool(pool, self.options.quantity) # Usually just one, but may as well be safe: for ent in ents: pool_json = ent["pool"] print( _("Successfully attached a subscription for: {name}").format( name=pool_json["productName"] ) ) log.debug( "Attached a subscription for {name}".format(name=pool_json["productName"]) ) subscribed = True except connection.RestlibException as re: log.exception(re) exception_mapper = ExceptionMapper() mapped_message = exception_mapper.get_message(re) if re.code == 403: print(mapped_message) # already subscribed. elif re.code == 400 or re.code == 404: print(mapped_message) # no such pool. else: system_exit(os.EX_SOFTWARE, mapped_message) # some other error.. don't try again if not subscribed: return_code = 1 # must be auto else: installed_products_num = len(products.InstalledProducts(self.cp).list()) # if we are green, we don't need to go to the server self.sorter = inj.require(inj.CERT_SORTER) if self.sorter.is_valid(): if not installed_products_num: print(_("No Installed products on system. " "No need to attach subscriptions.")) else: print( _( "All installed products are covered by valid entitlements. " "No need to update subscriptions at this time." ) ) cert_update = False else: # If service level specified, make an additional request to # verify service levels are supported on the server: if self.options.service_level: consumer = self.cp.getConsumer(self.identity.uuid) if "serviceLevel" not in consumer: system_exit( os.EX_UNAVAILABLE, _( "Error: The --servicelevel option is not " "supported by the server. Did not " "complete your request." ), ) attach_service.attach_auto(self.options.service_level) if self.options.service_level is not None: # RHBZ 1632797 we should only save the sla if the sla was actually # specified. The uep and consumer_uuid are None, because service_level was sent # to candlepin server using attach_service.attach_auto() save_sla_to_syspurpose_metadata( uep=None, consumer_uuid=None, service_level=self.options.service_level ) print(_("Service level set to: {}").format(self.options.service_level)) if cert_update: report = self.entcertlib.update() profile_action_client = ProfileActionClient() profile_action_client.update() if report and report.exceptions(): print(_("Entitlement Certificate(s) update failed due to the following reasons:")) for e in report.exceptions(): print("\t-", str(e)) elif self.auto_attach: if not installed_products_num: return_code = 1 else: self.sorter.force_cert_check() # Make sure that we get fresh status of installed products status_cache = inj.require(inj.ENTITLEMENT_STATUS_CACHE) status_cache.load_status( self.sorter.cp_provider.get_consumer_auth_cp(), self.sorter.identity.uuid, self.sorter.on_date, ) self.sorter.load() # run this after entcertlib update, so we have the new entitlements return_code = show_autosubscribe_output(self.cp, self.identity) except Exception as e: handle_exception("Unable to attach: {e}".format(e=e), e) # it is okay to call this no matter what happens above, # it's just a notification to perform a check self._request_validity_check() return return_code