Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 22 additions & 1 deletion infrahub_sdk/schema/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from enum import Enum
from typing import TYPE_CHECKING, Any, Optional, Union

from pydantic import BaseModel, Field
from pydantic import BaseModel, ConfigDict, Field

if TYPE_CHECKING:
from ..node import InfrahubNode, InfrahubNodeSync
Expand Down Expand Up @@ -89,6 +89,8 @@


class AttributeSchema(BaseModel):
model_config = ConfigDict(use_enum_values=True)

id: Optional[str] = None
state: SchemaState = SchemaState.PRESENT
name: str
Expand All @@ -108,12 +110,16 @@


class AttributeSchemaAPI(AttributeSchema):
model_config = ConfigDict(use_enum_values=True)

inherited: bool = False
read_only: bool = False
allow_override: AllowOverrideType = AllowOverrideType.ANY


class RelationshipSchema(BaseModel):
model_config = ConfigDict(use_enum_values=True)

id: Optional[str] = None
state: SchemaState = SchemaState.PRESENT
name: str
Expand All @@ -133,6 +139,8 @@


class RelationshipSchemaAPI(RelationshipSchema):
model_config = ConfigDict(use_enum_values=True)

inherited: bool = False
read_only: bool = False
hierarchical: Optional[str] = None
Expand Down Expand Up @@ -245,6 +253,8 @@


class BaseSchema(BaseModel):
model_config = ConfigDict(use_enum_values=True)

id: Optional[str] = None
state: SchemaState = SchemaState.PRESENT
name: str
Expand Down Expand Up @@ -275,6 +285,8 @@


class BaseNodeSchema(BaseSchema):
model_config = ConfigDict(use_enum_values=True)

inherit_from: list[str] = Field(default_factory=list)
branch: Optional[BranchSupportType] = None
default_filter: Optional[str] = None
Expand All @@ -299,6 +311,8 @@


class NodeExtensionSchema(BaseModel):
model_config = ConfigDict(use_enum_values=True)

name: Optional[str] = None
kind: str
description: Optional[str] = None
Expand All @@ -311,13 +325,20 @@


class SchemaRoot(BaseModel):
model_config = ConfigDict(use_enum_values=True)

version: str
generics: list[GenericSchema] = Field(default_factory=list)
nodes: list[NodeSchema] = Field(default_factory=list)
node_extensions: list[NodeExtensionSchema] = Field(default_factory=list)

def to_schema_dict(self) -> dict[str, Any]:
return self.model_dump(exclude_unset=True, exclude_defaults=True)

Check warning on line 336 in infrahub_sdk/schema/main.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/schema/main.py#L336

Added line #L336 was not covered by tests


class SchemaRootAPI(BaseModel):
model_config = ConfigDict(use_enum_values=True)

version: str
generics: list[GenericSchemaAPI] = Field(default_factory=list)
nodes: list[NodeSchemaAPI] = Field(default_factory=list)
Expand Down
Empty file.
18 changes: 18 additions & 0 deletions infrahub_sdk/testing/docker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pytest
from infrahub_testcontainers.helpers import TestInfrahubDocker

Check warning on line 2 in infrahub_sdk/testing/docker.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/docker.py#L1-L2

Added lines #L1 - L2 were not covered by tests

from .. import Config, InfrahubClient, InfrahubClientSync

Check warning on line 4 in infrahub_sdk/testing/docker.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/docker.py#L4

Added line #L4 was not covered by tests


class TestInfrahubDockerClient(TestInfrahubDocker):
@pytest.fixture(scope="class")

Check warning on line 8 in infrahub_sdk/testing/docker.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/docker.py#L7-L8

Added lines #L7 - L8 were not covered by tests
def client(self, infrahub_port: int) -> InfrahubClient:
return InfrahubClient(

Check warning on line 10 in infrahub_sdk/testing/docker.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/docker.py#L10

Added line #L10 was not covered by tests
config=Config(username="admin", password="infrahub", address=f"http://localhost:{infrahub_port}") # noqa: S106
)

