Renew dhcp via api rest

Hi everyone,

I’m using VyOS and I have an interface configured with DHCP. I’m looking for a way to trigger a DHCP renewal via the REST API, not the CLI.

I couldn’t find any reference to this in the official documentation.
Does anyone know if this is possible through the API, and if so, what the endpoint or method would be?

Thanks a lot for your help!

I don’t think there is a /renew endpoint for the REST API. There is a RenewClientLeaseDhcp in the GraphQL endpoint, but you may have to make a change to the op mode script since it requires raw as a parameter in the function, but raw isn’t allowed as an input.

curl -k --raw 'https://10.0.101.245/graphql' -H 'Content-Type: application/json' -d \
'{"query":"mutation { RenewClientLeaseDhcp (data: {key: \"test123\", family: inet, interface: \"eth1\"}) {\n success\n errors\n op_mode_error {name, message, vyos_code}\n data {\n   result\n }\n}\n}\n"}'|jq

{
  "data": {
    "RenewClientLeaseDhcp": {
      "success": false,
      "errors": [
        "TypeError(\"renew_client_lease() missing 1 required positional argument: 'raw'\")"
      ],
      "op_mode_error": null,
      "data": null
    }
  }
}

Add raw:

curl -k --raw 'https://10.0.101.245/graphql' -H 'Content-Type: application/json' -d \
'{"query":"mutation { RenewClientLeaseDhcp (data: {key: \"test123\", raw: false,family: inet, interface: \"eth1\"}) {\n success\n errors\n op_mode_error {name, message, vyos_code}\n data {\n   result\n }\n}\n}\n"}'|jq

{
  "errors": [
    {
      "message": "Field 'raw' is not defined by type 'RenewClientLeaseDhcpInput'.",
      "locations": [
        {
          "line": 1,
          "column": 57
        }
      ],
      "extensions": {
        "exception": null
      }
    }
  ]
}

The solution (someone may have a better one) would be to modify line 539 (may be slightly different in different versions) in /usr/libexec/vyos/op_mode/dhcp.py from:

def renew_client_lease(raw: bool, family: ArgFamily, interface: str):

to

def renew_client_lease(family: ArgFamily, interface: str, raw: bool = False):

Then you’re able to run this:

curl -k --raw 'https://10.0.101.245/graphql' -H 'Content-Type: application/json' -d \
'{"query":"mutation { RenewClientLeaseDhcp (data: {key: \"test123\", family: inet, interface: \"eth1\"}) {\n success\n errors\n op_mode_error {name, message, vyos_code}\n data {\n   result\n }\n}\n}\n"}'|jq

{
  "data": {
    "RenewClientLeaseDhcp": {
      "success": true,
      "errors": null,
      "op_mode_error": null,
      "data": {
        "result": null
      }
    }
  }
}

VyOS log:

Apr 24 00:29:48 dhclient[51062]: bound to 10.0.101.245 -- renewal in 35634 seconds.

Feel fre to submit a bug report at https://vyos.dev. Maybe even submit a PR to fix the issue.

1 Like

Thanks! Yeah, I think the best approach might be to either implement this through the REST server or extend the GraphQL API a bit to handle that. I’ll wait and see if anyone else has ideas or existing solutions :slightly_smiling_face:

If nothing comes up, I might try to put together a PR to add this functionality myself.

Done in this PR T7395: Add support for renew in REST Server by IDerr · Pull Request #4529 · vyos/vyos-1x · GitHub