Skip to content
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

Add RH auth extension recommendation #237

Merged
merged 5 commits into from
Jun 26, 2024
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
1 change: 1 addition & 0 deletions src/ansible_creator/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
GLOBAL_TEMPLATE_VARS = {
"DEV_CONTAINER_IMAGE": "ghcr.io/ansible/community-ansible-dev-tools:latest",
"DEV_FILE_IMAGE": "ghcr.io/ansible/ansible-workspace-env-reference:latest",
"RECOMMENDED_EXTENSIONS": ["redhat.ansible", "redhat.vscode-redhat-account"],
}

MIN_COLLECTION_NAME_LEN = 2
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ansible-dev-container-codespaces",
"image": "{{ DEV_CONTAINER_IMAGE }}",
"image": "{{ dev_container_image }}",
"containerUser": "podman",
"runArgs": [
"--security-opt",
Expand All @@ -18,7 +18,7 @@
"updateRemoteUserUID": true,
"customizations": {
"vscode": {
"extensions": ["redhat.ansible"]
"extensions": {{ recommended_extensions | json }}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ansible-dev-container-docker",
"image": "{{ DEV_CONTAINER_IMAGE }}",
"image": "{{ dev_container_image }}",
"containerUser": "podman",
"runArgs": [
"--security-opt",
Expand All @@ -18,7 +18,7 @@
"updateRemoteUserUID": true,
"customizations": {
"vscode": {
"extensions": ["redhat.ansible"]
"extensions": {{ recommended_extensions | json }}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ansible-dev-container-podman",
"image": "{{ DEV_CONTAINER_IMAGE }}",
"image": "{{ dev_container_image }}",
"containerUser": "root",
"runArgs": [
"--cap-add=SYS_ADMIN",
Expand All @@ -20,7 +20,7 @@
],
"customizations": {
"vscode": {
"extensions": ["redhat.ansible"]
"extensions": {{ recommended_extensions | json }}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ metadata:
components:
- name: tooling-container
container:
image: {{ DEV_FILE_IMAGE }}
image: {{ dev_file_image }}
memoryRequest: 256M
memoryLimit: 6Gi
cpuRequest: 250m
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": {{ recommended_extensions | json }}
}

This file was deleted.

46 changes: 24 additions & 22 deletions src/ansible_creator/subcommands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from ansible_creator.exceptions import CreatorError
from ansible_creator.templar import Templar
from ansible_creator.types import TemplateData
from ansible_creator.utils import Copier


Expand Down Expand Up @@ -89,28 +90,31 @@ def run(self: Init) -> None: # noqa: C901
self.output.debug(msg=f"creating new directory at {self._init_path}")
self._init_path.mkdir(parents=True)

common_resources = [
"common.devcontainer",
"common.devfile",
"common.gitignore",
"common.vscode",
]

if self._project == "collection":
if not isinstance(self._collection_name, str):
msg = "Collection name is required when scaffolding a collection."
raise CreatorError(msg)
# copy new_collection container to destination, templating files when found
self.output.debug(msg="started copying collection skeleton to destination")
template_data = TemplateData(
namespace=self._namespace,
collection_name=self._collection_name,
creator_version=self._creator_version,
)
copier = Copier(
resources=[
"new_collection",
"common.devcontainer",
"common.devfile",
"common.gitignore",
],
resources=["new_collection", *common_resources],
resource_id="new_collection",
dest=self._init_path,
output=self.output,
templar=self._templar,
template_data={
"namespace": self._namespace,
"collection_name": self._collection_name,
"creator_version": self._creator_version,
},
template_data=template_data,
)
copier.copy_containers()

Expand All @@ -132,22 +136,20 @@ def run(self: Init) -> None: # noqa: C901
"scaffolding an ansible-project."
)
raise CreatorError(msg)

template_data = TemplateData(
creator_version=self._creator_version,
scm_org=self._scm_org,
scm_project=self._scm_project,
)

copier = Copier(
resources=[
"ansible_project",
"common.devcontainer",
"common.devfile",
"common.gitignore",
],
resources=["ansible_project", *common_resources],
resource_id="ansible_project",
dest=self._init_path,
output=self.output,
templar=self._templar,
template_data={
"scm_org": self._scm_org,
"scm_project": self._scm_project,
"creator_version": self._creator_version,
},
template_data=template_data,
)
copier.copy_containers()

Expand Down
11 changes: 8 additions & 3 deletions src/ansible_creator/templar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

from __future__ import annotations

import json

from dataclasses import asdict
from typing import TYPE_CHECKING


if TYPE_CHECKING:
from typing import Any
from ansible_creator.types import TemplateData


try:
from jinja2 import Environment, StrictUndefined
Expand Down Expand Up @@ -37,8 +41,9 @@ def __init__(self: Templar) -> None:
undefined=StrictUndefined,
keep_trailing_newline=True,
)
self.env.filters["json"] = json.dumps

def render_from_content(self: Templar, template: str, data: dict[str, Any]) -> str:
def render_from_content(self: Templar, template: str, data: TemplateData) -> str:
"""Render a template with provided data.

Args:
Expand All @@ -48,4 +53,4 @@ def render_from_content(self: Templar, template: str, data: dict[str, Any]) -> s
Returns:
Templated content.
"""
return self.env.from_string(template).render(data)
return self.env.from_string(template).render(asdict(data))
35 changes: 35 additions & 0 deletions src/ansible_creator/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""A home for shared types."""

from collections.abc import Sequence
from dataclasses import dataclass, field

from ansible_creator.constants import GLOBAL_TEMPLATE_VARS


@dataclass
class TemplateData:
"""Dataclass representing the template data.

Attributes:
additions: A dictionary containing additional data to add to the gitignore.
collection_name: The name of the collection.
creator_version: The version of the creator.
dev_container_image: The devcontainer image.
dev_file_image: The devfile image.
namespace: The namespace of the collection.
recommended_extensions: A list of recommended VsCode extensions.
scm_org: The organization of the source control management.
scm_project: The project of the source control management.
"""

additions: dict[str, dict[str, dict[str, str | bool]]] = field(default_factory=dict)
collection_name: str = ""
creator_version: str = ""
dev_container_image: Sequence[str] = GLOBAL_TEMPLATE_VARS["DEV_CONTAINER_IMAGE"]
dev_file_image: Sequence[str] = GLOBAL_TEMPLATE_VARS["DEV_FILE_IMAGE"]
namespace: str = ""
recommended_extensions: Sequence[str] = field(
default_factory=lambda: GLOBAL_TEMPLATE_VARS["RECOMMENDED_EXTENSIONS"],
)
scm_org: str = ""
scm_project: str = ""
27 changes: 14 additions & 13 deletions src/ansible_creator/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@

from __future__ import annotations

import copy
import os

from dataclasses import dataclass, field
from dataclasses import dataclass
from importlib import resources as impl_resources
from pathlib import Path
from typing import TYPE_CHECKING

import yaml

from ansible_creator.constants import GLOBAL_TEMPLATE_VARS, SKIP_DIRS, SKIP_FILES_TYPES
from ansible_creator.constants import SKIP_DIRS, SKIP_FILES_TYPES


if TYPE_CHECKING:

from ansible_creator.compat import Traversable
from ansible_creator.output import Output
from ansible_creator.templar import Templar
from ansible_creator.types import TemplateData


PATH_REPLACERS = {
"project_org": "scm_org",
Expand Down Expand Up @@ -69,22 +73,22 @@ class Copier:
resource_id: The id of the resource to copy.
dest: The destination path to copy resources to.
output: An instance of the Output class.
template_data: A dictionary containing the original data to render templates with.
allow_overwrite: A list of paths that should be overwritten at destination.
index: Index of the current resource being copied.
resource_root: Root path for the resources.
templar: An instance of the Templar class.
template_data: A dictionary containing the original data to render templates with.
"""

resources: list[str]
resource_id: str
dest: Path
output: Output
template_data: TemplateData
allow_overwrite: list[str] | None = None
index: int = 0
resource_root: str = "ansible_creator.resources"
templar: Templar | None = None
template_data: dict[str, str] = field(default_factory=dict)

@property
def resource(self: Copier) -> str:
Expand All @@ -94,7 +98,7 @@ def resource(self: Copier) -> str:
def _recursive_copy( # noqa: C901, PLR0912
self: Copier,
root: Traversable,
template_data: dict[str, str],
template_data: TemplateData,
) -> None:
"""Recursively traverses a resource container and copies content to destination.

Expand All @@ -118,7 +122,7 @@ def _recursive_copy( # noqa: C901, PLR0912
for key, val in PATH_REPLACERS.items():
if key in str(dest_path) and template_data:
str_dest_path = str(dest_path)
repl_val = template_data.get(val, "")
repl_val = getattr(template_data, val)
dest_path = Path(str_dest_path.replace(key, repl_val))

if obj.is_dir():
Expand Down Expand Up @@ -171,11 +175,8 @@ def _per_container(self: Copier) -> None:
)
self.output.debug(msg=f"allow_overwrite set to {self.allow_overwrite}")

# Include the global template variables
self.template_data.update(GLOBAL_TEMPLATE_VARS)

# Copy the template data to not pollute the original
template_data = self.template_data.copy()
# Cast the template data to not pollute the original
template_data = copy.deepcopy(self.template_data)

# Collect and template any resource specific variables
meta_file = impl_resources.files(f"{self.resource_root}.{self.resource}") / "__meta__.yml"
Expand All @@ -198,9 +199,9 @@ def _per_container(self: Copier) -> None:
data=template_data,
)
deserialized = yaml.safe_load(templated)
template_data.update({key: deserialized})
setattr(template_data, key, deserialized)
else:
template_data.update({key: value["value"]})
setattr(template_data, key, value["value"])

self._recursive_copy(
root=impl_resources.files(f"{self.resource_root}.{self.resource}"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"updateRemoteUserUID": true,
"customizations": {
"vscode": {
"extensions": ["redhat.ansible"]
"extensions": ["redhat.ansible", "redhat.vscode-redhat-account"]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"updateRemoteUserUID": true,
"customizations": {
"vscode": {
"extensions": ["redhat.ansible"]
"extensions": ["redhat.ansible", "redhat.vscode-redhat-account"]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
],
"customizations": {
"vscode": {
"extensions": ["redhat.ansible"]
"extensions": ["redhat.ansible", "redhat.vscode-redhat-account"]
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"recommendations": ["redhat.ansible"]
"recommendations": ["redhat.ansible", "redhat.vscode-redhat-account"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"updateRemoteUserUID": true,
"customizations": {
"vscode": {
"extensions": ["redhat.ansible"]
"extensions": ["redhat.ansible", "redhat.vscode-redhat-account"]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"updateRemoteUserUID": true,
"customizations": {
"vscode": {
"extensions": ["redhat.ansible"]
"extensions": ["redhat.ansible", "redhat.vscode-redhat-account"]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
],
"customizations": {
"vscode": {
"extensions": ["redhat.ansible"]
"extensions": ["redhat.ansible", "redhat.vscode-redhat-account"]
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"recommendations": ["redhat.ansible"]
"recommendations": ["redhat.ansible", "redhat.vscode-redhat-account"]
}
Loading
Loading