# Generated by tosca.yaml2python from unfurl/tosca_plugins/googlecloud.yaml at 2024-02-18T10:26:42 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,
    Attribute,
    AttributeOptions,
    CONSTRAINED,
    Capability,
    Computed,
    DEFAULT,
    Eval,
    MISSING,
    NodeType,
    Property,
    PropertyOptions,
    Requirement,
    ToscaInputs,
    ToscaOutputs,
    operation,
)
import tosca
import unfurl.configurators.terraform
import unfurl.configurators.shell
from unfurl.tosca_plugins.artifacts import *


class unfurl_nodes_GoogleCloudObject(unfurl.nodes.CloudObject):
    _type_name = "unfurl.nodes.GoogleCloudObject"
    cloud: Union[
        Union[
            "unfurl.relationships.ConnectsToGoogleCloudProject",
            "unfurl_nodes_GoogleCloudProject",
        ],
        None,
    ] = Requirement(default=None, metadata={"visibility": "hidden"})


class unfurl_nodes_GoogleCloudProject(unfurl.nodes.CloudAccount):
    _type_name = "unfurl.nodes.GoogleCloudProject"
    name: str
    """User defined name of the project"""

    project_id: str = Eval({"eval": "name"})
    organization_id: Union[int, None] = Eval(
        {"eval": ".parent::.ancestors::organization_id"}
    )
    billing_account: Union[str, None] = Eval(
        {"eval": ".parent::.ancestors::billing_account"}
    )
    folder_id: Union[int, None] = Eval(
        {"eval": ".parent[.type=unfurl.nodes.GoogleCloudFolder]::id"}
    )
    activate_apis: List[Any] = Property(factory=lambda: ([]))

    project_number: Union[int, None] = Attribute(default=None)
    service_account_id: Union[str, None] = Attribute(default=None)
    account_id: str = Attribute(default=Eval({"eval": "project_id"}))
    """Cloud provider specific account identifier"""

    service_account_email: Union[str, None] = Attribute(default=None)

    host: Union[
        Union["tosca.relationships.HostedOn", "unfurl_nodes_GoogleCloudResource"], None
    ] = None

    @operation(apply_to=["Install.check", "Standard.delete", "Standard.configure"])
    def default(self, **kw: Any) -> Any:
        return unfurl.configurators.terraform.TerraformConfigurator(
            main=Eval(
                (
                    'module "project-factory" {\n'
                    '  source  = "terraform-google-modules/project-factory/google"\n'
                    '  version = "~> 10.1"\n'
                    "\n"
                    '  name              = "{{ SELF.name }}"\n'
                    '  billing_account   = "{{ SELF.billing_account }}"\n'
                    "  activate_apis = {{ SELF.activate_apis | mapValue | to_json }}\n"
                    "  {%if SELF.folder_id %}\n"
                    '    folder_id = "{{ SELF.folder_id }}"\n'
                    '    org_id = ""\n'
                    "  {% elif SELF.organization_id %}\n"
                    '    org_id = "{{ SELF.organization_id }}"\n'
                    "  {% endif %}\n"
                    "}\n"
                    "\n"
                    'output "project_id" {\n'
                    "  value       = module.project-factory.project_id\n"
                    "}\n"
                    "\n"
                    'output "project_number" {\n'
                    "  value       = module.project-factory.project_number\n"
                    "}\n"
                    "\n"
                    'output "project_name" {\n'
                    "  value       = module.project-factory.project_name\n"
                    "}\n"
                    "\n"
                    'output "service_account_name" {\n'
                    "  value       = module.project-factory.service_account_name\n"
                    "}\n"
                    "\n"
                    'output "service_account_id" {\n'
                    "  value       = module.project-factory.service_account_id\n"
                    "}\n"
                    "\n"
                    'output "service_account_email" {\n'
                    "  value       = module.project-factory.service_account_email\n"
                    "}\n"
                )
            ),
            resultTemplate=Eval(
                (
                    "# merge all outputs into attributes\n"
                    "attributes:\n"
                    "  <<: {{ outputs | mapValue | to_json }}\n"
                )
            ),
        )


class unfurl_nodes_GoogleCloudServiceKey(unfurl_nodes_GoogleCloudObject):
    _type_name = "unfurl.nodes.GoogleCloudServiceKey"
    account_id: str
    filename: str = Eval(
        {
            "eval": {
                "concat": [
                    {"get_property": ["SELF", "account_id"]},
                    "-credentials.json",
                ]
            }
        }
    )
    download_location: str = "secrets"
    filepath: str = Eval("{{ SELF.filename | abspath(SELF.download_location) }}")

    def create(self, **kw: Any) -> Any:
        return unfurl.configurators.shell.ShellConfigurator(
            command=Eval(
                (
                    'gcloud iam service-accounts keys create "{{ SELF.filepath }}"\n'
                    ' --iam-account "{{SELF.account_id}}" --user-output-enabled false\n'
                )
            ),
        )


class unfurl_nodes_GoogleCloudResource(unfurl_nodes_GoogleCloudObject):
    _type_name = "unfurl.nodes.GoogleCloudResource"
    display_name: str
    """User defined name of the resource"""

    id: int
    type: str
    name: str = Eval(
        {
            "concat": [
                {"get_property": ["SELF", "type"]},
                "/",
                {"get_property": ["SELF", "id"]},
            ]
        }
    )
    billing_account: Union[str, None] = None
    """Default billing account for projects in this resource"""

    host: "tosca.capabilities.Container" = Capability(
        factory=tosca.capabilities.Container,
        valid_source_types=["unfurl.nodes.GoogleCloudResource"],
    )


class unfurl_nodes_GoogleCloudOrganization(unfurl_nodes_GoogleCloudResource):
    _type_name = "unfurl.nodes.GoogleCloudOrganization"
    type: str = "organization"
    organization_id: int = Eval({"eval": "id"})
    parent: str = Eval({"eval": ".parent::.name"})


class unfurl_nodes_GoogleCloudFolder(unfurl_nodes_GoogleCloudResource):
    _type_name = "unfurl.nodes.GoogleCloudFolder"
    type: str = "folder"
    parent: str = Eval({"eval": ".parent::.name"})

    host_requirement: Union[
        "tosca.relationships.HostedOn", "unfurl_nodes_GoogleCloudResource"
    ] = Requirement(name="host")


__all__ = [
    "unfurl_nodes_GoogleCloudObject",
    "unfurl_nodes_GoogleCloudProject",
    "unfurl_nodes_GoogleCloudServiceKey",
    "unfurl_nodes_GoogleCloudResource",
    "unfurl_nodes_GoogleCloudOrganization",
    "unfurl_nodes_GoogleCloudFolder",
]

