Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
984285f
Add required change for object templates (#273)
gmazoyer Feb 20, 2025
4d03af6
Disable test integration-tests-local-infrahub for now
dgarros Feb 22, 2025
825363c
Merge pull request #279 from opsmill/dga-20250222-stable-develop
dgarros Feb 22, 2025
7003526
Add initial support for Task
dgarros Feb 21, 2025
d7a640f
Merge pull request #280 from opsmill/develop
dgarros Feb 23, 2025
1a6f3cd
prep for 1.8.0rc0
lykinsbd Feb 24, 2025
a2a496f
Replace pendulum with whenever
dgarros Feb 26, 2025
55be7bb
Merge pull request #256 from opsmill/dga-20250130-whenever
dgarros Feb 26, 2025
49486cb
fix: build for python 3.13
fatih-acar Feb 27, 2025
ae8510d
1.8.0b1 prep
lykinsbd Feb 28, 2025
4ec7730
Merge branch 'stable' into infrahub-develop
lykinsbd Feb 28, 2025
4de1ddf
Merge pull request #276 from opsmill/dga-20250220-tasks
dgarros Mar 1, 2025
97bfbf4
Update Protocols based on latest release-1.2 branch
dgarros Mar 2, 2025
701e445
Merge pull request #291 from opsmill/dga-20250302-update-protocols
dgarros Mar 3, 2025
fc93bfe
v1.8.0b2
lykinsbd Mar 4, 2025
c7311e2
Add support for OffsetDateTime
dgarros Mar 6, 2025
e59e6a5
Merge pull request #295 from opsmill/dga-20250305-OffsetDateTime
dgarros Mar 6, 2025
7c65729
Merge pull request #298 from opsmill/dga-20250309-stable-2-develop
dgarros Mar 9, 2025
826c188
Merge remote-tracking branch 'origin/develop' into infrahub-develop
dgarros Mar 9, 2025
5701b86
Add integration tests for Task and add support for Enum
dgarros Mar 9, 2025
a4fa7e5
Add newsfragment
dgarros Mar 9, 2025
86d8e1d
Fix integration tests
dgarros Mar 9, 2025
03772f1
Add support for request context
ogenstad Mar 7, 2025
2ea79fe
Merge pull request #296 from opsmill/pog-request-context-IFC-1340
ogenstad Mar 11, 2025
42ee591
Merge pull request #299 from opsmill/dga-20250309-cont-task-enum-support
dgarros Mar 11, 2025
41f6b85
add trailing microsecond 0s to serialized timestamps (#301)
ajtmccarty Mar 12, 2025
6b2dd46
Upgrade ruff=0.11.0
ogenstad Mar 18, 2025
547d764
Merge pull request #305 from opsmill/pog-upgrade-ruff
ogenstad Mar 19, 2025
2263ea3
Merge branch 'stable' of https://github.com/opsmill/infrahub-sdk-pyth…
lykinsbd Mar 20, 2025
15fe07d
prep for 1.8.0 release
lykinsbd Mar 20, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 68 additions & 65 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ jobs:
- name: "Check out repository code"
uses: "actions/checkout@v4"
- name: "Setup environment"
run: "pip install ruff==0.8.6"
run: "pip install ruff==0.11.0"
- name: "Linting: ruff check"
run: "ruff check ."
- name: "Linting: ruff format"
Expand Down Expand Up @@ -212,6 +212,7 @@ jobs:
- "3.10"
- "3.11"
- "3.12"
- "3.13"
if: |
always() && !cancelled() &&
!contains(needs.*.result, 'failure') &&
Expand Down Expand Up @@ -299,77 +300,79 @@ jobs:
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

integration-tests-local-infrahub:
if: |
always() && !cancelled() &&
!contains(needs.*.result, 'failure') &&
!contains(needs.*.result, 'cancelled') &&
needs.files-changed.outputs.python == 'true' &&
(github.base_ref == 'stable' || github.base_ref == 'develop')
needs: ["files-changed", "yaml-lint", "python-lint"]
runs-on:
group: "huge-runners"
timeout-minutes: 30
steps:
- name: "Check out repository code"
uses: "actions/checkout@v4"
# NOTE: Disabling this test for now because it's expected that we can't start the latest version of infrahub
# with the current shipping version of infrahub-testcontainers
# integration-tests-local-infrahub:
# if: |
# always() && !cancelled() &&
# !contains(needs.*.result, 'failure') &&
# !contains(needs.*.result, 'cancelled') &&
# needs.files-changed.outputs.python == 'true' &&
# (github.base_ref == 'stable' || github.base_ref == 'develop')
# needs: ["files-changed", "yaml-lint", "python-lint"]
# runs-on:
# group: "huge-runners"
# timeout-minutes: 30
# steps:
# - name: "Check out repository code"
# uses: "actions/checkout@v4"

- name: "Extract target branch name"
id: extract_branch
run: echo "TARGET_BRANCH=${{ github.base_ref }}" >> $GITHUB_ENV
# - name: "Extract target branch name"
# id: extract_branch
# run: echo "TARGET_BRANCH=${{ github.base_ref }}" >> $GITHUB_ENV

- name: "Checkout infrahub repository"
uses: "actions/checkout@v4"
with:
repository: "opsmill/infrahub"
path: "infrahub-server"
ref: ${{ github.base_ref }}
submodules: true
# - name: "Checkout infrahub repository"
# uses: "actions/checkout@v4"
# with:
# repository: "opsmill/infrahub"
# path: "infrahub-server"
# ref: ${{ github.base_ref }}
# submodules: true

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
# - name: Set up Python
# uses: actions/setup-python@v5
# with:
# python-version: "3.12"

- name: "Setup git credentials prior dev.build"
run: |
cd infrahub-server
git config --global user.name 'Infrahub'
git config --global user.email 'infrahub@opsmill.com'
git config --global --add safe.directory '*'
git config --global credential.usehttppath true
git config --global credential.helper /usr/local/bin/infrahub-git-credential
# - name: "Setup git credentials prior dev.build"
# run: |
# cd infrahub-server
# git config --global user.name 'Infrahub'
# git config --global user.email 'infrahub@opsmill.com'
# git config --global --add safe.directory '*'
# git config --global credential.usehttppath true
# git config --global credential.helper /usr/local/bin/infrahub-git-credential

- name: "Set environment variables prior dev.build"
run: |
echo "INFRAHUB_BUILD_NAME=infrahub-${{ runner.name }}" >> $GITHUB_ENV
RUNNER_NAME=$(echo "${{ runner.name }}" | grep -o 'ghrunner[0-9]\+' | sed 's/ghrunner\([0-9]\+\)/ghrunner_\1/')
echo "PYTEST_DEBUG_TEMPROOT=/var/lib/github/${RUNNER_NAME}/_temp" >> $GITHUB_ENV
echo "INFRAHUB_IMAGE_VER=local-${{ runner.name }}-${{ github.sha }}" >> $GITHUB_ENV
echo "INFRAHUB_TESTING_IMAGE_VER=local-${{ runner.name }}-${{ github.sha }}" >> $GITHUB_ENV
echo "INFRAHUB_TESTING_DOCKER_IMAGE=opsmill/infrahub" >> $GITHUB_ENV
# - name: "Set environment variables prior dev.build"
# run: |
# echo "INFRAHUB_BUILD_NAME=infrahub-${{ runner.name }}" >> $GITHUB_ENV
# RUNNER_NAME=$(echo "${{ runner.name }}" | grep -o 'ghrunner[0-9]\+' | sed 's/ghrunner\([0-9]\+\)/ghrunner_\1/')
# echo "PYTEST_DEBUG_TEMPROOT=/var/lib/github/${RUNNER_NAME}/_temp" >> $GITHUB_ENV
# echo "INFRAHUB_IMAGE_VER=local-${{ runner.name }}-${{ github.sha }}" >> $GITHUB_ENV
# echo "INFRAHUB_TESTING_IMAGE_VER=local-${{ runner.name }}-${{ github.sha }}" >> $GITHUB_ENV
# echo "INFRAHUB_TESTING_DOCKER_IMAGE=opsmill/infrahub" >> $GITHUB_ENV

- name: "Build container"
run: |
cd infrahub-server
inv dev.build
# - name: "Build container"
# run: |
# cd infrahub-server
# inv dev.build

- name: "Setup environment"
run: |
pipx install poetry==1.8.5
poetry config virtualenvs.create true --local
pip install invoke toml codecov
# - name: "Setup environment"
# run: |
# pipx install poetry==1.8.5
# poetry config virtualenvs.create true --local
# pip install invoke toml codecov

- name: "Install Package"
run: "poetry install --all-extras"
# - name: "Install Package"
# run: "poetry install --all-extras"

- name: "Integration Tests"
run: |
echo "Running tests for version: $INFRAHUB_TESTING_IMAGE_VER"
poetry run pytest --cov infrahub_sdk tests/integration/
# - name: "Integration Tests"
# run: |
# echo "Running tests for version: $INFRAHUB_TESTING_IMAGE_VER"
# poetry run pytest --cov infrahub_sdk tests/integration/

- name: "Upload coverage to Codecov"
run: |
codecov --flags integration-tests
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
# - name: "Upload coverage to Codecov"
# run: |
# codecov --flags integration-tests
# env:
# CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ This project uses [*towncrier*](https://towncrier.readthedocs.io/) and the chang

<!-- towncrier release notes start -->

## [1.8.0](https://github.com/opsmill/infrahub-sdk-python/tree/v1.8.0) - 2025-03-19

### Deprecated

- Timestamp: Direct access to `obj` and `add_delta` have been deprecated and will be removed in a future version. ([#255](https://github.com/opsmill/infrahub-sdk-python/issues/255))

### Added

- Added support for Enum in GraphQL query and mutation. ([#18](https://github.com/opsmill/infrahub-sdk-python/issues/18))

### Fixed

- Refactored Timestamp to use `whenever` instead of `pendulum` and extend Timestamp with `add()`, `subtract()`, and `to_datetime()`. ([#255](https://github.com/opsmill/infrahub-sdk-python/issues/255))
- Fixed support for Python 3.13 as it's no longer required to have Rust installed on the system.

## [1.7.2](https://github.com/opsmill/infrahub-sdk-python/tree/v1.7.2) - 2025-03-07

### Added
Expand All @@ -26,6 +41,14 @@ This project uses [*towncrier*](https://towncrier.readthedocs.io/) and the chang
### Fixed

- `protocols` CTL command properly gets default branch setting from environment variable. ([#104](https://github.com/opsmill/infrahub-sdk-python/issues/104))
- Fix typing for Python 3.9 ([#251](https://github.com/opsmill/infrahub-sdk-python/issues/251))
- Refactor Timestamp to use `whenever` instead of `pendulum` and extend Timestamp with add(), subtract(), and to_datetime(). ([#255](https://github.com/opsmill/infrahub-sdk-python/issues/255))
- Remove default value "main" for branch parameter from all Infrahub CTL commands. ([#264](https://github.com/opsmill/infrahub-sdk-python/issues/264))
- Fixed support for Python 3.13, it's no longer required to have Rust installed on the system.

### Housekeeping

- Move the function `read_file` from the ctl module to the SDK.
- Fixed typing for Python 3.9 and removed support for Python 3.13. ([#251](https://github.com/opsmill/infrahub-sdk-python/issues/251))
- Removed default value "main" for branch parameter from all Infrahub CTL commands. ([#264](https://github.com/opsmill/infrahub-sdk-python/issues/264))

Expand Down
19 changes: 19 additions & 0 deletions infrahub_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,16 @@
from .query_groups import InfrahubGroupContext, InfrahubGroupContextSync
from .schema import InfrahubSchema, InfrahubSchemaSync, NodeSchemaAPI
from .store import NodeStore, NodeStoreSync
from .task.manager import InfrahubTaskManager, InfrahubTaskManagerSync
from .timestamp import Timestamp
from .types import AsyncRequester, HTTPMethod, Order, SyncRequester
from .utils import decode_json, get_user_permissions, is_valid_uuid

if TYPE_CHECKING:
from types import TracebackType

from .context import RequestContext


SchemaType = TypeVar("SchemaType", bound=CoreNode)
SchemaTypeSync = TypeVar("SchemaTypeSync", bound=CoreNodeSync)
Expand Down Expand Up @@ -140,6 +143,7 @@
self.identifier = self.config.identifier
self.group_context: InfrahubGroupContext | InfrahubGroupContextSync
self._initialize()
self._request_context: RequestContext | None = None

def _initialize(self) -> None:
"""Sets the properties for each version of the client"""
Expand All @@ -154,6 +158,14 @@
if variables:
print(f"VARIABLES:\n{ujson.dumps(variables, indent=4)}\n")

@property
def request_context(self) -> RequestContext | None:
return self._request_context

@request_context.setter
def request_context(self, request_context: RequestContext) -> None:
self._request_context = request_context

Check warning on line 167 in infrahub_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/client.py#L167

Added line #L167 was not covered by tests

def start_tracking(
self,
identifier: str | None = None,
Expand Down Expand Up @@ -270,6 +282,7 @@
self.branch = InfrahubBranchManager(self)
self.object_store = ObjectStore(self)
self.store = NodeStore()
self.task = InfrahubTaskManager(self)
self.concurrent_execution_limit = asyncio.Semaphore(self.max_concurrent_execution)
self._request_method: AsyncRequester = self.config.requester or self._default_request_method
self.group_context = InfrahubGroupContext(self)
Expand Down Expand Up @@ -1505,13 +1518,19 @@


class InfrahubClientSync(BaseClient):
schema: InfrahubSchemaSync
branch: InfrahubBranchManagerSync
object_store: ObjectStoreSync
store: NodeStoreSync
task: InfrahubTaskManagerSync
group_context: InfrahubGroupContextSync

def _initialize(self) -> None:
self.schema = InfrahubSchemaSync(self)
self.branch = InfrahubBranchManagerSync(self)
self.object_store = ObjectStoreSync(self)
self.store = NodeStoreSync()
self.task = InfrahubTaskManagerSync(self)
self._request_method: SyncRequester = self.config.sync_requester or self._default_request_method
self.group_context = InfrahubGroupContextSync(self)

Expand Down
13 changes: 13 additions & 0 deletions infrahub_sdk/context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from __future__ import annotations

Check warning on line 1 in infrahub_sdk/context.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/context.py#L1

Added line #L1 was not covered by tests

from pydantic import BaseModel, Field

Check warning on line 3 in infrahub_sdk/context.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/context.py#L3

Added line #L3 was not covered by tests


class ContextAccount(BaseModel):
id: str = Field(..., description="The ID of the account")

Check warning on line 7 in infrahub_sdk/context.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/context.py#L6-L7

Added lines #L6 - L7 were not covered by tests


class RequestContext(BaseModel):

Check warning on line 10 in infrahub_sdk/context.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/context.py#L10

Added line #L10 was not covered by tests
"""The context can be used to override settings such as the account within mutations."""

account: ContextAccount | None = Field(default=None, description="Account tied to the context")

Check warning on line 13 in infrahub_sdk/context.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/context.py#L13

Added line #L13 was not covered by tests
5 changes: 3 additions & 2 deletions infrahub_sdk/ctl/branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
from rich.table import Table

from ..async_typer import AsyncTyper
from ..ctl.client import initialize_client
from ..ctl.utils import calculate_time_diff, catch_exception
from ..utils import calculate_time_diff
from .client import initialize_client
from .parameters import CONFIG_PARAM
from .utils import catch_exception

app = AsyncTyper()
console = Console()
Expand Down
16 changes: 0 additions & 16 deletions infrahub_sdk/ctl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable, NoReturn, Optional, TypeVar

import pendulum
import typer
from click.exceptions import Exit
from httpx import HTTPError
from pendulum.datetime import DateTime
from rich.console import Console
from rich.logging import RichHandler
from rich.markup import escape
Expand Down Expand Up @@ -152,20 +150,6 @@ def parse_cli_vars(variables: Optional[list[str]]) -> dict[str, str]:
return {var.split("=")[0]: var.split("=")[1] for var in variables if "=" in var}


def calculate_time_diff(value: str) -> str | None:
"""Calculate the time in human format between a timedate in string format and now."""
try:
time_value = pendulum.parse(value)
except pendulum.parsing.exceptions.ParserError:
return None

if not isinstance(time_value, DateTime):
return None

pendulum.set_locale("en")
return time_value.diff_for_humans(other=pendulum.now(), absolute=True)


def find_graphql_query(name: str, directory: str | Path = ".") -> str:
if isinstance(directory, str):
directory = Path(directory)
Expand Down
6 changes: 6 additions & 0 deletions infrahub_sdk/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,9 @@ class FileNotValidError(Error):
def __init__(self, name: str, message: str = ""):
self.message = message or f"Cannot parse '{name}' content."
super().__init__(self.message)


class TimestampFormatError(Error):
def __init__(self, message: str | None = None):
self.message = message or "Invalid timestamp format"
super().__init__(self.message)
3 changes: 3 additions & 0 deletions infrahub_sdk/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

if TYPE_CHECKING:
from .client import InfrahubClient
from .context import RequestContext
from .node import InfrahubNode
from .store import NodeStore

Expand All @@ -29,6 +30,7 @@
params: dict | None = None,
convert_query_response: bool = False,
logger: logging.Logger | None = None,
request_context: RequestContext | None = None,
) -> None:
self.query = query
self.branch = branch
Expand All @@ -44,6 +46,7 @@
self.infrahub_node = infrahub_node
self.convert_query_response = convert_query_response
self.logger = logger if logger else logging.getLogger("infrahub.tasks")
self.request_context = request_context

Check warning on line 49 in infrahub_sdk/generator.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/generator.py#L49

Added line #L49 was not covered by tests

@property
def store(self) -> NodeStore:
Expand Down
Loading