Exemplo n.º 1
0
    def __func():
        data = request.get_json()
        filename = data[
            'filename'] if 'filename' in data else core.config.paths.default_appdevice_export_path
        try:
            with open(filename, 'r') as devices_file:
                read_file = devices_file.read()
                read_file = read_file.replace('\n', '')
                apps = json.loads(read_file)
        except (OSError, IOError) as e:
            current_app.logger.error(
                'Error importing devices from {0}: {1}'.format(filename, e))
            return {"error": "Error reading file."}, IO_ERROR
        for app in apps:
            for device in apps[app]:
                if device_db.session.query(Device).filter(
                        Device.name == device['name']).first() is not None:
                    current_app.logger.error('Could not import device {0}. '
                                             'Device already exists.'.format(
                                                 device['name']))
                    continue
                fields = {
                    field['name']: field['value']
                    for field in device['fields']
                }
                device_type = device['type']
                try:
                    device_api = get_app_device_api(app, device_type)
                    device_fields_api = device_api['fields']
                    validate_device_fields(device_fields_api, fields,
                                           device_type, app)
                except UnknownDevice:
                    current_app.logger.error(
                        'Cannot import device for app {0}, type {1}. '
                        'Type does not exist'.format(app, device_type))
                    continue
                except InvalidArgument as e:
                    current_app.logger.error(
                        'Cannot import device for app {0}, type {1}. '
                        'Invalid input'.format(app, device_type,
                                               format_exception_message(e)))
                    continue
                else:
                    fields = device['fields']
                    add_configuration_keys_to_device_json(
                        fields, device_fields_api)
                    app = device_db.session.query(App).filter(
                        App.name == app).first()
                    if app is None:
                        current_app.logger.error(
                            'SEVERE: App defined in api does not have corresponding entry in database. '
                            'Cannot import device')
                        continue
                    device_obj = Device.from_json(device)
                    app.add_device(device_obj)
                    device_db.session.add(device_obj)
                    device_db.session.commit()

        current_app.logger.debug('Imported devices from {0}'.format(filename))
        return {}, SUCCESS
Exemplo n.º 2
0
    def __func():
        add_device_json = request.get_json()
        if device_db.session.query(Device).filter(
                Device.name == add_device_json['name']).first() is not None:
            current_app.logger.error('Could not create device {0}. '
                                     'Device already exists.'.format(
                                         add_device_json['name']))
            return {"error": "Device already exists."}, OBJECT_EXISTS_ERROR

        fields = {
            field['name']: field['value']
            for field in add_device_json['fields']
        }
        app = add_device_json['app_name']
        device_type = add_device_json['type']
        try:
            device_api = get_app_device_api(app, device_type)
            device_fields_api = device_api['fields']
            validate_device_fields(device_fields_api, fields, device_type, app)
        except UnknownApp:
            current_app.logger.error(
                'Cannot create device for app {0}, type {1}. '
                'App does not exist'.format(app, device_type))
            return {'error': 'Unknown app'}, INVALID_INPUT_ERROR
        except UnknownDevice:
            current_app.logger.error(
                'Cannot create device for app {0}, type {1}. '
                'Type does not exist'.format(app, device_type))
            return {'error': 'Unknown device type'}, INVALID_INPUT_ERROR
        except InvalidArgument as e:
            current_app.logger.error(
                'Cannot create device for app {0}, type {1}. '
                'Invalid input'.format(app, device_type,
                                       format_exception_message(e)))
            return {'error': 'Invalid device fields'}, INVALID_INPUT_ERROR
        else:
            fields = add_device_json['fields']
            add_configuration_keys_to_device_json(fields, device_fields_api)
            app = device_db.session.query(App).filter(App.name == app).first()
            if app is None:
                current_app.logger.error(
                    'SEVERE: App defined in api does not have corresponding entry in database. '
                    'Cannot add device')
                return {'error': 'Unknown app'}, INVALID_INPUT_ERROR
            device = Device.from_json(add_device_json)
            app.add_device(device)
            device_db.session.add(device)
            device_db.session.commit()
            device_json = get_device_json_with_app_name(device)
            # remove_configuration_keys_from_device_json(device_json)
            return device_json, OBJECT_CREATED
