Skip to content

Commit 5de1743

Browse files
authored
Add pattern API endpoint (#587)
Also bumps the minimum required version of ansible-creator to support the new functionality
1 parent 3e3ce34 commit 5de1743

File tree

8 files changed

+110
-9
lines changed

8 files changed

+110
-9
lines changed

.config/constraints.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# This file was autogenerated by uv via the following command:
22
# tox run -e deps
33
ansible-builder==3.1.0
4-
ansible-compat==25.5.0
5-
ansible-core==2.18.6
6-
ansible-creator==25.5.0
4+
ansible-compat==25.6.0
5+
ansible-core==2.18.7
6+
ansible-creator==25.7.0
77
ansible-dev-environment==25.5.0
88
ansible-lint==25.5.0
99
ansible-navigator==25.5.0

.config/requirements-lock.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# This file was autogenerated by uv via the following command:
22
# tox run -e deps
33
ansible-builder==3.1.0
4-
ansible-compat==25.5.0
5-
ansible-core==2.18.6
6-
ansible-creator==25.5.0
4+
ansible-compat==25.6.0
5+
ansible-core==2.18.7
6+
ansible-creator==25.7.0
77
ansible-dev-environment==25.5.0
88
ansible-lint==25.5.0
99
ansible-navigator==25.5.0

.config/requirements.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ansible-builder
22
ansible-compat>=25.1.4
3-
ansible-creator>=24.10.1
3+
ansible-creator>=25.7.0
44
ansible-dev-environment
55
ansible-lint>=25.1.3
66
ansible-navigator

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ repos:
9999
args:
100100
- --output-format=colorized
101101
additional_dependencies:
102-
- ansible-creator
102+
- ansible-creator>=25.7.0
103103
- ansible-navigator
104104
- django
105105
- gunicorn
@@ -113,7 +113,7 @@ repos:
113113
hooks:
114114
- id: mypy
115115
additional_dependencies:
116-
- ansible-creator
116+
- ansible-creator>=25.7.0
117117
- ansible-navigator
118118
- django-stubs
119119
- jinja2

src/ansible_dev_tools/resources/server/creator_v2.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,27 @@ def devfile(
129129
response=response,
130130
)
131131

132+
def pattern(self, request: HttpRequest) -> FileResponse | HttpResponse:
133+
"""Add a pattern.
134+
135+
Args:
136+
request: HttpRequest object.
137+
138+
Returns:
139+
File or error response.
140+
"""
141+
result = validate_request(request)
142+
if isinstance(result, HttpResponse):
143+
return result
144+
with tempfile.TemporaryDirectory() as tmp_dir:
145+
tar_file = CreatorBackend(Path(tmp_dir)).pattern(**result.body) # type: ignore[arg-type]
146+
response = self._response_from_tar(tar_file)
147+
148+
return validate_response(
149+
request=request,
150+
response=response,
151+
)
152+
132153

133154
class CreatorOutput(Output):
134155
"""The creator output."""
@@ -238,3 +259,29 @@ def devfile(self) -> Path:
238259
tar_file = self.tmp_dir / "devfile.tar"
239260
create_tar_file(add_path, tar_file)
240261
return tar_file
262+
263+
def pattern(self, pattern_name: str) -> Path:
264+
"""Scaffold a pattern.
265+
266+
Args:
267+
pattern_name: Name of the pattern to add.
268+
269+
Returns:
270+
The tar file path.
271+
"""
272+
# Path where the pattern will be scaffolded.
273+
add_path = self.tmp_dir / pattern_name
274+
add_path.mkdir(parents=True, exist_ok=True)
275+
276+
config = Config(
277+
creator_version=creator_version,
278+
path=str(add_path),
279+
output=CreatorOutput(log_file=str(self.tmp_dir / "creator.log")),
280+
subcommand="add",
281+
resource_type="pattern",
282+
overwrite=True,
283+
)
284+
Add(config, skip_collection_check=True).run()
285+
tar_file = self.tmp_dir / f"{pattern_name}.tar"
286+
create_tar_file(add_path, tar_file)
287+
return tar_file

src/ansible_dev_tools/resources/server/data/openapi.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,28 @@ paths:
118118
application/json:
119119
schema:
120120
$ref: "#/components/schemas/Error"
121+
/v2/creator/pattern:
122+
post:
123+
summary: Add a pattern to an existing collection
124+
requestBody:
125+
content:
126+
application/json:
127+
schema:
128+
$ref: "#/components/schemas/CreatorPattern"
129+
required: true
130+
responses:
131+
"201":
132+
description: Created
133+
content:
134+
application/tar:
135+
schema:
136+
AnyValue: {}
137+
"400":
138+
description: Bad Request
139+
content:
140+
application/json:
141+
schema:
142+
$ref: "#/components/schemas/Error"
121143

122144
components:
123145
schemas:
@@ -142,6 +164,12 @@ components:
142164
type: string
143165
project:
144166
type: string
167+
CreatorPattern:
168+
type: object
169+
additionalProperties: false
170+
properties:
171+
pattern_name:
172+
type: string
145173
CreatorPlaybook:
146174
type: object
147175
additionalProperties: false

src/ansible_dev_tools/subcommands/server.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
path(route="v2/creator/playbook", view=CreatorFrontendV2().playbook),
2929
path(route="v2/creator/collection", view=CreatorFrontendV2().collection),
3030
path(route="v2/creator/devfile", view=CreatorFrontendV2().devfile),
31+
path(route="v2/creator/pattern", view=CreatorFrontendV2().pattern),
3132
)
3233

3334

tests/integration/test_server_creator_v2.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,28 @@ def test_devfile_v2(server_url: str, tmp_path: Path) -> None:
121121
tar_file.write(response.content)
122122
with tarfile.open(dest_file) as file:
123123
assert "./devfile.yaml" in file.getnames()
124+
125+
126+
def test_pattern_v2(server_url: str, tmp_path: Path) -> None:
127+
"""Test the pattern creation.
128+
129+
Args:
130+
server_url: The server URL.
131+
tmp_path: Pytest tmp_path fixture.
132+
"""
133+
pattern_name = "foo"
134+
response = requests.post(
135+
f"{server_url}/v2/creator/pattern",
136+
json={
137+
"pattern_name": pattern_name,
138+
},
139+
timeout=10,
140+
)
141+
assert response.status_code == requests.codes.get("created")
142+
assert response.headers["Content-Disposition"] == f'attachment; filename="{pattern_name}.tar"'
143+
assert response.headers["Content-Type"] == "application/tar"
144+
dest_file = tmp_path / f"{pattern_name}.tar"
145+
with dest_file.open(mode="wb") as tar_file:
146+
tar_file.write(response.content)
147+
with tarfile.open(dest_file) as file:
148+
assert "./extensions" in file.getnames()

0 commit comments

Comments
 (0)