Skip to content

Commit 263c0f8

Browse files
mgr/smb: move logic validating cluster changes to handler
When I first wrote the internal module for working with serialized objects in the internal store it seems to make sense to do some validation in the cluster class. At the time it was one of the few places in the code that would have both the new and old cluster objects for comparison. However, the code evolved and the handler module grew more responsibility for validation especially as it needed to do cross-object validation (do the cluster and share values align, etc). In addition, I initially couldn't find the code that handled the validation because I forgot there was validation in internal.py. Move the logic used to validate that certain properties of a cluster are unchanged into handler.py and out of internal.py. This makes the classes in internal.py even more regular as a bonus. Signed-off-by: John Mulligan <[email protected]>
1 parent 2f7fe66 commit 263c0f8

File tree

2 files changed

+31
-39
lines changed

2 files changed

+31
-39
lines changed

src/pybind/mgr/smb/handler.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,8 @@ def _check_cluster_removed(cluster: ClusterRef, staging: _Staging) -> None:
813813
def _check_cluster_present(cluster: ClusterRef, staging: _Staging) -> None:
814814
assert isinstance(cluster, resources.Cluster)
815815
cluster.validate()
816+
if not staging.is_new(cluster):
817+
_check_cluster_modifications(cluster, staging)
816818
for auth_ref in _auth_refs(cluster):
817819
auth = staging.get_join_auth(auth_ref)
818820
if (
@@ -841,6 +843,34 @@ def _check_cluster_present(cluster: ClusterRef, staging: _Staging) -> None:
841843
)
842844

843845

846+
def _check_cluster_modifications(
847+
cluster: resources.Cluster, staging: _Staging
848+
) -> None:
849+
"""cluster has some fields we do not permit changing after the cluster has
850+
been created.
851+
"""
852+
prev = ClusterEntry.from_store(
853+
staging.destination_store, cluster.cluster_id
854+
).get_cluster()
855+
if cluster.auth_mode != prev.auth_mode:
856+
raise ErrorResult(
857+
cluster,
858+
'auth_mode value may not be changed',
859+
status={'existing_auth_mode': prev.auth_mode},
860+
)
861+
if cluster.auth_mode == AuthMode.ACTIVE_DIRECTORY:
862+
assert prev.domain_settings
863+
if not cluster.domain_settings:
864+
# should not occur
865+
raise ErrorResult(cluster, "domain settings missing from cluster")
866+
if cluster.domain_settings.realm != prev.domain_settings.realm:
867+
raise ErrorResult(
868+
cluster,
869+
'domain/realm value may not be changed',
870+
status={'existing_domain_realm': prev.domain_settings.realm},
871+
)
872+
873+
844874
def _parse_earmark(earmark: str) -> dict:
845875
parts = earmark.split('.')
846876

src/pybind/mgr/smb/internal.py

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import Collection, Tuple, Type, TypeVar
55

66
from . import resources
7-
from .enums import AuthMode, ConfigNS, State
7+
from .enums import ConfigNS, State
88
from .proto import (
99
ConfigEntry,
1010
ConfigStore,
@@ -14,7 +14,6 @@
1414
Simplifiable,
1515
)
1616
from .resources import SMBResource
17-
from .results import ErrorResult
1817
from .utils import one
1918

2019
T = TypeVar('T')
@@ -108,43 +107,6 @@ def ids(cls, store: ConfigStoreListing) -> Collection[str]:
108107
def get_cluster(self) -> resources.Cluster:
109108
return self.get_resource_type(resources.Cluster)
110109

111-
def create_or_update(self, resource: Simplifiable) -> State:
112-
assert isinstance(resource, resources.Cluster)
113-
try:
114-
previous = self.config_entry.get()
115-
except KeyError:
116-
previous = None
117-
current = resource.to_simplified()
118-
if current == previous:
119-
return State.PRESENT
120-
elif previous is None:
121-
self.config_entry.set(current)
122-
return State.CREATED
123-
# cluster is special in that is has some fields that we do not
124-
# permit changing.
125-
prev = getattr(
126-
resources.Cluster, '_resource_config'
127-
).object_from_simplified(previous)
128-
if resource.auth_mode != prev.auth_mode:
129-
raise ErrorResult(
130-
resource,
131-
'auth_mode value may not be changed',
132-
status={'existing_auth_mode': prev.auth_mode},
133-
)
134-
if resource.auth_mode == AuthMode.ACTIVE_DIRECTORY:
135-
assert resource.domain_settings
136-
assert prev.domain_settings
137-
if resource.domain_settings.realm != prev.domain_settings.realm:
138-
raise ErrorResult(
139-
resource,
140-
'domain/realm value may not be changed',
141-
status={
142-
'existing_domain_realm': prev.domain_settings.realm
143-
},
144-
)
145-
self.config_entry.set(current)
146-
return State.UPDATED
147-
148110

149111
class ShareEntry(ResourceEntry):
150112
"""Share resource getter/setter for the smb internal data store(s)."""

0 commit comments

Comments
 (0)