Esempio n. 1
0
def showReport():
    rep = buildGAPIObject()
    throw_reasons = [gapi.errors.ErrorReason.INVALID]
    report = sys.argv[2].lower()
    report = REPORT_CHOICE_MAP.get(report.replace('_', ''), report)
    valid_apps = gapi.get_enum_values_minus_unspecified(
        rep._rootDesc['resources']['activities']['methods']['list']
        ['parameters']['applicationName']['enum']) + ['customer', 'user']
    if report not in valid_apps:
        controlflow.expected_argument_exit("report",
                                           ", ".join(sorted(valid_apps)),
                                           report)
    customerId = GC_Values[GC_CUSTOMER_ID]
    if customerId == MY_CUSTOMER:
        customerId = None
    filters = parameters = actorIpAddress = startTime = endTime = eventName = orgUnitId = None
    tryDate = datetime.date.today().strftime(YYYYMMDD_FORMAT)
    to_drive = False
    userKey = 'all'
    fullDataRequired = None
    i = 3
    while i < len(sys.argv):
        myarg = sys.argv[i].lower()
        if myarg == 'date':
            tryDate = utils.get_yyyymmdd(sys.argv[i + 1])
            i += 2
        elif myarg in ['orgunit', 'org', 'ou']:
            _, orgUnitId = __main__.getOrgUnitId(sys.argv[i + 1])
            i += 2
        elif myarg == 'fulldatarequired':
            fullDataRequired = []
            fdr = sys.argv[i + 1].lower()
            if fdr and fdr != 'all':
                fullDataRequired = fdr.replace(',', ' ').split()
            i += 2
        elif myarg == 'start':
            startTime = utils.get_time_or_delta_from_now(sys.argv[i + 1])
            i += 2
        elif myarg == 'end':
            endTime = utils.get_time_or_delta_from_now(sys.argv[i + 1])
            i += 2
        elif myarg == 'event':
            eventName = sys.argv[i + 1]
            i += 2
        elif myarg == 'user':
            userKey = __main__.normalizeEmailAddressOrUID(sys.argv[i + 1])
            i += 2
        elif myarg in ['filter', 'filters']:
            filters = sys.argv[i + 1]
            i += 2
        elif myarg in ['fields', 'parameters']:
            parameters = sys.argv[i + 1]
            i += 2
        elif myarg == 'ip':
            actorIpAddress = sys.argv[i + 1]
            i += 2
        elif myarg == 'todrive':
            to_drive = True
            i += 1
        else:
            controlflow.invalid_argument_exit(sys.argv[i], "gam report")
    if report == 'user':
        while True:
            try:
                if fullDataRequired is not None:
                    warnings = gapi.get_items(rep.userUsageReport(),
                                              'get',
                                              'warnings',
                                              throw_reasons=throw_reasons,
                                              date=tryDate,
                                              userKey=userKey,
                                              customerId=customerId,
                                              orgUnitID=orgUnitId,
                                              fields='warnings')
                    fullData, tryDate = _check_full_data_available(
                        warnings, tryDate, fullDataRequired)
                    if fullData < 0:
                        print('No user report available.')
                        sys.exit(1)
                    if fullData == 0:
                        continue
                page_message = gapi.got_total_items_msg('Users', '...\n')
                usage = gapi.get_all_pages(rep.userUsageReport(),
                                           'get',
                                           'usageReports',
                                           page_message=page_message,
                                           throw_reasons=throw_reasons,
                                           date=tryDate,
                                           userKey=userKey,
                                           customerId=customerId,
                                           orgUnitID=orgUnitId,
                                           filters=filters,
                                           parameters=parameters)
                break
            except gapi.errors.GapiInvalidError as e:
                tryDate = _adjust_date(str(e))
        if not usage:
            print('No user report available.')
            sys.exit(1)
        titles = ['email', 'date']
        csvRows = []
        ptypes = ['intValue', 'boolValue', 'datetimeValue', 'stringValue']
        for user_report in usage:
            if 'entity' not in user_report:
                continue
            row = {
                'email': user_report['entity']['userEmail'],
                'date': tryDate
            }
            for item in user_report.get('parameters', []):
                if 'name' not in item:
                    continue
                name = item['name']
                if not name in titles:
                    titles.append(name)
                for ptype in ptypes:
                    if ptype in item:
                        row[name] = item[ptype]
                        break
                else:
                    row[name] = ''
            csvRows.append(row)
        display.write_csv_file(csvRows, titles, f'User Reports - {tryDate}',
                               to_drive)
    elif report == 'customer':
        while True:
            try:
                if fullDataRequired is not None:
                    warnings = gapi.get_items(rep.customerUsageReports(),
                                              'get',
                                              'warnings',
                                              throw_reasons=throw_reasons,
                                              customerId=customerId,
                                              date=tryDate,
                                              fields='warnings')
                    fullData, tryDate = _check_full_data_available(
                        warnings, tryDate, fullDataRequired)
                    if fullData < 0:
                        print('No customer report available.')
                        sys.exit(1)
                    if fullData == 0:
                        continue
                usage = gapi.get_all_pages(rep.customerUsageReports(),
                                           'get',
                                           'usageReports',
                                           throw_reasons=throw_reasons,
                                           customerId=customerId,
                                           date=tryDate,
                                           parameters=parameters)
                break
            except gapi.errors.GapiInvalidError as e:
                tryDate = _adjust_date(str(e))
        if not usage:
            print('No customer report available.')
            sys.exit(1)
        titles = ['name', 'value', 'client_id']
        csvRows = []
        auth_apps = list()
        for item in usage[0]['parameters']:
            if 'name' not in item:
                continue
            name = item['name']
            if 'intValue' in item:
                value = item['intValue']
            elif 'msgValue' in item:
                if name == 'accounts:authorized_apps':
                    for subitem in item['msgValue']:
                        app = {}
                        for an_item in subitem:
                            if an_item == 'client_name':
                                app['name'] = 'App: ' + \
                                    subitem[an_item].replace('\n', '\\n')
                            elif an_item == 'num_users':
                                app['value'] = f'{subitem[an_item]} users'
                            elif an_item == 'client_id':
                                app['client_id'] = subitem[an_item]
                        auth_apps.append(app)
                    continue
                values = []
                for subitem in item['msgValue']:
                    if 'count' in subitem:
                        mycount = myvalue = None
                        for key, value in list(subitem.items()):
                            if key == 'count':
                                mycount = value
                            else:
                                myvalue = value
                            if mycount and myvalue:
                                values.append(f'{myvalue}:{mycount}')
                        value = ' '.join(values)
                    elif 'version_number' in subitem \
                         and 'num_devices' in subitem:
                        values.append(f'{subitem["version_number"]}:'
                                      f'{subitem["num_devices"]}')
                    else:
                        continue
                    value = ' '.join(sorted(values, reverse=True))
            csvRows.append({'name': name, 'value': value})
        for app in auth_apps:  # put apps at bottom
            csvRows.append(app)
        display.write_csv_file(csvRows,
                               titles,
                               f'Customer Report - {tryDate}',
                               todrive=to_drive)
    else:
        page_message = gapi.got_total_items_msg('Activities', '...\n')
        activities = gapi.get_all_pages(rep.activities(),
                                        'list',
                                        'items',
                                        page_message=page_message,
                                        applicationName=report,
                                        userKey=userKey,
                                        customerId=customerId,
                                        actorIpAddress=actorIpAddress,
                                        startTime=startTime,
                                        endTime=endTime,
                                        eventName=eventName,
                                        filters=filters,
                                        orgUnitID=orgUnitId)
        if activities:
            titles = ['name']
            csvRows = []
            for activity in activities:
                events = activity['events']
                del activity['events']
                activity_row = utils.flatten_json(activity)
                purge_parameters = True
                for event in events:
                    for item in event.get('parameters', []):
                        if set(item) == set(['value', 'name']):
                            event[item['name']] = item['value']
                        elif set(item) == set(['intValue', 'name']):
                            if item['name'] in ['start_time', 'end_time']:
                                val = item.get('intValue')
                                if val is not None:
                                    val = int(val)
                                    if val >= 62135683200:
                                        event[item['name']] = \
                                            datetime.datetime.fromtimestamp(
                                                val-62135683200).isoformat()
                            else:
                                event[item['name']] = item['intValue']
                        elif set(item) == set(['boolValue', 'name']):
                            event[item['name']] = item['boolValue']
                        elif set(item) == set(['multiValue', 'name']):
                            event[item['name']] = ' '.join(item['multiValue'])
                        elif item['name'] == 'scope_data':
                            parts = {}
                            for message in item['multiMessageValue']:
                                for mess in message['parameter']:
                                    value = mess.get(
                                        'value',
                                        ' '.join(mess.get('multiValue', [])))
                                    parts[mess['name']] = parts.get(
                                        mess['name'], []) + [value]
                            for part, v in parts.items():
                                if part == 'scope_name':
                                    part = 'scope'
                                event[part] = ' '.join(v)
                        else:
                            purge_parameters = False
                    if purge_parameters:
                        event.pop('parameters', None)
                    row = utils.flatten_json(event)
                    row.update(activity_row)
                    for item in row:
                        if item not in titles:
                            titles.append(item)
                    csvRows.append(row)
            display.sort_csv_titles([
                'name',
            ], titles)
            display.write_csv_file(csvRows, titles,
                                   f'{report.capitalize()} Activity Report',
                                   to_drive)
