# Generated by tosca.yaml2python from ../../unfurl/unfurl/tosca_plugins/googlecloud.yaml at 2023-10-10T18:42:36 overwrite not modified (change to "ok" to allow)

import unfurl
from typing import List, Dict, Any, Tuple, Union, Sequence
from typing_extensions import Annotated
from tosca import (
    ArtifactType,
    Attribute,
    B,
    BPS,
    Bitrate,
    Capability,
    CapabilityType,
    D,
    DataType,
    Eval,
    Frequency,
    GB,
    GBPS,
    GHZ,
    GHz,
    GIB,
    GIBPS,
    Gbps,
    GiB,
    Gibps,
    GroupType,
    H,
    HZ,
    Hz,
    InterfaceType,
    KB,
    KBPS,
    KHZ,
    KIB,
    KIBPS,
    Kbps,
    KiB,
    Kibps,
    M,
    MB,
    MBPS,
    MHZ,
    MHz,
    MIB,
    MIBPS,
    MS,
    Mbps,
    MiB,
    Mibps,
    NS,
    Namespace,
    NodeType,
    PolicyType,
    Property,
    REQUIRED,
    MISSING,
    RelationshipType,
    Requirement,
    S,
    Size,
    T,
    TB,
    TBPS,
    TIB,
    TIBPS,
    Tbps,
    TiB,
    Tibps,
    Time,
    ToscaDataType,
    ToscaInputs,
    ToscaOutputs,
    US,
    b,
    bps,
    d,
    equal,
    field,
    gb,
    gbps,
    ghz,
    gib,
    gibps,
    greater_or_equal,
    greater_than,
    h,
    hz,
    in_range,
    kB,
    kHz,
    kb,
    kbps,
    khz,
    kib,
    kibps,
    length,
    less_or_equal,
    less_than,
    loader,
    m,
    max_length,
    mb,
    mbps,
    metadata_to_yaml,
    mhz,
    mib,
    mibps,
    min_length,
    ms,
    ns,
    operation,
    pattern,
    s,
    tb,
    tbps,
    tib,
    tibps,
    tosca_timestamp,
    tosca_version,
    us,
    valid_values,
)
import tosca
import unfurl.configurators.shell
import unfurl.configurators.terraform
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()
    service_account_id: Union[str, None] = Attribute()
    account_id: str = Attribute()
    """Cloud provider specific account identifier"""

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

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

    @operation(apply_to=["Install.check", "Standard.delete", "Standard.configure"])
    def default(self, **kw):
        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):
        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")

