-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolicy_service.py
211 lines (189 loc) · 8.87 KB
/
policy_service.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
'''
Policy service allows for the creation and modification of snaplock policies
'''
from ast import For
import json
import sched
import warnings
import requests
import cli_box
from simple_term_menu import TerminalMenu
from colorama import init, Fore, Style
init(autoreset=True)
warnings.filterwarnings('ignore')
class PolicyService():
'''
PolicyService acts as a instantiation of the policy service to be
injected into dependencies or the main method as needed
'''
def __init__(self):
self.url = "https://rtp-sa-select01.naeastdemo.net/api"
self.headers = {
'accept': 'application/json',
'return_timeout': '15',
'return_records': 'true',
'Authorization': 'Basic TkFFQVNUREVNT1xkc3RlZmFubzpDYWNpb2VQZXBlMQ=='
}
self.svms = []
self.selected_svm = "None"
self.policy_name = "None"
self.schedule = "None"
self.keep_count = "None"
self.retention_period = "None"
def update_svms(self):
'''updates policy service with most recent list of svms in the cluster'''
url = f"{self.url}/svm/svms"
response = requests.request("GET", url, headers=self.headers, verify=False, timeout=50)
svms = []
for svm in json.loads(response.text)["records"]:
svms.append((svm["name"], svm["uuid"]))
self.svms = svms
def get_schedules(self):
'''retrieves cluster schedules'''
url = f"{self.url}/cluster/schedules?order_by=name"
response = requests.request("GET", url, headers=self.headers, verify=False, timeout=50)
schedules = json.loads(response.text)["records"]
return schedules
def clear_lines(self, num_lines):
#TODO: Dynamic display
for _ in range(num_lines):
print ("\033[A \033[A")
def show_policy_info(self):
self.clear_lines(7)
svm = "SVM: " + Fore.RED + self.selected_svm + Fore.BLACK + "\n"
policy = "Policy name: " + Fore.RED + self.policy_name + Fore.BLACK + "\n"
schedule = "Schedule: " + Fore.RED + self.schedule + Fore.BLACK + "\n"
keep_count = "Keep count: " + Fore.RED + str(self.keep_count) + Fore.BLACK + "\n"
retention_period = "Retention: " + Fore.RED + self.retention_period + Fore.BLACK + "\n"
print(cli_box.rounded(
svm + policy + schedule + keep_count + retention_period, align="left"
))
def show_policy_menu(self):
'''initiates policy creation menu'''
svm_chosen = False
name_chosen = False
schedule_chosen = False
count_chosen = False
retention_chosen = False
confirmed = False
exited = False
policy_creation_in_progress = ((not svm_chosen or
not name_chosen or
not schedule_chosen or
not count_chosen or
not retention_chosen or
not confirmed) and
not exited)
params_filled = (svm_chosen and
name_chosen and
schedule_chosen and
count_chosen and
retention_chosen)
policy = {
"copies": [
{
"schedule": {
"name": None
},
"count": None,
"retention_period": None
}
],
"svm": {
"name": None
},
"name": "tutorial_",
"enabled": False
}
for _ in range(7):
print()
while policy_creation_in_progress:
self.show_policy_info()
options = ["SVM", "Policy name", "Schedule", "Count", "Retention Period", None, "Exit"]
if params_filled:
options.insert(6, "Create this policy")
menu = TerminalMenu(options,
title="Provide the following policy parameters",
menu_cursor_style=("fg_blue", "bold"))
selection_i = menu.show()
selection = options[selection_i]
match selection:
case "SVM":
options = [x[0] for x in self.svms]
menu = TerminalMenu(options,
title="Select an SVM",
search_key='\\',
menu_cursor_style=("fg_blue", "bold"))
selection_i = menu.show()
policy["svm"]["name"] = options[selection_i]
self.selected_svm = Fore.GREEN + policy["svm"]["name"] + Fore.BLACK
svm_chosen = True
case "Policy name":
policy["name"] = input("Policy name: ")
self.policy_name = Fore.GREEN + policy["name"] + Fore.BLACK
name_chosen = True
self.clear_lines(1)
case "Schedule":
schedules = self.get_schedules()
options = [x["name"] for x in schedules]
menu = TerminalMenu(options,
title="Select schedule",
search_key='\\',
menu_cursor_style=("fg_blue", "bold"))
selection_i = menu.show()
policy["copies"][0]["schedule"]["name"] = schedules[selection_i]["name"]
self.schedule = Fore.GREEN + policy["copies"][0]["schedule"]["name"] + Fore.BLACK
schedule_chosen = True
case "Count":
policy["copies"][0]["count"] = str(input("Keep count: "))
self.keep_count = Fore.GREEN + policy["copies"][0]["count"] + Fore.BLACK
count_chosen = True
self.clear_lines(1)
case "Retention Period":
policy["copies"][0]["retention_period"] = "PT" + input("Select a retention period. \n\tW = weeks \n\tD = days \n\tH = hours \n\tM = minutes \n\tS = seconds\n(e.g. 20H = 20 hours):\n")
self.retention_period = Fore.GREEN + policy["copies"][0]["retention_period"]
retention_chosen = True
self.clear_lines(8)
case "Exit":
exited = True
return {
"status" : "early exit",
"policy" : None
}
case "Create this policy":
options = ["Yes", "No"]
menu = TerminalMenu(options,
title="Are you sure you want to create this policy?",
menu_cursor_style=("fg_blue", "bold"))
selection_i = menu.show()
if selection_i == 0:
self.selected_svm = Fore.RED + "None" + Fore.BLACK
self.policy_name = Fore.RED + "None" + Fore.BLACK
self.schedule = Fore.RED + "None" + Fore.BLACK
self.keep_count = Fore.RED + "None" + Fore.BLACK
self.retention_period = Fore.RED + 'None' + Fore.BLACK
confirmed = True
break
policy_creation_in_progress = ((not svm_chosen or
not name_chosen or
not schedule_chosen or
not count_chosen or
not retention_chosen or
not confirmed) and
not exited)
params_filled = (svm_chosen and
name_chosen and
schedule_chosen and
count_chosen and
retention_chosen)
json.dumps(policy)
url = self.url + "/storage/snapshot-policies"
response = requests.request("POST", url, headers=self.headers, data=json.dumps(policy), verify=False)
if str(response.status_code) == "201":
print(f"Successfully created policy '{policy['name']}.' This policy is currently inactive, enable through System Manager.")
else:
print(f"Could not create policy {policy['name']}. Reason: {response.reason}.")
return {
"status" : "success",
"policy" : policy
}