So… I’ve battled this exact use case before and I can tell you to quote Steve Jobs, your “holding (doing) it wrong”
First off, because of the underlying async nature of how communication with the CPE occurs, its going to be near impossible to get the instance ID. Genie does a good job of trying to make the async communication with the CPE appear synchronous, but at a fundamental level the addObject could actually occur at any point in time (now, or hours from now).
So here is how you want to handle this.
Create a provision script called PortForwards using this code.
Create a preset like so called UpdatePortForwards with a tag precondition of “UpdatePortForwards” and under configuration add a provision script and put in “PortForwards”.
In your genieacs/config/ext folder put the cpe-config.js file using this code.
Have your API return code that looks like this:
[
{
"InternalClient": "192.168.1.2",
"PortMappingDescription": "Foo",
"PortMappingProtocol": "TCP",
"PortMappingEnabled": 1,
"RemoteHost": "",
"InternalPort": 8080,
"ExternalPort": 8080,
"ExternalPortEndRange": 8080
}
]
Where each array entry maps directly to a key/value for an instance in PortMapping.
Finally, add the tag UpdatePortForwards
to the CPE in GenieACS and initiate a connection with the CPE.
While this whole process may seem more complicated initially, it will always ensure the port forwarding instances are created/deleted as needed.
I’ve had this code in production for two years with absolutely zero issues.