forked from jasonbisson/Google_Cloud_Service_Accounts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_service_account.sh
executable file
·45 lines (37 loc) · 1.67 KB
/
delete_service_account.sh
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/bash
#set -x
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
[[ "$#" -ne 2 ]] && { echo "Usage : `basename "$0"` --name <unique_name_flag>"; exit 1; }
[[ "$1" = "--name" ]] && export name=$2
export project_id=$(gcloud config list --format 'value(core.project)')
export service_account=$(gcloud iam service-accounts describe ${name}@${project_id}.iam.gserviceaccount.com --project ${project_id})
function check_variables () {
if [ -z "$project_id" ]; then
printf "ERROR: GCP PROJECT_ID is not set.\n\n"
printf "To view the current PROJECT_ID config: gcloud config list project \n\n"
printf "To view available projects: gcloud projects list \n\n"
printf "To update project config: gcloud config set project PROJECT_ID \n\n"
exit
fi
if [ -z "${service_account}" ]; then
printf "ERROR: Service account does not exist.\n\n"
printf "Review if there is a typo or permission issue to describe a service account \n\n"
exit
fi
}
function delete_service_account () {
gcloud iam service-accounts delete ${name}'@'${project_id}.iam.gserviceaccount.com --project ${project_id}
}
check_variables
delete_service_account