diff --git a/docs/examples/tosca-node-template.py b/docs/examples/tosca-node-template.py new file mode 100644 index 00000000..1c855adc --- /dev/null +++ b/docs/examples/tosca-node-template.py @@ -0,0 +1,23 @@ +import tosca +from tosca import GB, MB + +compute = tosca.nodes.Compute( + host=tosca.capabilities.Compute( + num_cpus=1, + disk_size=200 * GB, + mem_size=512 * MB, + ), +) + +mydb = PostgresDB(name="mydb") + +myApp = MyApplication( + ports=tosca.datatypes.NetworkPortSpec("80:8080"), + host=[compute], + db=mydb, +) +myApp.image = tosca.artifacts.DeploymentImageContainerDocker( + "image", + file="myapp:latest", + repository="docker_hub", +) diff --git a/docs/examples/tosca-node-template.yaml b/docs/examples/tosca-node-template.yaml new file mode 100644 index 00000000..6102174a --- /dev/null +++ b/docs/examples/tosca-node-template.yaml @@ -0,0 +1,31 @@ +topology_template: + node_templates: + myApp: + type: MyApplication + properties: + ports: + eval: + portspec: "80:8080" + artifacts: + image: + type: tosca.artifacts.Deployment.Image.Container.Docker + file: myapp:latest + repository: docker_hub + requirements: + - host: compute + - db: + node: mydb + + mydb: + type: PostgresDB + properties: + name: mydb + + compute: + type: tosca.nodes.Compute + capabilities: + host: + properties: + num_cpus: 1 + disk_size: 200GB + mem_size: 512MB \ No newline at end of file diff --git a/docs/examples/tosca-type-example.py b/docs/examples/tosca-type-example.py new file mode 100644 index 00000000..8d8d14de --- /dev/null +++ b/docs/examples/tosca-type-example.py @@ -0,0 +1,32 @@ +import unfurl +from typing import Any, Sequence +import tosca +from tosca import Attribute +import unfurl.configurators.shell +from unfurl.tosca_plugins.expr import get_input + +class MyApplication(tosca.nodes.SoftwareComponent): + domain: str = get_input("domain") + ports: "tosca.datatypes.NetworkPortSpec" + + private_address: str = Attribute() + + host: Sequence[ + "tosca.relationships.HostedOn | tosca.nodes.Compute | tosca.capabilities.Compute" + ] = () + db: "tosca.relationships.ConnectsTo | capabilities_postgresdb" + + def create(self, **kw): + return unfurl.configurators.shell.ShellConfigurator( + command=["create.sh"], + ) + + def configure(self, **kw): + return unfurl.configurators.shell.ShellConfigurator( + command=["configure.sh"], + ) + + def delete(self, **kw): + return unfurl.configurators.shell.ShellConfigurator( + command=["delete.sh"], + ) diff --git a/docs/examples/tosca-type-example.yaml b/docs/examples/tosca-type-example.yaml new file mode 100644 index 00000000..97a615d7 --- /dev/null +++ b/docs/examples/tosca-type-example.yaml @@ -0,0 +1,26 @@ +tosca_definitions_version: tosca_simple_unfurl_1_0_0 +node_types: + MyApplication: + derived_from: tosca.nodes.SoftwareComponent + attributes: + private_address: + type: string + properties: + domain: + type: string + default: { get_input: domain } + ports: + type: tosca.datatypes.network.PortSpec + requirements: + - host: + capability: tosca.capabilities.Compute + relationship: tosca.relationships.HostedOn + - db: + capability: capabilities.postgresdb + relationship: tosca.relationships.ConnectsTo + interfaces: + # TOSCA defines Standard interface for lifecycle management but you can define your own too + Standard: + create: create.sh + configure: configure.sh + delete: delete.sh diff --git a/docs/tosca.rst b/docs/tosca.rst index 607760c8..d4c512ed 100644 --- a/docs/tosca.rst +++ b/docs/tosca.rst @@ -94,33 +94,13 @@ Example This example defines a node type named "MyApplication" that inherits from the "tosca.nodes.SoftwareComponent" node type. -.. code:: yaml +.. tab-set-code:: - node_types: - MyApplication: - derived_from: tosca.nodes.SoftwareComponent - attributes: - private_address: - type: string - properties: - domain: - type: string - default: { get_input: domain } - ports: - type: tosca.datatypes.network.PortSpec - requirements: - - host: - capabilities: tosca.capabilities.Compute - relationship: tosca.relationships.HostedOn - - db: - capabilities: capabilities.postgresdb - relationship: tosca.relationships.ConnectsTo - interfaces: - # TOSCA defines Standard interface for lifecycle management but you can define your own too - Standard: - create: create.sh - configure: configure.sh - delete: delete.sh + .. literalinclude:: ./examples/tosca-type-example.yaml + :language: yaml + + .. literalinclude:: ./examples/tosca-type-example.py + :language: python Topology Template ^^^^^^^^^^^^^^^^^^ @@ -155,36 +135,13 @@ A Node Template defines a node that gets instantiated into an instance (or resou Example ------- -.. code:: yaml +.. tab-set-code:: - node_templates: - - myApp: - type: myApplication - artifacts: - image: - type: tosca.artifacts.Deployment.Image.Container.Docker - file: myapp:latest - repository: docker_hub - requirements: - - host: compute - - db: - node: mydb - relationship: mydb_connection - - mydb: - type: PostgresDB - properties: - name: mydb - - compute: - type: unfurl.nodes.Compute - capabilities: - host: - properties: - num_cpus: 1 - disk_size: 200GB - mem_size: 512MB + .. literalinclude:: ./examples/tosca-node-template.yaml + :language: yaml + + .. literalinclude:: ./examples/tosca-node-template.py + :language: python Requirements and Relationships diff --git a/tests/test_docs.py b/tests/test_docs.py index eb3484cd..cf92392f 100644 --- a/tests/test_docs.py +++ b/tests/test_docs.py @@ -43,7 +43,7 @@ def test_python_snippets(self): """ def skip(py_file): - for skip in ["*quickstart_*", "*inputs.py", "*node-types-2.py"]: + for skip in ["*quickstart_*", "*inputs.py", "*node-types-2.py", "*tosca-node-template.py"]: if fnmatch.fnmatch(py_file, skip): return True return False