Skip to content

Commit

Permalink
Merge pull request #3502 from rverdile/cherry-pick-for-templates
Browse files Browse the repository at this point in the history
[1.29] Backport of support for content templates
  • Loading branch information
jirihnidek authored Feb 3, 2025
2 parents 79b3bce + e7bdb17 commit db4183f
Show file tree
Hide file tree
Showing 3 changed files with 366 additions and 20 deletions.
6 changes: 6 additions & 0 deletions src/rhsm/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1535,6 +1535,7 @@ def registerConsumer(
facts: Optional[dict] = None,
owner: str = None,
environments: str = None,
environment_names: str = None,
keys: str = None,
installed_products: list = None,
uuid: str = None,
Expand Down Expand Up @@ -1580,6 +1581,11 @@ def registerConsumer(
for environment in environments.split(","):
env_list.append({"id": environment})
params["environments"] = env_list
elif environment_names is not None and self.has_capability(MULTI_ENV):
env_name_list = []
for env_name in environment_names.split(","):
env_name_list.append({"name": env_name})
params["environments"] = env_name_list

headers = {}
if jwt_token:
Expand Down
52 changes: 47 additions & 5 deletions src/rhsmlib/services/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from rhsm.connection import UEPConnection

from rhsmlib.services import exceptions
from rhsmlib.services.unregister import UnregisterService

from subscription_manager import injection as inj
from subscription_manager import managerlib
Expand All @@ -40,6 +41,8 @@ def register(
org: Optional[str],
activation_keys: list = None,
environments: list = None,
environment_names: list = None,
environment_type: str = None,
force: bool = False,
name: str = None,
consumerid: str = None,
Expand All @@ -49,7 +52,7 @@ def register(
service_level: str = None,
usage: str = None,
jwt_token: str = None,
**kwargs: dict
**kwargs: dict,
) -> dict:
# We accept a kwargs argument so that the DBus object can pass the options dictionary it
# receives transparently to the service via dictionary unpacking. This strategy allows the
Expand All @@ -60,6 +63,11 @@ def register(
if kwargs:
raise exceptions.ValidationError(_("Unknown arguments: %s") % kwargs.keys())

if environments is not None and environment_names is not None:
raise exceptions.ValidationError(
_("Environment IDs and environment names are mutually exclusive")
)

syspurpose = syspurposelib.read_syspurpose()

save_syspurpose = False
Expand Down Expand Up @@ -89,6 +97,7 @@ def register(
options = {
"activation_keys": activation_keys,
"environments": environments,
"environment_names": environment_names,
"force": force,
"name": name,
"consumerid": consumerid,
Expand Down Expand Up @@ -117,6 +126,7 @@ def register(
facts=facts_dict,
owner=org,
environments=environments,
environment_names=environment_names,
keys=options.get("activation_keys"),
installed_products=self.installed_mgr.format_for_server(),
content_tags=self.installed_mgr.tags,
Expand All @@ -132,6 +142,42 @@ def register(
cp_provider = inj.require(inj.CP_PROVIDER)
cp_provider.close_all_connections()

# If environment type was specified, then check that all returned
# environments have required type. Otherwise, raise exception
wrong_env_names = []
if environment_type is not None:
for environment in consumer.get("environments", []):
env_type = environment.get("type", None)
if env_type != environment_type:
environment_name = environment["name"]
log.error(
f"Environment: '{environment_name}' does not have required type: '{environment_type},"
f" it has '{env_type}' type"
)
wrong_env_names.append(environment_name)

if len(wrong_env_names) > 0:
# We will not use this consumer object. Thus, delete this object
# on the server
self.identity.reload()
UnregisterService(inj.require(inj.CP_PROVIDER).get_consumer_auth_cp()).unregister()
if len(wrong_env_names) == 1:
raise exceptions.ServiceError(
_(
"Environment: '{env_names}' does not have required type '{environment_type}'".format(
env_names=wrong_env_names[0], environment_type=environment_type
)
)
)
else:
raise exceptions.ServiceError(
_(
"Environments: '{env_names}' do not have required type '{environment_type}'".format(
env_names=", ".join(wrong_env_names), environment_type=environment_type
)
)
)

self.installed_mgr.write_cache()
self.plugin_manager.run("post_register_consumer", consumer=consumer, facts=facts_dict)
managerlib.persist_consumer_cert(consumer)
Expand Down Expand Up @@ -213,10 +259,6 @@ def validate_options(self, options: dict) -> None:
raise exceptions.ValidationError(
_("Error: Activation keys can not be used with previously" " registered IDs.")
)
elif options["environments"]:
raise exceptions.ValidationError(
_("Error: Activation keys do not allow environments to be" " specified.")
)
elif options.get("jwt_token") is not None:
# TODO: add more checks here
pass
Expand Down
Loading

0 comments on commit db4183f

Please sign in to comment.