forked from vmware/alb-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathavi_orphan_object.py
239 lines (221 loc) · 8.12 KB
/
avi_orphan_object.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env python3
from urllib.parse import urlparse, parse_qs
# global vs reference object
global vs_ref_dict_g
PATH_KEY_MAP = {'poolgroup': 'PoolGroup', 'healthmonitor': 'HealthMonitor',
'sslprofile': 'SSLProfile', 'httppolicyset': 'HTTPPolicySet',
'sslkeyandcertificate': 'SSLKeyAndCertificate', 'pool': 'Pool',
'networkprofile': 'NetworkProfile', 'pkiprofile': 'PKIProfile',
'stringgroup': 'StringGroup', 'vrfcontext': 'VrfContext',
'applicationprofile': 'ApplicationProfile', 'vsdatascriptset':
'VSDataScriptSet', 'networksecuritypolicy':
'NetworkSecurityPolicy', 'applicationpersistenceprofile':
'ApplicationPersistenceProfile', 'prioritylabels':
'PriorityLabels', 'vsvip': 'VsVip', 'tenant': "Tenant",
'serviceenginegroup': 'ServiceEngineGroup',
'virtualservice': 'VirtualService'}
DEFAULT_META_ORDER = [
"ControllerLicense",
"SeProperties",
"SecureChannelToken",
"SecureChannelMapping",
"VIMgrIPSubnetRuntime",
"Tenant",
"ControllerProperties",
"CloudProperties",
"SecureChannelAvailableLocalIPs",
"JobEntry",
"Role",
"DebugController",
"AutoScaleLaunchConfig",
"MicroService",
"AuthProfile",
"AnalyticsProfile",
"APICLifsRuntime",
"LogControllerMapping",
"SnmpTrapProfile",
"AlertSyslogConfig",
"NetworkRuntime",
"AlertObjectList",
"VIPGNameInfo",
"CertificateManagementProfile",
"CloudRuntime",
"CloudConnectorUser",
"DebugServiceEngine",
"HardwareSecurityModuleGroup",
"HealthMonitor",
"VIDCInfo",
"VIMgrControllerRuntime",
"GlobalHealthMonitor",
"IpamDnsProviderProfile",
"StringGroup",
"Backup",
"DebugVirtualService",
"AlertScriptConfig",
"NetworkProfile",
"GlobalLB",
"IpAddrGroup",
"Cluster",
"SSLProfile",
"PKIProfile",
"ApplicationPersistenceProfile",
"MicroServiceGroup",
"SSLKeyAndCertificate",
"GlobalService",
"ApplicationProfile",
"NetworkSecurityPolicy",
"SystemConfiguration",
"Cloud",
"AlertEmailConfig",
"PriorityLabels",
"PoolGroupDeploymentPolicy",
"VIMgrVMRuntime",
"VrfContext",
"ActionGroupConfig",
"VIMgrHostRuntime",
"AlertConfig",
"VIMgrNWRuntime",
"VIMgrClusterRuntime",
"VIMgrSEVMRuntime",
"ServerAutoScalePolicy",
"Network",
"VIMgrDCRuntime",
"ServiceEngineGroup",
"Pool",
"VIMgrVcenterRuntime",
"ServiceEngine",
"PoolGroup",
"HTTPPolicySet",
"VSDataScriptSet",
"VirtualService",
"Alert",
"Application"
]
def get_name_and_entity(url):
"""
Parses reference string to extract object type and
:param url: reference url to be parsed
:return: entity and object name
"""
parsed = urlparse(url)
return parsed.path.split('/')[2], parse_qs(parsed.query)['name'][0]
def search_obj(entity, name, new_config, avi_config, vs_ref_dict):
"""
Method to search referenced object
:param entity: object type
:param name: object name
:param new_config: filtered config
:param avi_config: full config
:param : Recursion to determine level in the vs reference tree
"""
avi_conf_key = PATH_KEY_MAP.get(entity, '')
found_obj = None
name_obj = None
if avi_conf_key in avi_config:
found_obj_list = avi_config[avi_conf_key]
found_obj = [obj for obj in found_obj_list if obj['name'] == name]
if found_obj:
found_obj = found_obj[0]
tenant = None
if 'tenant_ref' in found_obj:
link, tenant = get_name_and_entity(found_obj['tenant_ref'])
name_obj = '%s-%s-%s' % (found_obj['name'], avi_conf_key, tenant)
if new_config and name_obj not in new_config:
new_config.append(name_obj)
find_and_add_objects(found_obj, avi_config, new_config, vs_ref_dict)
def find_and_add_objects(obj_dict, avi_config, new_config, vs_ref_dict,
vs_flag=False):
"""
Method to iterate in one object find references and add those to output
:param obj_dict: Object to be iterated over
:param avi_config: Full config
:param new_config: Filtered config
:param : Recursion to determine level in the vs reference tree
"""
global vs_name
for key in obj_dict:
if (key.endswith('ref') and key not in ['cloud_ref']):
if not obj_dict[key]:
continue
entity, name = get_name_and_entity(obj_dict[key])
tenant_name = None
# creating vs reference dict with unique keys
if 'tenant_ref' in obj_dict:
ee, tenant_name = get_name_and_entity(obj_dict['tenant_ref'])
key = '%s$$%s$$%s' %(name, entity, tenant_name)
if vs_flag:
vs_name = obj_dict['name']
if vs_name:
if key in vs_ref_dict and vs_name not in vs_ref_dict[key]:
vs_ref_dict[key].append(vs_name)
else:
vs_ref_dict[key] = [vs_name]
search_obj(entity, name, new_config, avi_config, vs_ref_dict)
elif key.endswith('refs'):
for ref in obj_dict[key]:
tenant_name = None
entity, name = get_name_and_entity(ref)
# creating vs reference dict with unique keys
if 'tenant_ref' in obj_dict:
ee, tenant_name = get_name_and_entity(
obj_dict['tenant_ref'])
key = '%s$$%s$$%s' % (name, entity, tenant_name)
if vs_flag:
vs_name = obj_dict['name']
if key in vs_ref_dict and vs_name not in vs_ref_dict[key]:
vs_ref_dict[key].append(vs_name)
else:
vs_ref_dict[key] = [vs_name]
search_obj(entity, name, new_config, avi_config, vs_ref_dict)
elif isinstance(obj_dict[key], dict):
find_and_add_objects(obj_dict[key], avi_config, new_config,
vs_ref_dict)
elif obj_dict[key] and isinstance(obj_dict[key], list) \
and isinstance(obj_dict[key][0], dict):
for member in obj_dict[key]:
find_and_add_objects(member, avi_config, new_config,
vs_ref_dict)
return
def filter_for_vs(avi_config):
"""
Filters vs and its references from full configuration
:param avi_config: full configuration
:param vs_names: comma separated vs names to filter
:return: Filtered config dict
"""
global vs_ref_dict_g
new_config = []
vs_ref_dict = dict()
for vs in avi_config['VirtualService']:
vs_flag = True
if 'tenant_ref' in vs:
link, tenant = get_name_and_entity(vs['tenant_ref'])
name = '%s-%s-%s' % (vs['name'], 'VirtualService', tenant)
new_config.append(name)
find_and_add_objects(vs, avi_config, new_config, vs_ref_dict,
vs_flag=vs_flag)
vs_ref_dict_g = vs_ref_dict
return new_config
def get_vs_ref():
""" Function to return the global vs reference object """
return vs_ref_dict_g
def get_full_name(ele, key):
tenant = None
if 'tenant_ref' in ele:
link, tenant = get_name_and_entity(ele['tenant_ref'])
name = '%s-%s-%s' % (ele['name'], key, tenant)
return name
def wipe_out_not_in_use(avi_config):
"""
:param avi_config:
:return:
"""
use_obj = filter_for_vs(avi_config)
for key in DEFAULT_META_ORDER:
if key not in avi_config:
continue
obj_list = avi_config[key]
new_list = [obj for obj in obj_list if
get_full_name(obj, key) in use_obj]
avi_config[key] = new_list
return avi_config