def clean_up_realm(realm_id):
    # Delete the realm if it still exists.
    print(f"Deleting realm: {realm_id}")
    try:
        delete_realm.delete_realm(PROJECT_ID, REALM_LOCATION, realm_id)
    except exceptions.NotFound:  # Ignore the non-existent realm
        return
def clean_up_realm_and_clusters(realm_id):
    # Delete the realm and the game server clusters in the realm.
    try:
        get_realm.get_realm(PROJECT_ID, REALM_LOCATION, realm_id)
    except exceptions.NotFound:  # Ignore the non-existent realm
        return

    clusters = list_clusters.list_clusters(PROJECT_ID, REALM_LOCATION, realm_id)
    for cluster in clusters:
        cluster_id = cluster.name.rsplit("/", 1)[-1]
        print(f"Deleting cluster {cluster_id} in realm {realm_id}")
        try:
            delete_cluster.delete_cluster(
                PROJECT_ID, REALM_LOCATION, realm_id, cluster_id
            )
        except exceptions.NotFound:  # Ignore the non-existent cluster
            return

    print(f"Deleting realm: {realm_id}")
    try:
        delete_realm.delete_realm(PROJECT_ID, REALM_LOCATION, realm_id)
    except exceptions.NotFound:  # Ignore the non-existent realm
        return
def test_delete_realm(test_realm):
    delete_realm.delete_realm(PROJECT_ID, REALM_LOCATION, test_realm)
    with pytest.raises(exceptions.NotFound):
        get_realm.get_realm(PROJECT_ID, REALM_LOCATION, test_realm)