|  | 
|  | 1 | +import os | 
|  | 2 | +import requests.exceptions | 
|  | 3 | +import csv | 
|  | 4 | + | 
|  | 5 | +from thoughtspot_rest_api_v1 import * | 
|  | 6 | + | 
|  | 7 | + | 
|  | 8 | +# Example functions for reporting of various counts of objects | 
|  | 9 | +# Useful for reporting various users / groups / domains / orgs for contractual compliance | 
|  | 10 | + | 
|  | 11 | +# | 
|  | 12 | +server = os.getenv('server')        # or type in yourself | 
|  | 13 | + | 
|  | 14 | +# Supply access token from REST API Playground or provide username/password securely | 
|  | 15 | +full_access_token = "" | 
|  | 16 | +username = os.getenv('username')  # or type in yourself | 
|  | 17 | +password = os.getenv('password')  # or type in yourself | 
|  | 18 | + | 
|  | 19 | + | 
|  | 20 | +ts: TSRestApiV2 = TSRestApiV2(server_url=server) | 
|  | 21 | +if full_access_token != "": | 
|  | 22 | +    ts.bearer_token = full_access_token | 
|  | 23 | +else: | 
|  | 24 | +    try: | 
|  | 25 | +        auth_token_response = ts.auth_token_full(username=username, password=password, validity_time_in_sec=3000) | 
|  | 26 | +        ts.bearer_token = auth_token_response['token'] | 
|  | 27 | +    except requests.exceptions.HTTPError as e: | 
|  | 28 | +        print(e) | 
|  | 29 | +        print(e.response.content) | 
|  | 30 | +        exit() | 
|  | 31 | + | 
|  | 32 | +# | 
|  | 33 | +# Main function to request every user | 
|  | 34 | +# Returns an Array with a simplified object of useful details | 
|  | 35 | +# | 
|  | 36 | +def all_users_details(org_id=0): | 
|  | 37 | +    print("\nRetrieving All Users Listing from /users/search endpoint") | 
|  | 38 | +    # record_size : -1 gets all listing | 
|  | 39 | +    search_request = { | 
|  | 40 | +        "record_size": -1, | 
|  | 41 | +        "record_offset": 0, | 
|  | 42 | +        "sort_options": { | 
|  | 43 | +            "field_name": "NAME", | 
|  | 44 | +            "order": "ASC" | 
|  | 45 | +        } | 
|  | 46 | +    } | 
|  | 47 | +    users = ts.users_search(request=search_request) | 
|  | 48 | +    users_table = [] | 
|  | 49 | +    # print(json.dumps(users, indent=2)) | 
|  | 50 | +    i = 0 | 
|  | 51 | +    for user in users: | 
|  | 52 | + | 
|  | 53 | +        # Grab all the essential details about a user, since the original object has so much | 
|  | 54 | +        guid = user["id"] | 
|  | 55 | +        name = user["name"] | 
|  | 56 | +        display_name = user["display_name"] | 
|  | 57 | + | 
|  | 58 | +        email = user["email"] | 
|  | 59 | +        email_parts = email.split("@") | 
|  | 60 | +        email_domain = "" | 
|  | 61 | +        if len(email_parts) == 2: | 
|  | 62 | +            email_domain = email_parts[1] | 
|  | 63 | + | 
|  | 64 | +        groups = user["user_groups"] | 
|  | 65 | +        orgs = user["orgs"] | 
|  | 66 | + | 
|  | 67 | +        groups_count = len(groups) | 
|  | 68 | +        orgs_count = len(orgs) | 
|  | 69 | + | 
|  | 70 | +        detail_row = {"id": guid, | 
|  | 71 | +                      "name": name, | 
|  | 72 | +                      "display_name": display_name, | 
|  | 73 | +                      "email": email, | 
|  | 74 | +                      "email_domain": email_domain, | 
|  | 75 | +                      "groups_count": groups_count, | 
|  | 76 | +                      "orgs_count": orgs_count, | 
|  | 77 | +                      "groups": groups, | 
|  | 78 | +                      "orgs": orgs | 
|  | 79 | +                      } | 
|  | 80 | +        users_table.append(detail_row) | 
|  | 81 | +    return users_table | 
|  | 82 | + | 
|  | 83 | +def all_orgs_details(): | 
|  | 84 | +    print("\nRetrieving All Orgs Listing from /orgs/search endpoint") | 
|  | 85 | +    # record_size : -1 gets all listing | 
|  | 86 | +    search_request = {} | 
|  | 87 | +    orgs = ts.orgs_search(request=search_request) | 
|  | 88 | +    # /orgs/search response is simple enough to simply use as is | 
|  | 89 | +    return orgs | 
|  | 90 | + | 
|  | 91 | + | 
|  | 92 | +def email_domains_csv(filename): | 
|  | 93 | +    user_details = all_users_details() | 
|  | 94 | + | 
|  | 95 | +    with open(filename, 'w', newline='') as csvfile: | 
|  | 96 | +        fieldnames = ['id', 'email_domain', 'groups_count', 'orgs_count'] | 
|  | 97 | +        writer = csv.DictWriter(csvfile, fieldnames=fieldnames, extrasaction='ignore') | 
|  | 98 | +        writer.writeheader() | 
|  | 99 | + | 
|  | 100 | +        for u in user_details: | 
|  | 101 | +            writer.writerow(u) | 
|  | 102 | + | 
|  | 103 | +def email_domains_count_csv(filename): | 
|  | 104 | +    user_details = all_users_details() | 
|  | 105 | +    domains = {} | 
|  | 106 | +    for u in user_details: | 
|  | 107 | +        if u["email_domain"] not in domains: | 
|  | 108 | +            domains[u["email_domain"]] = 1 | 
|  | 109 | +        else: | 
|  | 110 | +            domains[u["email_domain"]] += 1 | 
|  | 111 | + | 
|  | 112 | +    # print(json.dumps(domains, indent=2)) | 
|  | 113 | + | 
|  | 114 | +    with open(filename, 'w', newline='') as csvfile: | 
|  | 115 | +        fieldnames = ['email_domain', 'users_count'] | 
|  | 116 | + | 
|  | 117 | +        writer = csv.writer(csvfile) | 
|  | 118 | +        # Write headers | 
|  | 119 | +        writer.writerow(fieldnames) | 
|  | 120 | +        for d in domains: | 
|  | 121 | +            writer.writerow([d, domains[d]]) | 
|  | 122 | + | 
|  | 123 | +def all_user_details_csv(filename, org_id=0, list_orgs=False, list_groups=False): | 
|  | 124 | +    user_details = all_users_details(org_id=0) | 
|  | 125 | + | 
|  | 126 | +    with open(filename, 'w', newline='') as csvfile: | 
|  | 127 | +        fieldnames = ['id', 'name', 'display_name', 'email', 'email_domain', 'groups_count', 'orgs_count'] | 
|  | 128 | +        if list_orgs is True: | 
|  | 129 | +            fieldnames.append('orgs') | 
|  | 130 | +        if list_groups is True: | 
|  | 131 | +            fieldnames.append('groups') | 
|  | 132 | + | 
|  | 133 | +        writer = csv.DictWriter(csvfile, fieldnames=fieldnames, extrasaction='ignore') | 
|  | 134 | +        writer.writeheader() | 
|  | 135 | + | 
|  | 136 | +        for u in user_details: | 
|  | 137 | +            writer.writerow(u) | 
|  | 138 | + | 
|  | 139 | +def all_org_details_csv(filename): | 
|  | 140 | +    org_details = all_orgs_details() | 
|  | 141 | + | 
|  | 142 | +    with open(filename, 'w', newline='') as csvfile: | 
|  | 143 | +        fieldnames = ['id', 'name', 'status', 'description', 'visibility'] | 
|  | 144 | + | 
|  | 145 | +        writer = csv.DictWriter(csvfile, fieldnames=fieldnames, extrasaction='ignore') | 
|  | 146 | +        writer.writeheader() | 
|  | 147 | + | 
|  | 148 | +        for o in org_details: | 
|  | 149 | +            writer.writerow(o) | 
|  | 150 | + | 
|  | 151 | + | 
|  | 152 | +email_domains_csv("user_domains_full.csv") | 
|  | 153 | +email_domains_count_csv("domains_count.csv") | 
|  | 154 | + | 
|  | 155 | +# all_user_details_csv("users_export.csv", list_orgs=False, list_groups=False) | 
|  | 156 | + | 
|  | 157 | +# all_org_details_csv("orgs.csv") | 
|  | 158 | + | 
0 commit comments