def _get_batch_resource_group()

in src/hpcadvisor/batch_handler.py [0:0]


def _get_batch_resource_group(poolid):
    credentials = DefaultAzureCredential()

    subscription_id = env["SUBSCRIPTION"]
    resource_group = env["RG"]

    resource_client = ResourceManagementClient(credentials, subscription_id)

    attempts = 3
    resource_groups = []
    for i in range(attempts):
        try:
            resource_groups = list(resource_client.resource_groups.list())
        except Exception as e:
            log.error(f"Error getting resource groups: {e}")
            if i == attempts - 1:
                log.error(f"Cannot get resource groups. Max attempts reached")
                return None

    tag_key = "BatchAccountName"
    tag_value = env["BATCHACCOUNT"]

    target_resource_group = None
    for rg in resource_groups:
        tags = rg.tags or {}
        if (
            tag_key in tags
            and tags[tag_key] == tag_value
            and "PoolName" in tags
            and tags["PoolName"] == poolid
        ):
            target_resource_group = rg
            break

    if target_resource_group:
        log.debug(
            f"Resource group with '{tag_key}={tag_value}' and PoolName={poolid} is found:"
        )

        log.debug(f"Name: {target_resource_group.name}")
        log.debug(f"Location: {target_resource_group.location}")
        return target_resource_group.name
    else:
        log.critical(
            f"No resource group with '{tag_key}={tag_value} and PoolName={poolid}' found."
        )
        return None