|
2 | 2 | # Copyright (c) 2019 by Delphix. All rights reserved. |
3 | 3 | # |
4 | 4 |
|
5 | | -from dlpx.virtualization.api import common_pb2 |
| 5 | +from abc import ABCMeta |
| 6 | +from dlpx.virtualization.api import common_pb2, libs_pb2 |
6 | 7 | from dlpx.virtualization.common.exceptions import IncorrectTypeError |
| 8 | +from enum import IntEnum |
7 | 9 |
|
8 | 10 | """Classes used for Plugin Operations |
9 | 11 |
|
|
17 | 19 | "RemoteConnection", |
18 | 20 | "RemoteEnvironment", |
19 | 21 | "RemoteHost", |
20 | | - "RemoteUser"] |
| 22 | + "RemoteUser", |
| 23 | + "Credentials", |
| 24 | + "PasswordCredentials", |
| 25 | + "KeyPairCredentials"] |
21 | 26 |
|
22 | 27 |
|
23 | 28 | class RemoteConnection(object): |
@@ -293,3 +298,121 @@ def from_proto(user): |
293 | 298 | common_pb2.RemoteUser) |
294 | 299 | remote_user = RemoteUser(name=user.name, reference=user.reference) |
295 | 300 | return remote_user |
| 301 | + |
| 302 | + |
| 303 | +class Credentials(object): |
| 304 | + """Plugin base class for CredentialsResult to be used for plugin operations |
| 305 | + and library functions. |
| 306 | +
|
| 307 | + Plugin authors should use this instead of the corresponding protobuf generated |
| 308 | + class. |
| 309 | +
|
| 310 | + Args: |
| 311 | + username: User name. |
| 312 | + """ |
| 313 | + def __init__(self, username): |
| 314 | + if not isinstance(username, basestring): |
| 315 | + raise IncorrectTypeError( |
| 316 | + Credentials, |
| 317 | + 'username', |
| 318 | + type(username), |
| 319 | + basestring) |
| 320 | + self.__username = username |
| 321 | + |
| 322 | + __metaclass__ = ABCMeta |
| 323 | + |
| 324 | + @property |
| 325 | + def username(self): |
| 326 | + return self.__username |
| 327 | + |
| 328 | + |
| 329 | +class PasswordCredentials(Credentials): |
| 330 | + """Plugin class for CredentialsResult with password to be used for plugin operations |
| 331 | + and library functions. |
| 332 | +
|
| 333 | + Plugin authors should use this instead of the corresponding protobuf generated |
| 334 | + class. |
| 335 | +
|
| 336 | + Args: |
| 337 | + username: User name. |
| 338 | + password: Password. |
| 339 | + """ |
| 340 | + def __init__(self, username, password): |
| 341 | + super(PasswordCredentials, self).__init__(username) |
| 342 | + if not isinstance(password, basestring): |
| 343 | + raise IncorrectTypeError( |
| 344 | + PasswordCredentials, |
| 345 | + 'password', |
| 346 | + type(password), |
| 347 | + basestring) |
| 348 | + self.__password = password |
| 349 | + |
| 350 | + @property |
| 351 | + def password(self): |
| 352 | + return self.__password |
| 353 | + |
| 354 | + @staticmethod |
| 355 | + def from_proto(credentials_result): |
| 356 | + """Converts protobuf class libs_pb2.CredentialsResult to plugin class PasswordCredentials |
| 357 | + """ |
| 358 | + if not isinstance(credentials_result, libs_pb2.CredentialsResult): |
| 359 | + raise IncorrectTypeError( |
| 360 | + PasswordCredentials, |
| 361 | + 'credentials_result', |
| 362 | + type(credentials_result), |
| 363 | + libs_pb2.CredentialsResult) |
| 364 | + return PasswordCredentials( |
| 365 | + username=credentials_result.username, password=credentials_result.pasword) |
| 366 | + |
| 367 | + |
| 368 | +class KeyPairCredentials(Credentials): |
| 369 | + """Plugin class for CredentialsResult with key pair to be used for plugin operations |
| 370 | + and library functions. |
| 371 | +
|
| 372 | + Plugin authors should use this instead of the corresponding protobuf generated |
| 373 | + class. |
| 374 | +
|
| 375 | + Args: |
| 376 | + username (str): User name. |
| 377 | + private_key (str): Private key. |
| 378 | + public_key (str): Public key corresponding to private key. Empty string if not present. |
| 379 | + """ |
| 380 | + def __init__(self, username, private_key, public_key): |
| 381 | + super(KeyPairCredentials, self).__init__(username) |
| 382 | + if not isinstance(private_key, basestring): |
| 383 | + raise IncorrectTypeError( |
| 384 | + KeyPairCredentials, |
| 385 | + 'private_key', |
| 386 | + type(private_key), |
| 387 | + basestring) |
| 388 | + self.__private_key = private_key |
| 389 | + if not isinstance(public_key, basestring): |
| 390 | + raise IncorrectTypeError( |
| 391 | + KeyPairCredentials, |
| 392 | + 'public_key', |
| 393 | + type(public_key), |
| 394 | + basestring) |
| 395 | + self.__public_key = public_key |
| 396 | + |
| 397 | + @property |
| 398 | + def private_key(self): |
| 399 | + return self.__private_key |
| 400 | + |
| 401 | + @property |
| 402 | + def public_key(self): |
| 403 | + return self.__public_key |
| 404 | + |
| 405 | + @staticmethod |
| 406 | + def from_proto(credentials_result): |
| 407 | + """Converts protobuf class libs_pb2.CredentialsResult to plugin class KeyPairCredentials |
| 408 | + """ |
| 409 | + if not isinstance(credentials_result, libs_pb2.CredentialsResult): |
| 410 | + raise IncorrectTypeError( |
| 411 | + KeyPairCredentials, |
| 412 | + 'credentials_result', |
| 413 | + type(credentials_result), |
| 414 | + libs_pb2.CredentialsResult) |
| 415 | + return KeyPairCredentials( |
| 416 | + username=credentials_result.username, |
| 417 | + private_key=credentials_result.key_pair.private_key, |
| 418 | + public_key=credentials_result.key_pair.public_key) |
0 commit comments