Esempio n. 2
0
def showUsage():
    rep = buildGAPIObject()
    throw_reasons = [gapi.errors.ErrorReason.INVALID,
                     gapi.errors.ErrorReason.BAD_REQUEST]
    todrive = False
    report = sys.argv[3].lower()
    titles = ['date']
    if report == 'customer':
        endpoint = rep.customerUsageReports()
        kwargs = [{}]
    elif report == 'user':
        endpoint = rep.userUsageReport()
        kwargs = [{'userKey': 'all'}]
        titles.append('user')
    else:
        controlflow.expected_argument_exit(
            'usage', ['user', 'customer'], report)
    customerId = GC_Values[GC_CUSTOMER_ID]
    if customerId == MY_CUSTOMER:
        customerId = None
    parameters = []
    filters = None
    start_date = end_date = orgUnitId = None
    skip_day_numbers = []
    skip_dates = []
    i = 4
    while i < len(sys.argv):
        myarg = sys.argv[i].lower().replace('_', '')
        if myarg == 'startdate':
            start_date = parse(sys.argv[i+1])
            i += 2
        elif myarg == 'enddate':
            end_date = parse(sys.argv[i+1])
            i += 2
        elif myarg == 'todrive':
            todrive = True
            i += 1
        elif myarg in ['orgunit', 'org', 'ou']:
            if report != 'user':
                controlflow.invalid_argument_exit(myarg, f'gam usage {report}')
            _, orgUnitId = __main__.getOrgUnitId(sys.argv[i+1])
            i += 2
        elif myarg == 'parameters':
            parameters = sys.argv[i+1].split(',')
            i += 2
        elif myarg == 'skipdates':
            skips = sys.argv[i+1].split(',')
            skip_dates = [utils.get_yyyymmdd(d) for d in skips]
            i += 2
        elif myarg == 'skipdaysofweek':
            skipdaynames = sys.argv[i+1].split(',')
            dow = [d.lower() for d in calendar.day_abbr]
            skip_day_numbers = [dow.index(d) for d in skipdaynames if d in dow]
            i += 2
        elif myarg in usergroup_types:
            if report != 'user':
                controlflow.invalid_argument_exit(myarg, f'gam usage {report}')
            entity_type = myarg
            entity = sys.argv[i+1]
            users = __main__.getUsersToModify(entity_type, entity)
            kwargs = [{'userKey': user} for user in users]
            i += 3
        else:
            controlflow.invalid_argument_exit(sys.argv[i], "gam usage")
    if not start_date:
        start_date = datetime.datetime.now() + relativedelta(months=-1)
    if not end_date:
        end_date = datetime.datetime.now()
    if orgUnitId:
        for i in range(len(kwargs)):
            kwargs[i-1]['orgUnitID'] = orgUnitId
    one_day = datetime.timedelta(days=1)
    usage_on_date = start_date
    titles.extend(parameters)
    csvRows = []
    vtypes = ['intValue', 'stringValue', 'intValue',
              'boolValue', 'datetimeValue']
    while usage_on_date <= end_date:
        use_date = usage_on_date.strftime('%Y-%m-%d')
        if usage_on_date.weekday() in skip_day_numbers or \
           use_date in skip_dates:
            usage_on_date += one_day
            continue
        usage_on_date += one_day
        try:
            for kwarg in kwargs:
                try:
                    usage = gapi.get_all_pages(endpoint, 'get',
                                               'usageReports',
                                               throw_reasons=throw_reasons,
                                               customerId=customerId,
                                               date=use_date,
                                               parameters=','.join(parameters),
                                               **kwarg)
                except gapi.errors.GapiBadRequestError:
                    continue
                for entity in usage:
                    row = {'date': use_date}
                    if 'userEmail' in entity['entity']:
                        row['user'] = entity['entity']['userEmail']
                    for item in entity['parameters']:
                        if 'name' not in item:
                            continue
                        name = item['name']
                        if name == 'cros:device_version_distribution':
                            for cros_ver in item['msgValue']:
                                v = cros_ver['version_number']
                                column_name = f'cros:num_devices_chrome_{v}'
                                if column_name not in titles:
                                    titles.append(column_name)
                                row[column_name] = cros_ver['num_devices']
                        else:
                            for vtype in vtypes:
                                if vtype in item:
                                    value = item[vtype]
                                    break
                            row[name] = value
                    csvRows.append(row)
        except gapi.errors.GapiInvalidError:
            continue
    display.write_csv_file(
        csvRows, titles, f'Usage Reports', todrive)