@pytest.fixture(scope="class")

Check warning on line 14 in infrahub_sdk/testing/docker.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/docker.py#L14

Added line #L14 was not covered by tests
def client_sync(self, infrahub_port: int) -> InfrahubClientSync:
return InfrahubClientSync(

Check warning on line 16 in infrahub_sdk/testing/docker.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/docker.py#L16

Added line #L16 was not covered by tests
config=Config(username="admin", password="infrahub", address=f"http://localhost:{infrahub_port}") # noqa: S106
)
103 changes: 103 additions & 0 deletions infrahub_sdk/testing/repository.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import asyncio
import shutil
from dataclasses import dataclass, field
from enum import Enum
from pathlib import Path
from typing import Optional

Check warning on line 6 in infrahub_sdk/testing/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/repository.py#L1-L6

Added lines #L1 - L6 were not covered by tests

from git.repo import Repo

Check warning on line 8 in infrahub_sdk/testing/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/repository.py#L8

Added line #L8 was not covered by tests

from infrahub_sdk import InfrahubClient
from infrahub_sdk.graphql import Mutation
from infrahub_sdk.protocols import CoreGenericRepository

Check warning on line 12 in infrahub_sdk/testing/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/repository.py#L10-L12

Added lines #L10 - L12 were not covered by tests


# NOTE we shouldn't duplicate this, need to figure out a better solution
class RepositorySyncStatus(str, Enum):
UNKNOWN = "unknown"
IN_SYNC = "in-sync"
ERROR_IMPORT = "error-import"
SYNCING = "syncing"

Check warning on line 20 in infrahub_sdk/testing/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/repository.py#L16-L20

Added lines #L16 - L20 were not covered by tests


class GitRepoType(str, Enum):
INTEGRATED = "CoreRepository"
READ_ONLY = "CoreReadOnlyRepository"

Check warning on line 25 in infrahub_sdk/testing/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/repository.py#L23-L25

Added lines #L23 - L25 were not covered by tests


@dataclass
class GitRepo:
name: str
src_directory: Path
dst_directory: Path

Check warning on line 32 in infrahub_sdk/testing/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/repository.py#L29-L32

Added lines #L29 - L32 were not covered by tests

type: GitRepoType = GitRepoType.INTEGRATED

Check warning on line 34 in infrahub_sdk/testing/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/repository.py#L34

Added line #L34 was not covered by tests

_repo: Optional[Repo] = None
initial_branch: str = "main"
directories_to_ignore: list[str] = field(default_factory=list)
remote_directory_name: str = "/remote"
_branches: list[str] = field(default_factory=list)

Check warning on line 40 in infrahub_sdk/testing/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/repository.py#L36-L40

Added lines #L36 - L40 were not covered by tests

@property

Check warning on line 42 in infrahub_sdk/testing/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/repository.py#L42

Added line #L42 was not covered by tests
def repo(self) -> Repo:
if self._repo:
return self._repo
raise ValueError("Repo hasn't been initialized yet")

Check warning on line 46 in infrahub_sdk/testing/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/repository.py#L45-L46

Added lines #L45 - L46 were not covered by tests

def __post_init__(self) -> None:
self.init()

Check warning on line 49 in infrahub_sdk/testing/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/repository.py#L48-L49

Added lines #L48 - L49 were not covered by tests

@property

Check warning on line 51 in infrahub_sdk/testing/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/repository.py#L51

Added line #L51 was not covered by tests
def path(self) -> str:
return str(self.src_directory / self.name)

Check warning on line 53 in infrahub_sdk/testing/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/repository.py#L53

Added line #L53 was not covered by tests

def init(self) -> None:
shutil.copytree(

Check warning on line 56 in infrahub_sdk/testing/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/repository.py#L55-L56

Added lines #L55 - L56 were not covered by tests
src=self.src_directory,
dst=self.dst_directory / self.name,
ignore=shutil.ignore_patterns(".git"),
)
self._repo = Repo.init(self.dst_directory / self.name, initial_branch=self.initial_branch)

