diagrams icon indicating copy to clipboard operation
diagrams copied to clipboard

Node as cluster

Open bkmeneguello opened this issue 4 years ago • 14 comments

This PR contains and supersedes the #407 I'm collaborating with the OP @dan-ash but he seems to be having personal issues to keep this PR up to date.

from diagrams import Cluster, Diagram
from diagrams.aws.compute import ECS
from diagrams.aws.database import RDS, Aurora
from diagrams.aws.network import Route53, VPC

with Diagram("Simple Web Service with DB Cluster", show=True, filename="mysql"):
    dns = Route53("dns")
    web = ECS("service")

    with VPC('VPC'):
        # using cluster with an icon
        with Cluster("DB ClusterA", icon=ECS):
            db_master1 = RDS("main")
            db_master1 - [RDS("replica1"), RDS("replica2")]
        # using the node
        with Aurora("DB ClusterA") as db2:
            db_master2 = RDS("main")
            db_master2 - [RDS("replica1"), RDS("replica2")]

        dns >> web >> db_master1
        # link to/from cluster
        dns >> web >> db2

diagram

bkmeneguello avatar Jan 19 '21 19:01 bkmeneguello

@bkmeneguello I suggest to close this in favour of #439 what do you think? did you got a change to go over it?

dan-ash avatar Jan 28 '21 18:01 dan-ash

@dan-ash Sorry, but I think this PR is more mature and featureful than #439. Maybe I could simplify the commits in more logical ones but this could remove your changes.

bkmeneguello avatar Feb 01 '21 13:02 bkmeneguello

I tried this, but I got the error.

    with VPC('VPC'):
AttributeError: __enter__

Can anyone help?

seema1711 avatar Feb 19 '21 10:02 seema1711

I tried this, but I got the error.

    with VPC('VPC'):
AttributeError: __enter__

Can anyone help?

Update: I tried implementing this:

class VPC(Cluster):
    # fmt: off
    _default_graph_attrs = {
        "shape": "box",
        "style": "dashed",
        "labeljust": "l",
        "pencolor": "#8c8c8c",
        "fontname": "sans-serif",
        "fontsize": "12",
    }
    # fmt: on

class ECS(Cluster):
     # fmt: off
    _default_graph_attrs = {
        "shape": "box",
        "style": "dashed",
        "labeljust": "l",
        "pencolor": "#8c8c8c",
        "fontname": "sans-serif",
        "fontsize": "12",
    }
    # fmt: on

class Aurora(Cluster):
     # fmt: off
    _default_graph_attrs = {
        "shape": "box",
        "style": "dashed",
        "labeljust": "l",
        "pencolor": "#8c8c8c",
        "fontname": "sans-serif",
        "fontsize": "12",
    }
    # fmt: on

but it's not still displaying the cluster branding. Any solution?

One more doubt: Line Spacing is not working i.e. \n. And what if we want to display two lines on one edge, one on top, and one on bottom. What to do in this case? Edit: @clayms can you help?

seema1711 avatar Feb 20 '21 08:02 seema1711

