-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathenable_deletion_protection.py
26 lines (20 loc) · 1.3 KB
/
enable_deletion_protection.py
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
import subprocess
# Input project name from the user
PROJECT_IDNAME = input("Please enter the project you need to enable deletion protection for an instance: ")
# Set the project for gcloud config
subprocess.run(["gcloud", "config", "set", "project", PROJECT_IDNAME], check=True)
# Get the list of instances
instance_list_output = subprocess.run(["gcloud", "compute", "instances", "list", "--project", PROJECT_IDNAME, "--format", "csv[no-heading](name,zone)"], stdout=subprocess.PIPE, text=True, check=True)
instance_lines = instance_list_output.stdout.strip().split('\n')
for line in instance_lines:
name, zone = line.split(',')
FULSE = "deletionProtection: false"
# Describe the instance to check deletionProtection
describe_output = subprocess.run(["gcloud", "compute", "instances", "describe", name, "--zone", zone, "--project", PROJECT_IDNAME], stdout=subprocess.PIPE, text=True, check=True)
# Check if deletionProtection is false
if FULSE in describe_output.stdout:
# Enable deletion protection
subprocess.run(["gcloud", "compute", "instances", "update", name, "--deletion-protection", "--zone", zone, "--project", PROJECT_IDNAME], check=True)
print(f"{name},Protection,NOW")
else:
print(f"{name},deletion protection already")