-
Notifications
You must be signed in to change notification settings - Fork 1
TF-30335 Add Foundational SDK Core Framework and Structure #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fec08a8
TF-30335 Add Foundational SDK Core Framework and Structure
6a33784
remove older test cases
da056ef
Ruff formatter rerun
iam404 b070f02
Fix runff checks
iam404 73da74f
Fix mypy lint errors / warning
iam404 21485c7
fix ruff formating
iam404 73be753
ruff check fixes
iam404 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ## |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| from tfe import TFEClient, TFEConfig | ||
|
|
||
|
|
||
| def main(): | ||
| client = TFEClient(TFEConfig.from_env()) | ||
| org = "tfe-xxxxx" | ||
| for ws in client.workspaces.list(org): | ||
| print("WS:", ws.name, ws.id) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from . import errors | ||
| from .client import TFEClient | ||
| from .config import TFEConfig | ||
|
|
||
| __all__ = ["TFEConfig", "TFEClient", "errors"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import time | ||
| from collections.abc import Mapping | ||
| from typing import Any | ||
|
|
||
| import anyio | ||
| import httpx | ||
|
|
||
| from ._jsonapi import build_headers, parse_error_payload | ||
| from .errors import ( | ||
| AuthError, | ||
| NotFound, | ||
| RateLimited, | ||
| ServerError, | ||
| TFEError, | ||
| ) | ||
|
|
||
| _RETRY_STATUSES = {429, 502, 503, 504} | ||
|
|
||
|
|
||
| class HTTPTransport: | ||
| def __init__( | ||
| self, | ||
| address: str, | ||
| token: str, | ||
| *, | ||
| timeout: float, | ||
| verify_tls: bool, | ||
| user_agent_suffix: str | None, | ||
| max_retries: int, | ||
| backoff_base: float, | ||
| backoff_cap: float, | ||
| backoff_jitter: bool, | ||
| http2: bool, | ||
| proxies: dict | None, | ||
| ca_bundle: str | None, | ||
| ): | ||
| self.base = address.rstrip("/") | ||
| self.headers = build_headers(user_agent_suffix) | ||
| if token: | ||
| self.headers["Authorization"] = f"Bearer {token}" | ||
| self.timeout = timeout | ||
| self.verify = verify_tls | ||
| self.max_retries = max_retries | ||
| self.backoff_base = backoff_base | ||
| self.backoff_cap = backoff_cap | ||
| self.backoff_jitter = backoff_jitter | ||
| self.http2 = http2 | ||
| self.proxies = proxies | ||
| self.ca_bundle = ca_bundle | ||
| self._sync = httpx.Client( | ||
| http2=http2, timeout=timeout, verify=ca_bundle or verify_tls | ||
| ) # proxies=proxies | ||
| self._async = httpx.AsyncClient( | ||
| http2=http2, timeout=timeout, verify=ca_bundle or verify_tls | ||
| ) # proxies=proxies | ||
|
|
||
| def request( | ||
| self, | ||
| method: str, | ||
| path: str, | ||
| *, | ||
| params: Mapping[str, Any] | None = None, | ||
| json_body: Mapping[str, Any] | None = None, | ||
| headers: dict[str, str] | None = None, | ||
| allow_redirects: bool = True, | ||
| ) -> httpx.Response: | ||
| url = f"{self.base}{path}" | ||
| hdrs = dict(self.headers) | ||
| if headers: | ||
| hdrs.update(headers) | ||
| attempt = 0 | ||
| while True: | ||
| try: | ||
| resp = self._sync.request( | ||
| method, | ||
| url, | ||
| params=params, | ||
| json=json_body, | ||
| headers=hdrs, | ||
| follow_redirects=allow_redirects, | ||
| ) | ||
| except httpx.HTTPError as e: | ||
| if attempt >= self.max_retries: | ||
| raise ServerError(str(e)) from e | ||
| self._sleep(attempt, None) | ||
| attempt += 1 | ||
| continue | ||
| if resp.status_code in _RETRY_STATUSES and attempt < self.max_retries: | ||
| retry_after = _parse_retry_after(resp) | ||
| self._sleep(attempt, retry_after) | ||
| attempt += 1 | ||
| continue | ||
| self._raise_if_error(resp) | ||
| return resp | ||
|
|
||
| async def arequest( | ||
| self, | ||
| method: str, | ||
| path: str, | ||
| *, | ||
| params: Mapping[str, Any] | None = None, | ||
| json_body: Mapping[str, Any] | None = None, | ||
| headers: dict[str, str] | None = None, | ||
| allow_redirects: bool = True, | ||
| ) -> httpx.Response: | ||
| url = f"{self.base}{path}" | ||
| hdrs = dict(self.headers) | ||
| hdrs.update(headers or {}) | ||
| attempt = 0 | ||
| while True: | ||
| try: | ||
| resp = await self._async.request( | ||
| method, | ||
| url, | ||
| params=params, | ||
| json=json_body, | ||
| headers=hdrs, | ||
| follow_redirects=allow_redirects, | ||
| ) | ||
| except httpx.HTTPError as e: | ||
| if attempt >= self.max_retries: | ||
| raise ServerError(str(e)) from e | ||
| await self._asleep(attempt, None) | ||
| attempt += 1 | ||
| continue | ||
| if resp.status_code in _RETRY_STATUSES and attempt < self.max_retries: | ||
| retry_after = _parse_retry_after(resp) | ||
| await self._asleep(attempt, retry_after) | ||
| attempt += 1 | ||
| continue | ||
| self._raise_if_error(resp) | ||
| return resp | ||
|
|
||
| def _sleep(self, attempt: int, retry_after: float | None) -> None: | ||
| if retry_after is not None: | ||
| time.sleep(retry_after) | ||
| return | ||
| delay = min(self.backoff_cap, self.backoff_base * (2**attempt)) | ||
| time.sleep(delay) | ||
iam404 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| async def _asleep(self, attempt: int, retry_after: float | None) -> None: | ||
| if retry_after is not None: | ||
| await anyio.sleep(retry_after) | ||
| return | ||
| delay = min(self.backoff_cap, self.backoff_base * (2**attempt)) | ||
| await anyio.sleep(delay) | ||
|
|
||
| def _raise_if_error(self, resp: httpx.Response) -> None: | ||
| status = resp.status_code | ||
|
|
||
| if 200 <= status < 300: | ||
| return | ||
| try: | ||
| payload: Any = resp.json() | ||
| except Exception: | ||
| payload = {} | ||
| errors = parse_error_payload(payload) | ||
| msg: str = f"HTTP {status}" | ||
| if errors: | ||
| maybe_detail = errors[0].get("detail") | ||
| maybe_title = errors[0].get("title") | ||
| if isinstance(maybe_detail, str) and maybe_detail: | ||
| msg = maybe_detail | ||
| elif isinstance(maybe_title, str) and maybe_title: | ||
| msg = maybe_title | ||
|
|
||
| if status in (401, 403): | ||
| raise AuthError(msg, status=status, errors=errors) | ||
| if status == 404: | ||
| raise NotFound(msg, status=status, errors=errors) | ||
| if status == 429: | ||
| ra = _parse_retry_after(resp) | ||
| raise RateLimited(msg, status=status, errors=errors, retry_after=ra) | ||
| if status >= 500: | ||
| raise ServerError(msg, status=status, errors=errors) | ||
| raise TFEError(msg, status=status, errors=errors) | ||
|
|
||
|
|
||
| def _parse_retry_after(resp: httpx.Response) -> float | None: | ||
| ra = resp.headers.get("Retry-After") | ||
| if not ra: | ||
| return None | ||
| try: | ||
| return float(ra) | ||
| except Exception: | ||
| return None | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
|
|
||
| def build_headers(user_agent_suffix: str | None = None) -> dict[str, str]: | ||
| ua = "python-tfe/0.1" | ||
| if user_agent_suffix: | ||
| ua = f"{ua} {user_agent_suffix}" | ||
| return { | ||
| "Accept": "application/vnd.api+json", | ||
| "Content-Type": "application/vnd.api+json", | ||
| "User-Agent": ua, | ||
| } | ||
|
|
||
|
|
||
| def parse_error_payload(payload: dict[str, Any]) -> list[dict]: | ||
| errs = payload.get("errors") | ||
| if isinstance(errs, list): | ||
| return errs | ||
| if "message" in payload: | ||
| return [{"detail": payload.get("message")}] | ||
| return [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| """ | ||
| Async TFE Client: This client should not be used for now. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from ._http import HTTPTransport | ||
| from .config import TFEConfig | ||
|
|
||
|
|
||
| class AsyncTFEClient: | ||
| def __init__(self, config: TFEConfig | None = None): | ||
| cfg = config or TFEConfig.from_env() | ||
| self._transport = HTTPTransport( | ||
| cfg.address, | ||
| cfg.token, | ||
| timeout=cfg.timeout, | ||
| verify_tls=cfg.verify_tls, | ||
| user_agent_suffix=cfg.user_agent_suffix, | ||
| max_retries=cfg.max_retries, | ||
| backoff_base=cfg.backoff_base, | ||
| backoff_cap=cfg.backoff_cap, | ||
| backoff_jitter=cfg.backoff_jitter, | ||
| http2=cfg.http2, | ||
| proxies=cfg.proxies, | ||
| ca_bundle=cfg.ca_bundle, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from ._http import HTTPTransport | ||
| from .config import TFEConfig | ||
| from .resources.organizations import Organizations | ||
| from .resources.projects import Projects | ||
| from .resources.workspaces import Workspaces | ||
|
|
||
|
|
||
| class TFEClient: | ||
| def __init__(self, config: TFEConfig | None = None): | ||
| cfg = config or TFEConfig.from_env() | ||
| self._transport = HTTPTransport( | ||
| cfg.address, | ||
| cfg.token, | ||
| timeout=cfg.timeout, | ||
| verify_tls=cfg.verify_tls, | ||
| user_agent_suffix=cfg.user_agent_suffix, | ||
| max_retries=cfg.max_retries, | ||
| backoff_base=cfg.backoff_base, | ||
| backoff_cap=cfg.backoff_cap, | ||
| backoff_jitter=cfg.backoff_jitter, | ||
| http2=cfg.http2, | ||
| proxies=cfg.proxies, | ||
| ca_bundle=cfg.ca_bundle, | ||
| ) | ||
| self.organizations = Organizations(self._transport) | ||
| self.projects = Projects(self._transport) | ||
| self.workspaces = Workspaces(self._transport) | ||
|
|
||
| def close(self) -> None: | ||
| pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import os | ||
|
|
||
| from pydantic import BaseModel, Field | ||
|
|
||
|
|
||
| class TFEConfig(BaseModel): | ||
| address: str = Field( | ||
| default_factory=lambda: os.getenv("TFE_ADDRESS", "https://app.terraform.io") | ||
| ) | ||
| token: str = Field(default_factory=lambda: os.getenv("TFE_TOKEN", "")) | ||
| timeout: float = float(os.getenv("TFE_TIMEOUT", "30")) | ||
| verify_tls: bool = os.getenv("TFE_VERIFY_TLS", "true").lower() not in ( | ||
| "0", | ||
| "false", | ||
| "no", | ||
| ) | ||
| user_agent_suffix: str | None = None | ||
| max_retries: int = int(os.getenv("TFE_MAX_RETRIES", "5")) | ||
| backoff_base: float = 0.5 | ||
| backoff_cap: float = 8.0 | ||
| backoff_jitter: bool = True | ||
| http2: bool = True | ||
| proxies: dict[str, str] | None = None | ||
| ca_bundle: str | None = os.getenv("SSL_CERT_FILE", None) | ||
|
|
||
| @classmethod | ||
| def from_env(cls) -> TFEConfig: | ||
| return cls() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The commented out
proxies=proxiessuggests incomplete implementation. Either implement proxy support or remove the commented code and the proxies parameter if not needed.