Design Builder
nautobot_design_builder.design
¶
Provides ORM interaction for design builder.
Environment
¶
The design builder build environment.
The build Environment
contains all of the components needed to implement a design.
This includes custom action tag extensions and an optional JobResult
for logging. The
build environment also is used by some extensions (such as the ref
action tag) to store
information about the designs being implemented.
Source code in nautobot_design_builder/design.py
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 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 |
|
__init__(logger=None, extensions=None, change_set=None, import_mode=False)
¶
Create a new build environment for implementing designs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
logger |
Logger
|
A logger to use. If not supplied one will be created. |
None
|
extensions |
List[Extension]
|
Any custom extensions to use when implementing designs. Defaults to None. |
None
|
change_set |
ChangeSet
|
A change set object to use for logging changes
in the environment. This defaults to |
None
|
import_mode |
bool
|
Whether or not the environment is in import mode. Defaults to False. |
False
|
Raises:
Type | Description |
---|---|
DesignImplementationError
|
If a provided extension is not a subclass
of |
Source code in nautobot_design_builder/design.py
commit()
¶
The commit
method iterates all extensions and calls their commit
methods.
Some extensions need to perform an action after a design has been successfully
implemented. For instance, the config context extension waits until after the
design has been implemented before committing changes to a config context
repository. The commit
method will find all extensions that include a commit
method and will call each of them in order.
Source code in nautobot_design_builder/design.py
decommission_object(object_id, object_name)
¶
This method decommissions an specific object_id from the design instance.
Source code in nautobot_design_builder/design.py
get_extension(ext_type, tag)
¶
Look up an extension based on its tag name and return an instance of that Extension type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ext_type |
str
|
the type of the extension, i.e. 'attribute' or 'value' |
required |
tag |
str
|
the tag used for the extension, i.e. 'ref' or 'git_context' |
required |
Returns:
Name | Type | Description |
---|---|---|
Extension |
Union[Extension, None]
|
An instance of the Extension class |
Source code in nautobot_design_builder/design.py
implement_design(design, commit=False)
¶
Iterates through items in the design and creates them.
This process is wrapped in a transaction. If either commit=False (default) or an exception is raised, then the transaction is rolled back and no database changes will be present. If commit=True and no exceptions are raised then the database state should represent the changes provided in the design.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
design |
Dict
|
An iterable mapping of design changes. |
required |
commit |
bool
|
Whether or not to commit the transaction. Defaults to False. |
False
|
Raises:
Type | Description |
---|---|
DesignImplementationError
|
if the model is not in the model map |
Source code in nautobot_design_builder/design.py
model_factory(django_class)
¶
factory
takes a normal Django model class and creates a dynamic ModelInstance proxy class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
django_class |
Type[Model]
|
The Django model class to wrap in a proxy class. |
required |
Returns:
Type | Description |
---|---|
ModelInstance
|
type[ModelInstance]: The newly created proxy class. |
Source code in nautobot_design_builder/design.py
resolve_value(value)
¶
Resolve a single value using extensions, if needed.
This method will examine a value to determine if it is an action tag. If the value is an action tag, then the corresponding extension is called and the result of the extension execution is returned.
If the value is not an action tag then the original value is returned.
Source code in nautobot_design_builder/design.py
resolve_values(value)
¶
Resolve a value, or values, using extensions.
This method is used to evaluate action tags and call their associated extensions for a given value tree. The method will iterate the values of a list or dictionary and determine if each value represents an action tag. If so, the extension for that tag is called and the original value is replaced with the result of the extension's execution.
Lists and dictionaries are copied so that the original values remain un-altered.
If the value is string and the string is an action tag, that tag is evaluated and the result is returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Union[list, dict, str]
|
The value to attempt to resolve. |
required |
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
The resolved value. |
Source code in nautobot_design_builder/design.py
roll_back()
¶
Looks for any extensions with a roll back method and executes it.
Used for for rolling back changes that can't be undone with a database rollback, for example config context files.
Source code in nautobot_design_builder/design.py
Journal
¶
Keep track of the objects created or updated during the course of a design's implementation.
The Journal provides a way to do post-implementation processing. For instance, if every item created in a design needs to be updated with a tag, then a post_implementation method can be created in the job and the journal.created items can be iterated and updated. The Journal contains three indices:
index: a set of all the model UUIDs that have been created or updated
created: a dictionary of objects created. The keys of this index are model classes and the values are sets of primary key UUIDs
updated: like created, this index is a dictionary of objects that have been updated. The keys are model classes and the values are the primary key UUIDs
An object's UUID may appear in both created and updated. However, they will only be in each of those indices at most once.
Source code in nautobot_design_builder/design.py
created_objects: Dict[str, List[Model]]
property
¶
Return a dictionary of Nautobot objects that were created.
Returns:
Type | Description |
---|---|
Dict[str, List[Model]]
|
Dict[str, List[BaseModel]]: A dictionary of created objects. The |
Dict[str, List[Model]]
|
keys of the dictionary are the lower case content type labels |
Dict[str, List[Model]]
|
(such as |
Dict[str, List[Model]]
|
of the corresponding type. |
__init__(change_set=None, import_mode=False)
¶
Constructor for Journal object.
Source code in nautobot_design_builder/design.py
log(model)
¶
Log that a model has been created or updated.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
BaseModel
|
The model that has been created or updated |
required |
Source code in nautobot_design_builder/design.py
ModelInstance
¶
An individual object to be created or updated as Design Builder iterates through a rendered design YAML file.
ModelInstance
objects are essentially proxy objects between the design builder implementation process
and normal Django models. The ModelInstance
intercepts value assignments to fields and properly
defers database saves so that ForeignKey
and ManyToMany
fields are set and saved in the correct order.
This field proxying also provides a system to model relationships that are more complex than simple database fields and relationships (such as Nautobot custom relationships).
Source code in nautobot_design_builder/design.py
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 |
|
__init__(environment, attributes, relationship_manager=None, parent=None)
¶
Create a proxy instance for the model.
This constructor will create a new ModelInstance
object that wraps a Django
model instance. All assignments to this instance will proxy to the underlying
object using the descriptors in the fields
module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
environment |
Environment
|
The build environment for the current design. |
required |
attributes |
dict
|
The attributes dictionary for the current object. |
required |
relationship_manager |
_type_
|
The relationship manager to use for lookups. Defaults to None. |
None
|
parent |
_type_
|
The parent this object belongs to in the design tree. Defaults to None. |
None
|
Raises:
Type | Description |
---|---|
DoesNotExistError
|
If the object is being retrieved or updated (not created) and can't be found. |
MultipleObjectsReturnedError
|
If the object is being retrieved or updated (not created) and more than one object matches the lookup criteria. |
Source code in nautobot_design_builder/design.py
__str__()
¶
connect(signal, handler)
¶
Connect a handler between this model instance (as sender) and signal.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
signal |
Signal
|
Signal to listen for. |
required |
handler |
FunctionType
|
Callback function |
required |
Source code in nautobot_design_builder/design.py
save()
¶
Save the model instance to the database.
This method will save the underlying model object to the database and
will send signals (PRE_SAVE
, POST_INSTANCE_SAVE
and POST_SAVE
). The
design journal is updated in this step.
Source code in nautobot_design_builder/design.py
ModelMetadata
¶
ModelMetadata
contains all the information design builder needs to track a ModelInstance
.
The model metadata includes the query necessary to find a ModelInstance
in the database, any
attributes to be updated in the instance, the action to take (get, create, update, etc) and
additional metadata about the operation (e.g. whether or not the assignment must be deferred).
In addition to tracking the metadata of an object being manipulated, ModelMetadata
also
encapsulates the signal mechanism used by fields and extensions to perform actions based on
when an object gets saved.
Source code in nautobot_design_builder/design.py
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 |
|
action: str
property
writable
¶
Determine the action.
This property will always return a value. If no action has been explicitly
set in a design object, then the default action is CREATE
. If an action
has been determined (based on action tags) then that action is returned.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
One of the valid values for action: |
attributes
property
writable
¶
Get any attributes that have been processed.
custom_fields: Dict[str, Any]
property
¶
custom_fields
property.
When attributes are processed, the custom_fields
key is removed and assigned
to the custom_fields
property.
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict[str, Any]: A dictionary of custom fields/values. |
deferred: bool
property
¶
Whether or not this model object's save should be deferred.
Sometimes a model, specified as a child within a design, must be
saved after the parent. One good example of this is (in Nautobot 1.x)
a Device.primary_ip4
. If the IP address itself is created within
the device's interface block, and that interface block is defined in the
same block as the primary_ip4
, then the primary_ip4
field cannot be
set until after the interface's IP has been created. Since the interface
cannot be created until after the device has been saved (since the interface
has a required foreign-key field to device) then the sequence must go like this:
1) Save the new device.
2) Save the IP address that will be assigned to the interface
3) Save the interface with foreign keys for device and IP address
4) Set device's primary_ip4
and re-save the device.
The only way to tell design builder to do step 4 last is to set the value on
the field to deferred
. This deferral can be specified as in the following example:
# Note: the following example is for Nautobot 1.x
devices:
- name: "device_1"
site__name: "site_1"
status__name: "Active"
device_type__model: "model name"
device_role__name: "device role"
interfaces:
- name: "Ethernet1/1"
type: "virtual"
status__name: "Active"
description: "description for Ethernet1/1"
ip_addresses:
- address: "192.168.56.1/24"
status__name: "Active"
primary_ip4: {"!get:address": "192.168.56.1/24", "deferred": true}
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
Whether or not the object's assignment should be deferred. |
filter
property
¶
The processed query filter to find the object.
import_mode: bool
property
¶
Indicates whether the underlying environment is in import mode or not.
kwargs
property
¶
Any keyword arguments needed for the creation of the model object.
query_filter: Dict[str, Any]
property
¶
Calculate the query filter for the object.
The query_filter
property collects all of the lookups for an object
(set by !create_or_update
and !get
tags) and computes a dictionary
that can be used as keyword arguments to a model manager .get
method.
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict[str, Any]: The computed query filter. |
query_filter_values
property
¶
Returns a copy of the query-filter field/values.
__init__(model_instance, environment, **kwargs)
¶
Initialize the metadata object for a given model instance.
By default, the metadata object doesn't really have anything in it. In order
to set the internal values for things like action
and kwargs
then the
attributes setter must be used.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_instance |
ModelInstance
|
The model instance to which this metadata refers. |
required |
environment |
Environment
|
The implementation environment being used for the current design. |
required |
**kwargs |
Any
|
Additional metadata specified in the object. |
{}
|
Source code in nautobot_design_builder/design.py
connect(signal, handler)
¶
Connect a handler between this model instance (as sender) and signal.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
signal |
str
|
Signal to listen for. |
required |
handler |
FunctionType
|
Callback function |
required |
Source code in nautobot_design_builder/design.py
create_child(model_class, attributes, relationship_manager=None)
¶
Create a new ModelInstance that is linked to the current instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_class |
Type[Model]
|
Class of the child model. |
required |
attributes |
Dict
|
Design attributes for the child. |
required |
relationship_manager |
Manager
|
Database relationship manager to use for the new instance. |
None
|
Returns:
Name | Type | Description |
---|---|---|
ModelInstance |
ModelInstance
|
Model instance that has its parent correctly set. |
Source code in nautobot_design_builder/design.py
load_instance()
¶
Load the model instance's design instance from the database.
This method will either create a new object or load an existing object from the database, based on the action tags and query strings specified in the design.
Source code in nautobot_design_builder/design.py
send(signal)
¶
Send a signal to all associated listeners.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
signal |
str
|
The signal to send |
required |
Source code in nautobot_design_builder/design.py
Builder(*args, **kwargs)
¶
Builder
is an alias to the Environment
class.
This function is used to provide backwards compatible access to the Builder
class,
which was renamed to Environment
. This function will be removed in the future.