-
Notifications
You must be signed in to change notification settings - Fork 6
Add infrahubctl menu load
command to load menu items from a file
#73
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
3 commits
Select commit
Hold shift + click to select a range
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,55 @@ | ||
import logging | ||
from pathlib import Path | ||
|
||
import typer | ||
from rich.console import Console | ||
|
||
from infrahub_sdk.async_typer import AsyncTyper | ||
from infrahub_sdk.ctl.client import initialize_client | ||
from infrahub_sdk.ctl.utils import catch_exception, init_logging | ||
from infrahub_sdk.spec.menu import MenuFile | ||
|
||
from .parameters import CONFIG_PARAM | ||
from .utils import load_yamlfile_from_disk_and_exit | ||
|
||
app = AsyncTyper() | ||
console = Console() | ||
|
||
|
||
@app.callback() | ||
def callback() -> None: | ||
""" | ||
Manage the menu in a remote Infrahub instance. | ||
""" | ||
|
||
|
||
@app.command() | ||
@catch_exception(console=console) | ||
async def load( | ||
menus: list[Path], | ||
debug: bool = False, | ||
branch: str = typer.Option("main", help="Branch on which to load the menu."), | ||
_: str = CONFIG_PARAM, | ||
) -> None: | ||
"""Load one or multiple menu files into Infrahub.""" | ||
|
||
init_logging(debug=debug) | ||
|
||
logging.getLogger("infrahub_sdk").setLevel(logging.INFO) | ||
|
||
files = load_yamlfile_from_disk_and_exit(paths=menus, file_type=MenuFile, console=console) | ||
client = await initialize_client() | ||
|
||
for file in files: | ||
file.validate_content() | ||
schema = await client.schema.get(kind=file.spec.kind, branch=branch) | ||
|
||
for idx, item in enumerate(file.spec.data): | ||
await file.spec.create_node( | ||
client=client, | ||
schema=schema, | ||
data=item, | ||
branch=branch, | ||
default_schema_kind=file.spec.kind, | ||
context={"list_index": idx}, | ||
) |
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,48 @@ | ||
import logging | ||
from pathlib import Path | ||
|
||
import typer | ||
from rich.console import Console | ||
|
||
from infrahub_sdk.async_typer import AsyncTyper | ||
from infrahub_sdk.ctl.client import initialize_client | ||
from infrahub_sdk.ctl.utils import catch_exception, init_logging | ||
from infrahub_sdk.spec.object import ObjectFile | ||
|
||
from .parameters import CONFIG_PARAM | ||
from .utils import load_yamlfile_from_disk_and_exit | ||
|
||
app = AsyncTyper() | ||
console = Console() | ||
|
||
|
||
@app.callback() | ||
def callback() -> None: | ||
""" | ||
Manage objects in a remote Infrahub instance. | ||
""" | ||
|
||
|
||
@app.command() | ||
@catch_exception(console=console) | ||
async def load( | ||
paths: list[Path], | ||
debug: bool = False, | ||
branch: str = typer.Option("main", help="Branch on which to load the objects."), | ||
_: str = CONFIG_PARAM, | ||
) -> None: | ||
"""Load one or multiple objects files into Infrahub.""" | ||
|
||
init_logging(debug=debug) | ||
|
||
logging.getLogger("infrahub_sdk").setLevel(logging.INFO) | ||
|
||
files = load_yamlfile_from_disk_and_exit(paths=paths, file_type=ObjectFile, console=console) | ||
client = await initialize_client() | ||
|
||
for file in files: | ||
file.validate_content() | ||
schema = await client.schema.get(kind=file.spec.kind, branch=branch) | ||
|
||
for item in file.spec.data: | ||
await file.spec.create_node(client=client, schema=schema, data=item, branch=branch) | ||
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
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
Empty file.
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,35 @@ | ||
from typing import Optional | ||
|
||
from infrahub_sdk.yaml import InfrahubFile, InfrahubFileKind | ||
|
||
from .object import InfrahubObjectFileData | ||
|
||
|
||
class InfrahubMenuFileData(InfrahubObjectFileData): | ||
kind: str = "CoreMenuItem" | ||
|
||
@classmethod | ||
def enrich_node(cls, data: dict, context: dict) -> dict: | ||
if "kind" in data and "path" not in data: | ||
data["path"] = "/objects/" + data["kind"] | ||
|
||
if "list_index" in context and "order_weight" not in data: | ||
data["order_weight"] = (context["list_index"] + 1) * 1000 | ||
|
||
return data | ||
|
||
|
||
class MenuFile(InfrahubFile): | ||
_spec: Optional[InfrahubMenuFileData] = None | ||
|
||
@property | ||
def spec(self) -> InfrahubMenuFileData: | ||
if not self._spec: | ||
self._spec = InfrahubMenuFileData(**self.data.spec) | ||
return self._spec | ||
|
||
def validate_content(self) -> None: | ||
super().validate_content() | ||
if self.kind != InfrahubFileKind.MENU: | ||
raise ValueError("File is not an Infrahub Menu file") | ||
self._spec = InfrahubMenuFileData(**self.data.spec) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.