Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions clab_connector/core/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@

# Create the Jinja2 environment with the correct template directory
template_environment = Environment(
loader=FileSystemLoader(TEMPLATE_DIR),
autoescape=select_autoescape()
loader=FileSystemLoader(TEMPLATE_DIR), autoescape=select_autoescape()
)

# set up logging
Expand Down Expand Up @@ -49,7 +48,16 @@ def parse_topology(topology_file) -> "Topology":
data = json.load(f)
# Check if this is a topology-data.json file
if "type" in data and data["type"] == "clab":
topo = Topology(data["name"], "", [], [])
# Initialize with empty values, will be populated by from_topology_data
topo = Topology(
name=data["name"],
mgmt_ipv4_subnet="",
ssh_pub_keys=data.get(
"ssh-pub-keys", []
), # Get SSH keys with empty list as default
nodes=[],
links=[],
)
topo = topo.from_topology_data(data)
# Sanitize the topology name after parsing
original_name = topo.name
Expand All @@ -66,6 +74,7 @@ def parse_topology(topology_file) -> "Topology":
)
sys.exit(1)


def render_template(template_name, data):
"""
Loads a jinja template and renders it with the data provided
Expand Down Expand Up @@ -106,4 +115,4 @@ def normalize_name(name: str) -> str:
if not safe_name[-1].isalnum():
safe_name = safe_name + "0"

return safe_name
return safe_name
4 changes: 3 additions & 1 deletion clab_connector/core/integrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,10 @@ def create_node_users(self):
"node_user": "admin",
"username": "admin",
"password": "NokiaSrl1!",
"ssh_pub_keys": self.topology.ssh_pub_keys
if hasattr(self.topology, "ssh_pub_keys")
else [],
}

node_user = helpers.render_template("node-user.j2", data)
logger.debug(node_user)
item = self.eda.add_replace_to_transaction(node_user)
Expand Down
9 changes: 7 additions & 2 deletions clab_connector/core/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from clab_connector.core.node import from_obj as node_from_obj



# set up logging
logger = logging.getLogger(__name__)

Expand All @@ -15,6 +14,7 @@ def __init__(
self,
name,
mgmt_ipv4_subnet,
ssh_pub_keys,
nodes,
links,
clab_file_path="",
Expand All @@ -37,6 +37,7 @@ def __init__(
"""
self.name = name
self.mgmt_ipv4_subnet = mgmt_ipv4_subnet
self.ssh_pub_keys = ssh_pub_keys
self.nodes = nodes
self.links = links
self.clab_file_path = clab_file_path
Expand Down Expand Up @@ -175,6 +176,8 @@ def from_topology_data(self, json_obj):
name = json_obj["name"]
mgmt_ipv4_subnet = json_obj["clab"]["config"]["mgmt"]["ipv4-subnet"]

ssh_pub_keys = json_obj.get("ssh-pub-keys", [])

clab_file_path = ""
for node_name, node_data in json_obj["nodes"].items():
if clab_file_path == "":
Expand Down Expand Up @@ -225,4 +228,6 @@ def from_topology_data(self, json_obj):
f"Skipping link between {link_data['a']['node']} and {link_data['z']['node']} as one or both nodes are not supported"
)

return Topology(name, mgmt_ipv4_subnet, nodes, links, clab_file_path)
return Topology(
name, mgmt_ipv4_subnet, ssh_pub_keys, nodes, links, clab_file_path
)
5 changes: 4 additions & 1 deletion clab_connector/templates/node-user.j2
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ spec:
- sudo
nodeSelector:
- containerlab=managed
sshPublicKeys: []
sshPublicKeys:
{%- for key in ssh_pub_keys %}
- {{ key }}
{%- endfor %}