Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating a device #2

Open
anaspeople opened this issue Jun 30, 2017 · 1 comment
Open

Creating a device #2

anaspeople opened this issue Jun 30, 2017 · 1 comment

Comments

@anaspeople
Copy link

anaspeople commented Jun 30, 2017

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.

@anaspeople
Copy link
Author

anaspeople commented Jul 5, 2017

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant