Created
November 4, 2022 20:05
-
-
Save dataders/418b15e06d602b73dbb9122151c1d763 to your computer and use it in GitHub Desktop.
"Bulk" DELETE users from Databricks Account
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "004a147a-b6f6-4b62-8fe7-6f5b87dd5028", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import requests\n", | |
"import json\n", | |
"import os" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "ecf0c5d3-0a27-421f-a57f-6230ca14f96e", | |
"metadata": {}, | |
"source": [ | |
"# starting info" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "46b8800c-756e-4487-b47e-058b60246708", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"headers = {\n", | |
" 'Authorization': 'Basic GET_YOUR_OWN_DAMN_TOKEN!' # (I used postman and basic auth then copied out the token)\n", | |
"}\n", | |
"\n", | |
"# this will only work if you have a Databricks Account which contains workspaces\n", | |
"account_id = \"asdfacvf-asdf-asdf-asdf-asdfasdfasdf\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "3c03240c-d393-45d1-9f7f-72d909b6a207", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"url_base = f\"https://accounts.cloud.databricks.com/api/2.0/accounts/{account_id}\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "b1fc1d63-7f24-42d5-93e9-e245b20aef76", | |
"metadata": {}, | |
"source": [ | |
"# get list of all users" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "ddca3e4e-ad1e-4974-89fe-5eadc2f28e54", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"url = os.path.join(url_base, \"scim/v2/Users?attributes=emails.value\")\n", | |
"\n", | |
"response = requests.request(\"GET\", url, headers=headers)\n", | |
"accounts = response.json()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "82b683e1-d67a-4d52-9d26-8745aef269b6", | |
"metadata": {}, | |
"source": [ | |
"then i used `jq` on the resulting JSON to get just the IDs that need to be deleted\n", | |
"`.Resources[] | select(.emails[].value | contains(\"dbt\") | not) | .id `\n", | |
"for some reason I can't get the python bindings for `jq` working correctly so I was lazy and used [this online sandbox](https://jqplay.org/). I also spent an hour trying out `glom` and it was terrible" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "4b343ece-b8e1-440b-ac1a-56fb176dadf1", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# users to delete\n", | |
"not_dbt = [\"123456789012345\", \"123456789012346\"]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "87ad13f0-ccff-4bff-b05d-423c237ac5d7", | |
"metadata": {}, | |
"source": [ | |
"# this will delete each user!" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "18b6ddcc-8b44-4749-9db4-30b137036dff", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"for user_id in not_dbt:\n", | |
" url = os.path.join(url_base, f\"scim/v2/Users/{user_id}\")\n", | |
" response = requests.request(\"DELETE\", url, headers=headers)\n", | |
" print(user_id)\n", | |
" print(response.text)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.8.13" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment