[SOLVED] REST API not setting IPV4 address for LXC container

simonjcarr

New Member
Apr 30, 2021
7
0
1
53
I have a working REST API Connection to proxmox. I can create and clone an LXC container just fine using the API.

After cloning a container, I am trying to use the REST API to set the containers IP Address, but it's not working. My NodeJS code is below.

Code:
let formBody = await this.getFormBody({net0: "name=eth0,bridge=vmbr99,ip=192.168.22.1/20,gw=192.168.16.1"})
const proxmoxResponse = await fetch(`https://cloud1.xxxxx.com:8006/api2/json/nodes/cloud1/lxc/${vmid}/config/`, {
    method: "PUT",
    headers: {
        Authorization: "PVEAPIToken=simon@pam!xxxxxx-automation=xxxxxxxxxxxxxxxxxx",
        "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
        Accept: "application/json"
    },
    body: formBody
})
const responseData = await proxmoxResponse.json()
console.log("issueIpV4: ", responseData)

The response I get from the above from the sever is

Code:
{ data: null }

I am sure I am hitting the correct API endpoint, because if I change the field name from net0 to something not recognised by the API for that endpoint it gives me an error

Code:
{
  data: null,
  errors: {
    ffnet0: 'property is not defined in schema and the schema does not allow additional properties'
  }

Can someone provide an example of changing an LXC container IP address using a REST API call.
 
Last edited:
PUTting a config update doesn't return the config but an empty (successful) response.. you need to GET afterwards (same API path)
 
  • Like
Reactions: oguz
@fabian I was using the ProxMox GUI to see that the IP Address had not been set, however I have found the problem.

I was making the API call to set the IP Address following an API call to clone an existing container. It turns out the failure to set the IP Address was because the new VM had not had chance to be created when the second API call was made. Putting a delay in the second call fixed the issue. I know need to update my code to recursively check for the existence of the VM before making the second API call.

Thanks
 
@fabian I was using the ProxMox GUI to see that the IP Address had not been set, however I have found the problem.

I was making the API call to set the IP Address following an API call to clone an existing container. It turns out the failure to set the IP Address was because the new VM had not had chance to be created when the second API call was made. Putting a delay in the second call fixed the issue. I know need to update my code to recursively check for the existence of the VM before making the second API call.

Thanks
FYI, you should get an "UPID" returned from the create call as it spawns a task-worker, and you can use that UPID to poll the status of said task through the "/nodes/localhost/tasks/$upid/status" call, e.g. checking its status property for stopped and may additionally the exitstatus in that case.

https://pve.proxmox.com/pve-docs/api-viewer/index.html#/nodes/{node}/tasks/{upid}/status
 
@fabian I was using the ProxMox GUI to see that the IP Address had not been set, however I have found the problem.

I was making the API call to set the IP Address following an API call to clone an existing container. It turns out the failure to set the IP Address was because the new VM had not had chance to be created when the second API call was made. Putting a delay in the second call fixed the issue. I know need to update my code to recursively check for the existence of the VM before making the second API call.

Thanks
in addition to what thomas said - in that case you should have gotten an error (for the JSON API via the HTTP status code & message, the extjs API formatter used by the GUI encodes errors in the response body) - likely because locking was not possible since the clone still held the lock for the VM config..
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get your own in 60 seconds.

Buy now!