Esempio n. 3
0
def createExport():
    v = buildGAPIObject()
    allowed_corpuses = gapi.get_enum_values_minus_unspecified(
        v._rootDesc['schemas']['Query']['properties']['corpus']['enum'])
    allowed_scopes = gapi.get_enum_values_minus_unspecified(
        v._rootDesc['schemas']['Query']['properties']['dataScope']['enum'])
    allowed_formats = gapi.get_enum_values_minus_unspecified(
        v._rootDesc['schemas']['MailExportOptions']['properties']
        ['exportFormat']['enum'])
    export_format = 'MBOX'
    showConfidentialModeContent = None  # default to not even set
    matterId = None
    body = {'query': {'dataScope': 'ALL_DATA'}, 'exportOptions': {}}
    i = 3
    while i < len(sys.argv):
        myarg = sys.argv[i].lower().replace('_', '')
        if myarg == 'matter':
            matterId = getMatterItem(v, sys.argv[i+1])
            body['matterId'] = matterId
            i += 2
        elif myarg == 'name':
            body['name'] = sys.argv[i+1]
            i += 2
        elif myarg == 'corpus':
            body['query']['corpus'] = sys.argv[i+1].upper()
            if body['query']['corpus'] not in allowed_corpuses:
                controlflow.expected_argument_exit(
                    "corpus", ", ".join(allowed_corpuses), sys.argv[i+1])
            i += 2
        elif myarg in VAULT_SEARCH_METHODS_MAP:
            if body['query'].get('searchMethod'):
                message = f'Multiple search methods ' \
                          f'({", ".join(VAULT_SEARCH_METHODS_LIST)})' \
                          f'specified, only one is allowed'
                controlflow.system_error_exit(3, message)
            searchMethod = VAULT_SEARCH_METHODS_MAP[myarg]
            body['query']['searchMethod'] = searchMethod
            if searchMethod == 'ACCOUNT':
                body['query']['accountInfo'] = {
                    'emails': sys.argv[i+1].split(',')}
                i += 2
            elif searchMethod == 'ORG_UNIT':
                body['query']['orgUnitInfo'] = {
                    'orgUnitId': __main__.getOrgUnitId(sys.argv[i+1])[1]}
                i += 2
            elif searchMethod == 'SHARED_DRIVE':
                body['query']['sharedDriveInfo'] = {
                    'sharedDriveIds': sys.argv[i+1].split(',')}
                i += 2
            elif searchMethod == 'ROOM':
                body['query']['hangoutsChatInfo'] = {
                    'roomId': sys.argv[i+1].split(',')}
                i += 2
            else:
                i += 1
        elif myarg == 'scope':
            body['query']['dataScope'] = sys.argv[i+1].upper()
            if body['query']['dataScope'] not in allowed_scopes:
                controlflow.expected_argument_exit(
                    "scope", ", ".join(allowed_scopes), sys.argv[i+1])
            i += 2
        elif myarg in ['terms']:
            body['query']['terms'] = sys.argv[i+1]
            i += 2
        elif myarg in ['start', 'starttime']:
            body['query']['startTime'] = utils.get_date_zero_time_or_full_time(
                sys.argv[i+1])
            i += 2
        elif myarg in ['end', 'endtime']:
            body['query']['endTime'] = utils.get_date_zero_time_or_full_time(
                sys.argv[i+1])
            i += 2
        elif myarg in ['timezone']:
            body['query']['timeZone'] = sys.argv[i+1]
            i += 2
        elif myarg in ['excludedrafts']:
            body['query']['mailOptions'] = {
                'excludeDrafts': __main__.getBoolean(sys.argv[i+1], myarg)}
            i += 2
        elif myarg in ['driveversiondate']:
            body['query'].setdefault('driveOptions', {})['versionDate'] = \
                utils.get_date_zero_time_or_full_time(sys.argv[i+1])
            i += 2
        elif myarg in ['includeshareddrives', 'includeteamdrives']:
            body['query'].setdefault('driveOptions', {})[
                'includeSharedDrives'] = __main__.getBoolean(sys.argv[i+1], myarg)
            i += 2
        elif myarg in ['includerooms']:
            body['query']['hangoutsChatOptions'] = {
                'includeRooms': __main__.getBoolean(sys.argv[i+1], myarg)}
            i += 2
        elif myarg in ['format']:
            export_format = sys.argv[i+1].upper()
            if export_format not in allowed_formats:
                controlflow.expected_argument_exit(
                    "export format", ", ".join(allowed_formats), export_format)
            i += 2
        elif myarg in ['showconfidentialmodecontent']:
            showConfidentialModeContent = __main__.getBoolean(sys.argv[i+1], myarg)
            i += 2
        elif myarg in ['region']:
            allowed_regions = gapi.get_enum_values_minus_unspecified(
                v._rootDesc['schemas']['ExportOptions']['properties'][
                    'region']['enum'])
            body['exportOptions']['region'] = sys.argv[i+1].upper()
            if body['exportOptions']['region'] not in allowed_regions:
                controlflow.expected_argument_exit("region", ", ".join(
                    allowed_regions), body['exportOptions']['region'])
            i += 2
        elif myarg in ['includeaccessinfo']:
            body['exportOptions'].setdefault('driveOptions', {})[
                'includeAccessInfo'] = __main__.getBoolean(sys.argv[i+1], myarg)
            i += 2
        else:
            controlflow.invalid_argument_exit(sys.argv[i], "gam create export")
    if not matterId:
        controlflow.system_error_exit(
            3, 'you must specify a matter for the new export.')
    if 'corpus' not in body['query']:
        controlflow.system_error_exit(3, f'you must specify a corpus for the ' \
          f'new export. Choose one of {", ".join(allowed_corpuses)}')
    if 'searchMethod' not in body['query']:
        controlflow.system_error_exit(3, f'you must specify a search method ' \
          'for the new export. Choose one of ' \
          f'{", ".join(VAULT_SEARCH_METHODS_LIST)}')
    if 'name' not in body:
        corpus_name = body["query"]["corpus"]
        corpus_date = datetime.datetime.now()
        body['name'] = f'GAM {corpus_name} export - {corpus_date}'
    options_field = None
    if body['query']['corpus'] == 'MAIL':
        options_field = 'mailOptions'
    elif body['query']['corpus'] == 'GROUPS':
        options_field = 'groupsOptions'
    elif body['query']['corpus'] == 'HANGOUTS_CHAT':
        options_field = 'hangoutsChatOptions'
    if options_field:
        body['exportOptions'].pop('driveOptions', None)
        body['exportOptions'][options_field] = {'exportFormat': export_format}
        if showConfidentialModeContent is not None:
            body['exportOptions'][options_field][
                'showConfidentialModeContent'] = showConfidentialModeContent
    results = gapi.call(v.matters().exports(), 'create',
                        matterId=matterId, body=body)
    print(f'Created export {results["id"]}')
    display.print_json(results)
