Create a new host via REST API (Postman)

Problem:
I have an environment with Foreman + oVirt + FreeIPA. Everything is working just fine. When I need to create a new host I just do it via foreman UI and a new host is created on oVirt and the cloudinit script installs ipa-client and registers my new host on FreeIPA, setting up LDAP authentication on the host and adding it to DNS.

Now, I’m trying to reproduce the same scenario via Foreman REST API, in other words, create new hosts via API using postman.

I started by following the API documentation and I managed to figure out some of the missing fields because the HTTP response was clear enough, but now I’m stuck with the following procedure:

Via postman I’m sending a POST with the following body:

{
    "host": {
    "name": "banana",
    "hostgroup_id": 1,
    "environment_id": 1,
    "architecture_id": 1,
    "domain_id": 1,
    "puppet_proxy_id": 1,
    "puppet_ca_proxy_id": 1,
    "operatingsystem_id": 1,
    "provision_method": "image",
    "image_id": 1,
    "compute_resource_id": 1,
    "compute_profile_id": 1,
    "realm_id": 2,
    "enabled": true,
    "managed": true,
    "root_pass": "asdfasdfasdf"
  }
}

And the return is not very clear, after googling for the following code, it doesn’t show any (good) result (I hope I’m not the first one facing this issue)

{
  "error": {
    "id": null,
    "errors": {
      "base": [
        "Failed to create a compute oVirt (oVirt) instance banana.europe.intranet: ERF42-0973 [Foreman::Exception]:  is not valid, enter id or name\n "
      ]
    },
    "full_messages": [
      "Failed to create a compute oVirt (oVirt) instance banana.europe.intranet: ERF42-0973 [Foreman::Exception]:  is not valid, enter id or name\n "
    ]
  }
}

Expected outcome:
A host created on oVirt, managed by foreman

Product versions:
Foreman Version 2.1.1
oVirt 4.3.10
FreeIPA version: 4.6.6

Hi,
It seems like one of the parameters: cluster network, storage domain, cluster it not defined properly, I guess you created some storage or network in your compute profile correct? can you check that in this compute profile, the cluster network, storage domain and cluster are defined properly?

also, I have 2 tips for creating more easily a host not from the UI:

  1. create a hostgroup which defines everything: the compute resource, compute profile and everything else, to verify the hostgroup you created is OK, just create a host from the UI and change only the hostgroup, it that worked for you , you can send a clear post for example:

{
“location_id” => 15,
“organization_id” => 14,
“host” => {
“name” => “shira”,
“location_id” => 15,
“organization_id” => 14,
“hostgroup_id” => 1,
“build” => true,
“managed” => true,
“enabled” => true,
“overwrite” => true,
}
}

  1. For me, it’s much more easy to use our CLI tool - hammer, you can create a host easily like this:
    hammer -d host create --name shira --organization-id 14 --location-id 15 --hostgroup ovirt
1 Like

Hi @shiramax, thanks for the reply.

Regarding your tip nº 1 this is indeed the way I’m doing, not filling items manually.
Regarding your tip nº 2 I’ll give it a try, this one I heard about some issues with oVirt, that’s why I didn’t try it yet.

When I get the results, I’ll put it back here to maybe help other people.

Thanks,

Fabio G. Martins

1 Like

As promised: Foreman :: Manual

HI,
this issue is related only for API v3 which spouse to be deprecated soon you shouldn’t experience any issue in v4 , also this issue is relevant for creating a new Compute Resource, not host. and lastly it was fixed in 2.1.1

Something is weird.

I tried again with hammer and postman both, the result are still the same:


The only difference is that I reduced the number of arguments, since I’m using the host group and as you explained, the other fields are optional when we use hostgroup:
image

The interesting part, is the error code that does not show any results on google and the error message which seems to be missing information “is not valid, enter id or name” WHAT is not valid, that is the question?

Thanks again for the help so far.

I tried now fill exactly the same fields I do via web UI, after selecting the host group, I have to manually select the organization, location and change the Operating System info to image based instead of media.

In the following post I did exactly the same fields I do when doing it via UI, all IDs has been succesfully validated with their corresponding GET api calls. But unfortunately the results are still not the ones expected.

"Failed to create a compute oVirt (oVirt) instance banana.europe.intranet: ERF42-0973 [Foreman::Exception]: is not valid, enter id or name\n "

It might be my impression but after the colon on the error message (before “is not valid”) there are 2 blank spaces, I think there should be the name of the missing field and that looks like a bug.

can you share what attributes you have in your compute profile? including the volume, interface, and everything else.

Thanks once again @shiramax.

These are all the parameters I have on my compute profile:

you don’t have network interface, is that deliberately?
I think the problem is there, can you try add one? just to see if it’s working

1 Like

I was indeed on purpose since it was working properly via UI. For some reason a generic NIC1 was generated.

After adding the network to the profile and POSTing again via postman, the host creation worked, the new host has been properly created, but now I have another problem.

Maybe this is is expected and I should call another API to resume the host provisioning. What is happening now is that the host is created in a halted state and I need to power it on via portal (UI), but after manually starting it the cloud init script seems not to be fired up, because the machine never gets an IP from DHCP. I’ll try to debug more from this state and get back with more info.

You nailed it, would be nice if we could find a way to make the error message a little bit more explicit.

try to add --compute-attributes “start=1” it should start the vm after the provisioning.
l

Yes, that worked on hammer, but for some reason is not working on postman. I guess I’ll just switch to hammer instead of API directly.

on postman you should send it inside compute_attributes:
for example:

Params: {
        "location_id" => 15,
    "organization_id" => 14,
               "host" => {
                         "name" => "sfhdis",
                  "location_id" => 15,
              "organization_id" => 14,
                 "hostgroup_id" => 1,
           "compute_attributes" => {
                        "“start" => "1”",
            "volumes_attributes" => {}
        },
                        "build" => true,
                      "managed" => true,
                      "enabled" => true,
                    "overwrite" => true,
        "interfaces_attributes" => []
    }
}

hammer is the same as the API, it’s just easier because we can see in the help how to send the command properly.