Skip to content
401 changes: 227 additions & 174 deletions plugins/module_utils/bootflash/bootflash_files.py

Large diffs are not rendered by default.

138 changes: 70 additions & 68 deletions plugins/module_utils/bootflash/bootflash_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
# 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
Expand Down Expand Up @@ -40,12 +43,15 @@ class BootflashInfo:

## Raises

- ``ValueError`` if:
- params is not set.
- switches is not set.
- ``TypeError`` if:
- switches is not a list.
- switches contains anything other than strings.
### ValueError

- params is not set.
- switches is not set.

### TypeError

- switches is not a list.
- switches contains anything other than strings.

## Usage

Expand Down Expand Up @@ -265,10 +271,11 @@ def validate_refresh_parameters(self) -> None:

## Raises

- `ValueError` if:
- self.rest_send.params is not set.
- self.switches is not set.
- switches is not set.
### ValueError

- self.rest_send.params is not set.
- self.switches is not set.
- switches is not set.
"""
method_name: str = inspect.stack()[0][3]

Expand All @@ -293,8 +300,9 @@ def refresh(self) -> None:

## Raises

- `ValueError` if:
- switches is not set.
### ValueError

- switches is not set.

"""
self.validate_refresh_parameters()
Expand All @@ -318,8 +326,9 @@ def refresh_bootflash_info(self) -> None:

## Raises

- `ValueError` if:
- serial_number cannot be found for a switch.
### ValueError

- serial_number cannot be found for a switch.
"""
method_name: str = inspect.stack()[0][3]
self.info_dict = {}
Expand Down Expand Up @@ -355,13 +364,13 @@ def validate_prerequisites_for_build_matches(self) -> None:
"""
# Summary

Verify that mandatory prerequisites are met before calling
`build_matches()`.
Verify that mandatory prerequisites are met before calling `build_matches()`.

## Raises

- `ValueError` if:
- info_dict is empty i.e. `refresh` has not been called.
### ValueError

- info_dict is empty i.e. `refresh` has not been called.
"""
method_name: str = inspect.stack()[0][3]

Expand All @@ -373,18 +382,18 @@ def validate_prerequisites_for_build_matches(self) -> None:

def match_filter_filepath(self, target: dict[str, str]) -> bool:
"""
## Summary
# Summary

- Return True if the target's `filepath` matches `filter_filepath`.
- Return False otherwise.
- 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") or ""
filepath: str = target.get("filepath", "")
posix: PurePosixPath = PurePosixPath(filepath)
if not posix.match(self.filter_filepath):
return False
Expand All @@ -394,33 +403,33 @@ 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.
- 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", None) != self.filter_supervisor:
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.
- 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", None) != self.filter_switch:
if target.get("ip_address", "") != self.filter_switch:
return False
return True

Expand Down Expand Up @@ -463,8 +472,8 @@ def build_matches(self) -> None:

diff: dict[str, list[dict[str, str]]] = {}
for match in self._matches:
ip_address = match.get("ip_address", None)
if ip_address is None:
ip_address = match.get("ip_address", "")
if not ip_address:
continue
if ip_address not in diff:
diff[ip_address] = []
Expand All @@ -478,19 +487,18 @@ def filter_filepath(self) -> str:

Return the current `filter_filepath`.

`filter_filepath` is a file path used to filter the results
of the query. It can include file globbing.
`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"
- 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

Expand All @@ -513,9 +521,9 @@ def filter_supervisor(self) -> str:

## Raises

- `ValueError` if:
- value is not one of the valid_supervisor values
"active" or "standby".
### ValueError

- `value` is not one of the valid_supervisor values "active" or "standby".

## Example

Expand Down Expand Up @@ -604,26 +612,18 @@ def rest_send(self) -> RestSend:
"""
# Summary

An instance of the RestSend class.
Get/set an instance of the RestSend class.

## Raises

- setter: `TypeError` if the value is not an instance of RestSend.
- getter: `ValueError` if RestSend.params is not set.
### TypeError

## getter
- setter: if the value is not an instance of RestSend.

Return an instance of the RestSend class.
### ValueError

## setter

Set an instance of the RestSend class.
- getter: if RestSend.params is not set.
"""
method_name: str = inspect.stack()[0][3]
if not self._rest_send.params:
msg = f"{self.class_name}.{method_name}: "
msg += "RestSend.params must be set before accessing."
raise ValueError(msg)
return self._rest_send

@rest_send.setter
Expand All @@ -641,26 +641,25 @@ def rest_send(self, value: RestSend) -> None:
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

An instance of the Results class.
Get/set an instance of the Results class.

## Raises

- setter: `TypeError` if the value is not an instance of Results.

## getter
### TypeError

Return an instance of the Results class.
- setter: if the value is not an instance of Results.

## setter

Set an instance of the Results class.
"""
return self._results

Expand Down Expand Up @@ -690,7 +689,9 @@ def switch_details(self) -> SwitchDetails:

## Raises

- `TypeError` if `switch_details` is not an instance of `SwitchDetails()`.
### TypeError

- `switch_details` is not an instance of `SwitchDetails()`.
"""
return self._switch_details

Expand Down Expand Up @@ -720,13 +721,14 @@ def switches(self) -> list[str]:

## Raises

- getter: None
- setter:
- `TypeError` if:
- switches is not a list.
- switches contains anything other than strings.
- `ValueError` if:
- switches list is empty.
### TypeError

- setter: switches is not a list.
- setter: switches contains anything other than strings.

### ValueError

- setter: switches list is empty.

## Example

Expand Down
Loading