metabase-api-rs 0.1.0-beta.3

A simplified Rust client for the Metabase API (Beta release - feature complete)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! User and permission models for Metabase user management
//!
//! This module provides data structures for working with
//! Metabase users, groups, and permissions.

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

use super::common::UserId;

/// Unique identifier for a group
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct GroupId(pub i64);

/// Unique identifier for a permission
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct PermissionId(pub i64);

/// Represents a Metabase user
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct User {
    /// Unique identifier for the user
    pub id: UserId,

    /// User's email address
    pub email: String,

    /// User's first name
    pub first_name: String,

    /// User's last name
    pub last_name: String,

    /// Common name (usually first_name + last_name)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub common_name: Option<String>,

    /// Whether the user is a superuser
    #[serde(default)]
    pub is_superuser: bool,

    /// Whether the user is active
    #[serde(default = "default_true")]
    pub is_active: bool,

    /// Whether the user is a Metabase internal user
    #[serde(default)]
    pub is_qbnewb: bool,

    /// When the user joined
    pub date_joined: DateTime<Utc>,

    /// Last login time
    #[serde(skip_serializing_if = "Option::is_none")]
    pub last_login: Option<DateTime<Utc>>,

    /// Groups the user belongs to
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub group_ids: Vec<GroupId>,

    /// User's locale
    #[serde(skip_serializing_if = "Option::is_none")]
    pub locale: Option<String>,

    /// Google auth enabled
    #[serde(default)]
    pub google_auth: bool,

    /// LDAP auth enabled
    #[serde(default)]
    pub ldap_auth: bool,

    // Additional fields from API specification
    /// Login attributes (e.g., SAML, LDAP attributes)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub login_attributes: Option<serde_json::Value>,

    /// User group membership details
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub user_group_memberships: Vec<UserGroupMembership>,
}

/// Represents user group membership details
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct UserGroupMembership {
    /// Group ID
    pub id: GroupId,

    /// Whether this is the default group
    #[serde(default)]
    pub is_default: bool,
}

/// Health check status
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct HealthStatus {
    /// Overall health status
    pub status: String,

    /// Metabase version
    #[serde(skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,

    /// Database connection status
    #[serde(skip_serializing_if = "Option::is_none")]
    pub database: Option<bool>,
}

/// Represents a user group
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Group {
    /// Unique identifier for the group
    pub id: GroupId,

    /// Group name
    pub name: String,

    /// Group members (user IDs)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub members: Vec<UserId>,
}

/// Represents a permission for a group
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Permission {
    /// Unique identifier for the permission
    pub id: PermissionId,

    /// Permission object path (e.g., "/db/1/schema/PUBLIC/")
    pub object: String,

    /// Group this permission applies to
    pub group_id: GroupId,
}

/// Permission graph for a group
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PermissionGraph {
    /// Group ID
    pub group_id: GroupId,

    /// Native query permissions
    #[serde(skip_serializing_if = "Option::is_none")]
    pub native: Option<serde_json::Value>,

    /// Schema permissions
    #[serde(skip_serializing_if = "Option::is_none")]
    pub schemas: Option<serde_json::Value>,
}

/// Request to create a new user
#[derive(Debug, Clone, Serialize)]
pub struct CreateUserRequest {
    /// Email address
    pub email: String,

    /// First name
    pub first_name: String,

    /// Last name
    pub last_name: String,

    /// Password (required for email/password auth)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub password: Option<String>,

    /// Group IDs to add the user to
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub group_ids: Vec<GroupId>,

    /// User's locale
    #[serde(skip_serializing_if = "Option::is_none")]
    pub locale: Option<String>,
}

/// Request to update a user
#[derive(Debug, Clone, Default, Serialize)]
pub struct UpdateUserRequest {
    /// New email
    #[serde(skip_serializing_if = "Option::is_none")]
    pub email: Option<String>,

    /// New first name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub first_name: Option<String>,

    /// New last name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub last_name: Option<String>,

    /// New password
    #[serde(skip_serializing_if = "Option::is_none")]
    pub password: Option<String>,

    /// Whether user is active
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_active: Option<bool>,

    /// Whether user is superuser
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_superuser: Option<bool>,

    /// New group IDs
    #[serde(skip_serializing_if = "Option::is_none")]
    pub group_ids: Option<Vec<GroupId>>,

    /// User's locale
    #[serde(skip_serializing_if = "Option::is_none")]
    pub locale: Option<String>,
}

/// Request to create a new group
#[derive(Debug, Clone, Serialize)]
pub struct CreateGroupRequest {
    /// Group name
    pub name: String,

    /// Initial member IDs
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub members: Vec<UserId>,
}

/// Request to update a group
#[derive(Debug, Clone, Default, Serialize)]
pub struct UpdateGroupRequest {
    /// New name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,

    /// New member list
    #[serde(skip_serializing_if = "Option::is_none")]
    pub members: Option<Vec<UserId>>,
}

/// Membership change request
#[derive(Debug, Clone, Serialize)]
pub struct MembershipRequest {
    /// Group ID
    pub group_id: GroupId,

    /// User ID
    pub user_id: UserId,
}

fn default_true() -> bool {
    true
}

impl User {
    /// Creates a new user builder
    pub fn builder(
        email: impl Into<String>,
        first_name: impl Into<String>,
        last_name: impl Into<String>,
    ) -> UserBuilder {
        UserBuilder::new(email, first_name, last_name)
    }

