Creating host via API

I am trying to figure out if there is a better way to generate the JSON necessary for creating hosts via API. While the JSON structure not hard to understand, there are a number of values in the JSON that are referenced as id rather than name. For example: architecture_id, domain_id, subnet_id, operatingsystem_id, hostgroup_id and is_owned_by. It would be far easier if I can reference these values as names instead, which I cannot find any documentation on if that’s even possible. Does anyone know if there is a better way to do this?

I am able to use names instead of id using Hammer CLI, so this is no longer needed.

@eroji in my case hammer failed if I wanted to create 2 or more hosts at the same time. Could you please share your progress by using api?

Thanks

Hi @eroji,
It is not possible to reference the resources by name. The API documentation is here Foreman :: API v2 and as you can see majority of the calls accepts only resource ids. As you’ve find out you can reference by names in Hammer. Hammer does the name to id resolution for you by searching the resources by name using API. You can find some details about the searching in Foreman :: Manual in subchapter ‘Custom Search of Collections Per Response’.

Also if you run hammer in debug mode hammer -d host create --name.... you can see what is going on behind the scenes including all the data sent to and from API.

HTH,
Martin

I’m not sure how API will help you in that as we don’t have end points for bulk host creation. Either way you’ll have to loop over some host data list and create it one by one. If you prefer to loop in Ruby or have some complex logic in obtaining the source data you may be interested in trying apipie-bindings gem (https://github.com/Apipie/apipie-bindings/) that Hammer uses internally to talk to the API.

1 Like