airbyte.cloud.organizations
PyAirbyte classes and methods for Airbyte Cloud organizations.
1# Copyright (c) 2024 Airbyte, Inc., all rights reserved. 2"""PyAirbyte classes and methods for Airbyte Cloud organizations.""" 3 4from __future__ import annotations 5 6import logging 7from typing import Any 8 9from airbyte._util import api_util 10from airbyte.cloud._credentials import _AirbyteCredentials 11from airbyte.secrets.base import SecretString 12 13 14logger = logging.getLogger(__name__) 15 16 17class CloudOrganization: 18 """Information about an organization in Airbyte Cloud. 19 20 This class provides lazy loading of organization attributes including billing status. 21 It is typically created via `CloudWorkspace.get_organization()`. 22 """ 23 24 def __init__( 25 self, 26 organization_id: str, 27 organization_name: str | None = None, 28 email: str | None = None, 29 *, 30 client_id: str | SecretString | None = None, 31 client_secret: str | SecretString | None = None, 32 bearer_token: str | SecretString | None = None, 33 public_api_root: str | None = None, 34 config_api_root: str | None = None, 35 ) -> None: 36 """Initialize a `CloudOrganization`.""" 37 self.organization_id = organization_id 38 """The organization ID.""" 39 40 self._organization_name = organization_name 41 """Display name of the organization.""" 42 43 self._email = email 44 """Email associated with the organization.""" 45 46 self._credentials = _AirbyteCredentials( 47 client_id=SecretString(client_id) if client_id else None, 48 client_secret=SecretString(client_secret) if client_secret else None, 49 bearer_token=SecretString(bearer_token) if bearer_token else None, 50 public_api_root=public_api_root or api_util.CLOUD_API_ROOT, 51 config_api_root=config_api_root, 52 organization_id=organization_id, 53 ) 54 self._organization_info: dict[str, Any] | None = None 55 self._organization_info_fetch_failed: bool = False 56 57 def _fetch_organization_info(self, *, force_refresh: bool = False) -> dict[str, Any]: 58 """Fetch and cache organization info including billing status.""" 59 if force_refresh: 60 self._organization_info_fetch_failed = False 61 62 if self._organization_info_fetch_failed and self._organization_info is None: 63 return {} 64 65 if not force_refresh and self._organization_info is not None: 66 return self._organization_info 67 68 try: 69 self._organization_info = api_util.get_organization_info( 70 organization_id=self.organization_id, 71 api_root=self._credentials.public_api_root, 72 config_api_root=self._credentials.config_api_root, 73 client_id=self._credentials.client_id, 74 client_secret=self._credentials.client_secret, 75 bearer_token=self._credentials.bearer_token, 76 ) 77 except Exception as ex: 78 logger.debug("Failed to fetch organization info.", exc_info=ex) 79 if self._organization_info is None: 80 self._organization_info_fetch_failed = True 81 return self._organization_info or {} 82 else: 83 return self._organization_info 84 85 @property 86 def organization_name(self) -> str | None: 87 """Display name of the organization.""" 88 if self._organization_name is not None: 89 return self._organization_name 90 info = self._fetch_organization_info() 91 return info.get("organizationName") 92 93 @property 94 def email(self) -> str | None: 95 """Email associated with the organization.""" 96 if self._email is not None: 97 return self._email 98 info = self._fetch_organization_info() 99 return info.get("email") 100 101 @property 102 def payment_status(self) -> str | None: 103 """Payment status of the organization.""" 104 info = self._fetch_organization_info() 105 return (info.get("billing") or {}).get("paymentStatus") 106 107 @property 108 def subscription_status(self) -> str | None: 109 """Subscription status of the organization.""" 110 info = self._fetch_organization_info() 111 return (info.get("billing") or {}).get("subscriptionStatus") 112 113 @property 114 def is_account_locked(self) -> bool: 115 """Whether the account is locked due to billing issues.""" 116 return api_util.is_account_locked(self.payment_status, self.subscription_status)
logger =
<Logger airbyte.cloud.organizations (INFO)>
class
CloudOrganization:
18class CloudOrganization: 19 """Information about an organization in Airbyte Cloud. 20 21 This class provides lazy loading of organization attributes including billing status. 22 It is typically created via `CloudWorkspace.get_organization()`. 23 """ 24 25 def __init__( 26 self, 27 organization_id: str, 28 organization_name: str | None = None, 29 email: str | None = None, 30 *, 31 client_id: str | SecretString | None = None, 32 client_secret: str | SecretString | None = None, 33 bearer_token: str | SecretString | None = None, 34 public_api_root: str | None = None, 35 config_api_root: str | None = None, 36 ) -> None: 37 """Initialize a `CloudOrganization`.""" 38 self.organization_id = organization_id 39 """The organization ID.""" 40 41 self._organization_name = organization_name 42 """Display name of the organization.""" 43 44 self._email = email 45 """Email associated with the organization.""" 46 47 self._credentials = _AirbyteCredentials( 48 client_id=SecretString(client_id) if client_id else None, 49 client_secret=SecretString(client_secret) if client_secret else None, 50 bearer_token=SecretString(bearer_token) if bearer_token else None, 51 public_api_root=public_api_root or api_util.CLOUD_API_ROOT, 52 config_api_root=config_api_root, 53 organization_id=organization_id, 54 ) 55 self._organization_info: dict[str, Any] | None = None 56 self._organization_info_fetch_failed: bool = False 57 58 def _fetch_organization_info(self, *, force_refresh: bool = False) -> dict[str, Any]: 59 """Fetch and cache organization info including billing status.""" 60 if force_refresh: 61 self._organization_info_fetch_failed = False 62 63 if self._organization_info_fetch_failed and self._organization_info is None: 64 return {} 65 66 if not force_refresh and self._organization_info is not None: 67 return self._organization_info 68 69 try: 70 self._organization_info = api_util.get_organization_info( 71 organization_id=self.organization_id, 72 api_root=self._credentials.public_api_root, 73 config_api_root=self._credentials.config_api_root, 74 client_id=self._credentials.client_id, 75 client_secret=self._credentials.client_secret, 76 bearer_token=self._credentials.bearer_token, 77 ) 78 except Exception as ex: 79 logger.debug("Failed to fetch organization info.", exc_info=ex) 80 if self._organization_info is None: 81 self._organization_info_fetch_failed = True 82 return self._organization_info or {} 83 else: 84 return self._organization_info 85 86 @property 87 def organization_name(self) -> str | None: 88 """Display name of the organization.""" 89 if self._organization_name is not None: 90 return self._organization_name 91 info = self._fetch_organization_info() 92 return info.get("organizationName") 93 94 @property 95 def email(self) -> str | None: 96 """Email associated with the organization.""" 97 if self._email is not None: 98 return self._email 99 info = self._fetch_organization_info() 100 return info.get("email") 101 102 @property 103 def payment_status(self) -> str | None: 104 """Payment status of the organization.""" 105 info = self._fetch_organization_info() 106 return (info.get("billing") or {}).get("paymentStatus") 107 108 @property 109 def subscription_status(self) -> str | None: 110 """Subscription status of the organization.""" 111 info = self._fetch_organization_info() 112 return (info.get("billing") or {}).get("subscriptionStatus") 113 114 @property 115 def is_account_locked(self) -> bool: 116 """Whether the account is locked due to billing issues.""" 117 return api_util.is_account_locked(self.payment_status, self.subscription_status)
Information about an organization in Airbyte Cloud.
This class provides lazy loading of organization attributes including billing status.
It is typically created via CloudWorkspace.get_organization().
CloudOrganization( organization_id: str, organization_name: str | None = None, email: str | None = None, *, client_id: str | airbyte.secrets.SecretString | None = None, client_secret: str | airbyte.secrets.SecretString | None = None, bearer_token: str | airbyte.secrets.SecretString | None = None, public_api_root: str | None = None, config_api_root: str | None = None)
25 def __init__( 26 self, 27 organization_id: str, 28 organization_name: str | None = None, 29 email: str | None = None, 30 *, 31 client_id: str | SecretString | None = None, 32 client_secret: str | SecretString | None = None, 33 bearer_token: str | SecretString | None = None, 34 public_api_root: str | None = None, 35 config_api_root: str | None = None, 36 ) -> None: 37 """Initialize a `CloudOrganization`.""" 38 self.organization_id = organization_id 39 """The organization ID.""" 40 41 self._organization_name = organization_name 42 """Display name of the organization.""" 43 44 self._email = email 45 """Email associated with the organization.""" 46 47 self._credentials = _AirbyteCredentials( 48 client_id=SecretString(client_id) if client_id else None, 49 client_secret=SecretString(client_secret) if client_secret else None, 50 bearer_token=SecretString(bearer_token) if bearer_token else None, 51 public_api_root=public_api_root or api_util.CLOUD_API_ROOT, 52 config_api_root=config_api_root, 53 organization_id=organization_id, 54 ) 55 self._organization_info: dict[str, Any] | None = None 56 self._organization_info_fetch_failed: bool = False
Initialize a CloudOrganization.
organization_name: str | None
86 @property 87 def organization_name(self) -> str | None: 88 """Display name of the organization.""" 89 if self._organization_name is not None: 90 return self._organization_name 91 info = self._fetch_organization_info() 92 return info.get("organizationName")
Display name of the organization.
email: str | None
94 @property 95 def email(self) -> str | None: 96 """Email associated with the organization.""" 97 if self._email is not None: 98 return self._email 99 info = self._fetch_organization_info() 100 return info.get("email")
Email associated with the organization.
payment_status: str | None
102 @property 103 def payment_status(self) -> str | None: 104 """Payment status of the organization.""" 105 info = self._fetch_organization_info() 106 return (info.get("billing") or {}).get("paymentStatus")
Payment status of the organization.