    /// Gets the user's full name
    pub fn full_name(&self) -> String {
        format!("{} {}", self.first_name, self.last_name)
    }
}

/// Builder for creating User instances
pub struct UserBuilder {
    email: String,
    first_name: String,
    last_name: String,
    password: Option<String>,
    is_superuser: bool,
    group_ids: Vec<GroupId>,
    locale: Option<String>,
}

impl UserBuilder {
    /// Creates a new user builder
    pub fn new(
        email: impl Into<String>,
        first_name: impl Into<String>,
        last_name: impl Into<String>,
    ) -> Self {
        Self {
            email: email.into(),
            first_name: first_name.into(),
            last_name: last_name.into(),
            password: None,
            is_superuser: false,
            group_ids: Vec::new(),
            locale: None,
        }
    }

    /// Sets the password
    pub fn password(mut self, password: impl Into<String>) -> Self {
        self.password = Some(password.into());
        self
    }

    /// Sets whether the user is a superuser
    pub fn superuser(mut self, is_superuser: bool) -> Self {
        self.is_superuser = is_superuser;
        self
    }

    /// Adds a group ID
    pub fn add_group(mut self, group_id: GroupId) -> Self {
        self.group_ids.push(group_id);
        self
    }

    /// Sets the locale
    pub fn locale(mut self, locale: impl Into<String>) -> Self {
        self.locale = Some(locale.into());
        self
    }

    /// Builds the User instance
    pub fn build(self) -> User {
        User {
            id: UserId(0), // Will be set by the server
            email: self.email,
            first_name: self.first_name.clone(),
            last_name: self.last_name.clone(),
            common_name: Some(format!("{} {}", self.first_name, self.last_name)),
            is_superuser: self.is_superuser,
            is_active: true,
            is_qbnewb: false,
            date_joined: Utc::now(),
            last_login: None,
            group_ids: self.group_ids,
            locale: self.locale,
            google_auth: false,
            ldap_auth: false,
            login_attributes: None,
            user_group_memberships: Vec::new(),
        }
    }

    /// Builds a CreateUserRequest
    pub fn build_request(self) -> CreateUserRequest {
        CreateUserRequest {
            email: self.email,
            first_name: self.first_name,
            last_name: self.last_name,
            password: self.password,
            group_ids: self.group_ids,
            locale: self.locale,
        }
    }
}

impl Group {
    /// Creates a new group builder
    pub fn builder(name: impl Into<String>) -> GroupBuilder {
        GroupBuilder::new(name)
    }
}

/// Builder for creating Group instances
pub struct GroupBuilder {
    name: String,
    members: Vec<UserId>,
}

impl GroupBuilder {
    /// Creates a new group builder
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            members: Vec::new(),
        }
    }

    /// Adds a member to the group
    pub fn add_member(mut self, user_id: UserId) -> Self {
        self.members.push(user_id);
        self
    }

    /// Sets all members
    pub fn members(mut self, members: Vec<UserId>) -> Self {
        self.members = members;
        self
    }

    /// Builds the Group instance
    pub fn build(self) -> Group {
        Group {
            id: GroupId(0), // Will be set by the server
            name: self.name,
            members: self.members,
        }
    }

    /// Builds a CreateGroupRequest
    pub fn build_request(self) -> CreateGroupRequest {
        CreateGroupRequest {
            name: self.name,
            members: self.members,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_user_creation() {
        let user = User::builder("john@example.com", "John", "Doe")
            .password("secure123")
            .superuser(false)
            .add_group(GroupId(1))
            .locale("en_US")
            .build();

        assert_eq!(user.email, "john@example.com");
        assert_eq!(user.first_name, "John");
        assert_eq!(user.last_name, "Doe");
        assert_eq!(user.full_name(), "John Doe");
        assert!(!user.is_superuser);
        assert_eq!(user.group_ids.len(), 1);
    }

    #[test]
    fn test_create_user_request() {
        let request = User::builder("jane@example.com", "Jane", "Smith")
            .password("password123")
            .add_group(GroupId(2))
            .build_request();

        assert_eq!(request.email, "jane@example.com");
        assert_eq!(request.first_name, "Jane");
        assert_eq!(request.last_name, "Smith");
        assert_eq!(request.password, Some("password123".to_string()));
        assert_eq!(request.group_ids.len(), 1);
    }

    #[test]
    fn test_group_creation() {
        let group = Group::builder("Administrators")
            .add_member(UserId(1))
            .add_member(UserId(2))
            .build();

        assert_eq!(group.name, "Administrators");
        assert_eq!(group.members.len(), 2);
    }

    #[test]
    fn test_update_user_request() {
        let request = UpdateUserRequest {
            email: Some("newemail@example.com".to_string()),
            is_active: Some(false),
            ..Default::default()
        };

        assert_eq!(request.email, Some("newemail@example.com".to_string()));
        assert_eq!(request.is_active, Some(false));
        assert!(request.first_name.is_none());
    }

    #[test]
    fn test_permission() {
        let permission = Permission {
            id: PermissionId(1),
            object: "/db/1/schema/PUBLIC/".to_string(),
            group_id: GroupId(1),
        };

        assert_eq!(permission.object, "/db/1/schema/PUBLIC/");
        assert_eq!(permission.group_id, GroupId(1));
    }
}