Check warning on line 61 in infrahub_sdk/testing/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/repository.py#L61

Added line #L61 was not covered by tests
for untracked in self.repo.untracked_files:
self.repo.index.add(untracked)
self.repo.index.commit("First commit")

Check warning on line 64 in infrahub_sdk/testing/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/repository.py#L63-L64

Added lines #L63 - L64 were not covered by tests

self.repo.git.checkout(self.initial_branch)

Check warning on line 66 in infrahub_sdk/testing/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/repository.py#L66

Added line #L66 was not covered by tests

async def add_to_infrahub(self, client: InfrahubClient, branch: Optional[str] = None) -> dict:
input_data = {

Check warning on line 69 in infrahub_sdk/testing/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/repository.py#L68-L69

Added lines #L68 - L69 were not covered by tests
"data": {
"name": {"value": self.name},
"location": {"value": f"{self.remote_directory_name}/{self.name}"},
},
}

query = Mutation(

Check warning on line 76 in infrahub_sdk/testing/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/repository.py#L76

Added line #L76 was not covered by tests
mutation=f"{self.type.value}Create",
input_data=input_data,
query={"ok": None},
)

return await client.execute_graphql(

Check warning on line 82 in infrahub_sdk/testing/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/repository.py#L82

Added line #L82 was not covered by tests
query=query.render(), branch_name=branch or self.initial_branch, tracker="mutation-repository-create"
)

async def wait_for_sync_to_complete(

Check warning on line 86 in infrahub_sdk/testing/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/repository.py#L86

Added line #L86 was not covered by tests
self, client: InfrahubClient, branch: Optional[str] = None, interval: int = 5, retries: int = 6
) -> bool:
for _ in range(retries):
repo = await client.get(

Check warning on line 90 in infrahub_sdk/testing/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/repository.py#L90

Added line #L90 was not covered by tests
kind=CoreGenericRepository, # type: ignore[type-abstract]
name__value=self.name,
branch=branch or self.initial_branch,
)
status = repo.sync_status.value

Check warning on line 95 in infrahub_sdk/testing/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/repository.py#L95

Added line #L95 was not covered by tests
if status == RepositorySyncStatus.IN_SYNC.value:
return True

Check warning on line 97 in infrahub_sdk/testing/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/repository.py#L97

Added line #L97 was not covered by tests
if status == RepositorySyncStatus.ERROR_IMPORT.value:
return False

Check warning on line 99 in infrahub_sdk/testing/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/repository.py#L99

Added line #L99 was not covered by tests

await asyncio.sleep(interval)

Check warning on line 101 in infrahub_sdk/testing/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/repository.py#L101

Added line #L101 was not covered by tests

return False

Check warning on line 103 in infrahub_sdk/testing/repository.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/repository.py#L103

Added line #L103 was not covered by tests
Empty file.
145 changes: 145 additions & 0 deletions infrahub_sdk/testing/schemas/car_person.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import pytest

Check warning on line 1 in infrahub_sdk/testing/schemas/car_person.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/car_person.py#L1

Added line #L1 was not covered by tests

from infrahub_sdk import InfrahubClient
from infrahub_sdk.node import InfrahubNode
from infrahub_sdk.schema.main import AttributeKind, NodeSchema, RelationshipKind, SchemaRoot
from infrahub_sdk.schema.main import AttributeSchema as Attr
from infrahub_sdk.schema.main import RelationshipSchema as Rel

Check warning on line 7 in infrahub_sdk/testing/schemas/car_person.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/car_person.py#L3-L7

Added lines #L3 - L7 were not covered by tests

NAMESPACE = "Testing"

Check warning on line 9 in infrahub_sdk/testing/schemas/car_person.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/car_person.py#L9

Added line #L9 was not covered by tests

