-
Notifications
You must be signed in to change notification settings - Fork 16
/
check-tags-data-drift.py
53 lines (39 loc) · 1.75 KB
/
check-tags-data-drift.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
#!/usr/bin/env python3
from mist.api.config import TAGS_RESOURCE_TYPES
from mist.api.helpers import get_resource_model
from mist.api.tag.methods import get_tags_for_resource
from mist.api.models import Cloud # noqa F401
def check_difference(owner, resource_object, override=False):
canonical = get_tags_for_resource(owner, resource_object)
tags_attribute = resource_object.tags_to_dict()
if canonical != tags_attribute:
print_diff(canonical, tags_attribute, resource_object)
resource_object.tags = resource_object.tags_to_string(canonical)
resource_object.save()
check_difference(owner, resource_object, override=True)
elif override:
print(15 * '-', 'Fixing', 15 * '-')
print_diff(canonical, tags_attribute, resource_object)
def print_diff(canonical, tags_attribute, resource_object):
canon_minus_atrr = canonical.items() - tags_attribute.items()
attr_minus_canon = tags_attribute.items() - canonical.items()
print(f"{resource_object._meta['collection']} {resource_object.id}: \n\
canonical: {canonical} \n \
tags_attribute: {tags_attribute} \n \
canonical - attribute = {canon_minus_atrr} \n \
attribute - canonical = {attr_minus_canon} \n \
")
def main():
for resource_type in TAGS_RESOURCE_TYPES:
try:
resource_model = get_resource_model(resource_type.rstrip('s'))
except KeyError:
continue
for resource_object in resource_model.objects():
try:
owner = resource_object.owner
except AttributeError:
owner = resource_object.cloud.owner
check_difference(owner, resource_object)
if __name__ == "__main__":
main()