Name is null when adding a host via the API

After upgrading to 59, I found that the ‘name’ field is null when adding a host via the API. I’m adding a host to Openstack and passing in the host name. The host is added, but I had a check to verify the Rancher host information was added after the machine data was added. This check fails now - before the’name’ field was set in Rancher. I noticed that there is a “hostname” field that is populated with the name I passed to Openstack, so I’ll check that field for a match instead.

Is this a bug, or a change to the API?

What do you mean by “passing in the host name”?

Previously the name was set by the agent to the Host’s hostname once on registration and then never updated if it changed, so you ended up with Host names that don’t match their actual hostnames.

The agent in 0.59 syncs the hostname field, so that it changes if the machine’s hostname changes. The UI then displays the name field if it is set and hostname if it’s not (and (id) if somehow there’s no hostname either).

So the net result is name will be null by default and hostname will be displayed for each Host, but the user can override it by setting a name.

I pass the “name” in the json block of the Rancher API. Anyway, this seems like a recent change, so I thought I’d mention it. I have the information I need.

oh I see, you’re adding a machine in the API, not a host… the name of the machine is given to docker-machine, which sets the hostname on the VM it creates to that, and then the hostname is read in by the Rancher agent and populates the hostname field. So yes, the name will be null there.

Basically if you want the name that is displayed by the UI for a host, you need something like

function getName(host) {
  if ( host.name !== null ) {
    return host.name;
  } else if ( host.hostname !== null ) {
    return host.hostname;
  } else {
    return "(" + host.id + ")";
  }
}