FortigateApi
FortigateApi copied to clipboard
Creating a device
I am trying to create a device (object) with it's mac address and then trying to add it to a device group. I could not find any information related to it in the REST API documentation. This can be done like this in the command line:
config user device
edit device_name
set mac AA:AA:AA:AA:AA
set type windows-pc
...
next
end
And then adding it to a group:
config user device-group
edit group_name
set member "device_name"
next
end
...
Also if a device has more than one mac (ethernet and wifi), two separate devices need to be created, link then and set one as default.
Can this be done adapting your code? Thanks.
I managed to do it like this:
def GetDeviceGroup(self, name=''):
'''
Return the json device group object, when the param name is defined it returns the selected object, without name: return all the objects.
Parameters
----------
name: the device group object name (type string)
Returns
-------
Return the json object
'''
if not name:
req = self.ApiGet('cmdb/user/device-group/')
else:
req = self.ApiGet('cmdb/user/device-group/'+name+'/member/')
return req.text
def AddDeviceDeviceGroup(self, name, groupname):
"""
Add device to a device group
Parameters
----------
name: name of the device (type string)
groupname: name of device group
Returns
-------
Http status code: 200 if ok, 4xx if an error occurs (like 424 for existing mac address)
"""
payload = {'json':
{
'name': str(name),
'q_origin_key': str(name),
}
}
return self.ApiAdd('cmdb/user/device-group/'+groupname+'/member/', payload)
def GetDevice(self, name=''):
'''
Return the json device object, when the param name is defined it returns the selected object, without name: return all the objects.
Parameters
----------
name: the device object name (type string)
Returns
-------
Return the json object
'''
req = self.ApiGet('cmdb/user/device/' + name)
return req.text
def AddDevice(self, name, mac, devtype, user='',master_device='', comment='', avatar=''):
"""
Add device
Parameters
----------
name: name of the device (type string)
mac: mac address of network interface (type string)
user: user of device (type string)
devtype: type of device, must exist: "linux-pc","windows-pc","mac","other-network-device", "router-nat-device" (type string)
master-device: name of device belonging a secondary interface like wifi card (type string)
Returns
-------
Http status code: 200 if ok, 4xx if an error occurs (like 424 for existing mac address)
"""
payload = {'json':
{
'alias': str(name),
'q_origin_key': str(name),
'mac': str(mac),
'user': str(user),
'master-device': str(master_device),
'comment': str(comment),
'avatar': avatar,
'type': str(devtype),
}
}
return self.ApiAdd('cmdb/user/device/', payload)