Exemple #1
0
 def _processDeviceList(self, deviceList):
     '''
 '''
     ip_addr = ""
     device_name = ""
     resultCode, resultMessage = True, ""
     for host in deviceList:
         device = DeviceModel.find_by_name(
             host) if DeviceModel.find_by_name(
                 host) is not None else DeviceModel.find_by_ipAddress(host)
         if not device:
             if self.validIP(host):
                 try:
                     device_name = socket.getfqdn(host)
                 except:
                     continue
                 ip_addr = host
             else:
                 device_name = host
                 try:
                     ip_addr = socket.gethostbyname(host)
                 except:
                     continue
                 #except: ip_addr = host
             device = DeviceModel(name=device_name,
                                  ipAddress=ip_addr,
                                  deviceClass=1)
             try:
                 device.save_to_db(commit=False)
             except:
                 resultCode, resultMessage = False, "An error occurred inserting the device."
         self._hosts.append(device)
     return resultCode, resultMessage
Exemple #2
0
 def delete(self, name):
     device = DeviceModel.find_by_name(name)
     if device:
         device.delete_from_db()
     else:
         return {'error': 'device not found'}, 404
     return {'success': 'Device deleted'}, 202
Exemple #3
0
 def get(self, name=None):
     queryData = request.args.to_dict()
     if name:
         device = DeviceModel.find_by_name(name)
         if device: return device.json(), 200
         else: return {'error': 'device not found'}, 404
     devices = DeviceModel.find(**queryData)
     return {'devices': list(map(lambda x: x.json(), devices))}, 200
Exemple #4
0
 def _processTasks(self, job_id, hosts):
     '''
 '''
     resultCode, resultMessage = True, ""
     for deviceName in hosts:
         device = DeviceModel.find_by_name(deviceName)
         task = TaskModel(job_id, device.id)
         task.save_to_db(commit=True)
     return resultCode, resultMessage
Exemple #5
0
    def post(self, name):
        if DeviceModel.find_by_name(name):
            return {
                'message':
                f'Bad Request: A device with name {name} already exists. '
                f'No two duplicate devices can be created.'
            }, 400

        device = DeviceModel(name)
        device.save_to_db()
        return device.json(), 201
Exemple #6
0
    def put(self, name):
        data = Device.parser.parse_args()
        device = DeviceModel.find_by_name(name)

        if device is None:
            device = DeviceModel(name)
            device.save_to_db()
            return device.json(), 201
        else:
            device.state = data['state']
            device.save_to_db()
            return device.json(), 201
Exemple #7
0
 def _processDeviceList(self, deviceList):
     '''
 '''
     resultCode, resultMessage = True, ""
     for device_name in deviceList:
         device = DeviceModel.find_by_name(device_name)
         if not device:
             device = DeviceModel(device_name, deviceClass=1)
             try:
                 device.save_to_db(commit=True)
             except:
                 resultCode, resultMessage = False, "An error occurred inserting the device."
         self._hosts.append(device)
     return resultCode, resultMessage
Exemple #8
0
 def put(self, name):
     data = json.loads(request.data)
     device = DeviceModel.find_by_name(name)
     if device:
         cipher_suite = Fernet(KEY)
         if data.get('login'):
             data['login'] = cipher_suite.encrypt(
                 data.get('login').encode('ascii')).decode('ascii')
         if data.get('password'):
             data['password'] = cipher_suite.encrypt(
                 data.get('password').encode('ascii')).decode('ascii')
         device.update(**data)
     else:
         return {'error': 'device not found'}, 404
     return device.json(), 201
Exemple #9
0
    def put(self, devId):
        data = Device.parser.parse_args()

        device = DeviceModel.find_by_name(devId)

        if device is None:
            device = DeviceModel(devId, data['power'], data['voltage'],
                                 data['current'])
        else:
            device.power = data['power']
            device.voltage = data['voltage']
            device.current = data['current']

        device.save_to_db()

        return device.json()
Exemple #10
0
 def post(self, device_name, key_name):
     device = DeviceModel.find_by_name(device_name)
     if device:
         key = device.find_key_by_name(key_name)
         if key:
             result = key.press(device_name)
             if result == "OK":
                 return {'message': 'Action executed'}, 200
             else:
                 return {
                     'error':
                     'Something went wrong during the execution of action: {}'
                     .format(result)
                 }, 500
         return {'error': 'Key not found'}, 404
     return {'error': 'Device not found'}, 404
Exemple #11
0
 def buildCommand(self):
     for deviceName in self.job.parameters["hostsConfiguration"]:
         device = DeviceModel.find_by_name(deviceName)
         if not device:
             device = DeviceModel(deviceName, deviceClass=1)
             try:
                 device.save_to_db(commit=False)
             except:
                 resultCode, resultMessage = False, "An error occurred inserting the device."
         command = Environment().from_string(
             self.job.parameters["remoteCommand"]).render(
                 self.job.parameters["hostsConfiguration"][deviceName])
         task = TaskModel(self.job.id, device.id)
         task.parameters["command"] = command
         task.save_to_db(commit=False)
     return command
Exemple #12
0
    def post(self, devId):
        if DeviceModel.find_by_name(devId):
            return {
                'message':
                "A Device with devId '{}' already exists.".format(devId)
            }, 400

        data = Device.parser.parse_args()

        device = DeviceModel(devId, data['power'], data['voltage'],
                             data['current'])

        try:
            device.save_to_db()
        except:
            return {
                "message": "An error occurred inserting the device data."
            }, 500

        return device.json(), 201
Exemple #13
0
 def post(self, name=None):
     data = json.loads(request.data)
     print(data)
     cipher_suite = Fernet(KEY)
     if data.get('login'):
         data['login'] = cipher_suite.encrypt(
             data.get('login').encode('ascii')).decode('ascii')
     if data.get('password'):
         data['password'] = cipher_suite.encrypt(
             data.get('password').encode('ascii')).decode('ascii')
     name = data.get("name", name)
     if DeviceModel.find_by_name(name):
         return {
             'error':
             "A device with name : {}, already exists.".format(name)
         }, 400
     device = DeviceModel(**data)
     #try:
     device.save_to_db()
     #except:
     #return {"error": "An error occurred creating the device."}, 500
     return device.json(), 201
Exemple #14
0
 def get(self, devId):
     device = DeviceModel.find_by_name(devId)
     if device:
         return device.json()
     return {'message': 'Device not found'}, 404
Exemple #15
0
 def get(self, name):
     device = DeviceModel.find_by_name(name)
     if device:
         return {'device': device.json()}
     return {'message': 'Device not found'}, 404
Exemple #16
0
def get_remote_control_device(name):
    device = DeviceModel.find_by_name(name)
    return render_template('remote_control.j2',
                           title='Home',
                           device=device.json())
Exemple #17
0
 def get(self, name):
     device = DeviceModel.find_by_name(name)
     if device:
         return device.json()
     return {'message': "Bad Request: Device Could Not Be Found"}, 400