# Generated by tosca.yaml2python from unfurl/tosca_plugins/artifacts.yaml at 2024-02-28T08:06:46 overwrite not modified (change to "overwrite ok" to allow)

import unfurl
from typing import List, Dict, Any, Tuple, Union, Sequence
from typing_extensions import Annotated
from tosca import (
    Artifact,
    ArtifactType,
    Attribute,
    AttributeOptions,
    CONSTRAINED,
    Capability,
    Computed,
    DEFAULT,
    Eval,
    MISSING,
    NodeType,
    Property,
    PropertyOptions,
    Requirement,
    ToscaInputs,
    ToscaOutputs,
    operation,
)
import tosca
import unfurl.configurators
import unfurl.configurators.terraform


class artifact_AsdfTool(unfurl.artifacts.ShellExecutable):
    _type_name = "artifact.AsdfTool"
    version: Union[str, None] = "latest"

    @operation(apply_to=["Standard.configure", "Standard.delete"])
    def default(self, **kw: Any) -> Any:
        return unfurl.configurators.CmdConfigurator(
            cmd=Eval(
                (
                    'if ! [ -x "$(command -v asdf)" ]; then\n'
                    '  ASDF_REPO={{ "asdf" | get_dir }}\n'
                    '  export ASDF_DATA_DIR="${ASDF_DATA_DIR:-$ASDF_REPO}"\n'
                    "  source $ASDF_REPO/asdf.sh\n"
                    "fi\n"
                    "asdf plugin add {{SELF.file}}\n"
                    "asdf {%if task.operation == 'delete' %}uninstall{%else%}install{%endif%} "
                    "{{SELF.file}} {{SELF.version}}\n"
                    "{%if task.operation != 'delete' %}asdf local {{SELF.file}} "
                    "{{SELF.version}}{%endif%}\n"
                )
            ),
            cwd=Eval('{{ "project" | get_dir }}'),
            keeplines=True,
            shell=Eval('{{ "bash" | which }}'),
            resultTemplate=Eval(
                {
                    "to_env": {
                        "^PATH": '{{ lookup("env", "ASDF_DATA_DIR") }}/installs/{{ '
                        "SELF.file}}/{{SELF.version}}/bin }}"
                    },
                    "update_os_environ": True,
                }
            ),
        )


class artifact_PythonPackage(tosca.artifacts.Root):
    _type_name = "artifact.PythonPackage"
    version: str = ""
    """Full version specifier (e.g. "==1.0")"""

    def check(self, **kw: Any) -> Any:
        return unfurl.configurators.PythonPackageCheckConfigurator()

    def create(self, **kw: Any) -> Any:
        return unfurl.configurators.CmdConfigurator(
            cmd=Eval(
                (
                    "{%if lookup('env', 'VIRTUAL_ENV') %}pipenv --bare{% else %}{{ "
                    "__python_executable }} -m pip{% endif %} install "
                    "'{{SELF.file}}{{SELF.version}}'"
                )
            ),
        )

    def delete(self, **kw: Any) -> Any:
        return unfurl.configurators.CmdConfigurator(
            cmd=Eval(
                (
                    "{%if lookup('env', 'VIRTUAL_ENV') %}pipenv --bare{% else %}{{ "
                    "__python_executable }} -m pip{% endif %} uninstall "
                    "'{{SELF.file}}{{SELF.version}}'"
                )
            ),
        )


class artifact_AnsibleCollection(tosca.artifacts.Root):
    _type_name = "artifact.AnsibleCollection"
    version: Union[str, None] = None

    def check(self, **kw: Any) -> Any:
        return unfurl.configurators.CmdConfigurator(
            cmd=Eval("ansible-galaxy collection list --format json {{SELF.file}}"),
            done={"success": True},
            resultTemplate=Eval(
                (
                    "{%if returncode == 0 and stdout | from_json %}\n"
                    "readyState: ok\n"
                    "# {{ {'python': 'unfurl.configurators.ansible.reload_collections'} | eval "
                    "}}\n"
                    "{% else %}\n"
                    "readyState: absent\n"
                    "{% endif %}\n"
                )
            ),
        )

    def create(self, **kw: Any) -> Any:
        return unfurl.configurators.CmdConfigurator(
            cmd=Eval("ansible-galaxy collection install {{SELF.file}}"),
            resultTemplate=Eval(
                {"eval": {"python": "unfurl.configurators.ansible.reload_collections"}}
            ),
        )


class unfurl_nodes_Installer_Terraform(unfurl.nodes.Installer):
    _type_name = "unfurl.nodes.Installer.Terraform"
    main: Union[str, None] = Property(metadata={"user_settable": False}, default=None)

    @operation(apply_to=["Install.check", "Standard.delete"])
    def default(self, **kw: Any) -> Any:
        return unfurl.configurators.terraform.TerraformConfigurator(
            main=Eval({"get_property": ["SELF", "main"]}),
        )


configurator_artifacts = unfurl.nodes.LocalRepository(
    "configurator-artifacts",
    _directives=["default"],
)
setattr(
    configurator_artifacts,
    "terraform",
    artifact_AsdfTool(
        "terraform",
        version="1.1.4",
        file="terraform",
    ),
)
setattr(
    configurator_artifacts,
    "gcloud",
    artifact_AsdfTool(
        "gcloud",
        version="398.0.0",
        file="gcloud",
    ),
)
setattr(
    configurator_artifacts,
    "kompose",
    artifact_AsdfTool(
        "kompose",
        version="1.26.1",
        file="kompose",
    ),
)
setattr(
    configurator_artifacts,
    "google_auth",
    artifact_PythonPackage(
        "google-auth",
        file="google-auth",
    ),
)
setattr(
    configurator_artifacts,
    "octodns",
    artifact_PythonPackage(
        "octodns",
        version="==0.9.14",
        file="octodns",
    ),
)
setattr(
    configurator_artifacts,
    "kubernetes_core",
    artifact_AnsibleCollection(
        "kubernetes.core",
        version="2.4.0",
        file="kubernetes.core",
    ),
)
setattr(
    configurator_artifacts,
    "community_docker",
    artifact_AnsibleCollection(
        "community.docker",
        version="1.10.2",
        file="community.docker",
    ),
)
setattr(
    configurator_artifacts,
    "ansible_utils",
    artifact_AnsibleCollection(
        "ansible.utils",
        version="2.10.3",
        file="ansible.utils",
    ),
)


__all__ = [
    "artifact_AsdfTool",
    "artifact_PythonPackage",
    "artifact_AnsibleCollection",
    "unfurl_nodes_Installer_Terraform",
    "configurator_artifacts",
]

