diff --git a/ops/src/yawa_ops/commands/admin.py b/ops/src/yawa_ops/commands/admin.py index f87b5ec..1467436 100644 --- a/ops/src/yawa_ops/commands/admin.py +++ b/ops/src/yawa_ops/commands/admin.py @@ -1,22 +1,45 @@ import click import yawac from yawac.model.send_mail_request import SendMailRequest -from yawac.paths.admin_send_mail.post import SendMail +from yawac.paths.admin_mail.post import SendMail from yawa_ops.commands.base_command import BaseCommand from yawa_ops.utils import logutils from yawa_ops.utils.api import print_response, build_client +from yawa_ops.utils.cli_utils import string_to_dict log = logutils.get_logger(__name__) +MAIL_TYPES = [ + str(t) for t in [ + SendMailRequest.MetaOapg.properties.mailType.USER_CREATION_PENDING, + SendMailRequest.MetaOapg.properties.mailType.USER_CREATION_CONFIRMED, + SendMailRequest.MetaOapg.properties.mailType.USER_DELETION_PENDING, + SendMailRequest.MetaOapg.properties.mailType.USER_DELETION_CONFIRMED, + SendMailRequest.MetaOapg.properties.mailType.PASSWORD_RESET_PENDING, + SendMailRequest.MetaOapg.properties.mailType.PASSWORD_RESET_CONFIRMED + ] +] + @click.command(help="Send an email.", cls=BaseCommand) @click.pass_context -def send_mail(ctx, endpoint, identity, access_token, verify_ssl, ca_file, debug): +@click.option( + "--mail-type", + required=True, + help="Mail type.", + type=click.Choice(MAIL_TYPES, case_sensitive=False) +) +@click.option( + "--attributes", + required=False, + help="Attributes to inject in the email template, as comma separated list of key=value pairs.", +) +def send_mail(ctx, endpoint, profile, verify_ssl, ca_file, debug, mail_type, attributes): with build_client(**ctx.obj.get("CLIENT_CONFIG")) as api_client: try: - request = SendMailRequest(mailType="TEST_PLAIN") + request = SendMailRequest(mailType=mail_type, attributes=string_to_dict(attributes)) response = SendMail(api_client).send_mail(body=request) print_response(response) except yawac.ApiException as e: diff --git a/ops/src/yawa_ops/utils/cli_utils.py b/ops/src/yawa_ops/utils/cli_utils.py new file mode 100644 index 0000000..40c837e --- /dev/null +++ b/ops/src/yawa_ops/utils/cli_utils.py @@ -0,0 +1,15 @@ +def string_to_dict(s: str): + result = {} + elements = s.strip().split(",") if s else [] + for element in elements: + pair = element.strip().split("=") + key, value = pair[0], pair[1] + result[key] = value + return result + + +if __name__ == "__main__": + print(string_to_dict("a=1")) + print(string_to_dict("a=1,b=2")) + print(string_to_dict("")) + print(string_to_dict(None))