redhat_subscription: require credentials only when needed (#5664)

The module currently has a static 'required_if' statement for its
parameters that forces any of 'username' or 'activationkey' or 'token'
in case state=present; while this is generally a good idea, it can be
an extra requirements in some cases. In particular, if the system is
already registered, there is no need for credentials -- some of the
operations of the module, such as manipulating pools, can be done
perfectly without credentials.

Hence:
- change the static 'required_if' to require credentials only when
  forcing the registration
- check for credentials manually when a registration is needed, i.e.
  on an unregistered system; the fail message is the same as the one
  shown by 'required_if'

Adapt the tests to this new situation:
- test_without_required_parameters now needs to mock an unregistered
  system
- add a new version of test_without_required_parameters to test an
  already registered system
- add a simple test case for only state=present usable on an already
  registered system
- remove the credentials from a test case for pool attachment that
  mocks an already registered system
This commit is contained in:
Pino Toscano 2023-03-22 20:19:55 +01:00 committed by GitHub
parent 9f67cbbe36
commit bbd68e26a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 5 deletions

View file

@ -26,6 +26,11 @@ notes:
config file and default to None.
- It is possible to interact with C(subscription-manager) only as root,
so root permissions are required to successfully run this module.
- Since community.general 6.5.0, credentials (that is, I(username) and I(password),
I(activationkey), or I(token)) are needed only in case the the system is not registered,
or I(force_register) is specified; this makes it possible to use the module to tweak an
already registered system, for example attaching pools to it (using I(pool), or I(pool_ids)),
and modifying the C(syspurpose) attributes (using I(syspurpose)).
requirements:
- subscription-manager
- Optionally the C(dbus) Python library; this is usually included in the OS
@ -1073,7 +1078,7 @@ def main():
['activationkey', 'environment'],
['activationkey', 'auto_attach'],
['pool', 'pool_ids']],
required_if=[['state', 'present', ['username', 'activationkey', 'token'], True]],
required_if=[['force_register', True, ['username', 'activationkey', 'token'], True]],
)
if getuid() != 0:
@ -1155,6 +1160,8 @@ def main():
else:
module.exit_json(changed=False, msg="System already registered.")
else:
if not username and not activationkey and not token:
module.fail_json(msg="state is present but any of the following are missing: username, activationkey, token")
try:
rhsm.enable()
rhsm.configure(**module.params)