1
1
import yaml
2
2
import logging
3
+ from core .utils .env_expander import (
4
+ expand_env_vars ,
5
+ ) # if you put the function above in env_expander.py
3
6
4
7
logger = logging .getLogger (__name__ )
5
8
@@ -10,29 +13,36 @@ class TopologyLoaderError(Exception):
10
13
11
14
class TopologyLoader :
12
15
"""
13
- Loads containerlab topology data from a YAML file.
16
+ Loads containerlab topology data from a YAML file, expanding environment variables .
14
17
"""
15
18
16
19
def load (self , input_file : str ) -> dict :
17
20
"""
18
- Load the containerlab YAML topology file.
21
+ Load the containerlab YAML topology file, including environment-variable expansion .
19
22
20
23
:param input_file: Path to the containerlab YAML file.
21
- :return: Parsed containerlab topology data.
24
+ :return: Parsed containerlab topology data with env variables expanded .
22
25
:raises TopologyLoaderError: If file not found or parse error occurs.
23
26
"""
24
27
logger .debug (f"Loading topology from file: { input_file } " )
25
28
try :
26
29
with open (input_file , "r" ) as file :
27
- containerlab_data = yaml .safe_load (file )
30
+ raw_content = file .read ()
31
+
32
+ # Expand ${VAR:=default} placeholders before parsing
33
+ expanded_content = expand_env_vars (raw_content )
34
+
35
+ containerlab_data = yaml .safe_load (expanded_content )
28
36
logger .debug ("Topology successfully loaded." )
29
37
return containerlab_data
38
+
30
39
except FileNotFoundError :
31
40
error_message = (
32
41
f"Error: The specified clab file '{ input_file } ' does not exist."
33
42
)
34
43
logger .error (error_message )
35
44
raise TopologyLoaderError (error_message )
45
+
36
46
except Exception as e :
37
47
error_message = f"An error occurred while loading the config: { e } "
38
48
logger .error (error_message )
0 commit comments