Odyssey
Classes | Functions | Variables
CreateCustomVerificationTemplate Namespace Reference

Classes

class  CustomArgumentParser
 

Functions

def get_email_defaults ()
 
def get_parser ()
 
def main (argv)
 
def run ()
 

Variables

 AWS_REGION = os.getenv('AWS_REGION', 'us-west-2')
 
 VERIFICATION_TEMPLATE
 
tuple USAGE
 

Detailed Description

Create AWS SES custom verification email template

This script is used to mimic the following aws ses custom verification
email template creation function:

aws ses create-custom-verification-email-template
    --template-name <template-name>
    --from-email-address <from-email-address>
    --template-subject <template-subject>
    --template-content <template-content>
    --success-redirection-url <success-redirection-url>
    --failure-redirection-url <failure-redirection-url>


Default AWS_REGION is `us-west-2` but the tempalte can be created in other
regions by setting up AWS_REGION env variable (check aws docuementation
for restrictions in certain regions.)

As os 2018-10-02, Update and Deletion of existing templates need to be
performed using the aws account with sufficient permissions to perform those
operations. If the local dev user is decided to grant such permissions in
the future, this script can be extended for deletion and update of the
existing custom verification email templates.

As of 2018-10-02, you can only run 4
`create-custom-verification-email-template` aws ses requests in a day in each
region.

Function Documentation

◆ get_email_defaults()

def CreateCustomVerificationTemplate.get_email_defaults ( )
return custom email verification template defaults

Definition at line 54 of file CreateCustomVerificationTemplate.py.

54 def get_email_defaults():
55  '''return custom email verification template defaults'''
56  contents = None
57  with open(VERIFICATION_TEMPLATE, 'r') as fh:
58  contents = ''.join(fh.readlines())
59 
60  return {
61  "sender": "support@homecu.com",
62  "subject": "Please confirm your email address",
63  "content": contents,
64  "success": ("https://my.homecu.net/admbin/static/html/"
65  "AdmEmailSuccess.html"),
66  "failure": ("https://my.homecu.net/admbin/static/html/"
67  "AdmEmailFailure.html"),
68  }
69 
70 

◆ get_parser()

def CreateCustomVerificationTemplate.get_parser ( )
Prepare argument parser.

Returns:
    parser -- ArgumentParser object

Definition at line 98 of file CreateCustomVerificationTemplate.py.

98 def get_parser():
99  """Prepare argument parser.
100 
101  Returns:
102  parser -- ArgumentParser object
103  """
104  defaults = get_email_defaults()
105  parser = CustomArgumentParser(
106  description="AWS SES Create New Custom Email Verification Template."
107  )
108 
109  parser.add_argument("-tn", "--template-name",
110  help=('Name of the custom verification email template'
111  '(required)'),
112  required=True
113  )
114 
115  parser.add_argument("-fe", "--from-email-address",
116  help="Sender's email address (from email address)",
117  default=defaults["sender"]
118  )
119 
120  parser.add_argument("-su", "--subject",
121  help="Email subject",
122  default=defaults["subject"]
123  )
124 
125  parser.add_argument("-c", "--content",
126  help=('Email content (single lined json text with no '
127  'breaks)'),
128  default=defaults["content"]
129  )
130 
131  parser.add_argument("-s", "--success",
132  help="Success redirection url",
133  default=defaults["success"]
134  )
135 
136  parser.add_argument("-f", "--failure",
137  help="Failure redirection url",
138  default=defaults["failure"]
139  )
140  return parser
141 
142 

◆ main()

def CreateCustomVerificationTemplate.main (   argv)
Main method to create custom verification template

Arguments:
    argv {list} -- list of script arguments

Raises:
    SystemExit -- [description]

Definition at line 143 of file CreateCustomVerificationTemplate.py.

143 def main(argv):
144  """Main method to create custom verification template
145 
146  Arguments:
147  argv {list} -- list of script arguments
148 
149  Raises:
150  SystemExit -- [description]
151  """
152  main_return = {
153  'status': '000',
154  'error': '',
155  'response': {}
156  }
157 
158  # get argument parser
159  parser = get_parser()
160  try:
161  args = parser.parse_args(argv)
162  except (argparse.ArgumentError, Exception) as e:
163  main_return = {
164  'status': '999',
165  'error': e,
166  'response': {}
167  }
168  print(json.dumps(main_return))
169  raise SystemExit(2)
170 
171  response = None
172  excption = False
173  try:
174 
175  # Create boto3 client object
176  client = boto3.client('ses', region_name=AWS_REGION)
177 
178  # Create custom verification email template on a specified region
179  response = client.create_custom_verification_email_template(
180  TemplateName=args.template_name,
181  FromEmailAddress=args.from_email_address,
182  TemplateSubject=args.subject,
183  TemplateContent=args.content,
184  SuccessRedirectionURL=args.success,
185  FailureRedirectionURL=args.failure
186  )
187 
188  except ClientError as e:
189  main_return['status'] = '999'
190  main_return['error'] = e.response["Error"]["Message"]
191  excption = True
192  except BaseException as e:
193  main_return['status'] = '999'
194  main_return['error'] = str(e)
195  excption = True
196  except Exception as e:
197  main_return['status'] = '999'
198  main_return['error'] = e
199  excption = True
200 
201  main_return["response"] = response
202  print(json.dumps(main_return))
203 
204  if excption:
205  raise SystemExit(2)
206 
207 

◆ run()

def CreateCustomVerificationTemplate.run ( )
Script entrypoint

Definition at line 208 of file CreateCustomVerificationTemplate.py.

208 def run():
209  """Script entrypoint"""
210  main(sys.argv[1:])
211 
212 

Variable Documentation

◆ USAGE

tuple CreateCustomVerificationTemplate.USAGE
Initial value:
1 = ("USAGE: CreateCustomVerificationTemplate.py [-h] -tn TEMPLATE_NAME "
2  "[-fe FROM_EMAIL_ADDRESS] [-su SUBJECT] [-c CONTENT]"
3  "[-s SUCCESS] [-f FAILURE]")

Definition at line 49 of file CreateCustomVerificationTemplate.py.

◆ VERIFICATION_TEMPLATE

CreateCustomVerificationTemplate.VERIFICATION_TEMPLATE
Initial value:
1 = os.getenv('DEFAULT_VERIFICATION_TEMPLATE_PATH',
2  ('/home/homecu/tmp/aws/ses/'
3  'default_verification_email_template'))

Definition at line 45 of file CreateCustomVerificationTemplate.py.