|
5 | 5 | from ..common import Object |
6 | 6 | from ..exception import CTERAException, InputError |
7 | 7 | from .base_command import BaseCommand |
8 | | -from .types import ShareAccessControlEntry, RemoveShareAccessControlEntry |
| 8 | +from .types import NFSv3AccessControlEntry, RemoveNFSv3AccessControlEntry, ShareAccessControlEntry, RemoveShareAccessControlEntry |
9 | 9 |
|
10 | 10 |
|
11 | 11 | class Shares(BaseCommand): |
@@ -269,6 +269,73 @@ def delete(self, name): |
269 | 269 | logging.getLogger().error("Share deletion failed.") |
270 | 270 | raise CTERAException('Share deletion failed', error) |
271 | 271 |
|
| 272 | + def get_trusted_nfs_clients(self, name): |
| 273 | + """ |
| 274 | + Get the current trusted NFS client entries from an existing share. |
| 275 | +
|
| 276 | + :param str name: The share name |
| 277 | + """ |
| 278 | + return self._gateway.get('/config/fileservices/share/' + name + '/trustedNFSClients') |
| 279 | + |
| 280 | + def set_trusted_nfs_clients(self, name, trusted_nfs_clients): |
| 281 | + """ |
| 282 | + Set a network share's trusted NFS client entries. |
| 283 | +
|
| 284 | + :param str name: The share name |
| 285 | + :param list[cterasdk.edge.types.NFSv3AccessControlEntry] trusted_nfs_clients: Trusted NFS v3 clients |
| 286 | +
|
| 287 | + .. warning:: this method will override the existing access control entries |
| 288 | + """ |
| 289 | + Shares._validate_trusted_nfs_clients(trusted_nfs_clients) |
| 290 | + |
| 291 | + param = [client.to_server_object() for client in (trusted_nfs_clients or [])] |
| 292 | + self._gateway.put('/config/fileservices/share/' + name + '/trustedNFSClients', param) |
| 293 | + |
| 294 | + def add_trusted_nfs_clients(self, name, trusted_nfs_clients): |
| 295 | + """ |
| 296 | + Add one or more trusted NFS client entries to an existing share. |
| 297 | +
|
| 298 | + :param str name: The share name |
| 299 | + :param list[cterasdk.edge.types.NFSv3AccessControlEntry] trusted_nfs_clients: Trusted NFS v3 clients |
| 300 | + """ |
| 301 | + Shares._validate_trusted_nfs_clients(trusted_nfs_clients) |
| 302 | + |
| 303 | + new_trusted_nfs_clients_dict = { |
| 304 | + trusted_nfs_client.address + '#' + trusted_nfs_client.netmask: trusted_nfs_client.to_server_object() |
| 305 | + for trusted_nfs_client in trusted_nfs_clients |
| 306 | + } |
| 307 | + |
| 308 | + def entry_not_in_new(entry): |
| 309 | + trusted_nfs_client = NFSv3AccessControlEntry.from_server_object(entry) |
| 310 | + entry_key = trusted_nfs_client.address + '#' + trusted_nfs_client.netmask |
| 311 | + return entry_key not in new_trusted_nfs_clients_dict |
| 312 | + |
| 313 | + param = list(new_trusted_nfs_clients_dict.values()) + list(filter(entry_not_in_new, self.get_trusted_nfs_clients(name))) |
| 314 | + |
| 315 | + self._gateway.put('/config/fileservices/share/' + name + '/trustedNFSClients', param) |
| 316 | + |
| 317 | + def remove_trusted_nfs_clients(self, name, trusted_nfs_clients): |
| 318 | + """ |
| 319 | + Remove one or more trusted NFS client entries from an existing share. |
| 320 | +
|
| 321 | + :param str name: The share name |
| 322 | + :param list[cterasdk.edge.types.RemoveNFSv3AccessControlEntry] trusted_nfs_clients: Trusted NFS v3 clients |
| 323 | + """ |
| 324 | + Shares._validate_remove_trusted_nfs_clients(trusted_nfs_clients) |
| 325 | + |
| 326 | + remove_trusted_nfs_clients_dict = { |
| 327 | + trusted_nfs_client.address + '#' + trusted_nfs_client.netmask |
| 328 | + for trusted_nfs_client in trusted_nfs_clients |
| 329 | + } |
| 330 | + |
| 331 | + def entry_not_removed(entry): |
| 332 | + trusted_nfs_client = NFSv3AccessControlEntry.from_server_object(entry) |
| 333 | + entry_key = trusted_nfs_client.address + '#' + trusted_nfs_client.netmask |
| 334 | + return entry_key not in remove_trusted_nfs_clients_dict |
| 335 | + |
| 336 | + param = list(filter(entry_not_removed, self.get_trusted_nfs_clients(name))) |
| 337 | + self._gateway.put('/config/fileservices/share/' + name + '/trustedNFSClients', param) |
| 338 | + |
272 | 339 | def _validate_root_directory(self, name): |
273 | 340 | param = Object() |
274 | 341 | param.path = '/' |
@@ -303,3 +370,31 @@ def _validate_remove_acl(acl): |
303 | 370 | repr(acl_entry), |
304 | 371 | 'cterasdk.edge.types.RemoveShareAccessControlEntry' |
305 | 372 | ) |
| 373 | + |
| 374 | + @staticmethod |
| 375 | + def _validate_trusted_nfs_clients(trusted_nfs_clients): |
| 376 | + if not isinstance(trusted_nfs_clients, list): |
| 377 | + raise InputError( |
| 378 | + 'Invalid Trusted NFS Clients list format', |
| 379 | + repr(trusted_nfs_clients), |
| 380 | + '[cterasdk.edge.types.NFSv3AccessControlEntry, ...]' |
| 381 | + ) |
| 382 | + for entry in trusted_nfs_clients: |
| 383 | + if not isinstance(entry, NFSv3AccessControlEntry): |
| 384 | + raise InputError('Invalid Trusted NFS Clients entry format', repr(entry), 'cterasdk.edge.types.NFSv3AccessControlEntry') |
| 385 | + |
| 386 | + @staticmethod |
| 387 | + def _validate_remove_trusted_nfs_clients(trusted_nfs_clients): |
| 388 | + if not isinstance(trusted_nfs_clients, list): |
| 389 | + raise InputError( |
| 390 | + 'Invalid Trusted NFS Clients list format', |
| 391 | + repr(trusted_nfs_clients), |
| 392 | + '[cterasdk.edge.types.RemoveNFSv3AccessControlEntry, ...]' |
| 393 | + ) |
| 394 | + for entry in trusted_nfs_clients: |
| 395 | + if not isinstance(entry, RemoveNFSv3AccessControlEntry): |
| 396 | + raise InputError( |
| 397 | + 'Invalid Trusted NFS Clients entry format', |
| 398 | + repr(entry), |
| 399 | + 'cterasdk.edge.types.RemoveNFSv3AccessControlEntry' |
| 400 | + ) |
0 commit comments