Esempio n. 4
0
def updateHold():
    v = buildGAPIObject()
    hold = sys.argv[3]
    matterId = None
    body = {}
    query = None
    add_accounts = []
    del_accounts = []
    start_time = None
    end_time = None
    i = 4
    while i < len(sys.argv):
        myarg = sys.argv[i].lower().replace('_', '')
        if myarg == 'matter':
            matterId = getMatterItem(v, sys.argv[i+1])
            holdId = convertHoldNameToID(v, hold, matterId)
            i += 2
        elif myarg == 'query':
            query = sys.argv[i+1]
            i += 2
        elif myarg in ['orgunit', 'ou']:
            body['orgUnit'] = {'orgUnitId': __main__.getOrgUnitId(sys.argv[i+1])[1]}
            i += 2
        elif myarg in ['start', 'starttime']:
            start_time = utils.get_date_zero_time_or_full_time(sys.argv[i+1])
            i += 2
        elif myarg in ['end', 'endtime']:
            end_time = utils.get_date_zero_time_or_full_time(sys.argv[i+1])
            i += 2
        elif myarg in ['addusers', 'addaccounts', 'addgroups']:
            add_accounts = sys.argv[i+1].split(',')
            i += 2
        elif myarg in ['removeusers', 'removeaccounts', 'removegroups']:
            del_accounts = sys.argv[i+1].split(',')
            i += 2
        else:
            controlflow.invalid_argument_exit(myarg, "gam update hold")
    if not matterId:
        controlflow.system_error_exit(
            3, 'you must specify a matter for the hold.')
    if query or start_time or end_time or body.get('orgUnit'):
        fields = 'corpus,query,orgUnit'
        old_body = gapi.call(v.matters().holds(
        ), 'get', matterId=matterId, holdId=holdId, fields=fields)
        body['query'] = old_body['query']
        body['corpus'] = old_body['corpus']
        if 'orgUnit' in old_body and 'orgUnit' not in body:
            # bah, API requires this to be sent
            # on update even when it's not changing
            body['orgUnit'] = old_body['orgUnit']
        query_type = f'{body["corpus"].lower()}Query'
        if body['corpus'] == 'DRIVE':
            if query:
                try:
                    body['query'][query_type] = json.loads(query)
                except ValueError as e:
                    message = f'{str(e)}, query: {query}'
                    controlflow.system_error_exit(3, message)
        elif body['corpus'] in ['GROUPS', 'MAIL']:
            if query:
                body['query'][query_type]['terms'] = query
            if start_time:
                body['query'][query_type]['startTime'] = start_time
            if end_time:
                body['query'][query_type]['endTime'] = end_time
    if body:
        print(f'Updating hold {hold} / {holdId}')
        gapi.call(v.matters().holds(), 'update',
                  matterId=matterId, holdId=holdId, body=body)
    if add_accounts or del_accounts:
        cd = __main__.buildGAPIObject('directory')
        for account in add_accounts:
            print(f'adding {account} to hold.')
            add_body = {'accountId': __main__.convertEmailAddressToUID(account, cd)}
            gapi.call(v.matters().holds().accounts(), 'create',
                      matterId=matterId, holdId=holdId, body=add_body)
        for account in del_accounts:
            print(f'removing {account} from hold.')
            accountId = __main__.convertEmailAddressToUID(account, cd)
            gapi.call(v.matters().holds().accounts(), 'delete',
                      matterId=matterId, holdId=holdId, accountId=accountId)