TESTING_MANUFACTURER = f"{NAMESPACE}Manufacturer"
TESTING_PERSON = f"{NAMESPACE}Person"
TESTING_CAR = f"{NAMESPACE}Car"

Check warning on line 13 in infrahub_sdk/testing/schemas/car_person.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/car_person.py#L11-L13

Added lines #L11 - L13 were not covered by tests


class SchemaCarPerson:
@pytest.fixture(scope="class")

Check warning on line 17 in infrahub_sdk/testing/schemas/car_person.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/car_person.py#L16-L17

Added lines #L16 - L17 were not covered by tests
def schema_person_base(self) -> NodeSchema:
return NodeSchema(

Check warning on line 19 in infrahub_sdk/testing/schemas/car_person.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/car_person.py#L19

Added line #L19 was not covered by tests
name="Person",
namespace=NAMESPACE,
include_in_menu=True,
label="Person",
human_friendly_id=["name__value"],
attributes=[
Attr(name="name", kind=AttributeKind.TEXT, unique=True),
Attr(name="description", kind=AttributeKind.TEXT, optional=True),
Attr(name="height", kind=AttributeKind.NUMBER, optional=True),
Attr(name="age", kind=AttributeKind.NUMBER, optional=True),
],
relationships=[
Rel(name="cars", kind=RelationshipKind.GENERIC, optional=True, peer=TESTING_CAR, cardinality="many")
],
)

@pytest.fixture(scope="class")

Check warning on line 36 in infrahub_sdk/testing/schemas/car_person.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/car_person.py#L36

Added line #L36 was not covered by tests
def schema_car_base(self) -> NodeSchema:
return NodeSchema(

Check warning on line 38 in infrahub_sdk/testing/schemas/car_person.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/car_person.py#L38

Added line #L38 was not covered by tests
name="Car",
namespace=NAMESPACE,
include_in_menu=True,
default_filter="name__value",
human_friendly_id=["owner__name__value", "name__value"],
label="Car",
attributes=[
Attr(name="name", kind=AttributeKind.TEXT),
Attr(name="description", kind=AttributeKind.TEXT, optional=True),
Attr(name="color", kind=AttributeKind.TEXT),
],
relationships=[
Rel(
name="owner",
kind=RelationshipKind.ATTRIBUTE,
optional=False,
peer=TESTING_PERSON,
cardinality="one",
),
Rel(
name="manufacturer",
kind=RelationshipKind.ATTRIBUTE,
optional=False,
peer=TESTING_MANUFACTURER,
cardinality="one",
identifier="car__manufacturer",
),
],
)

@pytest.fixture(scope="class")

Check warning on line 69 in infrahub_sdk/testing/schemas/car_person.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/car_person.py#L69

Added line #L69 was not covered by tests
def schema_manufacturer_base(self) -> NodeSchema:
return NodeSchema(

Check warning on line 71 in infrahub_sdk/testing/schemas/car_person.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/car_person.py#L71

Added line #L71 was not covered by tests
name="Manufacturer",
namespace=NAMESPACE,
include_in_menu=True,
label="Manufacturer",
human_friendly_id=["name__value"],
attributes=[
Attr(name="name", kind=AttributeKind.TEXT),
Attr(name="description", kind=AttributeKind.TEXT, optional=True),
],
relationships=[
Rel(
name="cars",
kind=RelationshipKind.GENERIC,
optional=True,
peer=TESTING_CAR,
cardinality="many",
identifier="car__manufacturer",
),
Rel(
name="customers",
kind=RelationshipKind.GENERIC,
optional=True,
peer=TESTING_PERSON,
cardinality="many",
identifier="person__manufacturer",
),
],
)

@pytest.fixture(scope="class")

Check warning on line 101 in infrahub_sdk/testing/schemas/car_person.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/car_person.py#L101

