-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathbootflash_info.py
More file actions
765 lines (611 loc) · 23.9 KB
/
Copy pathbootflash_info.py
File metadata and controls
765 lines (611 loc) · 23.9 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
# Copyright (c) 2024-2025 Cisco and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Retrieve and filter bootflash contents.
"""
from __future__ import absolute_import, division, print_function
__metaclass__ = type # pylint: disable=invalid-name
__copyright__ = "Copyright (c) 2024-2025 Cisco Systems, Inc."
__author__ = "Allen Robel"
import copy
import inspect
import logging
from pathlib import PurePosixPath
from typing import Any, Literal
from ..common.api.v1.imagemanagement.rest.discovery.discovery import EpBootflashDiscovery
from ..common.api.v1.imagemanagement.rest.imagemgnt.bootflash.bootflash import EpBootflashInfo
from ..common.conversion import ConversionUtils
from ..common.rest_send_v2 import RestSend
from ..common.results import Results
from ..common.switch_details import SwitchDetails
from .convert_file_info_to_target import ConvertFileInfoToTarget
class BootflashInfo:
"""
# Summary
Retrieve and filter bootflash contents.
## Raises
### ValueError
- params is not set.
- switches is not set.
### TypeError
- switches is not a list.
- switches contains anything other than strings.
## Usage
We start with list of targets, where target is a dictionary containing
a filepath and a supervisor key:
```python
targets = [
{
"filepath": "bootflash:/*.txt",
"supervisor": "active"
},
{
"filepath": "bootflash:/abc.txt",
"supervisor": "standby"
}
]
```
1. Create an instance of BootflashInfo() and set the switches
property to a list of switch ip addresses.
2. Set instance.switch_details to the SwitchDetails() class and
pass it a separate instance of Results() since we don't want
to save the results of the switch details query.
3. Define a list of switch IP addresses and pass this to the
``instance.switches`` property.
4. Call ``instance.refresh()`` to retrieve switch details for each of the
switches in the switches. This is used to convert switch ip address
to serial_number, which is required by the bootflash-info endpoint,
defined in ``EpBootflashInfo()``.
5. We then call ``instance.refresh_bootflash_info()`` to retrieve
bootflash contents for each switch in the switches list.
6. We can then filter the results by switch (``filter_switch``),
supervisor (``filter_supervisor``), and filepath (``filter_filepath``).
7. ``filter_filepath`` supports file globbing. Below, we are filtering
for any file on any partition with a three-letter name and a .txt
extension. e.g. bootflash:/abc.txt.
8. We call ``instance.build_matches()`` to build a list of files matching
the filters.
9. We call ``instance.results.register_task_result()`` to register the
results, which creates instance.results.diff, a list of dictionaries
keyed on the switch ip address. Each dictionary contains a list of
matches for that switch. The matches are dictionaries containing the
bootflash information for the file.
```python
sender = Sender()
sender.ansible_module = ansible_module
rest_send = RestSend(ansible_module.params)
rest_send.response_handler = ResponseHandler()
rest_send.sender = sender
instance = BootflashInfo()
instance.results = Results()
instance.rest_send = rest_send
instance.switches = ["192.168.1.1", "192.168.1.2"]
instance.refresh()
# Filters can be added independently of each other.
# The more filters added, the more specific the results.
# ``filter_switch`` is limited to the switches in the
# ``instance.switches`` list, since this is the information
# that ``instance.refresh`` caches when ``instance.refresh``
# is called.
instance.filter_switch = "192.168.1.1"
instance.filter_supervisor = "active"
# filter_filepath supports file globbing.
# The below means "Any file on any partition with a three-letter
# name and a .txt extension." e.g. bootflash:/abc.txt
instance.filter_filepath = "*:/???.txt"
instance.build_matches()
instance.results.register_task_result()
# The results can be printed by accessing instance.results.diff.
# instance.results.diff is a list of dictionaries. Each dictionary
# is keyed on the switch ip address and contains a list of matches for
# that switch. The matches are dictionaries containing the bootflash
# information for the file.
print(f"{json.dumps(instance.results.diff, sort_keys=True, indent=4)}")
```
## instance.info_dict Structure
```json
{
"172.22.150.112": {
"bootFlashDataMap": {
"bootflash:": [
{
"bootflash_type": "active",
"date": "Aug 08 22:50:37 2024",
"deviceName": "cvd-1211-spine",
"fileName": "air.txt",
"filePath": "bootflash:",
"name": "bootflash:",
"serialNumber": "FOX2109PGCS",
"size": "2"
}
]
},
"bootFlashSpaceMap": {
"bootflash:": {
"bootflash_type": "active",
"deviceName": "cvd-1211-spine",
"freeSpace": 12995166208,
"ipAddr": " 172.22.150.112",
"name": "bootflash:",
"serialNumber": "FOX2109PGCS",
"totalSpace": 21685153792,
"usedSpace": 8689987584
}
},
"partitions": [
"bootflash:"
],
"requiredSpace": "NA"
}
}
```
## instance.results.diff Structure
```json
"diff": [
{
"172.22.150.112": [
{
"date": "2024-08-06 16:14:59",
"device_name": "cvd-1211-spine",
"filepath": "bootflash:/bling.txt",
"ip_address": "172.22.150.112",
"serial_number": "FOX2109PGCS",
"size": "2",
"supervisor": "active"
}
],
"172.22.150.113": [
{
"date": "2024-08-06 16:15:59",
"device_name": "cvd-1212-spine",
"filepath": "bootflash:/blong.txt",
"ip_address": "172.22.150.113",
"serial_number": "FOX2109PGD0",
"size": "2",
"supervisor": "active"
}
],
"sequence_number": 1
}
]
```
## bootflash_data_map Structure
```json
{
"bootflash:": [
{
"bootflash_type": "active",
"date": "Aug 08 22:50:37 2024",
"deviceName": "cvd-1211-spine",
"fileName": "air.txt",
"filePath": "bootflash:",
"name": "bootflash:",
"serialNumber": "FOX2109PGCS",
"size": "2"
}
]
}
```
"""
def __init__(self) -> None:
self.class_name: str = self.__class__.__name__
self.action: str = "bootflash_info"
self.bootflash_data_map: dict[str, list[dict[str, str]]] = {}
self.conversion: ConversionUtils = ConversionUtils()
self.convert_file_info_to_target: ConvertFileInfoToTarget = ConvertFileInfoToTarget()
self.ep_bootflash_discovery: EpBootflashDiscovery = EpBootflashDiscovery()
self.ep_bootflash_info: EpBootflashInfo = EpBootflashInfo()
self.info_dict: dict[str, Any] = {}
self._matches: list[dict[str, str]] = []
# Used to collect individual responses and results for each
# switch in self.switches. Keyed on switch ip_address.
# Updated in refresh_bootflash_info().
self.diff_dict: dict[str, list[dict[str, str]]] = {}
self.response_dict: dict[str, dict[str, Any]] = {}
self.result_dict: dict[str, dict[str, bool]] = {}
self._rest_send: RestSend = RestSend({})
self._results: Results = Results()
self._switch_details: SwitchDetails = SwitchDetails()
self._switches: list[str] = []
self._filter_filepath: str = ""
self._filter_supervisor: str = ""
self._filter_switch: str = ""
self.valid_supervisor: list[str] = ["active", "standby"]
self.log: logging.Logger = logging.getLogger(f"dcnm.{self.class_name}")
msg = "ENTERED BootflashQuery(): "
msg += f"action {self.action}, "
self.log.debug(msg)
def validate_refresh_parameters(self) -> None:
"""
# Summary
Verify that mandatory prerequisites are met before calling refresh.
## Raises
### ValueError
- self.rest_send.params is not set.
- self.switches is not set.
- switches is not set.
"""
method_name: str = inspect.stack()[0][3]
def raise_value_error_if_not_set(property_name: str) -> None:
msg = f"{self.class_name}.{method_name}: "
msg += f"{property_name} must be set prior to calling refresh."
raise ValueError(msg)
if not self.rest_send.params:
raise_value_error_if_not_set("rest_send")
if not self.switches:
raise_value_error_if_not_set("switches")
def refresh(self) -> None:
"""
# Summary
Retrieve switch details for each of the switches in self.switches.
This is used to convert switch ip address to serial_number, which is
required by EpBootflashInfo().
## Raises
### ValueError
- switches is not set.
"""
self.validate_refresh_parameters()
self.results.action = self.action
self.results.check_mode = self.rest_send.check_mode
self.results.state = self.rest_send.state
# TODO: remove type: ignore[attr-defined] after refactoring SwitchDetails
self.switch_details.rest_send = self.rest_send # type: ignore[attr-defined]
self.switch_details.results = Results() # type: ignore[attr-defined]
self.switch_details.refresh()
self.refresh_bootflash_info()
def refresh_bootflash_info(self) -> None:
"""
# Summary
Retrieve bootflash information for each switch in self.switches.
## Raises
### ValueError
- serial_number cannot be found for a switch.
"""
method_name: str = inspect.stack()[0][3]
self.info_dict = {}
self.response_dict = {}
self.result_dict = {}
for switch in self.switches:
self.switch_details.filter = switch
try:
serial_number: str = self.switch_details.serial_number
except ValueError as error:
msg = f"{self.class_name}.{method_name}: "
msg += f"serial_number not found for switch {switch}. "
msg += f"Error detail {error}"
raise ValueError(msg) from error
# rediscover bootflash contents for the switch
self.ep_bootflash_discovery.serial_number = serial_number
self.rest_send.path = self.ep_bootflash_discovery.path
self.rest_send.verb = self.ep_bootflash_discovery.verb
self.rest_send.commit()
# retrieve bootflash information for the switch
self.ep_bootflash_info.serial_number = serial_number
self.rest_send.path = self.ep_bootflash_info.path
self.rest_send.verb = self.ep_bootflash_info.verb
self.rest_send.commit()
self.info_dict[switch] = copy.deepcopy(self.rest_send.response_current.get("DATA", {}))
self.response_dict[switch] = copy.deepcopy(self.rest_send.response_current)
self.result_dict[switch] = copy.deepcopy(self.rest_send.result_current)
def validate_prerequisites_for_build_matches(self) -> None:
"""
# Summary
Verify that mandatory prerequisites are met before calling `build_matches()`.
## Raises
### ValueError
- info_dict is empty i.e. `refresh` has not been called.
"""
method_name: str = inspect.stack()[0][3]
if not self.info:
msg = f"{self.class_name}.{method_name}: "
msg += "refresh must be called before retrieving bootflash "
msg += "properties."
raise ValueError(msg)
def match_filter_filepath(self, target: dict[str, str]) -> bool:
"""
# Summary
- Return True if the target's `filepath` matches `filter_filepath`.
- Return False otherwise.
## Raises
None
"""
if not self.filter_filepath:
return False
filepath: str = target.get("filepath", "")
posix: PurePosixPath = PurePosixPath(filepath)
if not posix.match(self.filter_filepath):
return False
return True
def match_filter_supervisor(self, target: dict[str, str]) -> bool:
"""
# Summary
- Return True if the target's `bootflash_type` matches `filter_supervisor`.
- Return False otherwise.
## Raises
None
"""
if not self.filter_supervisor:
return False
if target.get("supervisor", "") != self.filter_supervisor:
return False
return True
def match_filter_switch(self, target: dict[str, str]) -> bool:
"""
# Summary
- Return True if the target's `ip_address` matches `filter_switch`.
- Return False otherwise.
## Raises
None
"""
if not self.filter_switch:
return False
if target.get("ip_address", "") != self.filter_switch:
return False
return True
def build_matches(self) -> None:
"""
# Summary
Build a list of matches from the info_dict.
## Raises
None
"""
method_name: str = inspect.stack()[0][3]
self.validate_prerequisites_for_build_matches()
self._matches = []
if self.filter_switch not in self.info:
msg = f"{self.class_name}.{method_name}: "
msg += f"filter_switch {self.filter_switch} not found in info."
self.log.debug(msg)
return
data: dict[str, Any] = self.info.get(self.filter_switch, {})
self.bootflash_data_map = data.get("bootFlashDataMap", {})
for partition in self.bootflash_data_map:
for file_info in self.bootflash_data_map[partition]:
self.convert_file_info_to_target.file_info = file_info
self.convert_file_info_to_target.commit()
target = self.convert_file_info_to_target.target
# no need to test match_filter_switch since we have
# already filtered on the switch above.
if not self.match_filter_filepath(target):
continue
if not self.match_filter_supervisor(target):
continue
self._matches.append(target)
diff: dict[str, list[dict[str, str]]] = {}
for match in self._matches:
ip_address = match.get("ip_address", "")
if not ip_address:
continue
if ip_address not in diff:
diff[ip_address] = []
diff[ip_address].append(match)
self.diff_dict = diff
@property
def filter_filepath(self) -> str:
"""
# Summary
Return the current `filter_filepath`.
`filter_filepath` is a file path used to filter the results of the query. It can include file globbing.
## Raises
None
## Examples
- All txt files in the bootflash directory
- instance.filter_filepath = "bootflash:/*.txt"
- All txt files on all flash devices
- instance.filter_filepath = "*:/*.txt"
"""
return self._filter_filepath
@filter_filepath.setter
def filter_filepath(self, value: str) -> None:
msg = f"{self.class_name}.filter_filepath.setter: "
msg += f"value {value}"
self.log.debug(msg)
self._filter_filepath = value
@property
def filter_supervisor(self) -> str:
"""
# Summary
Return the current `filter_supervisor`.
`filter_supervisor` is either "active" or "standby" and represents
the state of the supervisor which hosts `filepath`.
## Raises
### ValueError
- `value` is not one of the valid_supervisor values "active" or "standby".
## Example
instance.filter_supervisor = "active"
"""
return self._filter_supervisor
@filter_supervisor.setter
def filter_supervisor(self, value: str) -> None:
if value not in self.valid_supervisor:
msg = f"{self.class_name}.filter_supervisor.setter: "
msg += f"value {value} is not a valid value for supervisor. "
msg += f"Valid values: {','.join(self.valid_supervisor)}."
raise ValueError(msg)
self._filter_supervisor = value
@property
def filter_switch(self) -> str:
"""
# Summary
Return the current `filter_switch`.
`filter_switch` is a switch ipv4 address used to filter the results of the query.
## Raises
None
"""
return self._filter_switch
@filter_switch.setter
def filter_switch(self, value: str) -> None:
self._filter_switch = value
@property
def info(self) -> dict[str, Any]:
"""
# Summary
Return the info_dict dictionary.
"""
return self.info_dict
@property
def matches(self) -> list[dict[str, str]]:
"""
# Summary
Return a list of file_info dicts that match the query filters.
## Raises
None
## Associated key
None
## Example value
The leading space with ipAddr's value in pre-3.2.1e Nexus Dashboard responses
is stripped in build_matches() so you won't have to worry about it.
```python
matches = [
{
"bootflash_type": "active",
"date": "Sep 19 22:20:07 2023",
"deviceName": "cvd-1212-spine",
"fileName": "n9000-epld.10.2.5.M.img",
"filePath": "bootflash:",
"ipAddr": "172.22.150.113",
"name": "bootflash:",
"serialNumber": "BDY3814QDD0",
"size": "218233885"
}
]
```
"""
self.build_matches()
return self._matches
@property
def rest_send(self) -> RestSend:
"""
# Summary
Get/set an instance of the RestSend class.
## Raises
### TypeError
- setter: if the value is not an instance of RestSend.
### ValueError
- getter: if RestSend.params is not set.
"""
return self._rest_send
@rest_send.setter
def rest_send(self, value: RestSend) -> None:
method_name: str = inspect.stack()[0][3]
_class_have: str = ""
_class_need: Literal["RestSend"] = "RestSend"
msg = f"{self.class_name}.{method_name}: "
msg += f"value must be an instance of {_class_need}. "
msg += f"Got value {value} of type {type(value).__name__}."
try:
_class_have = value.class_name
except AttributeError as error:
msg += f" Error detail: {error}."
raise TypeError(msg) from error
if _class_have != _class_need:
raise TypeError(msg)
if not value.params:
msg = f"{self.class_name}.{method_name}: "
msg += "RestSend.params must be set."
raise ValueError(msg)
self._rest_send = value
@property
def results(self) -> Results:
"""
# Summary
Get/set an instance of the Results class.
## Raises
### TypeError
- setter: if the value is not an instance of Results.
"""
return self._results
@results.setter
def results(self, value: Results) -> None:
method_name: str = inspect.stack()[0][3]
_class_have: str = ""
_class_need: Literal["Results"] = "Results"
msg = f"{self.class_name}.{method_name}: "
msg += f"value must be an instance of {_class_need}. "
msg += f"Got value {value} of type {type(value).__name__}."
try:
_class_have = value.class_name
except AttributeError as error:
msg += f" Error detail: {error}."
raise TypeError(msg) from error
if _class_have != _class_need:
raise TypeError(msg)
self._results = value
@property
def switch_details(self) -> SwitchDetails:
"""
# Summary
Return the switch_details instance
## Raises
### TypeError
- `switch_details` is not an instance of `SwitchDetails()`.
"""
return self._switch_details
@switch_details.setter
def switch_details(self, value: SwitchDetails) -> None:
method_name: str = inspect.stack()[0][3]
_class_have: str = ""
_class_need: Literal["SwitchDetails"] = "SwitchDetails"
msg = f"{self.class_name}.{method_name}: "
msg += f"value must be an instance of {_class_need}. "
msg += f"Got value {value} of type {type(value).__name__}."
try:
_class_have = value.class_name
except AttributeError as error:
msg += f" Error detail: {error}."
raise TypeError(msg) from error
if _class_have != _class_need:
raise TypeError(msg)
self._switch_details = value
@property
def switches(self) -> list[str]:
"""
# Summary
A list of switch ip addresses.
## Raises
### TypeError
- setter: switches is not a list.
- setter: switches contains anything other than strings.
### ValueError
- setter: switches list is empty.
## Example
```python
instance = BootflashInfo()
instance.switches = ["192.168.1.1", "192.168.1.2"]
switches = instance.switches
```
"""
return self._switches
@switches.setter
def switches(self, value: list[str]) -> None:
method_name: str = inspect.stack()[0][3]
if not isinstance(value, list):
msg = f"{self.class_name}.{method_name}: "
msg += "switches must be a list. "
msg += f"got {type(value).__name__} for "
msg += f"value {value}."
raise TypeError(msg)
if len(value) == 0:
msg = f"{self.class_name}.{method_name}: "
msg += "switches must be a list with at least one ip address. "
msg += f"got {value}."
raise ValueError(msg)
for item in value:
if not isinstance(item, str):
msg = f"{self.class_name}.{method_name}: "
msg += "switches must be a list of ip addresses. "
msg += f"got type {type(item).__name__} for "
msg += f"value {item}."
raise TypeError(msg)
self._switches = value