pltr-cli 0.1.2__py3-none-any.whl → 0.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pltr/services/admin.py ADDED
@@ -0,0 +1,314 @@
1
+ """
2
+ Admin service wrapper for Foundry SDK admin operations.
3
+ Provides a high-level interface for user, group, role, and organization management.
4
+ """
5
+
6
+ from typing import Any, Dict, Optional
7
+ import json
8
+
9
+ from .base import BaseService
10
+
11
+
12
+ class AdminService(BaseService):
13
+ """Service wrapper for Foundry admin operations."""
14
+
15
+ def _get_service(self) -> Any:
16
+ """Get the Foundry admin service."""
17
+ return self.client.admin
18
+
19
+ # User Management Methods
20
+ def list_users(
21
+ self, page_size: Optional[int] = None, page_token: Optional[str] = None
22
+ ) -> Dict[str, Any]:
23
+ """
24
+ List all users in the organization.
25
+
26
+ Args:
27
+ page_size: Maximum number of users to return per page
28
+ page_token: Token for pagination (from previous response)
29
+
30
+ Returns:
31
+ Dictionary containing user list and pagination info
32
+ """
33
+ try:
34
+ response = self.service.User.list(
35
+ page_size=page_size, page_token=page_token
36
+ )
37
+ return self._serialize_response(response)
38
+ except Exception as e:
39
+ raise RuntimeError(f"Failed to list users: {str(e)}")
40
+
41
+ def get_user(self, user_id: str) -> Dict[str, Any]:
42
+ """
43
+ Get a specific user by ID.
44
+
45
+ Args:
46
+ user_id: The user ID or RID
47
+
48
+ Returns:
49
+ Dictionary containing user information
50
+ """
51
+ try:
52
+ response = self.service.User.get(user_id)
53
+ return self._serialize_response(response)
54
+ except Exception as e:
55
+ raise RuntimeError(f"Failed to get user {user_id}: {str(e)}")
56
+
57
+ def get_current_user(self) -> Dict[str, Any]:
58
+ """
59
+ Get information about the current authenticated user.
60
+
61
+ Returns:
62
+ Dictionary containing current user information
63
+ """
64
+ try:
65
+ response = self.service.User.get_current()
66
+ return self._serialize_response(response)
67
+ except Exception as e:
68
+ raise RuntimeError(f"Failed to get current user: {str(e)}")
69
+
70
+ def search_users(
71
+ self,
72
+ query: str,
73
+ page_size: Optional[int] = None,
74
+ page_token: Optional[str] = None,
75
+ ) -> Dict[str, Any]:
76
+ """
77
+ Search for users by query string.
78
+
79
+ Args:
80
+ query: Search query string
81
+ page_size: Maximum number of users to return per page
82
+ page_token: Token for pagination (from previous response)
83
+
84
+ Returns:
85
+ Dictionary containing search results and pagination info
86
+ """
87
+ try:
88
+ response = self.service.User.search(
89
+ query=query, page_size=page_size, page_token=page_token
90
+ )
91
+ return self._serialize_response(response)
92
+ except Exception as e:
93
+ raise RuntimeError(f"Failed to search users: {str(e)}")
94
+
95
+ def get_user_markings(self, user_id: str) -> Dict[str, Any]:
96
+ """
97
+ Get markings/permissions for a specific user.
98
+
99
+ Args:
100
+ user_id: The user ID or RID
101
+
102
+ Returns:
103
+ Dictionary containing user markings information
104
+ """
105
+ try:
106
+ response = self.service.User.get_markings(user_id)
107
+ return self._serialize_response(response)
108
+ except Exception as e:
109
+ raise RuntimeError(f"Failed to get user markings for {user_id}: {str(e)}")
110
+
111
+ def revoke_user_tokens(self, user_id: str) -> Dict[str, Any]:
112
+ """
113
+ Revoke all tokens for a specific user.
114
+
115
+ Args:
116
+ user_id: The user ID or RID
117
+
118
+ Returns:
119
+ Dictionary containing operation result
120
+ """
121
+ try:
122
+ self.service.User.revoke_all_tokens(user_id)
123
+ return {
124
+ "success": True,
125
+ "message": f"All tokens revoked for user {user_id}",
126
+ }
127
+ except Exception as e:
128
+ raise RuntimeError(f"Failed to revoke tokens for user {user_id}: {str(e)}")
129
+
130
+ # Group Management Methods
131
+ def list_groups(
132
+ self, page_size: Optional[int] = None, page_token: Optional[str] = None
133
+ ) -> Dict[str, Any]:
134
+ """
135
+ List all groups in the organization.
136
+
137
+ Args:
138
+ page_size: Maximum number of groups to return per page
139
+ page_token: Token for pagination (from previous response)
140
+
141
+ Returns:
142
+ Dictionary containing group list and pagination info
143
+ """
144
+ try:
145
+ response = self.service.Group.list(
146
+ page_size=page_size, page_token=page_token
147
+ )
148
+ return self._serialize_response(response)
149
+ except Exception as e:
150
+ raise RuntimeError(f"Failed to list groups: {str(e)}")
151
+
152
+ def get_group(self, group_id: str) -> Dict[str, Any]:
153
+ """
154
+ Get a specific group by ID.
155
+
156
+ Args:
157
+ group_id: The group ID or RID
158
+
159
+ Returns:
160
+ Dictionary containing group information
161
+ """
162
+ try:
163
+ response = self.service.Group.get(group_id)
164
+ return self._serialize_response(response)
165
+ except Exception as e:
166
+ raise RuntimeError(f"Failed to get group {group_id}: {str(e)}")
167
+
168
+ def search_groups(
169
+ self,
170
+ query: str,
171
+ page_size: Optional[int] = None,
172
+ page_token: Optional[str] = None,
173
+ ) -> Dict[str, Any]:
174
+ """
175
+ Search for groups by query string.
176
+
177
+ Args:
178
+ query: Search query string
179
+ page_size: Maximum number of groups to return per page
180
+ page_token: Token for pagination (from previous response)
181
+
182
+ Returns:
183
+ Dictionary containing search results and pagination info
184
+ """
185
+ try:
186
+ response = self.service.Group.search(
187
+ query=query, page_size=page_size, page_token=page_token
188
+ )
189
+ return self._serialize_response(response)
190
+ except Exception as e:
191
+ raise RuntimeError(f"Failed to search groups: {str(e)}")
192
+
193
+ def create_group(
194
+ self,
195
+ name: str,
196
+ description: Optional[str] = None,
197
+ organization_rid: Optional[str] = None,
198
+ ) -> Dict[str, Any]:
199
+ """
200
+ Create a new group.
201
+
202
+ Args:
203
+ name: The group name
204
+ description: Optional group description
205
+ organization_rid: Optional organization RID
206
+
207
+ Returns:
208
+ Dictionary containing created group information
209
+ """
210
+ try:
211
+ # Build create request parameters
212
+ create_params = {"name": name}
213
+ if description:
214
+ create_params["description"] = description
215
+ if organization_rid:
216
+ create_params["organization_rid"] = organization_rid
217
+
218
+ response = self.service.Group.create(**create_params)
219
+ return self._serialize_response(response)
220
+ except Exception as e:
221
+ raise RuntimeError(f"Failed to create group '{name}': {str(e)}")
222
+
223
+ def delete_group(self, group_id: str) -> Dict[str, Any]:
224
+ """
225
+ Delete a specific group.
226
+
227
+ Args:
228
+ group_id: The group ID or RID
229
+
230
+ Returns:
231
+ Dictionary containing operation result
232
+ """
233
+ try:
234
+ self.service.Group.delete(group_id)
235
+ return {
236
+ "success": True,
237
+ "message": f"Group {group_id} deleted successfully",
238
+ }
239
+ except Exception as e:
240
+ raise RuntimeError(f"Failed to delete group {group_id}: {str(e)}")
241
+
242
+ # Organization Management Methods
243
+ def get_organization(self, organization_id: str) -> Dict[str, Any]:
244
+ """
245
+ Get organization information.
246
+
247
+ Args:
248
+ organization_id: The organization ID or RID
249
+
250
+ Returns:
251
+ Dictionary containing organization information
252
+ """
253
+ try:
254
+ response = self.service.Organization.get(organization_id)
255
+ return self._serialize_response(response)
256
+ except Exception as e:
257
+ raise RuntimeError(
258
+ f"Failed to get organization {organization_id}: {str(e)}"
259
+ )
260
+
261
+ # Role Management Methods
262
+ def get_role(self, role_id: str) -> Dict[str, Any]:
263
+ """
264
+ Get role information.
265
+
266
+ Args:
267
+ role_id: The role ID or RID
268
+
269
+ Returns:
270
+ Dictionary containing role information
271
+ """
272
+ try:
273
+ response = self.service.Role.get(role_id)
274
+ return self._serialize_response(response)
275
+ except Exception as e:
276
+ raise RuntimeError(f"Failed to get role {role_id}: {str(e)}")
277
+
278
+ def _serialize_response(self, response: Any) -> Dict[str, Any]:
279
+ """
280
+ Convert response object to serializable dictionary.
281
+
282
+ Args:
283
+ response: Response object from SDK
284
+
285
+ Returns:
286
+ Serializable dictionary representation
287
+ """
288
+ if response is None:
289
+ return {}
290
+
291
+ # Handle different response types
292
+ if hasattr(response, "dict"):
293
+ # Pydantic models
294
+ return response.dict()
295
+ elif hasattr(response, "__dict__"):
296
+ # Regular objects
297
+ result = {}
298
+ for key, value in response.__dict__.items():
299
+ if not key.startswith("_"):
300
+ try:
301
+ # Try to serialize the value
302
+ json.dumps(value)
303
+ result[key] = value
304
+ except (TypeError, ValueError):
305
+ # Convert non-serializable values to string
306
+ result[key] = str(value)
307
+ return result
308
+ else:
309
+ # Primitive types or already serializable
310
+ try:
311
+ json.dumps(response)
312
+ return response
313
+ except (TypeError, ValueError):
314
+ return {"data": str(response)}