Esempio n. 5
0
def createHold():
    v = buildGAPIObject()
    allowed_corpuses = gapi.get_enum_values_minus_unspecified(
        v._rootDesc['schemas']['Hold']['properties']['corpus']['enum'])
    body = {'query': {}}
    i = 3
    query = None
    start_time = None
    end_time = None
    matterId = None
    accounts = []
    while i < len(sys.argv):
        myarg = sys.argv[i].lower().replace('_', '')
        if myarg == 'name':
            body['name'] = sys.argv[i+1]
            i += 2
        elif myarg == 'query':
            query = sys.argv[i+1]
            i += 2
        elif myarg == 'corpus':
            body['corpus'] = sys.argv[i+1].upper()
            if body['corpus'] not in allowed_corpuses:
                controlflow.expected_argument_exit(
                    "corpus", ", ".join(allowed_corpuses), sys.argv[i+1])
            i += 2
        elif myarg in ['accounts', 'users', 'groups']:
            accounts = sys.argv[i+1].split(',')
            i += 2
        elif myarg in ['orgunit', 'ou']:
            body['orgUnit'] = {
                'orgUnitId': __main__.getOrgUnitId(sys.argv[i+1])[1]}
            i += 2
        elif myarg in ['start', 'starttime']:
            start_time = utils.get_date_zero_time_or_full_time(sys.argv[i+1])
            i += 2
        elif myarg in ['end', 'endtime']:
            end_time = utils.get_date_zero_time_or_full_time(sys.argv[i+1])
            i += 2
        elif myarg == 'matter':
            matterId = getMatterItem(v, sys.argv[i+1])
            i += 2
        else:
            controlflow.invalid_argument_exit(sys.argv[i], "gam create hold")
    if not matterId:
        controlflow.system_error_exit(
            3, 'you must specify a matter for the new hold.')
    if not body.get('name'):
        controlflow.system_error_exit(
            3, 'you must specify a name for the new hold.')
    if not body.get('corpus'):
        controlflow.system_error_exit(3, f'you must specify a corpus for ' \
          f'the new hold. Choose one of {", ".join(allowed_corpuses)}')
    if body['corpus'] == 'HANGOUTS_CHAT':
        query_type = 'hangoutsChatQuery'
    else:
        query_type = f'{body["corpus"].lower()}Query'
    body['query'][query_type] = {}
    if body['corpus'] == 'DRIVE':
        if query:
            try:
                body['query'][query_type] = json.loads(query)
            except ValueError as e:
                controlflow.system_error_exit(3, f'{str(e)}, query: {query}')
    elif body['corpus'] in ['GROUPS', 'MAIL']:
        if query:
            body['query'][query_type] = {'terms': query}
        if start_time:
            body['query'][query_type]['startTime'] = start_time
        if end_time:
            body['query'][query_type]['endTime'] = end_time
    if accounts:
        body['accounts'] = []
        cd = __main__.buildGAPIObject('directory')
        account_type = 'group' if body['corpus'] == 'GROUPS' else 'user'
        for account in accounts:
            body['accounts'].append(
                {'accountId': __main__.convertEmailAddressToUID(account,
                                                                cd,
                                                                account_type)}
            )
    holdId = gapi.call(v.matters().holds(), 'create',
                       matterId=matterId, body=body, fields='holdId')
    print(f'Created hold {holdId["holdId"]}')
