Beispiel #1
0
    def validate(self) -> None:
        exceptions: List[ValidationError] = []

        # verify that the metadata file is present and valid
        try:
            metadata = load_metadata(self.contents)
        except ValidationError as exc:
            exceptions.append(exc)
            metadata = None

        for file_name, content in self.contents.items():
            prefix = file_name.split("/")[0]
            schema = schemas.get(f"{prefix}/")
            if schema:
                try:
                    config = load_yaml(file_name, content)
                    schema.load(config)
                    self._configs[file_name] = config
                except ValidationError as exc:
                    exc.messages = {file_name: exc.messages}
                    exceptions.append(exc)

        # validate that the type declared in METADATA_FILE_NAME is correct
        if metadata:
            type_validator = validate.Equal(SqlaTable.__name__)
            try:
                type_validator(metadata["type"])
            except ValidationError as exc:
                exc.messages = {METADATA_FILE_NAME: {"type": exc.messages}}
                exceptions.append(exc)

        if exceptions:
            exception = CommandInvalidError("Error importing dataset")
            exception.add_list(exceptions)
            raise exception
Beispiel #2
0
    def _load__configs(self, exceptions: List[ValidationError]) -> None:
        # load existing databases so we can apply the password validation
        db_passwords: Dict[str, str] = {
            str(uuid): password
            for uuid, password in db.session.query(Database.uuid,
                                                   Database.password).all()
        }
        for file_name, content in self.contents.items():
            # skip directories
            if not content:
                continue

            prefix = file_name.split("/")[0]
            schema = self.schemas.get(f"{prefix}/")
            if schema:
                try:
                    config = load_yaml(file_name, content)

                    # populate passwords from the request or from existing DBs
                    if file_name in self.passwords:
                        config["password"] = self.passwords[file_name]
                    elif prefix == "databases" and config[
                            "uuid"] in db_passwords:
                        config["password"] = db_passwords[config["uuid"]]

                    schema.load(config)
                    self._configs[file_name] = config
                except ValidationError as exc:
                    exc.messages = {file_name: exc.messages}
                    exceptions.append(exc)
    def validate(self) -> None:
        exceptions: List[ValidationError] = []

        # load existing databases so we can apply the password validation
        db_passwords = {
            str(uuid): password
            for uuid, password in db.session.query(Database.uuid,
                                                   Database.password).all()
        }

        # verify that the metadata file is present and valid
        try:
            metadata: Optional[Dict[str, str]] = load_metadata(self.contents)
        except ValidationError as exc:
            exceptions.append(exc)
            metadata = None

        # validate objects
        for file_name, content in self.contents.items():
            prefix = file_name.split("/")[0]
            schema = self.schemas.get(f"{prefix}/")
            if schema:
                try:
                    config = load_yaml(file_name, content)

                    # populate passwords from the request or from existing DBs
                    if file_name in self.passwords:
                        config["password"] = self.passwords[file_name]
                    elif prefix == "databases" and config[
                            "uuid"] in db_passwords:
                        config["password"] = db_passwords[config["uuid"]]

                    schema.load(config)
                    self._configs[file_name] = config
                except ValidationError as exc:
                    exc.messages = {file_name: exc.messages}
                    exceptions.append(exc)

        # validate that the type declared in METADATA_FILE_NAME is correct
        if metadata:
            type_validator = validate.Equal(
                self.dao.model_cls.__name__)  # type: ignore
            try:
                type_validator(metadata["type"])
            except ValidationError as exc:
                exc.messages = {METADATA_FILE_NAME: {"type": exc.messages}}
                exceptions.append(exc)

        if exceptions:
            exception = CommandInvalidError(
                f"Error importing {self.model_name}")
            exception.add_list(exceptions)
            raise exception
Beispiel #4
0
    def validate(self) -> None:
        exceptions: List[ValidationError] = []

        # load existing databases so we can apply the password validation
        db_passwords = {
            str(uuid): password
            for uuid, password in db.session.query(Database.uuid,
                                                   Database.password).all()
        }

        # verify that the metadata file is present and valid
        try:
            metadata: Optional[Dict[str, str]] = load_metadata(self.contents)
        except ValidationError as exc:
            exceptions.append(exc)
            metadata = None

        # validate that the type declared in METADATA_FILE_NAME is correct
        if metadata and "type" in metadata:
            type_validator = validate.Equal(
                self.dao.model_cls.__name__)  # type: ignore
            try:
                type_validator(metadata["type"])
            except ValidationError as exc:
                exc.messages = {METADATA_FILE_NAME: {"type": exc.messages}}
                exceptions.append(exc)

        # validate objects
        for file_name, content in self.contents.items():
            # skip directories
            if not content:
                continue

            prefix = file_name.split("/")[0]
            schema = self.schemas.get(f"{prefix}/")
            if schema:
                try:
                    config = load_yaml(file_name, content)

                    # populate passwords from the request or from existing DBs
                    if file_name in self.passwords:
                        config["password"] = self.passwords[file_name]
                    elif prefix == "databases" and config[
                            "uuid"] in db_passwords:
                        config["password"] = db_passwords[config["uuid"]]

                    schema.load(config)
                    self._configs[file_name] = config
                except ValidationError as exc:
                    exc.messages = {file_name: exc.messages}
                    exceptions.append(exc)

        # check if the object exists and shouldn't be overwritten
        if not self.overwrite:
            existing_uuids = self._get_uuids()
            for file_name, config in self._configs.items():
                if (file_name.startswith(self.prefix)
                        and config["uuid"] in existing_uuids):
                    exceptions.append(
                        ValidationError({
                            file_name:
                            (f"{self.model_name.title()} already exists "
                             "and `overwrite=true` was not passed"),
                        }))

        if exceptions:
            exception = CommandInvalidError(
                f"Error importing {self.model_name}")
            exception.add_list(exceptions)
            raise exception