def get_or_set_crashdumps_configuration()

in CrashDumpsConfigure/crashdumps_configure.py [0:0]


def get_or_set_crashdumps_configuration(args:argparse.Namespace, azsphere_api_client):
    should_set_on = args.set is not None and args.set[0] == "on"
    # Confirm first if setting all device groups
    if args.set is not None and args.tenantid is None:
        choice = input("\nThis will " + ("enable" if should_set_on else "disable") + " crash dumps for *all* device groups. Are you sure? [y/n] ")
        if (len(choice) == 0 or choice.lower() not in 'yes'):
            # abort if no confirmation received
            print("Aborting...")
            return
        print(("Enabling" if should_set_on else "Disabling") + " crash dumps for all device groups...")
    try:
        tenants = [(args.tenantid[0], "")] if args.tenantid else [(tenant["Id"], tenant["Name"]) for tenant in json.loads(azsphere_api_client.list_tenants())]
    except (json.decoder.JSONDecodeError, TypeError) as e:
        logging.error("Error retrieving list of tenants.")
        logging.error(e)
        return
    if not tenants:
        print("No tenants found.")
        return
    for tenant_id, tenant_name in tenants:
        print("\nTenant Id: " + tenant_id + ("\nTenant Name: " + tenant_name if tenant_name else ""))
        if args.get:
            # doing --get
            if args.devicegroupid is None:
                # get all device groups
                try:
                    devicegroups = json.loads(azsphere_api_client.list_devicegroups(tenant_id))["Items"]
                    print_devicegroups_table(devicegroups)
                except (json.decoder.JSONDecodeError, TypeError) as e:
                    logging.error("Error getting list of device groups. Make sure the tenant id is correct and the account you selected has access to that tenant id. To switch accounts, run with the --forceinteractive argument.")
                    logging.debug(e)
                    return
            else:
                # get select device groups
                devicegroups = []
                for devicegroupid in args.devicegroupid:
                    try:
                        devicegroups.append(json.loads(azsphere_api_client.get_devicegroup(tenant_id, devicegroupid)))
                    except (json.decoder.JSONDecodeError, TypeError) as e:
                        logging.error("Error getting device group: " + devicegroupid)
                        logging.debug(e)
                        continue
                print_devicegroups_table(devicegroups)
        elif args.set:
            # doing --set
            if args.devicegroupid is None:
                # set all device groups
                try:
                    devicegroupids = [devicegroup["Id"] for devicegroup in json.loads(azsphere_api_client.list_devicegroups(tenant_id))["Items"]]
                except (json.decoder.JSONDecodeError, TypeError) as e:
                    logging.error("Error parsing list of device groups: " + devicegroupid)
                    logging.error(e)
                    return
            else:
                # set select device groups
                devicegroupids = args.devicegroupid
            logging.info("Updating device group(s). This may take a few seconds...")
            devicegroups = azsphere_api_client.set_allowcrashdumpscollection_devicegroups(tenant_id, devicegroupids, should_set_on)
            print_devicegroups_table(devicegroups)
        else:
            logging.error("Unexpected error with get and set arguments")
            return