|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | | -use databend_common_exception::Result; |
16 | 15 | use databend_common_meta_app::principal::UserIdentity; |
17 | 16 | use databend_common_meta_app::principal::UserInfo; |
18 | | -use databend_common_meta_app::schema::CreateOption; |
| 17 | +use databend_common_meta_app::principal::tenant_user_ident::Resource as UserIdentResource; |
| 18 | +use databend_common_meta_app::tenant_key::errors::ExistError; |
| 19 | +use databend_common_meta_app::tenant_key::errors::UnknownError; |
19 | 20 | use databend_meta_kvapi::kvapi::ListKVReply; |
20 | | -use databend_meta_types::MatchSeq; |
| 21 | +use databend_meta_types::MetaError; |
21 | 22 | use databend_meta_types::SeqV; |
22 | 23 |
|
23 | 24 | #[async_trait::async_trait] |
24 | 25 | pub trait UserApi: Sync + Send { |
25 | | - async fn add_user(&self, user_info: UserInfo, create_option: &CreateOption) -> Result<()>; |
| 26 | + /// Create a user. |
| 27 | + /// Returns `Ok(Ok(()))` on success. |
| 28 | + /// Returns `Ok(Err(ExistError))` if the user already exists and `overriding` is false. |
| 29 | + async fn create_user( |
| 30 | + &self, |
| 31 | + user_info: UserInfo, |
| 32 | + overriding: bool, |
| 33 | + ) -> Result<Result<(), ExistError<UserIdentResource, UserIdentity>>, MetaError>; |
26 | 34 |
|
27 | | - async fn get_user(&self, user: UserIdentity, seq: MatchSeq) -> Result<SeqV<UserInfo>>; |
| 35 | + /// Get a user by identity. |
| 36 | + /// Returns `Some(SeqV)` if the user exists, `None` otherwise. |
| 37 | + async fn get_user(&self, user: &UserIdentity) -> Result<Option<SeqV<UserInfo>>, MetaError>; |
28 | 38 |
|
29 | | - async fn get_users(&self) -> Result<Vec<SeqV<UserInfo>>>; |
| 39 | + async fn get_users(&self) -> Result<Vec<SeqV<UserInfo>>, MetaError>; |
30 | 40 |
|
31 | 41 | /// Just get user count in meta |
32 | | - async fn get_raw_users(&self) -> Result<ListKVReply>; |
| 42 | + async fn get_raw_users(&self) -> Result<ListKVReply, MetaError>; |
33 | 43 |
|
34 | | - /// General user's grants update. |
| 44 | + /// Update a user in place. |
35 | 45 | /// |
36 | | - /// It fetches the user that matches the specified seq number, update it in place, then write it back with the seq it sees. |
| 46 | + /// This method internally calls `get_user` to fetch the current user state, |
| 47 | + /// applies the update function, then writes back with optimistic concurrency control. |
| 48 | + /// The seq from the internal `get_user` is used for CAS (compare-and-swap). |
37 | 49 | /// |
38 | | - /// Seq number ensures there is no other write happens between get and set. |
39 | | - /// Example: |
40 | | - /// ```ignore |
41 | | - /// self.update_user_with(user_ident, MatchSeq::GE(1), |ui: &mut UserInfo| ui.update_auth_option(foo())).await; |
42 | | - /// ``` |
| 50 | + /// Returns `Ok(Ok(new_seq))` on success. |
| 51 | + /// Returns `Ok(Err(UnknownError))` if the user does not exist or seq mismatch. |
43 | 52 | async fn update_user_with<F>( |
44 | 53 | &self, |
45 | | - user: UserIdentity, |
46 | | - seq: MatchSeq, |
| 54 | + user: &UserIdentity, |
47 | 55 | f: F, |
48 | | - ) -> Result<Option<u64>> |
| 56 | + ) -> Result<Result<u64, UnknownError<UserIdentResource, UserIdentity>>, MetaError> |
49 | 57 | where |
50 | 58 | F: FnOnce(&mut UserInfo) + Send; |
51 | 59 |
|
52 | | - async fn upsert_user_info(&self, user: &UserInfo, seq: MatchSeq) -> Result<u64>; |
| 60 | + /// Upsert user info with exact seq matching. |
| 61 | + /// Returns `Ok(Ok(new_seq))` on success. |
| 62 | + /// Returns `Ok(Err(UnknownError))` if seq mismatch. |
| 63 | + async fn upsert_user_info( |
| 64 | + &self, |
| 65 | + user: &UserInfo, |
| 66 | + seq: u64, |
| 67 | + ) -> Result<Result<u64, UnknownError<UserIdentResource, UserIdentity>>, MetaError>; |
53 | 68 |
|
54 | | - async fn drop_user(&self, user: UserIdentity, seq: MatchSeq) -> Result<()>; |
| 69 | + /// Drop a user. |
| 70 | + /// Returns `Ok(Ok(()))` if the user was dropped. |
| 71 | + /// Returns `Ok(Err(UnknownError))` if the user did not exist. |
| 72 | + async fn drop_user( |
| 73 | + &self, |
| 74 | + user: &UserIdentity, |
| 75 | + ) -> Result<Result<(), UnknownError<UserIdentResource, UserIdentity>>, MetaError>; |
55 | 76 | } |
0 commit comments