Util
nautobot_design_builder.util
¶
Main design builder app module, contains DesignJob and base methods and functions.
conditional_load_job(designs, module_path, module_name, class_name)
¶
Populate the designs
dictionary if the loaded design works with the current version of Nautobot.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
designs |
dict
|
Dictionary of design classes |
required |
module_path |
str
|
path to module containing the design class |
required |
module_name |
str
|
name of the module to load from the package. |
required |
class_name |
str
|
name of the class to load from the module. |
required |
Source code in nautobot_design_builder/util.py
designs_in_directory(path, package_name, local_logger=logger, module_name=None, reload_modules=False)
¶
Find all the designs in a directory.
Walk the available Python modules in the given directory, and for each module, walk its DesignJob class members.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
Directory to import modules from, outside of sys.path |
required |
package_name |
str
|
The package to which discovered modules will belong. |
required |
local_logger |
Logger
|
The logging instance to use. This is especially useful when a logger includes a JobResult. |
logger
|
module_name |
str
|
Specific module name to select; if unspecified, all modules will be inspected |
None
|
reload_modules |
bool
|
Whether to force reloading of modules even if previously loaded into Python. |
False
|
Yields:
Type | Description |
---|---|
str
|
("package_name.module_name", "DesignJobClassName") |
Source code in nautobot_design_builder/util.py
designs_in_repository(repo, local_logger=logger, module_name=None, reload_modules=False)
¶
Iterate over the designs in a given git repository.
Returns:
Name | Type | Description |
---|---|---|
Iterator |
Iterator[Tuple[str, Type[DesignJob]]]
|
an iterator that will return tuples of package names and class names. |
For the following directory structure
repo-base-dir/designs/design1.py # containing class Design1(DesignJob) repo-base-dir/designs/design2.py # containing class Design2(DesignJob)
The returned values will be yielded
("repo_base_dir.design1", "Design1") ("repo_base_dir.design2", "Design2")
Note that the package name is the full package name where the base package is a normalized version of the GitRepository slug. The name is normalized by replacing dashes and spaces with underscores and converting to lower case.
The reason designs are loaded with a base package of the git repo slug is to prevent collisions of class names and modules between design repos and also to allow for designs spanning multiple files (breaking out contexts and so forth into other files).
Source code in nautobot_design_builder/util.py
get_class_dir(cls)
¶
Function to return the directory where a given path is stored.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
A path to a directory |
get_created_and_last_updated_usernames_for_model(instance)
¶
Get the user who created and last updated an instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instance |
Model
|
A model class instance |
required |
Returns:
Name | Type | Description |
---|---|---|
created_by |
str
|
Username of the user that created the instance |
last_updated_by |
str
|
Username of the user that last modified the instance |
Source code in nautobot_design_builder/util.py
get_design_class(path, module_name, class_name)
¶
Retrieve the Python class using a filesystem path, module name and class name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
filesystem path to the package containing the module. |
required |
module_name |
str
|
name of the module to load from the package. |
required |
class_name |
str
|
name of the class to load from the module. |
required |
Returns:
Type | Description |
---|---|
Type[DesignJob]
|
Type[DesignJob]: The class. |
Source code in nautobot_design_builder/util.py
load_design_file(cls, resource)
¶
Reads data from a file and returns it as string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
type
|
The class to use to determine the path to find the resource. |
required |
resource |
str
|
name of the YAML design file without the path |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
contents of design file as string |
Source code in nautobot_design_builder/util.py
load_design_module(path, package_name, module_name)
¶
Load module_name from the path and set the parent package to package_name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
Path to directory containing the module. |
required |
package_name |
str
|
Name to give the loaded package. |
required |
module_name |
str
|
Name of the module to load from the path. |
required |
Raises:
Type | Description |
---|---|
ModuleNotFoundError
|
If the package cannot be found (missing init.py) or the module cannot be found. |
Returns:
Type | Description |
---|---|
Type[ModuleType]
|
Type[ModuleType]: The loaded module. |
Source code in nautobot_design_builder/util.py
load_design_package(path, package_name)
¶
Load the package (init.py) from the path and assign it package_name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
description |
required |
package_name |
str
|
description |
required |
Raises:
Type | Description |
---|---|
ModuleNotFoundError
|
description |
Returns:
Name | Type | Description |
---|---|---|
_type_ |
Type[ModuleType]
|
_description_import |
Source code in nautobot_design_builder/util.py
load_design_yaml(cls, resource)
¶
Loads data from a YAML design file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
type
|
The class to use to determine the path to find the resource. |
required |
resource |
str
|
name of the YAML design file without the path |
required |
Returns:
Type | Description |
---|---|
List | Dict
|
list or dict: list or dictionary containing data from YAML design files |
Source code in nautobot_design_builder/util.py
load_jobs(module_name=None)
¶
Expose designs to the Nautobot Jobs framework.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
module_name |
str
|
Module name to limit design discovery. Defaults to None. |
None
|
This method is used inside a jobs module to expose designs to Nautobot. Due
to a limitation with the way jobs are loaded, it is not possible for jobs
to be organized in multiple files. The load_jobs
method essentially overcomes
this limitation by discovering designs and creating dynamic classes within
a jobs module.
To use this method, create a jobs module like so:
# jobs.py
from nautobot_design_builder.util import load_jobs
load_jobs()
If designs are stored in different modules and that module separation is desired, then a module name can be supplied to the method:
# jobs/tenant1.py
from nautobot_design_builder.util import load_jobs
load_jobs(module_name="tenant1")
Source code in nautobot_design_builder/util.py
package_name_for_repo(repo)
¶
Generate the package name for a git repository.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
repo |
GitRepository
|
Git Repository containing some design builder designs |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
package name, such as design_builder_designs.repo_slug |