-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathsa-delete
executable file
·30 lines (29 loc) · 1.03 KB
/
sa-delete
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/bin/bash
# This script will delete all service accounts in a project
# List all projects and store them in an array
PROJECTS=($(gcloud projects list --format="value(projectId)"))
# Print the array with indices
echo "Available projects:"
for i in "${!PROJECTS[@]}"
do
echo "$i) ${PROJECTS[$i]}"
done
# Prompt the user to enter an index
read -p "Enter the index of the project you want to delete all service accounts for: " INDEX
# Validate the input
if [[ $INDEX =~ ^[0-9]+$ ]] && [ $INDEX -ge 0 ] && [ $INDEX -lt ${#PROJECTS[@]} ]
then
# Get the project ID from the array
PROJECT_ID=${PROJECTS[$INDEX]}
# List all service accounts for the project and store them in an array
SERVICE_ACCOUNTS=($(gcloud iam service-accounts list --project $PROJECT_ID --format="value(email)"))
# Loop over the array and delete each service account
for SA in "${SERVICE_ACCOUNTS[@]}"
do
echo "Deleting service account $SA"
gcloud iam service-accounts delete $SA --quiet
done
else
# Invalid input
echo "Invalid index. Please try again."
fi