Skip to content

Commit d1b03cc

Browse files
authored
Cleanup of init functionality (#290)
* Unsplit collection internally * Use playbook internally instead of ansible-project
1 parent 5c501c4 commit d1b03cc

File tree

12 files changed

+42
-84
lines changed

12 files changed

+42
-84
lines changed

src/ansible_creator/arg_parser.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,6 @@ def parse_args(self: Parser) -> tuple[argparse.Namespace, list[Msg]]:
103103
self.pending_logs.append(Msg(prefix=Level.CRITICAL, message="Goodbye."))
104104
return self.args, self.pending_logs
105105

106-
# The internal still reference the old project name
107-
if self.args.project == "playbook":
108-
self.args.project = "ansible-project"
109-
with contextlib.suppress(ValueError):
110-
self.args.scm_org, self.args.scm_project = self.args.collection.split(
111-
".",
112-
maxsplit=1,
113-
)
114-
self.args.collection = None
115-
116106
return self.args, self.pending_logs
117107

118108
def _add(self: Parser, subparser: SubParser[ArgumentParser]) -> None:

src/ansible_creator/config.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ class Config:
2626
force: Whether to overwrite existing files.
2727
init_path: The path to initialize the project.
2828
project: The type of project to scaffold.
29-
scm_org: The SCM organization for the project.
30-
scm_project: The SCM project for the project.
3129
collection_name: The name of the collection.
3230
namespace: The namespace for the collection.
3331
"""
@@ -40,8 +38,6 @@ class Config:
4038
force: bool = False
4139
init_path: str | Path = "./"
4240
project: str = ""
43-
scm_org: str | None = None
44-
scm_project: str | None = None
4541
collection_name: str | None = None
4642
namespace: str = ""
4743

src/ansible_creator/resources/common/gitignore/__meta__.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ playbook_project:
1414
!collections/ansible_collections
1515
!collections/requirements.yml
1616
collections/ansible_collections/*
17-
!collections/ansible_collections/{{ scm_org}}
18-
collections/ansible_collections/{{ scm_org }}/*
19-
!collections/ansible_collections/{{ scm_org }}/{{ scm_project }}
17+
!collections/ansible_collections/{{ namespace }}
18+
collections/ansible_collections/{{ namespace }}/*
19+
!collections/ansible_collections/{{ namespace }}/{{ collection_name }}

src/ansible_creator/resources/playbook_project/README.md.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# {{ scm_org|capitalize }} {{ scm_project|capitalize }} Ansible Project
1+
# {{ namespace|capitalize }} {{ collection_name|capitalize }} Ansible Project
22

33
## Included content/ Directory Structure
44

src/ansible_creator/resources/playbook_project/collections/ansible_collections/project_org/project_repo/README.md.j2

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# {{ scm_org|capitalize }} {{ scm_project|capitalize }} Collection
1+
# {{ namespace|capitalize }} {{ collection_name|capitalize }} Collection
22

3-
This repository contains the `{{ scm_org }}.{{ scm_project }}` Ansible Collection.
3+
This repository contains the `{{ namespace }}.{{ collection_name }}` Ansible Collection.
44

55
## Tested with Ansible
66

@@ -12,31 +12,31 @@ Some modules and plugins require external libraries. Please check the requiremen
1212

1313
## Included content
1414

15-
Please check the included content on the [Ansible Galaxy page for this collection](https://galaxy.ansible.com/{{ scm_org }}/{{ scm_project }}).
15+
Please check the included content on the [Ansible Galaxy page for this collection](https://galaxy.ansible.com/{{ namespace }}/{{ collection_name }}).
1616

1717
## Using this collection
1818

1919
```
20-
ansible-galaxy collection install {{ scm_org }}.{{ scm_project }}
20+
ansible-galaxy collection install {{ namespace }}.{{ collection_name }}
2121
```
2222

2323
You can also include it in a `requirements.yml` file and install it via `ansible-galaxy collection install -r requirements.yml` using the format:
2424

2525
```yaml
2626
collections:
27-
- name: {{ scm_org }}.{{ scm_project }}
27+
- name: {{ namespace }}.{{ collection_name }}
2828
```
2929

3030
To upgrade the collection to the latest available version, run the following command:
3131

3232
```bash
33-
ansible-galaxy collection install {{ scm_org }}.{{ scm_project }} --upgrade
33+
ansible-galaxy collection install {{ namespace }}.{{ collection_name }} --upgrade
3434
```
3535

36-
You can also install a specific version of the collection, for example, if you need to downgrade when something is broken in the latest version (please report an issue in this repository). Use the following syntax where `X.Y.Z` can be any [available version](https://galaxy.ansible.com/{{ scm_org }}/{{ scm_project }}):
36+
You can also install a specific version of the collection, for example, if you need to downgrade when something is broken in the latest version (please report an issue in this repository). Use the following syntax where `X.Y.Z` can be any [available version](https://galaxy.ansible.com/{{ namespace }}/{{ collection_name }}):
3737

3838
```bash
39-
ansible-galaxy collection install {{ scm_org }}.{{ scm_project }}:==X.Y.Z
39+
ansible-galaxy collection install {{ namespace }}.{{ collection_name }}:==X.Y.Z
4040
```
4141

4242
See [Ansible Using Collections](https://docs.ansible.com/ansible/latest/user_guide/collections_using.html) for more details.

src/ansible_creator/resources/playbook_project/collections/ansible_collections/project_org/project_repo/roles/run/README.md.j2

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{ scm_org|capitalize }}.{{ scm_project|capitalize }} Run Role
1+
{{ namespace|capitalize }}.{{ collection_name|capitalize }} Run Role
22
========================
33

44
A brief description of the role is here.
@@ -27,20 +27,20 @@ Including an example of how to use your role (for instance, with variables passe
2727
- name: Execute tasks on servers
2828
hosts: servers
2929
roles:
30-
- role: {{ scm_org }}.{{ scm_project }}.run
30+
- role: {{ namespace }}.{{ collection_name }}.run
3131
run_x: 42
3232
```
3333

3434
Another way to consume this role would be:
3535

3636
```yaml
37-
- name: Initialize the run role from {{ scm_org }}.{{ scm_project }}
37+
- name: Initialize the run role from {{ namespace }}.{{ collection_name }}
3838
hosts: servers
3939
gather_facts: false
4040
tasks:
4141
- name: Trigger invocation of run role
4242
ansible.builtin.include_role:
43-
name: {{ scm_org }}.{{ scm_project }}.run
43+
name: {{ namespace }}.{{ collection_name }}.run
4444
vars:
4545
run_x: 42
4646
```

src/ansible_creator/resources/playbook_project/site.yml.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
- name: Example playbook
33
hosts: localhost
44
roles:
5-
- role: {{ scm_org }}.{{ scm_project }}.run
5+
- role: {{ namespace }}.{{ collection_name }}.run

src/ansible_creator/subcommands/init.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ def __init__(
4949
self._force = config.force
5050
self._creator_version = config.creator_version
5151
self._project = config.project
52-
self._scm_org = config.scm_org or ""
53-
self._scm_project = config.scm_project or ""
5452
self._templar = Templar()
5553
self.output: Output = config.output
5654

@@ -65,12 +63,12 @@ def run(self: Init) -> None:
6563

6664
if self._project == "collection":
6765
self._scaffold_collection()
68-
elif self._project == "ansible-project":
66+
elif self._project == "playbook":
6967
self._scaffold_playbook()
7068

7169
def _construct_init_path(self: Init) -> None:
7270
"""Construct the init path based on project type."""
73-
if self._project == "ansible-project":
71+
if self._project == "playbook":
7472
return
7573

7674
if (
@@ -117,11 +115,7 @@ def unique_name_in_devfile(self) -> str:
117115
Returns:
118116
Unique name entry.
119117
"""
120-
final_name: str
121-
if self._project == "collection":
122-
final_name = f"{self._namespace}.{self._collection_name}"
123-
if self._project == "ansible-project":
124-
final_name = f"{self._scm_org}.{self._scm_project}"
118+
final_name = f"{self._namespace}.{self._collection_name}"
125119
final_uuid = str(uuid.uuid4())[:8]
126120
return f"{final_name}-{final_uuid}"
127121

@@ -151,12 +145,12 @@ def _scaffold_collection(self) -> None:
151145

152146
def _scaffold_playbook(self: Init) -> None:
153147
"""Scaffold a playbook project."""
154-
self.output.debug(msg="started copying ansible-project skeleton to destination")
148+
self.output.debug(msg="started copying playbook skeleton to destination")
155149

156150
template_data = TemplateData(
157151
creator_version=self._creator_version,
158-
scm_org=self._scm_org,
159-
scm_project=self._scm_project,
152+
namespace=self._namespace,
153+
collection_name=self._collection_name,
160154
dev_file_name=self.unique_name_in_devfile(),
161155
)
162156

src/ansible_creator/types.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ class TemplateData:
2525
dev_file_name: The unique name entry in devfile.
2626
namespace: The namespace of the collection.
2727
recommended_extensions: A list of recommended VsCode extensions.
28-
scm_org: The organization of the source control management.
29-
scm_project: The project of the source control management.
3028
"""
3129

3230
additions: dict[str, dict[str, dict[str, str | bool]]] = field(default_factory=dict)
@@ -39,5 +37,3 @@ class TemplateData:
3937
recommended_extensions: Sequence[str] = field(
4038
default_factory=lambda: GLOBAL_TEMPLATE_VARS["RECOMMENDED_EXTENSIONS"],
4139
)
42-
scm_org: str = ""
43-
scm_project: str = ""

src/ansible_creator/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525

2626
PATH_REPLACERS = {
27-
"project_org": "scm_org",
28-
"project_repo": "scm_project",
27+
"project_org": "namespace",
28+
"project_repo": "collection_name",
2929
}
3030

3131

tests/units/test_basic.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,10 @@ def test_configuration_class(output: Output) -> None:
7373
"log_append": "true",
7474
"json": False,
7575
"verbose": 0,
76-
"collection": None,
76+
"collection": "weather.demo",
7777
"init_path": f"{Path.home()}/my-ansible-project",
7878
"force": False,
79-
"project": "ansible-project",
80-
"scm_org": "weather",
81-
"scm_project": "demo",
79+
"project": "playbook",
8280
},
8381
],
8482
[
@@ -133,12 +131,10 @@ def test_configuration_class(output: Output) -> None:
133131
"log_append": "false",
134132
"json": True,
135133
"verbose": 3,
136-
"collection": None,
134+
"collection": "weather.demo",
137135
"init_path": f"{Path.home()}/my-ansible-project",
138136
"force": True,
139-
"project": "ansible-project",
140-
"scm_org": "weather",
141-
"scm_project": "demo",
137+
"project": "playbook",
142138
},
143139
],
144140
[
@@ -175,10 +171,8 @@ def test_configuration_class(output: Output) -> None:
175171
],
176172
{
177173
"subcommand": "init",
178-
"project": "ansible-project",
179-
"scm_org": "foo",
180-
"scm_project": "bar",
181-
"collection": None,
174+
"project": "playbook",
175+
"collection": "foo.bar",
182176
"init_path": "/test/test",
183177
"force": False,
184178
"json": False,

tests/units/test_init.py

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ class ConfigDict(TypedDict):
3131
init_path: Path to initialize the project.
3232
project: The type of project to scaffold.
3333
force: Force overwrite of existing directory.
34-
scm_org: The SCM organization for the project.
35-
scm_project: The SCM project for the project.
3634
"""
3735

3836
creator_version: str
@@ -42,8 +40,6 @@ class ConfigDict(TypedDict):
4240
init_path: str
4341
project: str
4442
force: bool
45-
scm_org: str | None
46-
scm_project: str | None
4743

4844

4945
@pytest.fixture(name="cli_args")
@@ -65,8 +61,6 @@ def fixture_cli_args(tmp_path: Path, output: Output) -> ConfigDict:
6561
"init_path": str(tmp_path / "testorg" / "testcol"),
6662
"project": "",
6763
"force": False,
68-
"scm_org": "",
69-
"scm_project": "",
7064
}
7165

