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
43 changes: 43 additions & 0 deletions src/ansible_dev_tools/resources/server/creator_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,30 @@ def devfile(
response=response,
)

def ee_project(
self,
request: HttpRequest,
) -> FileResponse | HttpResponse:
"""Create a new execution environment project.

Args:
request: HttpRequest object.

Returns:
File or error response.
"""
result = validate_request(request)
if isinstance(result, HttpResponse):
return result
with tempfile.TemporaryDirectory() as tmp_dir:
tar_file = CreatorBackend(Path(tmp_dir)).ee_project()
response = self._response_from_tar(tar_file)

return validate_response(
request=request,
response=response,
)


class CreatorOutput(Output):
"""The creator output."""
Expand Down Expand Up @@ -238,3 +262,22 @@ def devfile(self) -> Path:
tar_file = self.tmp_dir / "devfile.tar"
create_tar_file(add_path, tar_file)
return tar_file

def ee_project(self) -> Path:
"""Scaffold an execution environment project.

Returns:
The tar file path.
"""
init_path = self.tmp_dir / "ee_project"
config = Config(
creator_version=creator_version,
init_path=str(init_path),
output=CreatorOutput(log_file=str(self.tmp_dir / "creator.log")),
project="execution_env",
subcommand="init",
)
Init(config).run()
tar_file = self.tmp_dir / "ee_project.tar"
create_tar_file(init_path, tar_file)
return tar_file
16 changes: 16 additions & 0 deletions src/ansible_dev_tools/resources/server/data/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,22 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/Error"
/v2/creator/ee_project:
post:
summary: Create a new execution environment project
responses:
"201":
description: Created
content:
application/tar:
schema:
AnyValue: {}
"400":
description: Bad Request
content:
application/json:
schema:
$ref: "#/components/schemas/Error"

components:
schemas:
Expand Down
1 change: 1 addition & 0 deletions src/ansible_dev_tools/subcommands/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
path(route="v2/creator/playbook", view=CreatorFrontendV2().playbook),
path(route="v2/creator/collection", view=CreatorFrontendV2().collection),
path(route="v2/creator/devfile", view=CreatorFrontendV2().devfile),
path(route="v2/creator/ee_project", view=CreatorFrontendV2().ee_project),
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,38 @@ def test_devfile_v2(server_url: str, tmp_path: Path) -> None:
tar_file.write(response.content)
with tarfile.open(dest_file) as file:
assert "./devfile.yaml" in file.getnames()


def test_error_ee_project_v2(server_url: str) -> None:
"""Test the error response when a GET request is sent to the ee_project endpoint.

Args:
server_url: The server URL.
"""
response = requests.get(f"{server_url}/v2/creator/ee_project", timeout=10)
assert response.status_code == requests.codes.get("bad_request"), (
f"Expected 400 but got {response.status_code}"
)
assert response.text == "Operation get not found for " + f"{server_url}/v2/creator/ee_project"


def test_ee_project_v2(server_url: str, tmp_path: Path) -> None:
"""Test the execution environment project creation.

Args:
server_url: The server URL.
tmp_path: Pytest tmp_path fixture.
"""
response = requests.post(
f"{server_url}/v2/creator/ee_project",
json={},
timeout=10,
)
assert response.status_code == requests.codes.get("created")
assert response.headers["Content-Disposition"] == 'attachment; filename="ee_project.tar"'
assert response.headers["Content-Type"] == "application/tar"
dest_file = tmp_path / "ee_project.tar"
with dest_file.open(mode="wb") as tar_file:
tar_file.write(response.content)
with tarfile.open(dest_file) as file:
assert "./execution-environment.yml" in file.getnames()
Loading