Enabling the Automation Context feature for plug-ins

You can use the Automation Context feature to automate the orchestration of Site Planner objects. To use the Automation Context feature with a custom plug-in, you must create an automation.py file for the plug-in and complete other URL, template, and registration tasks.

Before you begin

You must implement GraphQL service support for your custom plug-in model to use the Site Planner Automation Context feature.

GraphQL is a query language, and a server-side runtime for testing queries. For more information about enabling GraphQL service on your custom plug-in model, see the GraphQL documentation.

Testing queries for your plug-ins

When you create an Automation Context instance, you must test queries and copy them to the Data Expressions field. Use GraphQL UI to run and test queries for your custom Site Planner plug-in models. If the query test is satisfactory, copy the query into the Data Expressions field of the Automation Context feature. The Data Expressions field contains GraphQL queries that are designed to fetch data from the Site Planner models. The GraphQL query in the Data Expressions field runs when an object that matches the Automation Context feature is built in the custom Site Planner plug-in model.

The following sample query shows the id, name, device role, and site of the devices with the name example-role:
{
    devices (filters: {device_role__name: "example-role"}){
      id
      name
    	device_role {
    	  id
        name
    	}
      site {
        id
        name
      }
    }
}
The following response is received for the query:
{
  "data": {
    "devices": [
      {
        "id": "1",
        "name": "example-device",
        "device_role": {
          "id": "1",
          "name": "example-role"
        },
        "site": {
          "id": "1",
          "name": "example-site"
        }
      }
    ]
  }
}

For more information about GraphQL, see Data expressions.

Overview of steps

Complete the following steps to add custom plug-ins to the Automation Context feature:
  1. Create an automation file
  2. Register the metadata instance
  3. Register URL patterns for the Automation tab view
  4. Register a template extension

For more information about Automation Contexts, see Automation Contexts.

Create an automation file

Create an automation file called automation.py in your custom plug-in model. Create the file at the same level as the __init__.py file. In the automation.py file, for each model, create a new class that inherits from the ObjectTypeAutomationMetadata class.

The following sample automation.py file generates an Application model with the following fields:
  • name
  • display_name
  • model
  • automation_url_group
  • automation_url_prefix
  • graphql_arg_fields
from nfvi_automation.models.object_type_metadata import ObjectTypeAutomationMetadata
from .models import Application

class ApplicationAutomationMetadata(ObjectTypeAutomationMetadata):

    ## <app_name>.<model_name> (lowercase)
    name = 'netbox_applications.application'

    ## Name displayed to user. Normally the "verbose name" of your model
    display_name = 'Application'

    ## The Model (imported from models.py)
    model = Application

    ## URL for viewing automation
    ## automation_url_group should be "plugins:<app_name>"
    automation_url_group = 'plugins:netbox_applications'
    ## should match the first section (before the first "/") in the paths from urls.py for this model
    automation_url_prefix = 'applications'

    ## Fields to be extracted from the instance and passed as query arguments to the "data_expression" GraphQL query on the Automation Context
    ## Allows a user to query for data based on identifiable information of this object 
    ## We recommend providing any unique/identifiable attributes for your model
    ## Plus any unique/identifiable attributes for related models (e.g. "service__id" is the id of the related "service")
    graphql_arg_fields = ['id', 'installation_name', 'app_name', 'service__id', 'service__name']

    ## If your model can be categorised by "type" then you can enable these fields 
    ## to allow Automation Contexts to be associated with a particular type 
    ## (e.g. Devices have a DeviceType, so we can associate Automation Context to all Devices with DeviceType "HP XYZ")
    ## Name of the field on your model which relates to a type
    #assignable_type_field = 'type'

    ## The model which represents the type
    #assignable_type_model = ApplicationType

    ## Name of the field to be extracted from the related type when exporting an instance of this Automation Context
    #assignable_type_model_export_field = 'name'

    ## Alternative to "assignable_type_model" and "assignable_type_model_export_field"
    ## When the model is categorised by types presented as a ChoiceSet rather than a model
    #assignable_type_choices = None

    ## If your model can be categorised by "role" then you can enable these fields 
    ## to allow Automation Contexts to be associated with a particular role 
    ## (e.g. Devices have a DeviceRole, so we can associate Automation Context to all Devices with DeviceRole "Compute")
    
    ## Name of the field on your model which relates to a role
    #assignable_role_field = 'role'
    
    ## The model which represents the role
    #assignable_role_model = None
    
    ## Name of the field to be extracted from the related role when exporting an instance of this Automation Context
    #assignable_role_model_export_field = 'name'
    
    ## Alternative to "assignable_role_field" and "assignable_role_model_export_field"
    ## When the model is categorised by roles presented as a ChoiceSet rather than a model
    #assignable_role_choices = None
    

# Recommended you create an instance as this will be reused later
application_metadata = ApplicationAutomationMetadata()

Register the metadata instance

The metadata instance describes the information that is needed to correctly associate your Site Planner model to the Automation Contexts. To register a metadata instance, open the __init__.py file in any text editor and add the ready method to your PluginConfig class. The PluginConfig class contains all the information that you need to know about your plug-in to install it. Each PluginConfig class provides a subclass that defines the name, metadata, and default and required configuration parameters for the plug-in. These configuration parameters are included in the __init__.py file.

The ready method registers an instance of the metadata to the ObjectTypeAutomationMetadata.register function. Register all of your metadata instances.

The following sample ready call registers the application_metadata instance:
def ready(self):
        super().ready()
        from .automation import application_metadata
        from nfvi_automation.models import ObjectTypeAutomationMetadata
        
        ObjectTypeAutomationMetadata.register.add(application_metadata)
Note: You must keep the import statements inside the ready method. Use ready method to call super().ready() on the parent class.

Register URL patterns for the Automation tab view

For your custom plug-in model, register the Automation tab view as an extra path to the urlpatterns variable in the urls.py file.

The following sample code maps a URL pattern for the application_metadata instance to an Automation tab view:
from django.urls import path
from .automation import application_metadata
from nfvi_automation.views import AutomationContextProcessView

urlpatterns = (
    ...
    other paths
    ...
    path(f'{application_metadata.automation_url_prefix}/<int:object_pk>/automation/',  AutomationContextProcessView.as_view(), 
        name=application_metadata.get_automation_view_name(), kwargs={'metadata': application_metadata})
)
A URL pattern has the following components:
Component Description
path The unique portion of the URL that is dedicated to the Automation tab view.
view The Automation tab view.
name The name used to identify the URL path.

Register a template extension

Templates are used to render HTML content that is generated from Automaton Context data. Create a custom Django template content file that is called template_content.py, if one does not exist already.

The following sample code registers a template content extension for the Application plug-in:
from nfvi_automation.template_content import AutomationExtensions
from .automation import application_metadata

class ApplicationAutomationExtensions(AutomationExtensions):
    metadata = application_metadata
    model = 'netbox_applications.application'
    
template_extensions = [ApplicationAutomationExtensions]