Skip to content

Commit

Permalink
docs: add more python dsl versions of example yaml.
Browse files Browse the repository at this point in the history
  • Loading branch information
aszs committed Nov 22, 2024
1 parent 745630d commit 642d94e
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 56 deletions.
23 changes: 23 additions & 0 deletions docs/examples/tosca-node-template.py
Original file line number Diff line number Diff line change
@@ -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",
)
31 changes: 31 additions & 0 deletions docs/examples/tosca-node-template.yaml
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions docs/examples/tosca-type-example.py
Original file line number Diff line number Diff line change
@@ -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"],
)
26 changes: 26 additions & 0 deletions docs/examples/tosca-type-example.yaml
Original file line number Diff line number Diff line change
@@ -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
67 changes: 12 additions & 55 deletions docs/tosca.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/test_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 642d94e

Please sign in to comment.