Description

In addition to the method of counting instance IP backref's inside a VN object, then adding it with the number of services and gateway IP, there is a better way to obtain the total number of used IP addresses in a subnet, as discussed in this article.

Solution

The following VN has 11 instance IP backrefs in the object, plus gateway and service IP, there are 13 IP addresses in use right now. We can easily get this number with the uuid of the VN and subnet along with the following command:

curl -u admin:Juniper -X POST http:// <config api ip> :8095/virtual-network/ <vn uuid> /subnet-ip-count -H "Content-Type: application/json" -d '{"subnet_list": [" <subnet uuid> "]}'
 
root@cont101:~# curl -u admin:Juniper  http://localhost:8095/virtual-network/bccebf94-c6b7-48fb-86aa-a98faa39140e  | python -m json.tool
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  7409  100  7409    0     0   894k      0 --:--:-- --:--:-- --:--:--  904k
{
    "virtual-network": {
        "access_control_lists": [
            {
                "href": "http://localhost:8095/access-control-list/4d4bbb11-94f5-4489-940c-e8eb5018eebd",
                "to": [
                    "default-domain",
                    "jjzhang",
                    "jjzhang-testvn",
                    "jjzhang-testvn"
                ],
                "uuid": "4d4bbb11-94f5-4489-940c-e8eb5018eebd"
            }
        ],
        "display_name": "jjzhang-testvn",
        "ecmp_hashing_include_fields": {},
        "export_route_target_list": {
            "route_target": []
        },
        "flood_unknown_unicast": false,
        "fq_name": [
            "default-domain",
            "jjzhang",
            "jjzhang-testvn"
        ],
        "href": "http://localhost:8095/virtual-network/bccebf94-c6b7-48fb-86aa-a98faa39140e",
        "id_perms": {
            "created": "2018-10-03T23:11:58.214254",
            "creator": null,
            "description": null,
            "enable": true,
            "last_modified": "2019-04-06T02:19:59.755006",
            "permissions": {
                "group": "_member_",
                "group_access": 7,
                "other_access": 7,
                "owner": "admin",
                "owner_access": 7
            },
            "user_visible": true,
            "uuid": {
                "uuid_lslong": 9703754781624439822,
                "uuid_mslong": 13605022170042812667
            }
        },
        "import_route_target_list": {
            "route_target": []
        },
        "instance_ip_back_refs": [
            {
                "attr": null,
                "href": "http://localhost:8095/instance-ip/4c8438e4-65bf-43e3-83ac-40dcc120a6ef",
                "to": [
                    "4c8438e4-65bf-43e3-83ac-40dcc120a6ef"
                ],
                "uuid": "4c8438e4-65bf-43e3-83ac-40dcc120a6ef"
            },
            {
                "attr": null,
                "href": "http://localhost:8095/instance-ip/3e2f256e-cb9f-4774-8197-7a330827e150",
                "to": [
                    "3e2f256e-cb9f-4774-8197-7a330827e150"
                ],
                "uuid": "3e2f256e-cb9f-4774-8197-7a330827e150"
            },
            {
                "attr": null,
                "href": "http://localhost:8095/instance-ip/71a5c306-77be-47d6-bd4f-0d3951c5c941",
                "to": [
                    "71a5c306-77be-47d6-bd4f-0d3951c5c941"
                ],
                "uuid": "71a5c306-77be-47d6-bd4f-0d3951c5c941"
            },
            {
                "attr": null,
                "href": "http://localhost:8095/instance-ip/63e70f8a-ca0a-40f0-9a75-f31258a783fc",
                "to": [
                    "63e70f8a-ca0a-40f0-9a75-f31258a783fc"
                ],
                "uuid": "63e70f8a-ca0a-40f0-9a75-f31258a783fc"
            },
            {
                "attr": null,
                "href": "http://localhost:8095/instance-ip/b0a62b31-dd68-445b-8add-2719fef21e64",
                "to": [
                    "b0a62b31-dd68-445b-8add-2719fef21e64"
                ],
                "uuid": "b0a62b31-dd68-445b-8add-2719fef21e64"
            },
            {
                "attr": null,
                "href": "http://localhost:8095/instance-ip/90e0ce45-c6d8-416e-90b0-62c49edf550f",
                "to": [
                    "90e0ce45-c6d8-416e-90b0-62c49edf550f"
                ],
                "uuid": "90e0ce45-c6d8-416e-90b0-62c49edf550f"
            },
            {
                "attr": null,
                "href": "http://localhost:8095/instance-ip/0537a8b6-039f-48d6-8d16-d3febfce7e1d",
                "to": [
                    "0537a8b6-039f-48d6-8d16-d3febfce7e1d"
                ],
                "uuid": "0537a8b6-039f-48d6-8d16-d3febfce7e1d"
            },
            {
                "attr": null,
                "href": "http://localhost:8095/instance-ip/617b4106-6028-4743-9fdc-df780b28aad3",
                "to": [
                    "617b4106-6028-4743-9fdc-df780b28aad3"
                ],
                "uuid": "617b4106-6028-4743-9fdc-df780b28aad3"
            },
            {
                "attr": null,
                "href": "http://localhost:8095/instance-ip/5eec7ab1-1455-4ea1-bdb1-92387d2067fa",
                "to": [
                    "5eec7ab1-1455-4ea1-bdb1-92387d2067fa"
                ],
                "uuid": "5eec7ab1-1455-4ea1-bdb1-92387d2067fa"
            },
            {
                "attr": null,
                "href": "http://localhost:8095/instance-ip/f2136b5d-6d8d-4ae9-90a9-e81b39934707",
                "to": [
                    "f2136b5d-6d8d-4ae9-90a9-e81b39934707"
                ],
                "uuid": "f2136b5d-6d8d-4ae9-90a9-e81b39934707"
            },
            {
                "attr": null,
                "href": "http://localhost:8095/instance-ip/e06c53e1-0321-40b4-9c8c-94fb1167cc27",
                "to": [
                    "e06c53e1-0321-40b4-9c8c-94fb1167cc27"
                ],
                "uuid": "e06c53e1-0321-40b4-9c8c-94fb1167cc27"
            }
        ],
        "is_shared": false,
        "multi_policy_service_chains_enabled": false,
        "name": "jjzhang-testvn",
        "network_ipam_refs": [
            {
                "attr": {
                    "ipam_subnets": [
                        {
                            "addr_from_start": true,
                            "default_gateway": "111.111.111.1",
                            "dns_server_address": "111.111.111.2",
                            "enable_dhcp": true,
                            "subnet": {
                                "ip_prefix": "111.111.111.0",
                                "ip_prefix_len": 24
                            },
                            "subnet_name": "0a4bca7e-bdd9-4fba-bb8d-06887aba43d9",
                            "subnet_uuid": "0a4bca7e-bdd9-4fba-bb8d-06887aba43d9"
                        }
                    ]
                },
                "href": "http://localhost:8095/network-ipam/8b143a9e-745d-4010-884a-c9e78255dd6b",
                "to": [
                    "default-domain",
                    "admin",
                    "admin-ipam"
                ],
                "uuid": "8b143a9e-745d-4010-884a-c9e78255dd6b"
            }
        ],
        "network_policy_refs": [
            {
                "attr": {
                    "sequence": {
                        "major": 0,
                        "minor": 0
                    },
                    "timer": null
                },
                "href": "http://localhost:8095/network-policy/6a5acc21-c853-4e12-a34d-d7efc40bfd2a",
                "to": [
                    "default-domain",
                    "jjzhang",
                    "jjzhang-policy"
                ],
                "uuid": "6a5acc21-c853-4e12-a34d-d7efc40bfd2a"
            }
        ],
        "parent_href": "http://localhost:8095/project/7fccf71a-7e7d-4c79-8413-513ee809435a",
        "parent_type": "project",
        "parent_uuid": "7fccf71a-7e7d-4c79-8413-513ee809435a",
        "perms2": {
            "global_access": 0,
            "owner": "7fccf71a7e7d4c798413513ee809435a",
            "owner_access": 7,
            "share": []
        },
        "provider_properties": null,
        "route_target_list": {
            "route_target": [
                "target:60111:800111"
            ]
        },
        "router_external": false,
        "routing_instances": [
            {
                "href": "http://localhost:8095/routing-instance/418154b1-f1ed-43a5-b4d8-73ce3a63fbf4",
                "to": [
                    "default-domain",
                    "jjzhang",
                    "jjzhang-testvn",
                    "jjzhang-testvn"
                ],
                "uuid": "418154b1-f1ed-43a5-b4d8-73ce3a63fbf4"
            }
        ],
        "uuid": "bccebf94-c6b7-48fb-86aa-a98faa39140e",
        "virtual_machine_interface_back_refs": [
            {
                "attr": null,
                "href": "http://localhost:8095/virtual-machine-interface/87589a4b-92d2-4b44-a7a5-71338ec63f1c",
                "to": [
                    "default-domain",
                    "jjzhang",
                    "87589a4b-92d2-4b44-a7a5-71338ec63f1c"
                ],
                "uuid": "87589a4b-92d2-4b44-a7a5-71338ec63f1c"
            },
            {
                "attr": null,
                "href": "http://localhost:8095/virtual-machine-interface/df2efdf9-51c7-4fb4-ad61-a8523d4451da",
                "to": [
                    "default-domain",
                    "jjzhang",
                    "df2efdf9-51c7-4fb4-ad61-a8523d4451da"
                ],
                "uuid": "df2efdf9-51c7-4fb4-ad61-a8523d4451da"
            },
            {
                "attr": null,
                "href": "http://localhost:8095/virtual-machine-interface/9ad9e9aa-b02c-4a60-ade9-6a082ddec791",
                "to": [
                    "default-domain",
                    "jjzhang",
                    "9ad9e9aa-b02c-4a60-ade9-6a082ddec791"
                ],
                "uuid": "9ad9e9aa-b02c-4a60-ade9-6a082ddec791"
            },
            {
                "attr": null,
                "href": "http://localhost:8095/virtual-machine-interface/11476728-56ed-45ae-884d-5e965188882e",
                "to": [
                    "default-domain",
                    "jjzhang",
                    "test_tap"
                ],
                "uuid": "11476728-56ed-45ae-884d-5e965188882e"
            },
            {
                "attr": null,
                "href": "http://localhost:8095/virtual-machine-interface/44ce2b4e-8de6-4843-9cdf-b4f2d26990a5",
                "to": [
                    "default-domain",
                    "jjzhang",
                    "44ce2b4e-8de6-4843-9cdf-b4f2d26990a5"
                ],
                "uuid": "44ce2b4e-8de6-4843-9cdf-b4f2d26990a5"
            },
            {
                "attr": null,
                "href": "http://localhost:8095/virtual-machine-interface/5a1f8ab9-e173-4cb8-8bb9-523af1667aaf",
                "to": [
                    "default-domain",
                    "jjzhang",
                    "test1"
                ],
                "uuid": "5a1f8ab9-e173-4cb8-8bb9-523af1667aaf"
            },
            {
                "attr": null,
                "href": "http://localhost:8095/virtual-machine-interface/edacf650-c845-4cf7-a99b-aa11343a2db8",
                "to": [
                    "default-domain",
                    "jjzhang",
                    "edacf650-c845-4cf7-a99b-aa11343a2db8"
                ],
                "uuid": "edacf650-c845-4cf7-a99b-aa11343a2db8"
            },
            {
                "attr": null,
                "href": "http://localhost:8095/virtual-machine-interface/e2c8dadf-5f58-48b0-b52a-0cef70f6f2eb",
                "to": [
                    "default-domain",
                    "jjzhang",
                    "e2c8dadf-5f58-48b0-b52a-0cef70f6f2eb"
                ],
                "uuid": "e2c8dadf-5f58-48b0-b52a-0cef70f6f2eb"
            },
            {
                "attr": null,
                "href": "http://localhost:8095/virtual-machine-interface/e792a99b-0990-4774-bf9a-330a5d67deae",
                "to": [
                    "default-domain",
                    "jjzhang",
                    "e792a99b-0990-4774-bf9a-330a5d67deae"
                ],
                "uuid": "e792a99b-0990-4774-bf9a-330a5d67deae"
            },
            {
                "attr": null,
                "href": "http://localhost:8095/virtual-machine-interface/0d5bd133-e5c1-48e7-be5c-b361ee11d2fb",
                "to": [
                    "default-domain",
                    "jjzhang",
                    "test"
                ],
                "uuid": "0d5bd133-e5c1-48e7-be5c-b361ee11d2fb"
            },
            {
                "attr": null,
                "href": "http://localhost:8095/virtual-machine-interface/06ba9e7c-9a68-475c-a64e-6be704c346d2",
                "to": [
                    "default-domain",
                    "jjzhang",
                    "06ba9e7c-9a68-475c-a64e-6be704c346d2"
                ],
                "uuid": "06ba9e7c-9a68-475c-a64e-6be704c346d2"
            }
        ],
        "virtual_network_network_id": 56,
        "virtual_network_properties": {
            "allow_transit": false,
            "mirror_destination": false,
            "rpf": "enable"
        }
    }
}
root@cont101:~# curl -u admin:Juniper -X POST http://localhost:8095/virtual-network/bccebf94-c6b7-48fb-86aa-a98faa39140e/subnet-ip-count -H "Content-Type: application/json" -d '{"subnet_list": ["0a4bca7e-bdd9-4fba-bb8d-06887aba43d9"]}'
{"ip_count_list": [13]}
root@cont101:~# 

From the results above, we know that 13 IP addresses have already been used in this virtual network.