Skip to content

Commit 35709b4

Browse files
author
Arik Kfir
committed
Support verbosity in creating Cloud SQL instances.
Also fix tests not to send "config" to "init" action of resources.
1 parent e530d57 commit 35709b4

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

resources/src/dresources.py

+4
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ def verbose(self) -> bool:
151151
def workspace(self) -> Path:
152152
return Path(self._data['workspace'])
153153

154+
@property
155+
def has_config(self)->bool:
156+
return 'config' in self._data
157+
154158
@property
155159
def config(self) -> Mapping[str, Any]:
156160
"""The resource configuration, which in essence is the *desired* state of the resource, as depicted by the user

resources/src/gcp_cloud_sql.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -525,10 +525,12 @@ def __init__(self, data: dict, svc: ExternalServices = ExternalServices()) -> No
525525
}
526526
})
527527

528-
cfg: dict = self.info.config
529-
if "maintenance" in cfg and cfg["maintenance"] is not None:
530-
if 'day' in cfg["maintenance"] and type(cfg["maintenance"]['day']) == str:
531-
cfg["maintenance"]['day'] = _translate_day_name_to_number(cfg["maintenance"]['day'])
528+
# translate maintenance window name to number
529+
if self.info.has_config:
530+
cfg: dict = self.info.config
531+
if "maintenance" in cfg and cfg["maintenance"] is not None:
532+
if 'day' in cfg["maintenance"] and type(cfg["maintenance"]['day']) == str:
533+
cfg["maintenance"]['day'] = _translate_day_name_to_number(cfg["maintenance"]['day'])
532534

533535
def discover_state(self):
534536
cfg: dict = self.info.config
@@ -861,9 +863,13 @@ def create_sql_instance(self, args) -> None:
861863

862864
# create instance
863865
project_id: str = cfg['project_id']
866+
if self.info.verbose:
867+
print(f"Creating Cloud SQL instance using:\n{json.dumps(body, indent=2)}")
864868
self.svc.create_gcp_sql_instance(project_id=project_id, body=body)
865869

866870
# set 'root' password
871+
if self.info.verbose:
872+
print(f"Updating root user's password")
867873
self.svc.update_gcp_sql_user(project_id=project_id, instance=cfg['name'], password=cfg['root-password'])
868874

869875
# check for scripts that need to be executed

tests/test_resources.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,20 @@ def find_scenarios(scenario_pattern: str) -> Sequence[Tuple[str, dict, dict, dic
5050
return scenarios
5151

5252

53-
def create_resource(svc: MockExternalServices, resource: dict, extra_data: dict = None) -> DResource:
53+
def create_resource(svc: MockExternalServices,
54+
resource: dict,
55+
include_config: bool = False,
56+
extra_data: dict = None) -> DResource:
5457
"""Creates a resource instance (subclass of DResource)."""
5558
module = importlib.import_module(resource["module"])
5659
resource_type: Callable[[dict, ExternalServices], DResource] = getattr(module, resource["class"])
5760
data = {"name": resource["name"] if 'name' in resource else 'test',
5861
"type": resource["class"],
5962
"version": resource["version"] if "version" in resource else "0.0.0",
6063
"verbose": resource["verbose"] if 'verbose' in resource else True,
61-
"workspace": "/workspace",
62-
"config": resource["config"]}
64+
"workspace": "/workspace"}
65+
if include_config:
66+
data.update({"config": resource["config"]})
6367
if extra_data is not None:
6468
data.update(extra_data)
6569
return resource_type(data, svc)
@@ -102,9 +106,9 @@ def test_resources(capsys, description: str, mock: dict, resource: dict, expecte
102106
# test "state" action, and read its result from stdout
103107
if 'exception' in expected:
104108
with pytest.raises(eval(expected['exception']), match=expected["match"] if 'match' in expected else r'.*'):
105-
create_resource(svc=mock_services, resource=resource).execute(['state'])
109+
create_resource(svc=mock_services, resource=resource, include_config=True).execute(['state'])
106110
else:
107-
create_resource(svc=mock_services, resource=resource).execute(['state'])
111+
create_resource(svc=mock_services, resource=resource, include_config=True).execute(['state'])
108112
state = json.loads(capsys.readouterr().out)
109113
assert state == expected
110114

@@ -113,4 +117,7 @@ def test_resources(capsys, description: str, mock: dict, resource: dict, expecte
113117
for action in state["actions"]:
114118
extra_data: dict = {'staleState': state['staleState'] if 'staleState' in state else {}}
115119
args = action['args'] if 'args' in action else []
116-
create_resource(svc=mock_services, resource=resource, extra_data=extra_data).execute(args)
120+
create_resource(svc=mock_services,
121+
resource=resource,
122+
include_config=True,
123+
extra_data=extra_data).execute(args)

0 commit comments

Comments
 (0)