-
Notifications
You must be signed in to change notification settings - Fork 0
/
protection_policy.py
201 lines (186 loc) · 7.45 KB
/
protection_policy.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
#####################################################################################################
# author : Amit Yadav
# The code contains both sync and asyn replication policy creation
# fetch the az url i.e., cluster url i.e., uuid of unnmaed cluster present (idk why)
# fetch uuid of cluster pass it
# it auto adds vm with particular category
# change rpo time as required
#####################################################################################################
import json
import requests
import urllib3
import secrets
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
username = secrets.username
password = secrets.password
category_name = "DR"
value = "GOLD"
headers = {'content-type': 'application/json'}
PC1 = secrets.PC1
PC2 = secrets.PC2
def get_cluser_url(PC=PC1):
URL= f'https://{PC}:9440/api/nutanix/v3/clusters/list'
payload = {
"kind": "cluster",
"filter": "name== unnamed"
}
#### filter not working for cluster name
resp= requests.post(URL, verify=False, auth=(username,password), headers=headers, json=payload)
if resp.status_code == 200 :
for cluster in resp.json()['entities']:
if cluster['spec']['name'] == 'Unnamed':
print(f"cluster url == {cluster['metadata']['uuid']}")
return cluster['metadata']['uuid']
else:
print('can connect, check ')
def fetch_cluster_uuid(PC=PC1):
payload = {"kind": "cluster"}
URL = f"https://{PC}:9440/api/nutanix/v3/clusters/list"
resp = requests.post(URL, json=payload, headers=headers, verify=False, auth=(username,password))
# print(resp.json())
clusters= {}
if resp.status_code == 200:
for entity in resp.json()['entities']:
clusters[entity['spec']['name']]= entity['metadata']['uuid']
return clusters.items()
def get_protection_policy(PC=PC1):
URL= f'https://{PC}:9440/api/nutanix/v3/protection_rules/list'
payload = {"kind": "protection_rule"}
resp= requests.post(URL, verify=False, auth=(username,password), headers=headers, json=payload)
if resp.status_code == 200:
return resp.json()
def create_protecion_policy_sync(cluster_url,cluster_1_uuid, cluster_2_uuid, PC=PC1):
'''
cluster url is availability_zone_url
'''
URL=f'https://{PC}:9440/api/nutanix/v3/protection_rules'
payload_sync = {
"spec": {
"name": "amt-test1",
"resources": {
"ordered_availability_zone_list": [
{
"availability_zone_url": f"{cluster_url}",
"cluster_uuid": f"{cluster_1_uuid}"
},
{
f"availability_zone_url": f"{cluster_url}",
"cluster_uuid": f"{cluster_2_uuid}"
}
],
"availability_zone_connectivity_list": [
{
"source_availability_zone_index": 0,
"destination_availability_zone_index": 1,
"snapshot_schedule_list": [
{
"recovery_point_objective_secs": 0,
"auto_suspend_timeout_secs": 30,
"snapshot_type": "CRASH_CONSISTENT"
}
]
},
{
"source_availability_zone_index": 1,
"destination_availability_zone_index": 0,
"snapshot_schedule_list": [
{
"recovery_point_objective_secs": 0,
"auto_suspend_timeout_secs": 30,
"snapshot_type": "CRASH_CONSISTENT"
}
]
}
]
}
},
"metadata": {
"spec_version": 0,
"kind": "protection_rule"
}
}
resp=requests.post(URL, verify=False, auth=(username,password), headers=headers,json=payload_sync)
if resp.status_code == 202:
print('creating protection policy ')
else:
print("couldn't create protection policy please check")
def create_protection_policy_async(cluster_url, cluster_1_uuid, cluster_2_uuid, PC=PC1):
'''
writing code for linear snapshots ( if needed rolling see api )
'''
URL=f'https://{PC}:9440/api/nutanix/v3/protection_rules'
payload_async= {
"spec": {
"name": "amt-test2",
"resources": {
"category_filter": {
"type": "CATEGORIES_MATCH_ANY",
"params": {
"DR": [
"SILVER"
]
}
},
"ordered_availability_zone_list": [
{
"availability_zone_url": f"{cluster_url}",
"cluster_uuid": f"{cluster_1_uuid}"
},
{
"availability_zone_url": f"{cluster_url}",
"cluster_uuid": f"{cluster_2_uuid}"
}
],
"availability_zone_connectivity_list": [
{
"source_availability_zone_index": 0,
"destination_availability_zone_index": 1,
"snapshot_schedule_list": [
{
"recovery_point_objective_secs": 3600,
"auto_suspend_timeout_secs": 30,
"snapshot_type": "CRASH_CONSISTENT",
"local_snapshot_retention_policy": {
"num_snapshots": 1
},
"remote_snapshot_retention_policy": {
"num_snapshots": 1
}
}
]
},
{
"source_availability_zone_index": 1,
"destination_availability_zone_index": 0,
"snapshot_schedule_list": [
{
"recovery_point_objective_secs": 3600,
"auto_suspend_timeout_secs": 30,
"snapshot_type": "CRASH_CONSISTENT",
"local_snapshot_retention_policy": {
"num_snapshots": 1
},
"remote_snapshot_retention_policy": {
"num_snapshots": 1
}
}
]
}
]
}
},
"metadata": {
"spec_version": 0,
"kind": "protection_rule"
}
}
resp=requests.post(URL, verify=False, auth=(username,password), headers=headers,json=payload_async)
if resp.status_code == 202:
print('creating protection policy ')
else:
print("couldn't create protection policy please check")
cluster_url = get_cluser_url() # <---- use this to get cluster url
print(fetch_cluster_uuid())
# print(json.dumps(get_protection_policy(),indent=4))
# create_protecion_policy_sync(cluster_url,cluster_1_uuid="00062487-d917-b3d3-776a-7cc25530ea93",cluster_2_uuid="0006264e-0fc6-1949-0000-000000011597",PC=PC1 )
create_protection_policy_async(cluster_url,cluster_1_uuid="00062487-d917-b3d3-776a-7cc25530ea93",cluster_2_uuid="0006264e-0fc6-1949-0000-000000011597",PC=PC1 )