Utility Functions
Copyright 2023 Infoblox
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
1 | |
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.
csv_filtered_header(row, col_filter=None)
fetches csv required fields for header w/ any other(s) in filter list
@param row: csv header row with fieldnames @param col_filter: list of other columns to filter on @return header_object: dictionary of column and index in the header row
Source code in src/ibx_sdk/util/util.py
extract_filename_from_url(url)
The function extract_filename_from_url retrieves the name of the CSV file from a given URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
An Infoblox download URL for CSV export. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The name of the file that would be downloaded from the specified URL. |
Raises:
| Type | Description |
|---|---|
Exception
|
An exception is raised if the URL cannot be parsed. |
Logging
This function logs debug messages as it parses the URL, and an error message if an exception was raised while parsing.
Usage
Use this function to retrieve the name of a file from an Infoblox download URL:
file_name = extract_filename_from_url('https://my-infoblox-instance/export.csv')
Source code in src/ibx_sdk/util/util.py
generate_from_includes(chroot, filepath)
The function generate_from_includes generates a configuration file from include(s) directives found in another configuration file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chroot
|
str
|
The path to the chroot environment where the config file to process exists. |
required |
filepath
|
str
|
Path to the initial config file to process. The path is relative to chroot if it starts with '/'. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A single string containing the full contents of the config file. This includes data |
str
|
from the initial config file and all files included with include directives. If the file specified in the filepath parameter does not exist, an empty string is returned. |
Logging
This function logs informational messages indicating the files it's processing and any 'include' directives it encounters. An error message is logged if it attempts to process a file that doesn't exist.
Usage
Use this function to generate a full config file from one that includes other files with include directives:
full_config = generate_from_includes('/path/to/chroot', '/path/to/my_config')
Source code in src/ibx_sdk/util/util.py
get_csv_header(csvfile)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
header = get_csv_header(csv_file) ... print(header)
Source code in src/ibx_sdk/util/util.py
ibx_csv_file_split(filename, output_path='.')
The function ibx_csv_file_split splits a globally exported CSV file into separate CSV files based on the CSV object type(s).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
The name of the source CSV file to be split. |
required |
output_path
|
str
|
The directory where the output CSV files will be written. If the directory does not exist, it will be created. Defaults to the current directory. |
'.'
|
Raises:
| Type | Description |
|---|---|
Exception
|
An exception is raised if the output directory cannot be created. |
Logging
This function logs warning messages when a CSV object type has no associated objects in the source CSV. It also logs informational messages when it creates output CSV files.
Usage
Use this function to split a Infoblox exported CSV file into separate CSV files for
each CSV object type:
ibx_csv_file_split('/path/to/my_exported_csv_file', '/path/to/output_directory')
Source code in src/ibx_sdk/util/util.py
named_checkconf(chroot_path, conf_path)
perform named-checkconf and re-write a canonicalized copy of the named.conf file
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chroot_path
|
str
|
Description of the first argument |
required |
conf_path
|
str
|
Description of the second argument |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Example
Here you can provide an example of how to use your function.
named_checkconf("/path/to/chroot", "/path/to/conf")
Source code in src/ibx_sdk/util/util.py
named_compilezone(zone_name, zone_file, output_file, input_format='text')
The function named_compilezone performs the named-compilezone command to canonicalize and rewrite a specified DNS zone file, checking for errors in the file during the process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
zone_name
|
str
|
The name of the DNS zone being processed. |
required |
zone_file
|
str
|
The path to the file containing the DNS zone information to be read/processed. |
required |
output_file
|
str
|
The path where the canonicalized zone file will be written to. |
required |
input_format
|
str
|
The format in which the zone information is currently presented in the zone file. This should be either 'text' or 'raw'. Defaults to 'text'. |
'text'
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
A list of strings, each string being a line from the output of the named-compilezone command that indicates an error which prevented the DNS zone from being loaded. If there are no such errors, an empty list is returned. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Usage
Call this function to check for, log, and return errors in a DNS zone file as it is being processed:
errors = named_compilezone('my_zone', '/path/to/my_zone_file', '/path/to/output_file') if errors: ... print('Errors found during zone processing.') ... for error in errors: ... print(error)
Source code in src/ibx_sdk/util/util.py
remove_lines_from_file(file_path, lines_to_remove, output_path=None)
The function remove_lines_from_file removes specific lines from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
The fully qualified path to the file from which lines are to be removed. |
required |
lines_to_remove
|
list
|
A list of integers, with each integer being a line number (1-indexed) of the line to be removed from the file. |
required |
output_path
|
str
|
Path to the output file. If provided, the function will write result to this file. If not provided, the function will overwrite the original file. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Logging
This function logs warning messages for each line that it removes from the file. It also logs an info message once the file has been successfully rewritten.
Usage
Use this function to remove specific lines from a file:
remove_lines_from_file('/path/to/my_file', [2, 3]) This will remove the 2nd and 3rd lines from the file located at /path/to/my_file.
To write the result to a different file, specify the output_path argument:
remove_lines_from_file('/path/to/my_file', [2, 3], '/path/to/output_file') This will remove the 2nd and 3rd lines from the file located at /path/to/my_file and write the result to a file located at /path/to/output_file.