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

Fix #5313: redhat_subscription module is not idempotent when pool_ids are used #5319

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- redhat_subscription - make module idempotent when ``pool_ids`` are used (https://github.com/ansible-collections/community.general/issues/5313).
16 changes: 13 additions & 3 deletions plugins/modules/packaging/os/redhat_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,15 +593,22 @@ def update_subscriptions_by_pool_ids(self, pool_ids):
consumed_pools = RhsmPools(self.module, consumed=True)

existing_pools = {}
serials_to_remove = []
for p in consumed_pools:
existing_pools[p.get_pool_id()] = p.QuantityUsed
pool_id = p.get_pool_id()
quantity_used = p.get_quantity_used()
existing_pools[pool_id] = quantity_used

quantity = pool_ids.get(pool_id, 0)
if quantity is not None and quantity != quantity_used:
serials_to_remove.append(p.Serial)

serials_to_remove = [p.Serial for p in consumed_pools if pool_ids.get(p.get_pool_id(), 0) != p.QuantityUsed]
serials = self.unsubscribe(serials=serials_to_remove)

missing_pools = {}
for pool_id, quantity in sorted(pool_ids.items()):
if existing_pools.get(pool_id, 0) != quantity:
quantity_used = existing_pools.get(pool_id, 0)
if quantity is None and quantity_used == 0 or quantity not in (None, 0, quantity_used):
missing_pools[pool_id] = quantity

self.subscribe_by_pool_ids(missing_pools)
Expand Down Expand Up @@ -635,6 +642,9 @@ def __str__(self):
def get_pool_id(self):
return getattr(self, 'PoolId', getattr(self, 'PoolID'))

def get_quantity_used(self):
return int(getattr(self, 'QuantityUsed'))

def subscribe(self):
args = "subscription-manager attach --pool %s" % self.get_pool_id()
rc, stdout, stderr = self.module.run_command(args, check_rc=True)
Expand Down