copr-backend: cleanup all orphaned subscriptions

Not just the first 100 numbers (the list query is limited to 100 items).
This commit is contained in:
Pavel Raiskup 2022-01-17 18:57:53 +01:00
parent 0e53196080
commit 9ea7f5a9b0

View file

@ -7,6 +7,7 @@ the 'copr-team' RHN account.
import logging
import os
import random
import subprocess
import sys
@ -14,7 +15,7 @@ import requests
URL_TOKEN = "https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token"
URL_SYSTEMS = "https://api.access.redhat.com/management/v1/systems?limit=1000"
URL_SYSTEMS = "https://api.access.redhat.com/management/v1/systems"
URL_DELETE = "https://api.access.redhat.com/management/v1/systems/{UUID}"
OFFLINE_TOKEN_FILE = "{{ rhn_offline_token_file }}"
@ -74,7 +75,35 @@ def get_systems(opts):
"""
Get the list of tracked systems in RHSM (list of dicts)
"""
return get_auth(URL_SYSTEMS, opts).json()["body"]
page = 1
all_systems = []
found_systems = set()
while True:
limit = 100
offset = (page-1) * limit
url = URL_SYSTEMS + "?limit={}&offset={}".format(limit, offset)
LOG.debug("Getting %s", url)
page += 1
systems = get_auth(url, opts).json()["body"]
if not systems:
break
found = 0
for new_system in systems:
name = new_system["name"]
if name in found_systems:
continue
found += 1
found_systems.add(name)
all_systems.append(new_system)
if not found:
break
LOG.debug("Found %s systems (%s in total)", found, len(found))
# randomize the order, so we can parallelize this
random.shuffle(all_systems)
return all_systems
def filter_out_systems(systems):