- 18 Oct 2024
-
DarkLight
-
PDF
Bulk IP Address Allocation (Netbox) - Transformation
- Updated on 18 Oct 2024
-
DarkLight
-
PDF
The data copied in the previous step is what we get when we convert our Excel spreadsheet to JSON. Next, we need to build a transformation that will take this JSON data and restructure it to match the inputs required by the Assign Next IP Address in Prefix – NetBox pre-built workflow.
Transformation Video
Step 1
From Automation Studio, click the + icon to open the Create window.
Step 2
Select Transformation from the dropdown menu and name the Transformation Lab – Build Loop Data – NetBox – JST - XX. (XX represents your initials).
Click Create.
Step 3
The canvas loads with a lot of empty space in the center. If you would like to remove extra space from your canvas, you can do so by hovering your cursor near the top of the canvas and selecting the trash can icon. You can also add space to your canvas by hovering your cursor near the top of the canvas and selecting the plus icon.
Step 4
As you can see, we have an Incoming Schema and an Outgoing Schema. This describes how data will look before and after it is manipulated. Let’s start by creating our incoming schema. Hover your cursor over the Add+ button and select Incoming Schema from the list that appears. The Add Incoming Schema window opens.
Step 5
We would like to use the response data from the Excel to JSON workflow task to create our incoming schema. Paste the copied data into the window on the right and click Infer Schema From Example JSON.
{
"Hosts": [
{
"Hostname": "host1.local",
"Pool": "10.0.0.0/8",
"Status": "Active"
},
{
"Hostname": "host2.local",
"Pool": "10.0.0.0/8",
"Status": "Reserved"
},
{
"Hostname": "host3.local",
"Pool": "192.168.0.0/16",
"Status": "Active"
},
{
"Hostname": "host4.local",
"Pool": "192.168.0.0/16",
"Status": "Reserved"
},
{
"Hostname": "host5.local",
"Pool": "172.16.0.0/12",
"Status": "Active"
},
{
"Hostname": "host6.local",
"Pool": "172.16.0.0/12",
"Status": "Reserved"
}
]
}
Step 6
IAP will digest the sample JSON data, determine the appropriate data types, and create a JSON Schema for it, we just need to add an ID. In line 2 in the “$id” field, enter hostData between the double quotes. Click Save. The Incoming Schema has been created.
Step 7
Next, we need to create our outgoing schema to define the shape of the data leaving the transformation. Hover your cursor over the Add+ button under Outgoing Schema and select Outgoing Schema.
Step 8
The Outgoing Schema needs to be in the format required by the Start task of the pre-built workflow, as seen earlier in the training. Copy the JSON Schema from the resource document and paste it into the right side of the outgoing schema window. Click Infer Schema from Example JSON. The Outgoing Schema is created.
[
{
"ipStatus": "",
"dnsName": "",
"prefix": ""
}
]
Step 9
We again need to add an id. In line 2 in the “$id” field, enter loopData between the double quotes. Click Save. The Outgoing Schema has been created.
Step 10
We are now ready to connect our schemas. Incoming JST data can be transformed before being connected to an outgoing schema through the use of methods, which are low-code drag-and-drop representations of JavaScript methods. In our example, we will use a map method, which will allow us to go through the IPs requested in our spreadsheet, one by one, and map the data to match what the pre-built workflow expects.
Drag and drop a map method onto the designer canvas.
Note: For information about a method, click the i icon next to the method name to open the java script documentation. Here you will find a description of the method as well as the parameters and example configurations.
Step 11
Connect the return of the Hosts array of the incoming schema to the array input of the map method by clicking the half circle to the right of the Hosts array and dragging a line to the half circle to the left of the array input.
Step 12
Connect the return of the map method to the loopData array of the Outgoing Schema.
Step 13
The map method requires a callback function, which will execute for each element in the array. Its return value is added as a single element in the new array. Click +New Function to edit the callback function on the map method. The callback function will open in a new tab.
Step 14
Hover your cursor over the three vertical dots next to the function name and select Rename. Name the callback function Build NetBox Data and click Save.
Step 15
We are ready to build our function. The current value of the Incoming Schema is the element that is being processed in the array, which in our example is the Hosts array of the Incoming Schema of our main transformation.
Step 16
We need to edit our Outgoing Schema. Hover your cursor over the Outgoing Schema and select the pencil icon to open the edit window.
Step 17
Copy the JSON data and paste it into the window on the right side of the Edit Schema window. Click Infer Schema From Example JSON. The Outgoing Schema will update. Click Save.
{
"ipStatus": "",
"dnsName": "",
"prefix": ""
}
Step 18
Next, we need to add a method that will transform the data. Add a toLowerCase method to the canvas. This method will return the string converted to lower case.
Step 19
Connect the Status return value of the Incoming Schema to the string parameter of the toLowerCase method.
Step 20
Connect the return value of the toLowerCase method to the ipStatus property of the Outgoing Schema.
Step 21
Connect the Hostname property of the Incoming Schema to the dnsName property of the Outgoing Schema.
Step 22
Connect the Pool property of the Incoming Schema to the prefix property of the Outgoing Schema.
Step 23
Our callback function is complete. Click Save.
Step 24
Hover your cursor over the three vertical dots next to the callback function name and click Close. The callback function has been added to the map method of the Build Loop Data JST.
Step 25
Our transformation is complete. Click Save.
Step 26
To preview the format of the outgoing data, click the Run button.
Step 27
The Run Transformation window is populated with example data used to create the incoming schema. Click Run and then review the output.