Exemplo n.º 3
0
    def __func():
        update_device_json = request.get_json()
        device = device_db.session.query(Device).filter(
            Device.id == update_device_json['id']).first()
        if device is None:
            current_app.logger.error('Could not update device {0}. '
                                     'Device does not exist.'.format(
                                         update_device_json['id']))
            return {"error": "Device does not exist."}, OBJECT_DNE_ERROR

        fields = ({
            field['name']: field['value']
            for field in update_device_json['fields']
        } if 'fields' in update_device_json else None)
        app = update_device_json['app_name']
        device_type = update_device_json[
            'type'] if 'type' in update_device_json else device.type
        try:
            device_api = get_app_device_api(app, device_type)
            device_fields_api = device_api['fields']
            if fields is not None:
                validate_device_fields(device_fields_api, fields, device_type,
                                       app)
        except UnknownApp:
            current_app.logger.error(
                'Cannot update device for app {0}, type {1}. '
                'App does not exist'.format(app, device_type))
            return {'error': 'Unknown app'}, INVALID_INPUT_ERROR
        except UnknownDevice:
            current_app.logger.error(
                'Cannot update device for app {0}, type {1}. '
                'Type does not exist'.format(app, device_type))
            return {'error': 'Unknown device type'}, INVALID_INPUT_ERROR
        except InvalidArgument as e:
            current_app.logger.error(
                'Cannot update device for app {0}, type {1}. '
                'Invalid input'.format(app, device_type,
                                       format_exception_message(e)))
            return {'error': 'Invalid device fields'}, INVALID_INPUT_ERROR
        else:
            if fields is not None:
                fields = update_device_json[
                    'fields'] if 'fields' in update_device_json else None
                add_configuration_keys_to_device_json(fields,
                                                      device_fields_api)
            device.update_from_json(update_device_json)
            device_db.session.commit()
            device_json = get_device_json_with_app_name(device)
            # remove_configuration_keys_from_device_json(device_json)
            return device_json, SUCCESS
Exemplo n.º 4
0
 def test_device_fields_validation_with_defaults_with_required(self):
     device_fields = [{'name': 'param1', 'type': 'string', 'encrypted': True},
                      {'name': 'param2', 'type': 'boolean', 'default': False, 'required': True}]
     device_in = {'param1': 'somevalue'}
     validated = validate_device_fields(device_fields, device_in, '', '')
     device_in['param2'] = False
     self.assertDictEqual(validated, device_in)
Exemplo n.º 5
0
 def test_device_fields_validation_with_required_and_encrypted(self):
     device_fields = [{'name': 'param1', 'type': 'string', 'required': True, 'encrypted': True},
                      {'name': 'param2', 'type': 'boolean'}]
     device_in = {'param1': 'somevalue', 'param2': True}
     validate_device_fields(device_fields, device_in, '', '')
Exemplo n.º 6
0
 def test_device_fields_validation_with_some_required_in_api_too_few_fields(self):
     device_fields = [{'name': 'param1', 'type': 'string', 'required': True},
                      {'name': 'param2', 'type': 'boolean'}]
     device_in = {'param2': True}
     with self.assertRaises(InvalidArgument):
         validate_device_fields(device_fields, device_in, '', '')
Exemplo n.º 7
0
 def test_basic_device_fields_validation(self):
     device_fields = [{'name': 'param1', 'type': 'string'}, {'name': 'param2', 'type': 'boolean'}]
     device_in = {'param1': 'somevalue', 'param2': True}
     validate_device_fields(device_fields, device_in, '', '')
Exemplo n.º 8
0
 def test_device_fields_validation_with_all_required_in_api(self):
     device_fields = [{'name': 'param1', 'type': 'string', 'required': True},
                      {'name': 'param2', 'type': 'boolean', 'required': True}]
     device_in = {'param1': 'somevalue', 'param2': True}
     validate_device_fields(device_fields, device_in, '', '')
Exemplo n.º 9
0
 def test_device_fields_validation_with_encryption_with_invalid_value(self):
     device_fields = [{'name': 'param1', 'type': 'integer', 'encrypted': True},
                      {'name': 'param2', 'type': 'boolean'}]
     device_in = {'param1': 'somevalue', 'param2': True}
     with self.assertRaises(InvalidArgument):
         validate_device_fields(device_fields, device_in, '', '')
Exemplo n.º 10
0
 def test_device_fields_validation_with_no_fields_none_required(self):
     device_fields = [{'name': 'param1', 'type': 'string'}, {'name': 'param2', 'type': 'boolean'}]
     device_in = {}
     validate_device_fields(device_fields, device_in, '', '')
Exemplo n.º 11
0
 def test_basic_device_fields_validation_invalid_type(self):
     device_fields = [{'name': 'param1', 'type': 'string'}, {'name': 'param2', 'type': 'boolean'}]
     device_in = {'param1': 'somevalue', 'param2': 'invalid'}
     with self.assertRaises(InvalidArgument):
         validate_device_fields(device_fields, device_in, '', '')