7266

@@ -160,27 +154,25 @@ def test_run_success_ansible_project(
160154
) -> None:
161155
"""Test Init.run().
162156
163-
Successfully create new ansible-project
157+
Successfully create new playbook project
164158
165159
Args:
166160
capsys: Pytest fixture to capture stdout and stderr.
167161
tmp_path: Temporary directory path.
168162
cli_args: Dictionary, partial Init class object.
169163
monkeypatch: Pytest monkeypatch fixture.
170164
"""
171-
cli_args["collection"] = ""
172-
cli_args["project"] = "ansible-project"
165+
cli_args["collection"] = "weather.demo"
166+
cli_args["project"] = "playbook"
173167
cli_args["init_path"] = str(tmp_path / "new_project")
174-
cli_args["scm_org"] = "weather"
175-
cli_args["scm_project"] = "demo"
176168
init = Init(
177169
Config(**cli_args),
178170
)
179171

180172
# Mock the "unique_name_in_devfile" method
181173
def mock_unique_name_in_devfile(self: Init) -> str:
182-
coll_namespace = self._scm_org
183-
coll_name = self._scm_project
174+
coll_namespace = self._namespace
175+
coll_name = self._collection_name
184176
return f"{coll_namespace}.{coll_name}"
185177

186178
# Apply the mock
@@ -204,7 +196,7 @@ def mock_unique_name_in_devfile(self: Init) -> str:
204196
diff = has_differences(dcmp=cmp, errors=[])
205197
assert diff == [], diff
206198

