Skip to content

Commit

Permalink
Chore(): Unify generate function,
Browse files Browse the repository at this point in the history
  • Loading branch information
alithethird committed Feb 3, 2025
1 parent e850754 commit 02bf010
Showing 1 changed file with 15 additions and 32 deletions.
47 changes: 15 additions & 32 deletions src/paas_charm/charm_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,13 @@ def build( # pylint: disable=too-many-arguments
Return:
The IntegrationsState instance created.
"""
s3_parameters = generate_relation_parameters(s3_connection_info, S3Parameters)
saml_parameters = generate_saml_relation_parameters(saml_relation_data, SamlParameters)
s3_parameters = typing.cast(
S3Parameters | None, generate_relation_parameters(s3_connection_info, S3Parameters)
)
saml_parameters = typing.cast(
SamlParameters | None,
generate_relation_parameters(saml_relation_data, SamlParameters, True),
)

# Workaround as the Redis library temporarily sends the port
# as None while the integration is being created.
Expand All @@ -247,51 +252,29 @@ def build( # pylint: disable=too-many-arguments
)


def generate_saml_relation_parameters(
saml_relation_data: typing.MutableMapping[str, str] | None,
parameter_type: type,
) -> "SamlParameters | None":
"""Generate SAML relation parameter class from relation data.
Args:
saml_relation_data: Relation data.
parameter_type: Parameter type to use.
Return:
Parameter instance created.
Raises:
CharmConfigInvalidError: If some parameter in invalid.
"""
if saml_relation_data is None:
return None
try:
return parameter_type(**saml_relation_data)
except ValidationError as exc:
error_message = build_validation_error_message(exc)
raise CharmConfigInvalidError(
f"Invalid {parameter_type.__name__} configuration: {error_message}"
) from exc


def generate_relation_parameters(
relation_data: dict[str, str] | None,
relation_data: dict[str, str] | typing.MutableMapping[str, str] | None,
parameter_type: type,
) -> "S3Parameters | None":
support_empty: bool = False,
) -> "SamlParameters | S3Parameters | None":
"""Generate relation parameter class from relation data.
Args:
relation_data: Relation data.
parameter_type: Parameter type to use.
support_empty: Support empty relation data.
Return:
Parameter instance created.
Raises:
CharmConfigInvalidError: If some parameter in invalid.
"""
if not relation_data:
if not support_empty and not relation_data:
return None
if relation_data is None:
return None

try:
return parameter_type(**relation_data)
except ValidationError as exc:
Expand Down

0 comments on commit 02bf010

Please sign in to comment.