Vitor-Avila commented on code in PR #32231: URL: https://github.com/apache/superset/pull/32231#discussion_r1958841564
########## superset/commands/database/resync_permissions_async.py: ########## @@ -0,0 +1,101 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import logging + +from superset import security_manager +from superset.commands.base import BaseCommand +from superset.commands.database.exceptions import ( + DatabaseConnectionFailedError, + DatabaseNotFoundError, + UserNotFoundError, +) +from superset.commands.database.utils import ping +from superset.daos.database import DatabaseDAO +from superset.tasks.permissions import resync_database_permissions + +logger = logging.getLogger(__name__) + + +class ResyncPermissionsAsyncCommand(BaseCommand): + """ + Command to trigger an async task to resync database permissions. + """ + + def __init__( + self, + model_id: int, + username: str | None, + old_db_connection_name: str | None = None, + ): + """ + Constructor method. + """ + self.db_connection_id = model_id + self.username = username + self.old_db_connection_name = old_db_connection_name + + def validate(self) -> None: + """ + Validates the command before triggering the async task. + + Confirms both the DB connection user exist. Also tests the DB connection. + """ + database = DatabaseDAO.find_by_id(self.db_connection_id) + if not database: + raise DatabaseNotFoundError() + + if not self.old_db_connection_name: + self.old_db_connection_name = database.database_name + + if not self.username or not security_manager.get_user_by_username( + self.username + ): + raise UserNotFoundError() + + with database.get_sqla_engine() as engine: + # Make sure the connection works before delegating the task + try: + alive = ping(engine) + except Exception as err: + logger.error("Could not stablish a DB connection") + raise DatabaseConnectionFailedError() from err + + if not alive: + logger.error("Could not stablish a DB connection") + raise DatabaseConnectionFailedError() Review Comment: @korbit-ai this is also no longer an issue. Could you please mark it as resolved in your top summary? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For additional commands, e-mail: notifications-h...@superset.apache.org