Skip to content

Commit

Permalink
[Ops] Add command for the API SendMail
Browse files Browse the repository at this point in the history
  • Loading branch information
gmarciani committed Nov 3, 2024
1 parent 85f9191 commit f767fcf
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
29 changes: 26 additions & 3 deletions ops/src/yawa_ops/commands/admin.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
15 changes: 15 additions & 0 deletions ops/src/yawa_ops/utils/cli_utils.py
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit f767fcf

Please sign in to comment.