207-
# fail to override existing ansible-project directory with force=false (default)
199+
# fail to override existing playbook directory with force=false (default)
208200
fail_msg = (
209201
f"The directory {tmp_path}/new_project is not empty."
210202
"\nYou can use --force to re-initialize this directory."
@@ -213,7 +205,7 @@ def mock_unique_name_in_devfile(self: Init) -> str:
213205
with pytest.raises(CreatorError, match=fail_msg):
214206
init.run()
215207

216-
# override existing ansible-project directory with force=true
208+
# override existing playbook directory with force=true
217209
cli_args["force"] = True
218210
init = Init(
219211
Config(**cli_args),
@@ -345,15 +337,13 @@ def fixture_collection_project(tmp_path: Path, output: Output) -> Config:
345337
force=False,
346338
creator_version="1.0.0",
347339
project="collection",
348-
scm_org="",
349-
scm_project="",
350340
output=output,
351341
)
352342

353343

354344
@pytest.fixture(name="cli_args_playbook")
355345
def fixture_playbook_project(tmp_path: Path, output: Output) -> Config:
356-
"""Fixture for Config object with ansible-project.
346+
"""Fixture for Config object with playbook.
357347
358348
Args:
359349
tmp_path: Temporary directory path.
@@ -364,14 +354,12 @@ def fixture_playbook_project(tmp_path: Path, output: Output) -> Config:
364354
"""
365355
return Config(
366356
subcommand="init",
367-
namespace="",
368-
collection_name="",
357+
namespace="foo",
358+
collection_name="bar",
369359
init_path=str(tmp_path / "test_path"),
370360
force=False,
371361
creator_version="1.0.0",
372-
project="ansible-project",
373-
scm_org="foo",
374-
scm_project="bar",
362+
project="playbook",
375363
output=output,
376364
)
377365

0 commit comments

Comments
 (0)