This section introduces how to create and define a plugin step by step.

Example: Create a plugin to add devices by importing configurations.

1.On the Domain Management page, click Operations >Advanced Tools > Plugin Manager.

2.Point to the My Plugins folder, click the icon, and select New Plugin.

3.Enter a meaningful name, such as ImportConfig.

Note: The name can only contain letters, numbers, and underscores. It cannot start with a number and contain spaces.

4.On the Description tab, enter a description of the plugin.

5.On the Input tab, enter the input parameters for the plugin. The parameters can be modified at the plugin execution. In this case, enter the IP address, username and password and file name to retrieve the configuration file stored in an FTP server.

{
    "IP":"10.10.16.15",
    "usename":"test",
    "password":"test",
    "filename":"impoprt_DeviceData.csv"
}

Tip: The format of the input can be JSON, text, CSV or others, which is determined by the python scripts that you define on the main.py tab.

6.On the main.py tab, enter the python scripts to execute inputs (if defined) and import configurations in this case. You can call system APIs in the script.

import csv
import os
import json
import uuid
import traceback
 
from netbrain.sysapi import (
    pluginfw,
    devicedata,
)
from netbrain.sysapi.datamodel import QueryDataFromDB
from ftplib import FTP
 
ftpConn = FTP()
 
 
def parse_csv(csvfile):
    with open(csvfile, encoding='utf-8', errors='ignore') as csvDataFile:
        csvReader = csv.reader(csvDataFile)
        for index, row in enumerate(csvReader):
            if index == 0:
                continue
            file_name = row[0]
            driver = row[1]
            try:
                _uid = uuid.UUID(driver)
                if isinstance(_uid, uuid.UUID):
                    dev_id = driver
            except Exception:
                dev_objs = QueryDataFromDB('Initial_Tenant', 'Driver',{'name': driver})
                for dev_obj in dev_objs:
                    dev_id = dev_obj.get('_id')
                    break
            if dev_id:
                tempFile = 'C:\\Windows\\Temp\\%s' % dev_id
                with open(tempFile, 'wb') as f:
                    ftpConn.retrbinary('RETR ' + file_name, f.write, 1024)
                with open(tempFile, encoding='utf-8', errors='ignore') as ConfigFile:
                    configText = ConfigFile.read()
                    dev_items = devicedata.ImportConfig(dev_id, configText)
                    msg = '%s%s' % (dev_items, type(dev_items))
                    pluginfw.AddLog(msg, pluginfw.DEBUG)
                    msg = '[%s] Import config successfully' % file_name
                    pluginfw.AddLog(msg, pluginfw.DEBUG)
                os.remove(tempFile)
 
 
def run(input):
    '''
        this is the plugin entry point.
        todo: write the real logic in this function.
 
        return True if the everything is OK.
        return False if some error occur.
    '''
 
    ftpInfo = json.loads(input)
    ftpIP = ftpInfo.get('IP') or ''
    ftpPort = ftpInfo.get('port') or '21'
    if ftpPort and isinstance(ftpPort, str):
        ftpPort = int(ftpPort)
    ftpUser = ftpInfo['username']
    ftpPassword = ftpInfo['password']
    filename = ftpInfo['filename']
    TempFile = 'C:\\Windows\\Temp\\' + filename
    ftpConn.set_debuglevel(2)
    ftpConn.connect(host=ftpIP, port=ftpPort)
    ftpConn.login(user=ftpUser, passwd=ftpPassword)
    with open(TempFile, 'wb') as f:
        ftpConn.retrbinary('RETR ' + filename, f.write, 1024)
 
    try:
        parse_csv(TempFile)
        result = True
    except Exception:
        msg = traceback.format_exc()
        pluginfw.AddLog(msg, pluginfw.DEBUG)
        result = False
 
    ftpConn.close()
    os.remove(TempFile)
    return result

7.Set the Default Installation Settings. The Default Installation Settings is used to define whether to enable a plugin and apply the predefined execution point to a benchmark or discovery task automatically when you create the task.

8.(Optional) Define the device group that the plugin applies to. When the device group is defined, the system performs a qualification check based on the device group. If the device group is detected empty when running the plugin, the plugin is not be executed.

9.Click Save.

10. (Optional) Debug the plugin to test whether it works.

Note: The plugin will be executed in the system to create an instance when you debug it.

1)Point to the plugin and click the icon to select Debug Run.

2)Modify the input parameters if necessary.

3)Click Run and view the logs to determine whether the plugin works.

11. (Optional) Lock the plugin with a password. A locked plugin cannot be opened or modified without entering the correct password. To lock a plugin, point to the plugin, click the icon, select Lock, and enter a password.