Skip to content

Commit 530cf19

Browse files
authored
Spread policies script (#62)
1 parent 4aa810b commit 530cf19

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from django.core.management.base import BaseCommand
2+
from expiry_addon.models import ResourceExpiryPolicy
3+
from lockable_resource.models import LockableResource
4+
from lockable_resource.label_manager import LabelManager
5+
6+
7+
class Command(BaseCommand):
8+
help = "Create policies for given group of resources, based on label"
9+
10+
def add_arguments(self, parser):
11+
parser.add_argument("-l", "--label", type=str, required=True)
12+
parser.add_argument("-hr", "--hour", type=int, required=True)
13+
parser.add_argument(
14+
"-exl", "--exclude-locked", type=bool, required=False, default=True
15+
)
16+
17+
def handle(self, *args, **options):
18+
"""
19+
Create Resource Policies
20+
:return: None
21+
"""
22+
23+
# Run Validations:
24+
label = options.get("label")
25+
lbl_manager = LabelManager(label) # This validates that the label exists
26+
27+
hour = options.get("hour")
28+
exclude_locked = options.get("exclude_locked")
29+
30+
resources_to_spread = (
31+
lbl_manager.free_resources if exclude_locked else lbl_manager.all_resources
32+
)
33+
# Save a separated line version for showing in the confirmation message
34+
resources_line_separated = "\n".join(map(str, resources_to_spread))
35+
36+
print(f"The following resources are going to have a policy to be expired after {hour} hours:")
37+
print(resources_line_separated)
38+
answer = input("\n Press 'Y/y' to confirm")
39+
40+
if answer.lower() == "y":
41+
for resource in resources_to_spread:
42+
# Create a policy
43+
policy = ResourceExpiryPolicy(
44+
lockable_resource=resource, expiry_hour=hour
45+
)
46+
policy.save()
47+
print(f"Policy: {policy} created!")
48+
else:
49+
print("Operation canceled.")

0 commit comments

Comments
 (0)