-
Notifications
You must be signed in to change notification settings - Fork 59
feat: add dynamic creator API endpoints for schema-driven scaffolding #676
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
Open
cidrblock
wants to merge
1
commit into
ansible:main
Choose a base branch
from
cidrblock:dynamic_api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+499
−0
Open
Changes from all commits
Commits
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
163 changes: 163 additions & 0 deletions
163
src/ansible_dev_tools/resources/server/creator_dynamic.py
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,163 @@ | ||
| """Dynamic, schema-driven creator API endpoints.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import shutil | ||
|
|
||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| # pylint: disable-next=import-error,no-name-in-module | ||
| from ansible_creator.api import V1 # type: ignore[import-not-found] | ||
| from django.core.files.storage import FileSystemStorage | ||
| from django.http import FileResponse, HttpRequest, HttpResponse, JsonResponse | ||
|
|
||
| from ansible_dev_tools.resources.server.creator_v2 import create_tar_file | ||
| from ansible_dev_tools.server_utils import validate_request, validate_response | ||
|
|
||
|
|
||
| if TYPE_CHECKING: | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| class CreatorDynamic: | ||
| """Dynamic creator endpoints driven by ansible-creator's V1 API. | ||
|
|
||
| Provides discovery, schema inspection, and generic scaffolding | ||
| without hardcoding individual project types. | ||
| """ | ||
|
|
||
| def _response_from_tar(self, tar_file: Path) -> FileResponse: | ||
| """Create a FileResponse from a tar file. | ||
|
|
||
| Args: | ||
| tar_file: The tar file path. | ||
|
|
||
| Returns: | ||
| The file response. | ||
| """ | ||
| fs = FileSystemStorage(str(tar_file.parent)) | ||
| response = FileResponse( | ||
| fs.open(tar_file.name, "rb"), | ||
| content_type="application/tar", | ||
| status=201, | ||
| ) | ||
| response["Content-Disposition"] = f'attachment; filename="{tar_file.name}"' | ||
| return response | ||
|
|
||
| def capabilities(self, request: HttpRequest) -> JsonResponse | HttpResponse: | ||
| """Return the full ansible-creator capability tree. | ||
|
|
||
| Args: | ||
| request: HttpRequest object. | ||
|
|
||
| Returns: | ||
| JSON response with the capability schema. | ||
| """ | ||
| result = validate_request(request) | ||
| if isinstance(result, HttpResponse): | ||
| return result | ||
| api = V1() | ||
| return JsonResponse(api.schema(), status=200) | ||
|
|
||
| def schema(self, request: HttpRequest) -> JsonResponse | HttpResponse: | ||
| """Return the parameter schema for a specific command path. | ||
|
|
||
| The command path is provided via repeated ``command_path`` query | ||
| parameters, e.g. ``?command_path=init&command_path=collection``. | ||
|
|
||
| Args: | ||
| request: HttpRequest object. | ||
|
|
||
| Returns: | ||
| JSON response with the command schema, or 400 on error. | ||
| """ | ||
| result = validate_request(request) | ||
| if isinstance(result, HttpResponse): | ||
| return result | ||
| path_segments = request.GET.getlist("command_path") | ||
| if not path_segments: | ||
| return HttpResponse( | ||
| "Missing required query parameter: command_path", | ||
| status=400, | ||
| ) | ||
| try: | ||
| api = V1() | ||
| schema_result = api.schema_for(*path_segments) | ||
| except KeyError as exc: | ||
| return JsonResponse({"error": str(exc)}, status=400) | ||
| return JsonResponse(schema_result, status=200) | ||
|
|
||
| def scaffold(self, request: HttpRequest) -> FileResponse | HttpResponse: | ||
| """Scaffold an ansible-creator project dynamically. | ||
|
|
||
| Accepts a JSON body with ``command_path`` (list of strings) and | ||
| optional ``params`` (dict). Delegates to ``V1().run()`` and returns | ||
| the scaffolded content as a tar archive. | ||
|
|
||
| On success, logs are included in ``X-Creator-Logs`` and | ||
| ``X-Creator-Message`` response headers. | ||
|
|
||
| On error, returns a JSON body with ``status``, ``message``, | ||
| and ``logs``. | ||
|
|
||
| Args: | ||
| request: HttpRequest object. | ||
|
|
||
| Returns: | ||
| Tar file response on success, or JSON/HTTP error response. | ||
| """ | ||
| result = validate_request(request) | ||
| if isinstance(result, HttpResponse): | ||
| return result | ||
|
|
||
| body: dict[str, Any] = result.body # type: ignore[assignment] | ||
| command_path: list[str] = body.get("command_path", []) | ||
| params: dict[str, Any] = body.get("params", {}) | ||
|
|
||
| if not command_path: | ||
| return JsonResponse( | ||
| {"status": "error", "message": "Missing command_path", "logs": []}, | ||
| status=400, | ||
| ) | ||
|
|
||
| api = V1() | ||
| creator_result = api.run(*command_path, **params) | ||
|
|
||
| if creator_result.status == "error": | ||
| # Clean up the temp directory on error | ||
| if creator_result.path: | ||
| shutil.rmtree(creator_result.path, ignore_errors=True) | ||
| return JsonResponse( | ||
| { | ||
| "status": "error", | ||
| "message": creator_result.message, | ||
| "logs": creator_result.logs, | ||
| }, | ||
| status=400, | ||
| ) | ||
|
|
||
| if creator_result.path is None: | ||
| return JsonResponse( | ||
| { | ||
| "status": "error", | ||
| "message": "No output path", | ||
| "logs": creator_result.logs, | ||
| }, | ||
| status=400, | ||
| ) | ||
|
|
||
| try: | ||
| tar_name = f"{'_'.join(command_path)}.tar" | ||
| tar_file = creator_result.path.parent / tar_name | ||
| create_tar_file(creator_result.path, tar_file) | ||
| response = self._response_from_tar(tar_file) | ||
| response["X-Creator-Logs"] = json.dumps(creator_result.logs) | ||
| response["X-Creator-Message"] = creator_result.message | ||
| finally: | ||
| shutil.rmtree(creator_result.path, ignore_errors=True) | ||
|
|
||
| return validate_response( | ||
| request=request, | ||
| response=response, | ||
| ) | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.