Unable to create and update exiting host entry to be managed through the API.
Foreman 1.21.3
I am able to create a simple host with this API call.
curl -s -H "Accept:application/json,version=2" -H "Content-Type:application/json" \
-X POST -u ${FOREMANUSER}:${FOREMANPASSWORD} -k \
-d '{"host":{"environment_id":"'${ENVIRONMENT_ID}'","hostgroup_id":"'${HOSTGROUP_ID}'","host_parameters_attributes":[{"name":"app_tier","value":"'${APPTIER}'"}],"location_id":"'${LOCATION_ID}'","managed":"false","name":"'${SERVERNAME}'","organization_id":"13"}}' \
${FOREMANURL}/api/hosts
However if I add the managed option or try to add it later I tried this code and receive the below message.
curl -s -H "Accept:application/json,verion=2" -H "Content-Type:application/json" \
-X PUT -u ${FOREMANUSER}:${FOREMANPASSWORD} -k \
-d '{"host":{"operatingsystem_id":"Linux","architecture_id":"1","managed":"true","interfaces_attributes":[{"identifier":"eth0","mac":"00:0d:3a:cf:ad:83","managed":"false"}]}}' \
${FOREMANURL}/api/hosts/${SERVERNAME}
ERROR:
{
"error": {"message":"PG::ForeignKeyViolation: ERROR: insert or update on table \"hosts\" violates foreign key constraint \"hosts_operatingsystem_id_fk\"\nDETAIL: Key (operatingsystem_id)=(0) is not present in table \"operatingsystems\".\n: UPDATE \"hosts\" SET \"managed\" = $1, \"architecture_id\" = $2, \"operatingsystem_id\" = $3, \"updated_at\" = $4 WHERE \"hosts\".\"id\" = $5"}
}
Any help is much appreciated.
Cassuis
Figured things out.
There is actually a mistake in the API documentation for the operatingsystem_id option. The documentation notes that it should be a string, but should be the ID number for the operating system.
Regards,
Cassuis
1 Like
Hello Cassius242,
Thanks for reporting and giving the solution right away!
Although the issue in the header is no longer an issue, there is a small bug in the API documentation nonetheless.
I’ve created an upstream issue to track this and prevent similar problems: Bug #29295: Change string to number in API docs for ids - Foreman
1 Like
Hmm the Params module states we support operatingsystem
operatingsystem_id
and operatingsystem_name
.
:pxe_loader,
# Model relations sorted in alphabetical order
:architecture, :architecture_id, :architecture_name,
:domain, :domain_id, :domain_name,
:hardware_model_id, :hardware_model_name,
:hostgroup, :hostgroup_id, :hostgroup_name,
:location, :location_id, :location_name,
:medium, :medium_id, :medium_name,
:model, :model_id, :model_name,
:operatingsystem, :operatingsystem_id, :operatingsystem_name,
:organization, :organization_id, :organization_name,
:ptable, :ptable_id, :ptable_name,
:progress_report, :progress_report_id, :progress_report_name,
:realm, :realm_id, :realm_name,
:subnet, :subnet_id, :subnet_name,
:subnet6, :subnet6_id, :subnet6_name,
:host_parameters_attributes => [parameter_params_filter(HostParameter)],
:interfaces => [nic_interface_params_filter], :interfaces_attributes => [nic_interface_params_filter]
end
end
end
The API support only id? Is it because of hammer translates the name to id, and we don’t want to support names in API? Makes me think if it wouldn’t make things easier actually