Unable to create or update managed host via API

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" \
     -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"}}' \

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" \
     -d '{"host":{"operatingsystem_id":"Linux","architecture_id":"1","managed":"true","interfaces_attributes":[{"identifier":"eth0","mac":"00:0d:3a:cf:ad:83","managed":"false"}]}}' \


  "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.


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.


1 Like

Hello Cassius242,

Thanks for reporting and giving the solution right away! :slight_smile:

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.

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 :thinking: