|
14 | 14 | # limitations under the License. |
15 | 15 | # |
16 | 16 |
|
| 17 | +import copy |
17 | 18 | import glob |
18 | 19 | import json |
19 | 20 | import os |
|
31 | 32 | LOG = logging.getLogger(__name__) |
32 | 33 |
|
33 | 34 |
|
34 | | -class EsdJSONValidation(object): |
35 | | - """Class reads the json file(s) |
| 35 | +class EsdTagProcessor(object): |
| 36 | + """Class processes json dictionary |
36 | 37 |
|
37 | | - It checks and parses the content of json file(s) to a dictionary |
| 38 | + It checks compares the tags from esdjson dictionary to list of valid tags |
38 | 39 | """ |
39 | | - def __init__(self, esddir): |
40 | | - self.esdJSONFileList = glob.glob(os.path.join(esddir, '*.json')) |
41 | | - self.esdJSONDict = {} |
42 | | - |
43 | | - def read_json(self): |
44 | | - for fileList in self.esdJSONFileList: |
| 40 | + _instance = None |
| 41 | + esd_dict = {} |
| 42 | + |
| 43 | + def __new__(cls, *args, **kwargs): |
| 44 | + if not isinstance(cls._instance, cls): |
| 45 | + cls._instance = object.__new__(cls, *args, **kwargs) |
| 46 | + return cls._instance |
| 47 | + |
| 48 | + def read_json(self, esddir): |
| 49 | + esdJSONFileList = glob.glob(os.path.join(esddir, '*.json')) |
| 50 | + esdJSONDict = {} |
| 51 | + for fileList in esdJSONFileList: |
45 | 52 | try: |
46 | 53 | with open(fileList) as json_file: |
47 | 54 | # Reading each file to a dictionary |
48 | 55 | fileJSONDict = json.load(json_file) |
49 | 56 | # Combine all dictionaries to one |
50 | | - self.esdJSONDict.update(fileJSONDict) |
| 57 | + esdJSONDict.update(fileJSONDict) |
51 | 58 |
|
52 | 59 | except ValueError as err: |
53 | 60 | LOG.error('ESD JSON File is invalid: %s', err) |
54 | 61 | raise f5_ex.esdJSONFileInvalidException() |
55 | 62 |
|
56 | | - return self.esdJSONDict |
57 | | - |
58 | | - |
59 | | -class EsdTagProcessor(EsdJSONValidation): |
60 | | - """Class processes json dictionary |
61 | | -
|
62 | | - It checks compares the tags from esdjson dictionary to list of valid tags |
63 | | - """ |
64 | | - def __init__(self, esddir): |
65 | | - super(EsdTagProcessor, self).__init__(esddir) |
66 | | - |
67 | | - # this function will return intersection of known valid esd tags |
68 | | - # and the ones that user provided |
69 | | - def valid_tag_key_subset(self): |
70 | | - self.validtags = list(set(self.esdJSONDict.keys()) & |
71 | | - set(self.valid_esd_tags.keys())) |
72 | | - if not self.validtags: |
73 | | - LOG.error("Intersect of valid esd tags and user esd tags is empty") |
74 | | - |
75 | | - if set(self.validtags) != set(self.esdJSONDict.keys()): |
76 | | - LOG.error("invalid tags in the user esd tags") |
| 63 | + return esdJSONDict |
77 | 64 |
|
78 | | - def process_esd(self, bigips): |
| 65 | + def process_esd(self, bigips, esddir): |
| 66 | + esd_json_backup = {} |
| 67 | + esd_json_backup = copy.deepcopy(self.esd_dict) |
79 | 68 | try: |
80 | | - dict = self.read_json() |
| 69 | + dict = self.read_json(esddir) |
81 | 70 | self.esd_dict = self.verify_esd_dict(bigips, dict) |
82 | 71 | except f5_ex.esdJSONFileInvalidException: |
83 | | - self.esd_dict = {} |
| 72 | + # if error happens, we set backup esd. |
| 73 | + self.esd_dict = copy.deepcopy(esd_json_backup) |
| 74 | + LOG.warning( |
| 75 | + "ESD JSON File is invalid, " |
| 76 | + "ESD JSON Dict is restored to %s", |
| 77 | + esd_json_backup |
| 78 | + ) |
84 | 79 | raise |
85 | 80 |
|
86 | 81 | def get_esd(self, name): |
|
0 commit comments