@bkmeneguello I run all the examples I found in the source and figure out 2 issues: First one is with the color, somehow the color order is shifted (I didn't checked the code yet so sorry for the lack of details).

Basically when you do this:


from diagrams import Diagram
from diagrams.onprem.network import Nginx
from diagrams.onprem.container import Docker

with Diagram(name="", direction="TB", show=False, filename="aws"):
    with Docker("Docker") as container:
      with Docker("Docker2 "):
        with Docker("Docker3 "):
          with Docker("Docker4 "):
            with Docker("Docker5 "):
                Nginx("Nginx")

    container

it render that: aws

Where the first color is green.

If I run the same nested code with actual version I have this:

from diagrams import Diagram
from diagrams.onprem.network import Nginx
from diagrams.onprem.container import Docker

with Diagram(name="", direction="TB", show=False, filename="aws"):
    with Docker("Docker") as container:
      with Docker("Docker2 "):
        with Docker("Docker3 "):
          with Docker("Docker4 "):
            with Docker("Docker5 "):
                Nginx("Nginx")

    container

it render this one: color Which start with blue.

The second problem which is more serious even if there's no tests or example is the support for "graph_attr" in cluster.


from diagrams import Diagram, Cluster
from diagrams.onprem.network import Nginx
# from diagrams.onprem.container import Docker

with Diagram(name="", direction="TB", show=False, filename="aws"):
   with Cluster("AWS", graph_attr={"pencolor": "#60193C", "bgcolor": "#E587B5"}) as container: # overwrite attributes for the default cluster
       Nginx("Nginx")

   container
   

on master it render this: pink_nginx

on your PR branch it generate an error:


Traceback (most recent call last):
  File "/usr/src/diagrams/example.py", line 9, in <module>
    container
  File "/usr/src/diagrams/diagrams/__init__.py", line 217, in __exit__
    super().__exit__(*args)
  File "/usr/src/diagrams/diagrams/__init__.py", line 71, in __exit__
    self.dot.subgraph(subgraph.dot)
  File "/usr/local/lib/python3.9/site-packages/graphviz/dot.py", line 244, in subgraph
    lines = ['\t' + line for line in graph.__iter__(subgraph=True)]
  File "/usr/local/lib/python3.9/site-packages/graphviz/dot.py", line 244, in <listcomp>
    lines = ['\t' + line for line in graph.__iter__(subgraph=True)]
  File "/usr/local/lib/python3.9/site-packages/graphviz/dot.py", line 110, in __iter__
    yield self._attr % (kw, self._attr_list(None, attrs))
  File "/usr/local/lib/python3.9/site-packages/graphviz/lang.py", line 136, in attr_list
    content = a_list(label, kwargs, attributes)
  File "/usr/local/lib/python3.9/site-packages/graphviz/lang.py", line 109, in a_list
    items = ['%s=%s' % (quote(k), quote(v))
  File "/usr/local/lib/python3.9/site-packages/graphviz/lang.py", line 109, in <listcomp>
    items = ['%s=%s' % (quote(k), quote(v))
  File "/usr/local/lib/python3.9/site-packages/graphviz/lang.py", line 72, in quote
    if is_html_string(identifier) and not isinstance(identifier, NoHtml):
TypeError: expected string or bytes-like object

gabriel-tessier avatar Feb 22 '21 11:02 gabriel-tessier

from diagrams import Diagram
from diagrams.onprem.network import Nginx
from diagrams.onprem.container import Docker

with Diagram(name="", direction="TB", show=False, filename="aws"):
    with Docker("Docker") as container:
      with Docker("Docker2 "):
        with Docker("Docker3 "):
          with Docker("Docker4 "):
            with Docker("Docker5 "):
                Nginx("Nginx")

    container

this code is not working on my machine, it throws an error:

    with Docker("Docker") as container:
AttributeError: __enter__

seema1711 avatar Feb 22 '21 16:02 seema1711

@seema1711 Can you please stop posting on the issues, if you have problem or question you can use stackoverflow on how to use diagrams. To make it clear this code is not working with your diagrams version, if you want to use the new feature consider forking the PR branch, if you don't understand what I'm talking about, just wait that this feature go in the new release.

Your problem is that you use the actual library version which don't have the code of the PR and it's normal that this error raise. You are only making noise by posting that it's not working and make us waste time.

gabriel-tessier avatar Feb 23 '21 03:02 gabriel-tessier

Any progress on this one? Would love to have this functionality!

DonDebonair avatar Aug 03 '21 09:08 DonDebonair

I'm here. Sorry for the late reply. I'll review this PR ASAP. It could be really good improvements. Thank you :)

mingrammer avatar Nov 04 '22 08:11 mingrammer

@bkmeneguello Please resolve the conflicts.

mingrammer avatar Nov 04 '22 08:11 mingrammer

@bkmeneguello Do you have time to resolve the conflicts? We really would like to have this feature.

jobinjosem1 avatar Dec 08 '22 10:12 jobinjosem1

Sorry, two years ago I had the time, now I'm not involved anymore with this project.

bkmeneguello avatar Dec 08 '22 13:12 bkmeneguello

I am willing to take this branch and resolve the conflicts if no one minds? I will open this as a new PR.

lazzurs avatar Dec 14 '22 12:12 lazzurs