Esempio n. 6
0
def showUsage():
    rep = buildGAPIObject()
    throw_reasons = [
        gapi.errors.ErrorReason.INVALID, gapi.errors.ErrorReason.BAD_REQUEST
    ]
    todrive = False
    if len(sys.argv) == 3:
        controlflow.missing_argument_exit('user or customer', 'report usage')
    report = sys.argv[3].lower()
    titles = ['date']
    if report == 'customer':
        endpoint = rep.customerUsageReports()
        kwargs = [{}]
    elif report == 'user':
        endpoint = rep.userUsageReport()
        kwargs = [{'userKey': 'all'}]
        titles.append('user')
    else:
        controlflow.expected_argument_exit('usage', ['user', 'customer'],
                                           report)
    customerId = GC_Values[GC_CUSTOMER_ID]
    if customerId == MY_CUSTOMER:
        customerId = None
    parameters = []
    start_date = end_date = orgUnitId = None
    skip_day_numbers = []
    skip_dates = []
    i = 4
    while i < len(sys.argv):
        myarg = sys.argv[i].lower().replace('_', '')
        if myarg == 'startdate':
            start_date = utils.get_yyyymmdd(sys.argv[i + 1],
                                            returnDateTime=True)
            i += 2
        elif myarg == 'enddate':
            end_date = utils.get_yyyymmdd(sys.argv[i + 1], returnDateTime=True)
            i += 2
        elif myarg == 'todrive':
            todrive = True
            i += 1
        elif myarg in ['fields', 'parameters']:
            parameters = sys.argv[i + 1].split(',')
            i += 2
        elif myarg == 'skipdates':
            skips = sys.argv[i + 1].split(',')
            skip_dates = [utils.get_yyyymmdd(d) for d in skips]
            i += 2
        elif myarg == 'skipdaysofweek':
            skipdaynames = sys.argv[i + 1].split(',')
            dow = [d.lower() for d in calendar.day_abbr]
            skip_day_numbers = [dow.index(d) for d in skipdaynames if d in dow]
            i += 2
        elif report == 'user' and myarg in ['orgunit', 'org', 'ou']:
            _, orgUnitId = __main__.getOrgUnitId(sys.argv[i + 1])
            i += 2
        elif report == 'user' and myarg in usergroup_types:
            users = __main__.getUsersToModify(myarg, sys.argv[i + 1])
            kwargs = [{'userKey': user} for user in users]
            i += 2
        else:
            controlflow.invalid_argument_exit(sys.argv[i],
                                              f'gam report usage {report}')
    if parameters:
        titles.extend(parameters)
        parameters = ','.join(parameters)
    else:
        parameters = None
    if not start_date:
        start_date = datetime.datetime.now() + relativedelta(months=-1)
    if not end_date:
        end_date = datetime.datetime.now()
    if orgUnitId:
        for kw in kwargs:
            kw['orgUnitID'] = orgUnitId
    one_day = datetime.timedelta(days=1)
    usage_on_date = start_date
    start_date = usage_on_date.strftime('%Y-%m-%d')
    csvRows = []
    while usage_on_date <= end_date:
        use_date = usage_on_date.strftime('%Y-%m-%d')
        if usage_on_date.weekday() in skip_day_numbers or \
           use_date in skip_dates:
            usage_on_date += one_day
            continue
        usage_on_date += one_day
        try:
            for kwarg in kwargs:
                try:
                    usage = gapi.get_all_pages(endpoint,
                                               'get',
                                               'usageReports',
                                               throw_reasons=throw_reasons,
                                               customerId=customerId,
                                               date=use_date,
                                               parameters=parameters,
                                               **kwarg)
                except gapi.errors.GapiBadRequestError:
                    continue
                for entity in usage:
                    row = {'date': use_date}
                    if 'userEmail' in entity['entity']:
                        row['user'] = entity['entity']['userEmail']
                    for item in entity.get('parameters', []):
                        if 'name' not in item:
                            continue
                        name = item['name']
                        if name == 'cros:device_version_distribution':
                            for cros_ver in item['msgValue']:
                                v = cros_ver['version_number']
                                column_name = f'cros:num_devices_chrome_{v}'
                                if column_name not in titles:
                                    titles.append(column_name)
                                row[column_name] = cros_ver['num_devices']
                        else:
                            if not name in titles:
                                titles.append(name)
                            for ptype in REPORTS_PARAMETERS_SIMPLE_TYPES:
                                if ptype in item:
                                    row[name] = item[ptype]
                                    break
                            else:
                                row[name] = ''
                    csvRows.append(row)
        except gapi.errors.GapiInvalidError as e:
            display.print_warning(str(e))
            break
    display.write_csv_file(
        csvRows, titles,
        f'{report.capitalize()} Usage Report - {start_date}:{use_date}',
        todrive)