-
-
Save lognseth/43237ea97d489d9a09315112a4bf8ec0 to your computer and use it in GitHub Desktop.
Bulk Create Grafana Users
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name | role | ||
---|---|---|---|
Admin User | admin@example.com | Admin | |
Editor User | editor@example.com | Editor | |
Viewer User | viewer@example.com | Viewer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import string | |
import requests | |
from requests.auth import HTTPBasicAuth | |
import csv | |
# env - Replace with your credentials | |
BASE_URL = 'http://localhost:3000/' | |
USERNAME = 'admin' | |
PASSWORD = 'admin' | |
# URLs | |
CREATE_USERS_URL = BASE_URL + '/api/admin/users' | |
STATS_URL = BASE_URL + '/api/admin/stats' | |
DELETE_ORG_USER_URL = BASE_URL + '/api/orgs/{}/users/{}' | |
ADD_ORG_USER_URL = BASE_URL + '/api/orgs/{}/users' | |
# Authentication | |
BASIC_AUTH = HTTPBasicAuth(USERNAME, PASSWORD) | |
# Orgs | |
ORG_MAIN = 1 | |
# Org Roles | |
ORG_ROLES = ['Admin', 'Editor', 'Viewer'] | |
DEFAULT_ROLE = ORG_ROLES[2] | |
def get_username(name): | |
# You can customise the username logic here | |
return name.lower().replace(' ', '_') | |
def get_password(): | |
# You can customise the password logic here | |
letters = string.ascii_letters | |
return ''.join(random.choice(letters) for i in range(10)) | |
# Stats | |
def get_stats(): | |
stats_response = requests.get(STATS_URL, auth=BASIC_AUTH) | |
return stats_response.json() | |
# Create User | |
def create_user(name, email, role=DEFAULT_ROLE): | |
username = get_username(name) | |
password = get_password() | |
payload = { | |
'name': name, | |
'email': email, | |
'login': username, | |
'password': password, | |
'OrgId': 2 | |
} | |
create_user_response = requests.post(CREATE_USERS_URL, data=payload, auth=BASIC_AUTH) | |
if create_user_response.ok: | |
user_id = create_user_response.json()['id'] | |
return user_id, username, password | |
return None | |
# Delete User from Org | |
def delete_user_from_org(org_id, user_id): | |
url = DELETE_ORG_USER_URL.format(org_id, user_id) | |
response = requests.delete(url, auth=BASIC_AUTH) | |
return response.ok | |
# Add User to Org | |
def add_user_to_org(org_id, username, role=DEFAULT_ROLE): | |
url = ADD_ORG_USER_URL.format(org_id) | |
payload = { | |
'loginOrEmail': username, | |
'role': role | |
} | |
response = requests.post(url, json=payload, auth=BASIC_AUTH) | |
return response.ok | |
if __name__ == "__main__": | |
with open('grafana-users.csv') as csvfile: | |
users = csv.DictReader(csvfile) | |
for row in users: | |
response = create_user(row['name'], row['email']) | |
if response: | |
# Print: Name UserId Username Password | |
print(row['name'], '\t', response) | |
# Uncomment below to add current user to an organisation with ORG_ID | |
# added = add_user_to_org(ORG_MAIN, response[1], row['role']) | |
# Uncomment below to delete current user from an organisation with ORG_ID | |
# deleted = delete_user_from_org(ORG_MAIN, response[0]) | |
else: | |
print('Error creating user', row['name']) | |
stats = get_stats() | |
print(stats) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment