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

feat: Enable register with environment names & type #3480

Merged
merged 1 commit into from
Dec 19, 2024
Merged
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
6 changes: 6 additions & 0 deletions src/rhsm/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,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 @@ -1581,6 +1582,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
45 changes: 45 additions & 0 deletions src/rhsmlib/services/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,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 @@ -66,6 +68,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")
jvlcek marked this conversation as resolved.
Show resolved Hide resolved
)

syspurpose = syspurposelib.read_syspurpose()

save_syspurpose = False
Expand Down Expand Up @@ -95,6 +102,7 @@ def register(
options = {
"activation_keys": activation_keys,
"environments": environments,
"environment_names": environment_names,
"force": force,
"name": name,
"consumerid": consumerid,
Expand Down Expand Up @@ -123,6 +131,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 @@ -138,8 +147,44 @@ def register(
cp_provider: CPProvider = 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)

managerlib.persist_consumer_cert(consumer)

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
)
)
)

access_mode: str = consumer.get("owner", {}).get("contentAccessMode", "unknown")
if access_mode != "org_environment":
log.error(
Expand Down
Loading
Loading