Skip to content

Commit 4e6e48c

Browse files
authored
Merge pull request #137 from alperenkose/main
feat: non-interactive mode and firewall/batch CLI options to set firewalls to be upgraded
2 parents db0ad37 + ee7aefa commit 4e6e48c

File tree

3 files changed

+155
-81
lines changed

3 files changed

+155
-81
lines changed

pan_os_upgrade/components/device.py

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,42 +35,34 @@
3535

3636
# Common setup for all subcommands
3737
def common_setup(
38-
hostname: str,
39-
username: str,
40-
password: str,
4138
settings_file: LazySettings,
4239
settings_file_path: Path,
43-
) -> PanDevice:
40+
) -> None:
4441
"""
45-
Initializes the environment for interacting with a Palo Alto Networks device, including directory setup, logging configuration, and establishing a device connection.
42+
Initializes the environment for interacting with a Palo Alto Networks device, including directory setup and logging configuration.
4643
47-
This function consolidates essential preparatory steps required before performing operations on a Palo Alto Networks device. It ensures the creation of necessary directories for organized data storage and logs, sets up logging with a configurable verbosity level, and establishes a secure connection to the device using the provided API credentials. The function is designed to return a `PanDevice` object, which could be a `Firewall` or `Panorama` instance, ready for subsequent API interactions.
44+
This function consolidates essential preparatory steps required before performing operations on a Palo Alto Networks device. It ensures the creation of necessary directories for organized data storage and logs, and sets up logging with a configurable verbosity level.
4845
4946
Parameters
5047
----------
51-
hostname : str
52-
The network address or DNS name of the Palo Alto Networks device to connect to.
53-
username : str
54-
The API username for authenticating with the device.
55-
password : str
56-
The API password for authenticating with the device.
57-
58-
Returns
59-
-------
60-
PanDevice
61-
A connected `PanDevice` instance, representing the target Palo Alto Networks device, fully initialized and ready for further API operations.
48+
settings_file : LazySettings
49+
The LazySettings object containing configurations loaded from the settings file.
50+
settings_file_path : Path
51+
The filesystem path to the settings.yaml file, which contains custom configuration settings.
6252
6353
Example
6454
-------
6555
Initializing the environment for a device:
66-
>>> device = common_setup('10.0.0.1', 'apiuser', 'apipassword')
67-
# Ensures necessary directories exist, logging is configured, and returns a connected `PanDevice` instance.
56+
>>> common_setup(
57+
settings_file=Dynaconf(settings_files=[str(SETTINGS_FILE_PATH)]),
58+
settings_file_path=SETTINGS_FILE_PATH,
59+
)
60+
# Ensures necessary directories exist, and logging is configured.
6861
6962
Notes
7063
-----
7164
- Directory setup is performed only once; existing directories are not modified.
7265
- Logging configuration affects the entire application's logging behavior; the log level can be overridden by `settings.yaml` if `SETTINGS_FILE_PATH` is detected in the function.
73-
- A successful device connection is critical for the function to return; otherwise, it may raise exceptions based on connection issues.
7466
7567
The ability to override default settings with `settings.yaml` is supported for the log level configuration in this function if `SETTINGS_FILE_PATH` is utilized within `configure_logging`.
7668
"""
@@ -93,14 +85,6 @@ def common_setup(
9385
settings_file_path=settings_file_path,
9486
)
9587

96-
# Connect to the device
97-
device = connect_to_host(
98-
hostname=hostname,
99-
username=username,
100-
password=password,
101-
)
102-
return device
103-
10488

10589
def connect_to_host(
10690
hostname: str,
@@ -115,16 +99,16 @@ def connect_to_host(
11599
Parameters
116100
----------
117101
hostname : str
118-
The hostname or IP address of the target Palo Alto Networks device.
119-
api_username : str
120-
The API username for authentication.
121-
api_password : str
122-
The password corresponding to the API username.
102+
The network address or DNS name of the Palo Alto Networks device to connect to.
103+
username : str
104+
The API username for authenticating with the device.
105+
password : str
106+
The API password for authenticating with the device.
123107
124108
Returns
125109
-------
126110
PanDevice
127-
A PanDevice object representing the connected device, which may be a Firewall or Panorama instance.
111+
A PanDevice object representing the connected device, which may be a Firewall or Panorama instance, ready for further API operations.
128112
129113
Raises
130114
------
@@ -146,6 +130,7 @@ def connect_to_host(
146130
- Initiating a connection to a device is a prerequisite for performing any operational or configuration tasks via the API.
147131
- The function's error handling provides clear diagnostics, aiding in troubleshooting connection issues.
148132
- Configuration settings for the connection, such as timeout periods and retry attempts, can be customized through the `settings.yaml` file, if `settings_file_path` is utilized within the function.
133+
- A successful device connection is critical for the function to return; otherwise, it may raise exceptions based on connection issues.
149134
"""
150135

151136
try:

pan_os_upgrade/components/utilities.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ def get_emoji(action: str) -> str:
790790
return emoji_map.get(action, "")
791791

792792

793-
def ip_callback(value: str) -> str:
793+
def ip_callback(value: str) -> Union[str, None]:
794794
"""
795795
Validates the input as either a resolvable hostname or a valid IP address, intended for CLI input validation.
796796
@@ -825,6 +825,8 @@ def ip_callback(value: str) -> str:
825825
- The function's utility extends beyond mere validation, contributing to the tool's overall resilience and user-friendliness by preventing erroneous network operations.
826826
- Default settings can be overridden by configurations specified in a `settings.yaml` file if `SETTINGS_FILE_PATH` is used within the script, allowing for customized validation logic based on the application's needs.
827827
"""
828+
if value is None:
829+
return value
828830

829831
# First, try to resolve as a hostname
830832
if resolve_hostname(hostname=value):

0 commit comments

Comments
 (0)