Added line #L101 was not covered by tests
def schema_base(
self,
schema_car_base: NodeSchema,
schema_person_base: NodeSchema,
schema_manufacturer_base: NodeSchema,
) -> SchemaRoot:
return SchemaRoot(version="1.0", nodes=[schema_car_base, schema_person_base, schema_manufacturer_base])

Check warning on line 108 in infrahub_sdk/testing/schemas/car_person.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/car_person.py#L108

Added line #L108 was not covered by tests

async def create_persons(self, client: InfrahubClient, branch: str) -> list[InfrahubNode]:
john = await client.create(kind=TESTING_PERSON, name="John Doe", branch=branch)
await john.save()

Check warning on line 112 in infrahub_sdk/testing/schemas/car_person.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/car_person.py#L110-L112

Added lines #L110 - L112 were not covered by tests

jane = await client.create(kind=TESTING_PERSON, name="Jane Doe", branch=branch)
await jane.save()

Check warning on line 115 in infrahub_sdk/testing/schemas/car_person.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/car_person.py#L114-L115

Added lines #L114 - L115 were not covered by tests

return [john, jane]

Check warning on line 117 in infrahub_sdk/testing/schemas/car_person.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/car_person.py#L117

Added line #L117 was not covered by tests

async def create_manufacturers(self, client: InfrahubClient, branch: str) -> list[InfrahubNode]:
obj1 = await client.create(kind=TESTING_MANUFACTURER, name="Volkswagen", branch=branch)
await obj1.save()

Check warning on line 121 in infrahub_sdk/testing/schemas/car_person.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/car_person.py#L119-L121

Added lines #L119 - L121 were not covered by tests

obj2 = await client.create(kind=TESTING_MANUFACTURER, name="Renault", branch=branch)
await obj2.save()

Check warning on line 124 in infrahub_sdk/testing/schemas/car_person.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/car_person.py#L123-L124

Added lines #L123 - L124 were not covered by tests

obj3 = await client.create(kind=TESTING_MANUFACTURER, name="Mercedes", branch=branch)
await obj3.save()

Check warning on line 127 in infrahub_sdk/testing/schemas/car_person.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/car_person.py#L126-L127

Added lines #L126 - L127 were not covered by tests

return [obj1, obj2, obj3]

Check warning on line 129 in infrahub_sdk/testing/schemas/car_person.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/car_person.py#L129

Added line #L129 was not covered by tests

async def create_initial_data(self, client: InfrahubClient, branch: str) -> dict[str, list[InfrahubNode]]:
persons = await self.create_persons(client=client, branch=branch)
manufacturers = await self.create_manufacturers(client=client, branch=branch)

Check warning on line 133 in infrahub_sdk/testing/schemas/car_person.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/car_person.py#L131-L133

Added lines #L131 - L133 were not covered by tests

car10 = await client.create(

Check warning on line 135 in infrahub_sdk/testing/schemas/car_person.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/car_person.py#L135

Added line #L135 was not covered by tests
kind=TESTING_CAR, name="Golf", color="Black", manufacturer=manufacturers[0].id, owner=persons[0].id
)
await car10.save()

Check warning on line 138 in infrahub_sdk/testing/schemas/car_person.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/car_person.py#L138

Added line #L138 was not covered by tests

car20 = await client.create(

Check warning on line 140 in infrahub_sdk/testing/schemas/car_person.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/car_person.py#L140

Added line #L140 was not covered by tests
kind=TESTING_CAR, name="Megane", color="Red", manufacturer=manufacturers[1].id, owner=persons[1].id
)
await car20.save()

Check warning on line 143 in infrahub_sdk/testing/schemas/car_person.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/car_person.py#L143

Added line #L143 was not covered by tests

return {TESTING_PERSON: persons, TESTING_CAR: [car10, car20], TESTING_MANUFACTURER: manufacturers}

Check warning on line 145 in infrahub_sdk/testing/schemas/car_person.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/testing/schemas/car_person.py#L145

Added line #L145 was not covered by tests
Loading
Loading