Skip to content

Commit

Permalink
Fix ansible-collections#5313: redhat_subscription module is not idemp…
Browse files Browse the repository at this point in the history
…otent when pool_ids

This fix ensures the idempotency of the redhat_subscription module when pool_ids are used. The main problem was, that a 'None' quantity was not properly handled and that the quantity check compared a string with an integer.

Signed-off-by: Christoph Fiehe <[email protected]>
  • Loading branch information
Christoph Fiehe committed Oct 3, 2022
1 parent 394647d commit 1e66d0f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
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

0 comments on commit 1e66d0f

Please sign in to comment.