Bug #20888
closedcreating a hostgroup with invalid id option should raise proper error instead of foreign key constraint error
Description
Cloned from https://bugzilla.redhat.com/show_bug.cgi?id=1354568
Description of problem:
Creating a hostgroup with invalid subnet id raises:
~]# hammer -u admin -p changeme hostgroup create --name test123512 --organizations='Default Organization' --location-ids 1 --puppet-proxy-id 1 --puppet-ca-proxy-id 1 --root-pass redhat123 --subnet-id 1 --domain-id 1
Could not create the hostgroup:
ERROR: insert or update on table "hostgroups" violates foreign key constraint "hostgroups_subnet_id_fk"
DETAIL: Key (subnet_id)=(1) is not present in table "subnets".
Version-Release number of selected component (if applicable):
all
How reproducible:
100%
Steps to Reproduce:
1. create hostgroup with invalid subnet-id
Actual results:
see 500
Expected results:
user should get an error: subnet not found
Additional info:
Currently we don't check ids before we try to use them in DB. When users pass invalid id via API call, they experience 500, following (or similar) can be found in logs
PG::ForeignKeyViolation: ERROR: insert or update on table "domains" violates foreign key constraint "domains_dns_id_fk"
While there were fixes on few places (#17525), this is a generic problem and we should fix it in all API endpoints. It would be good to have a DSL that would accept a model and permission, which would do the validation for a given combination, similarly like in the linked issue. Then we should fix the issue with hostgroup-subnet_id with it.