Create host by calling foreman api doesn't work after upgrade to 1.7

Hi,

I recently upgraded katello to 2.1 as well as foreman from 1.6 to 1.7, we
previously has a tool to add host into foreman by calling the api, part of
the code is as follows:
def foremanAddHost(self):
buffer = StringIO()
payload = {"name":self.name, "hostgroup_id":self.hostGroupId,
"environment_id":self.environmentId, "organization_id":self.organizationId,
"location_id":self.locationId, "mac":self.mac,
"architecture_id":self.architectureId,
"operatingsystem_id":self.operatingsystemId, "root_pass":self.rootPass,
"ptable_id":self.pTable, "build":"true", "medium_id":self.mediumId}
c = pycurl.Curl()
c.setopt(pycurl.URL, self.apiHosts)
c.setopt(pycurl.USERPWD, self.userPwd)
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.HTTPHEADER, self.headers)
c.setopt(pycurl.SSL_VERIFYPEER, 0)
c.setopt(pycurl.SSL_VERIFYHOST, 0)
c.setopt(pycurl.POSTFIELDS, json.dumps(payload))
c.setopt(pycurl.WRITEFUNCTION, buffer.write)
c.perform()
responseCode = c.getinfo(c.RESPONSE_CODE)
returnData = json.loads(buffer.getvalue())
msg = "Adding host %s to Foreman" % self.name
if responseCode == 200:
colorPrint.statusColorMsg('ok', msg, 'Done')
logger.info('%s %s successfully.' % (username, msg ))
else:
errs = returnData['error']
colorPrint.statusColorMsg('fail', msg, 'Failed')
logger.warn('%s %s failed, reason might be %s' % (username,
msg, errs))
buffer.close()
c.close()

It works well until we upgrade: here is the error we got:
Adding host test.example.com to Foreman failed, reason might be
{u'message': u'operatingsystem_id is not allowed as nested parameter for
hosts. Allowed parameters are hostgroup_id, location_id, organization_id,
environment_id'}

Any ideas please?

it seems that the data structure changed, in the json data, it added "host"
key and make all the rest as value, I modified the code and it just worked.

    payload = {'host': {"name":self.name, "hostgroup_id":self.

hostGroupId, "environment_id":self.environmentId,
"organization_id":self.organizationId, "location_id":self.locationId,
"mac":self.mac, "architecture_id":self.architectureId,
"operatingsystem_id":self.operatingsystemId, "root_pass":self.rootPass,
"ptable_id":self.pTable, "build":"true", "medium_id":self.mediumId}}

ยทยทยท On Friday, February 27, 2015 at 10:23:55 AM UTC+8, sinux shen wrote: > > Hi, > > I recently upgraded katello to 2.1 as well as foreman from 1.6 to 1.7, we > previously has a tool to add host into foreman by calling the api, part of > the code is as follows: > def foremanAddHost(self): > buffer = StringIO() > payload = {"name":self.name, "hostgroup_id":self.hostGroupId, > "environment_id":self.environmentId, "organization_id":self.organizationId, > "location_id":self.locationId, "mac":self.mac, > "architecture_id":self.architectureId, > "operatingsystem_id":self.operatingsystemId, "root_pass":self.rootPass, > "ptable_id":self.pTable, "build":"true", "medium_id":self.mediumId} > c = pycurl.Curl() > c.setopt(pycurl.URL, self.apiHosts) > c.setopt(pycurl.USERPWD, self.userPwd) > c.setopt(pycurl.POST, 1) > c.setopt(pycurl.HTTPHEADER, self.headers) > c.setopt(pycurl.SSL_VERIFYPEER, 0) > c.setopt(pycurl.SSL_VERIFYHOST, 0) > c.setopt(pycurl.POSTFIELDS, json.dumps(payload)) > c.setopt(pycurl.WRITEFUNCTION, buffer.write) > c.perform() > responseCode = c.getinfo(c.RESPONSE_CODE) > returnData = json.loads(buffer.getvalue()) > msg = "Adding host %s to Foreman" % self.name > if responseCode == 200: > colorPrint.statusColorMsg('ok', msg, 'Done') > logger.info('%s %s successfully.' % (username, msg )) > else: > errs = returnData['error'] > colorPrint.statusColorMsg('fail', msg, 'Failed') > logger.warn('%s %s failed, reason might be %s' % (username, > msg, errs)) > buffer.close() > c.close() > > It works well until we upgrade: here is the error we got: > Adding host test.example.com to Foreman failed, reason might be > {u'message': u'operatingsystem_id is not allowed as nested parameter for > hosts. Allowed parameters are hostgroup_id, location_id, organization_id, > environment_id'} > > Any ideas please? > > >