Skip to content

Commit 1de0965

Browse files
authored
Merge pull request #68 from 2ndWatch/fix-cli-overwrites
Adjust CLI/config consolidation to avoid overwrites with empty values
2 parents 96b8283 + 8ecc259 commit 1de0965

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed

cloudendure/config.py

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import logging
66
import os
77
from pathlib import Path, PosixPath
8-
from typing import Any, Dict
8+
from typing import Any, Dict, List
99

1010
import yaml
1111

@@ -17,6 +17,26 @@
1717
class CloudEndureConfig:
1818
"""Define the CloudEndure Config object."""
1919

20+
BASE_CONFIG = {
21+
"host": "https://console.cloudendure.com",
22+
"api_version": "latest",
23+
"auth_ttl": "3600",
24+
"username": "",
25+
"password": "",
26+
"token": "",
27+
"user_api_token": "",
28+
"session_cookie": "",
29+
"project_name": "",
30+
"project_id": "",
31+
"max_lag_ttl": "90",
32+
"machines": "",
33+
"migration_wave": "0",
34+
"clone_status": "NOT_STARTED",
35+
"destination_account": "",
36+
"disk_type": "SSD",
37+
"public_ip": "DONT_ALLOCATE",
38+
}
39+
2040
def __init__(
2141
self, username: str = "", password: str = "", token: str = "", *args, **kwargs
2242
) -> None:
@@ -37,33 +57,20 @@ def __init__(
3757
"No CloudEndure YAML configuration found! Creating it at: (%s)",
3858
self.config_path,
3959
)
40-
self.write_yaml_config(
41-
config={
42-
"host": "https://console.cloudendure.com",
43-
"api_version": "latest",
44-
"auth_ttl": "3600",
45-
"username": "",
46-
"password": "",
47-
"token": "",
48-
"user_api_token": "",
49-
"session_cookie": "",
50-
"project_name": "",
51-
"project_id": "",
52-
"max_lag_ttl": "90",
53-
"machines": "",
54-
"migration_wave": "0",
55-
"clone_status": "NOT_STARTED",
56-
"destination_account": "",
57-
"disk_type": "SSD",
58-
"public_ip": "DONT_ALLOCATE",
59-
}
60-
)
60+
self.write_yaml_config(config=self.BASE_CONFIG)
6161
self.update_config()
6262

6363
def __str__(self) -> str:
6464
"""Define the string representation of the CloudEndure API object."""
6565
return "<CloudEndureAPI>"
6666

67+
def merge_config_dicts(self, values: List[Any]) -> Dict[str, str]:
68+
"""Merge a list of configuration dictionaries."""
69+
data: Dict[str, str] = self.BASE_CONFIG
70+
for value in values:
71+
data.update({k: v for k, v in value.items() if v})
72+
return data
73+
6774
def read_yaml_config(self) -> Dict[str, Any]:
6875
"""Read the CloudEndure YAML configuration file."""
6976
logger.info("Loading the CloudEndure YAML configuration file")
@@ -118,11 +125,9 @@ def update_config(self) -> None:
118125
"""Update the configuration."""
119126
self.yaml_config_contents: Dict[str, Any] = self.read_yaml_config()
120127
self.env_config = self.get_env_vars()
121-
self.active_config = {
122-
**self.yaml_config_contents,
123-
**self.env_config,
124-
**self.cli,
125-
}
128+
self.active_config = self.merge_config_dicts(
129+
[self.yaml_config_contents, self.env_config, self.cli]
130+
)
126131

127132
def update_token(self, token: str) -> bool:
128133
"""Update the CloudEndure token.

0 commit comments

Comments
 (0)