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

Allow to call RegisterWithActivationKeys() with enable_content #3488

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
27 changes: 15 additions & 12 deletions src/rhsmlib/dbus/objects/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

from subscription_manager.i18n import Locale
from subscription_manager.i18n import ugettext as _
from subscription_manager.entcertlib import EntCertActionInvoker
from subscription_manager.utils import is_true_value

if TYPE_CHECKING:
from rhsm.connection import UEPConnection
Expand Down Expand Up @@ -262,10 +262,12 @@ def register_with_activation_keys(
uep: UEPConnection = self.build_uep(connection_options)
service = RegisterService(uep)

enable_content: bool = self._remove_enable_content_option(register_options, default=True)
consumer: dict = service.register(organization, **register_options)

ent_cert_lib = EntCertActionInvoker()
ent_cert_lib.update()
# When consumer is created, we can try to enable content, if requested.
if enable_content is True:
self._enable_content(uep, consumer)

return consumer

Expand All @@ -284,20 +286,25 @@ def _on_no_organization(self, username: str) -> None:
"""
raise NoOrganizationException(username=username)

def _remove_enable_content_option(self, options: dict) -> bool:
@staticmethod
def _remove_enable_content_option(options: dict, default: bool = False) -> bool:
"""Try to remove enable_content option from options dictionary.

:returns: The value of 'enable_content' key.
"""
if "enable_content" not in options:
return False
return default

return bool(options.pop("enable_content"))
enable_content = options.pop("enable_content")
if is_true_value(enable_content):
return True
else:
return False

def _enable_content(self, uep: "UEPConnection", consumer: dict) -> None:
@staticmethod
def _enable_content(uep: "UEPConnection", consumer: dict) -> None:
"""Try to enable content: refresh SCA entitlement certs in SCA mode."""
content_access: str = consumer["owner"]["contentAccessMode"]
enabled_content = None

if content_access == "entitlement":
log.error("Entitlement content access mode is not supported")
Expand All @@ -306,13 +313,9 @@ def _enable_content(self, uep: "UEPConnection", consumer: dict) -> None:
service = EntitlementService(uep)
# TODO: try get anything useful from refresh result. It is not possible atm.
service.refresh(remove_cache=False, force=False)

else:
log.error(f"Unable to enable content due to unsupported content access mode: '{content_access}'")

if enabled_content is not None:
consumer["enabledContent"] = enabled_content

def _check_force_handling(self, register_options: dict, connection_options: dict) -> None:
"""
Handles "force=true" in the registration options
Expand Down
32 changes: 24 additions & 8 deletions test/rhsmlib/dbus/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,20 +299,12 @@ def setUp(self) -> None:
self.patches["is_registered"] = is_registered_patch.start()
self.addCleanup(is_registered_patch.stop)

update_patch = mock.patch(
"rhsmlib.dbus.objects.register.EntCertActionInvoker.update",
name="update",
)
self.patches["update"] = update_patch.start()
self.addCleanup(update_patch.stop)

build_uep_patch = mock.patch(
"rhsmlib.dbus.base_object.BaseImplementation.build_uep",
name="build_uep",
)
self.patches["build_uep"] = build_uep_patch.start()
self.addCleanup(build_uep_patch.stop)
self.patches["update"].return_value = None

def test_Register(self):
expected = json.loads(CONSUMER_CONTENT_JSON_SCA)
Expand Down Expand Up @@ -430,3 +422,27 @@ def test_RegisterWithActivationKeys__with_force_option(self):
{"host": "localhost", "port": "8443", "handler": "/candlepin"},
)
self.assertEqual(expected, result)

def test_RegisterWithActivationKeys__with_enable_content_option(self):
expected = json.loads(CONSUMER_CONTENT_JSON_SCA)
self.patches["is_registered"].return_value = False
self.patches["register"].return_value = expected

result = self.impl.register_with_activation_keys(
"username",
{"keys": ["key1", "key2"], "enable_content": "true"},
{"host": "localhost", "port": "8443", "handler": "/candlepin"},
)
self.assertEqual(expected, result)

def test_RegisterWithActivationKeys__with_disable_content_option(self):
expected = json.loads(CONSUMER_CONTENT_JSON_SCA)
self.patches["is_registered"].return_value = False
self.patches["register"].return_value = expected

result = self.impl.register_with_activation_keys(
"username",
{"keys": ["key1", "key2"], "enable_content": "false"},
{"host": "localhost", "port": "8443", "handler": "/candlepin"},
)
self.assertEqual(expected, result)
Loading