コード例 #1
0
    def test_send_email_error_call_send_email(self):
        trace_id = DataTestSes.TRACE_ID
        region_name = DataTestSes.REGIONS
        sender = DataTestSes.SENDER
        subject = DataTestSes.SUBJECT
        bcc = DataTestSes.BCC
        body = DataTestSes.BODY
        error_code = mock_common_utils.get_error_code()
        error_message = mock_common_utils.get_error_message()
        client = boto3.client(service_name='ses', region_name=region_name)
        response = {'ResponseMetadata': {'HTTPStatusCode': 200}}
        with patch.object(boto3, 'client', return_value=client):
            with patch.object(client, 'send_email',
                              return_value=response) as mock_send_email:
                mock_send_email.side_effect = ClientError(
                    {'Error': {
                        'Code': error_code,
                        'Message': error_message
                    }}, 'description error')

                # call function test
                with self.assertRaises(PmError) as exception:
                    aws_common.send_email(trace_id, region_name, sender, bcc,
                                          subject, body)

        # check result
        cause_error = exception.exception.cause_error.response['Error']
        self.assertEqual(cause_error['Code'], error_code)
        self.assertEqual(cause_error['Message'], error_message)
コード例 #2
0
    def test_send_email_success_with_status_code_200(self):
        trace_id = DataTestSes.TRACE_ID
        region_name = DataTestSes.REGIONS
        sender = DataTestSes.SENDER
        subject = DataTestSes.SUBJECT
        bcc = DataTestSes.BCC
        body = DataTestSes.BODY
        Destination = DataTestSes.DESTINATION
        Message = DataTestSes.MESSAGE
        client = boto3.client(service_name='ses', region_name=region_name)
        response = {'ResponseMetadata': {'HTTPStatusCode': 200}}
        with patch.object(boto3, 'client', return_value=client):

            # call function test
            with patch.object(client, 'send_email',
                              return_value=response) as mock_send_email:
                aws_common.send_email(trace_id, region_name, sender, bcc,
                                      subject, body)
        mock_send_email.assert_called_once_with(Source=sender,
                                                Destination=Destination,
                                                Message=Message)
コード例 #3
0
    def test_send_email_error_client_connect_ses(self):
        trace_id = DataTestSes.TRACE_ID
        region_name = DataTestSes.REGIONS
        sender = DataTestSes.SENDER
        subject = DataTestSes.SUBJECT
        bcc = DataTestSes.BCC
        body = DataTestSes.BODY
        self.create_mock_boto3_client_error()
        mock_common_utils.set_error_response("500", "ERROR")
        with patch.object(PmLogAdapter, 'error',
                          return_value=None) as error_method:

            # call function test
            with self.assertRaises(PmError) as exception:
                aws_common.send_email(trace_id, region_name, sender, bcc,
                                      subject, body)

        # check result
        cause_error = exception.exception.cause_error.response['Error']
        self.assertEqual(cause_error['Code'],
                         mock_common_utils.get_error_code())
        self.assertEqual(cause_error['Message'],
                         mock_common_utils.get_error_message())
        error_method.assert_any_call("[%s] SESクライアント作成に失敗しました。", region_name)
コード例 #4
0
ファイル: user_logic.py プロジェクト: phamkhachoa/prjs_sample
def apply_change_email(user_id, mail_before_change, data_body):
    pm_logger = common_utils.begin_logger(user_id, __name__,
                                          inspect.currentframe())
    # Parse JSON
    try:
        body_object = json.loads(data_body)
    except Exception as e:
        return common_utils.error_exception(MsgConst.ERR_REQUEST_202,
                                            HTTPStatus.BAD_REQUEST, e,
                                            pm_logger, True)

    mail_after_change = common_utils.get_value("mailAddress", body_object,
                                               None)
    caller_service_name = common_utils.get_value("callerServiceName",
                                                 body_object, None)

    mail_lang = common_utils.get_value("mailLang", body_object, None)

    # validate
    list_errors = validate_param_apply_change_email(user_id, mail_lang,
                                                    caller_service_name,
                                                    mail_before_change)
    if list_errors:
        return common_utils.error_validate(MsgConst.ERR_REQUEST_201,
                                           HTTPStatus.UNPROCESSABLE_ENTITY,
                                           list_errors, pm_logger)

    # Cognito UserPoolsから変更するメールアドレス{mailaddress}に該当するユーザー情報情報を取得します。
    try:
        list_users = aws_common.get_cognito_user_pools(user_id,
                                                       mail_after_change,
                                                       "email")
    except PmError as e:
        return common_utils.error_exception(MsgConst.ERR_COGNITO_501,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)
    if list_users:
        return common_utils.error_common(MsgConst.ERR_302, HTTPStatus.CONFLICT,
                                         pm_logger)

    # メールアドレス変更申請テーブルから申請レコードを取得します。
    try:
        list_email_change_apply = pm_emailChangeApply.query_user_index(user_id)
    except PmError as e:
        return common_utils.error_exception(MsgConst.ERR_402,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)
    if list_email_change_apply:
        return common_utils.error_common(MsgConst.ERR_302, HTTPStatus.CONFLICT,
                                         pm_logger)

    # メールアドレス変更申請テーブルに申請レコードを作成します。
    apply_id = common_utils.get_uuid4()
    time_to_live = common_utils.get_time_to_live(
        CommonConst.EMAIL_CHANGE_APPLY_EXPIRATION_DATE)
    try:
        pm_emailChangeApply.create(user_id, apply_id, mail_before_change,
                                   mail_after_change, time_to_live,
                                   caller_service_name)
    except PmError as e:
        return common_utils.error_exception(MsgConst.ERR_DB_403,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)

    # get record PM_EmailChangeApply
    try:
        result = pm_emailChangeApply.query_key(user_id,
                                               apply_id,
                                               convert_response=True)
    except PmError as e:
        return common_utils.error_exception(MsgConst.ERR_402,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)

    # S3から通知メール送信設定ファイルを取得します。
    try:
        config = FileUtils.read_yaml(user_id, CommonConst.S3_SETTING_BUCKET,
                                     CommonConst.NOTIFY_CONFIG_CIS_RESULT_MAIL)
    except PmError as e:
        pm_logger.error(
            "メールアドレス変更通知メール送信設定ファイルの取得に失敗しました。:s3://%s/%s",
            common_utils.get_environ(CommonConst.S3_SETTING_BUCKET),
            CommonConst.NOTIFY_CONFIG_CIS_RESULT_MAIL)
        return common_utils.error_exception(MsgConst.ERR_S3_702,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)

    path_file_template = config[
        CommonConst.KEY_GET_PATH_FILE_TEMPLATE_MAIL_SERVICE.format(
            language=mail_lang, serviceName=caller_service_name)]
    # 通知メール本文を作成
    try:
        template_body_mail = FileUtils.read_decode(
            user_id, CommonConst.S3_SETTING_BUCKET, path_file_template)
    except PmError as e:
        pm_logger.error(
            "メールアドレス変更通知メール本文テンプレートファイルの取得に失敗しました。:s3://%s/%s",
            common_utils.get_environ(CommonConst.S3_SETTING_BUCKET),
            path_file_template)
        return common_utils.error_exception(MsgConst.ERR_S3_702,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)

    # SESで通知メールを送信します。
    bcc_addresses = [mail_after_change]
    template_body_mail = Template(template_body_mail)
    body_mail = template_body_mail.render(ApplyID=apply_id)
    mail_subject = config[CommonConst.KEY_MAIL_SUBJECT_SERVICE.format(
        language=mail_lang, serviceName=caller_service_name)]
    mail_form = config[CommonConst.KEY_MAIL_FROM_SERVICE.format(
        serviceName=caller_service_name)]

    try:
        aws_common.send_email(user_id, config['ses.region'], mail_form,
                              bcc_addresses, mail_subject, body_mail)
    except PmError as e:
        pm_logger.error("通知メール送信に失敗しました。")
        return common_utils.error_exception(MsgConst.ERR_SES_801,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)

    # return data response
    response = common_utils.get_response_by_response_body(
        HTTPStatus.CREATED, result)
    return common_utils.response(response, pm_logger)
コード例 #5
0
def send_result_email(trace_id, check_history_id, language):
    pm_logger = common_utils.begin_logger(trace_id, __name__,
                                          inspect.currentframe())
    # チェック結果テーブルのCheckHistoryIndexから、チェック履歴ID{CheckHistoryId}をキーとしてチェック結果一覧を取得します。
    try:
        list_check_result = pm_checkResults.query_history_index(
            trace_id, check_history_id)
    except PmError as e:
        msg_err = "チェック結果の取得に失敗しました。"
        pm_logger.error(msg_err)
        raise common_utils.write_log_pm_error(e, pm_logger, msg_err)

    if (not list_check_result):
        msg_err = "チェック結果の取得に失敗しました。"
        pm_logger.error(msg_err)
        raise PmError(message=msg_err)

    # チェック結果一覧のうちの一つから、組織IDを取得します。
    organization_id = list_check_result[0]['OrganizationID']
    organization_name = list_check_result[0]['OrganizationName']
    project_name = list_check_result[0]['ProjectName']

    # 宛先ユーザーの確認をします。ユーザー所属テーブルのOrganizationIndexから、組織IDをキー
    try:
        users = pm_affiliation.query_users_organization_index(
            trace_id, organization_id, InvitationStatus.Belong)
    except PmError as e:
        msg_err = "ユーザー所属情報の取得に失敗しました。:OrganizationID={0}".format(
            organization_id)
        pm_logger.error(msg_err)
        raise common_utils.write_log_pm_error(e, pm_logger, msg_err)

    # 組織別通知メール宛先テーブルから、組織ID、通知コードCHECK_CISをキーとして宛先情報を取得します。
    try:
        org_notify_mail_destinations = pm_orgNotifyMailDestinations.query_key(
            trace_id, organization_id, CommonConst.NOTIFY_CODE)
    except PmError as e:
        msg_err = "宛先情報の取得に失敗しました。:OrganizationID={0}, CheckCode={1}".format(
            organization_id, CommonConst.NOTIFY_CODE)
        pm_logger.error(msg_err)
        raise common_utils.write_log_pm_error(e, pm_logger, msg_err)

    if (not org_notify_mail_destinations):
        msg_err = "宛先情報の取得に失敗しました。:OrganizationID={0}, CheckCode={1}".format(
            organization_id, CommonConst.NOTIFY_CODE)
        pm_logger.error(msg_err)
        raise PmError(message=msg_err)

    destinations = []
    for destination in org_notify_mail_destinations['Destinations']:
        for user in users:
            if destination['UserID'] == user['UserID']:
                destinations.append(destination)
                break
    try:
        if (len(destinations) == 0):
            pm_orgNotifyMailDestinations.delete(trace_id, organization_id,
                                                CommonConst.NOTIFY_CODE)
        else:
            attribute = {'Destinations': {"Value": destinations}}
            pm_orgNotifyMailDestinations.update(trace_id, organization_id,
                                                CommonConst.NOTIFY_CODE,
                                                attribute)
    except PmError as e:
        msg_err = "通知メール宛先ユーザーの更新に失敗しました。:OrganizationID={0}, CheckCode={1}".format(
            organization_id, CommonConst.NOTIFY_CODE)
        pm_logger.error(msg_err)
        raise common_utils.write_log_pm_error(e, pm_logger, msg_err)

    # S3から通知メール送信設定ファイルを取得します。
    try:
        config = FileUtils.read_yaml(trace_id, CommonConst.S3_SETTING_BUCKET,
                                     CommonConst.NOTIFY_CONFIG_CIS_RESULT_MAIL)
    except PmError as e:
        msg_err = "通知メール送信設定ファイルの取得に失敗しました。:s3://{0}/{1}".format(
            common_utils.get_environ(CommonConst.S3_SETTING_BUCKET),
            CommonConst.NOTIFY_CONFIG_CIS_RESULT_MAIL)
        pm_logger.error(msg_err)
        raise common_utils.write_log_pm_error(e, pm_logger, msg_err)

    language_mail = CommonConst.LANGUAGE_DEFAULT
    if language in CommonConst.LANGUAGE_SUPPORT:
        language_mail = language

    path_file_template = config[CommonConst.KEY_GET_PATH_FILE_TEMPLATE_MAIL.
                                format(language=language_mail)]
    mail_subject_config = config[CommonConst.KEY_MAIL_SUBJECT.format(
        language=language_mail)]

    # 通知メール本文を作成
    try:
        template_body = FileUtils.read_decode(trace_id,
                                              CommonConst.S3_SETTING_BUCKET,
                                              path_file_template)
    except PmError as e:
        msg_err = "知メール本文テンプレートファイルの取得に失敗しました。:s3://{0}/{1}".format(
            common_utils.get_environ(CommonConst.S3_SETTING_BUCKET),
            path_file_template)
        pm_logger.error(msg_err)
        raise common_utils.write_log_pm_error(e, pm_logger, msg_err)

    # 通知メール件名を作成
    subject_template = Template(mail_subject_config)
    mail_subject = subject_template.render(project_name=project_name)

    # メール本文
    executed_date_time = date_utils.toString(
        date_utils.toDate(list_check_result[0]['ExecutedDateTime'],
                          date_utils.UTC), date_utils.PATTERN_YYYYMMDDHHMM,
        date_utils.ASIA_TOKYO)

    executed_date_time_utc = date_utils.toString(
        date_utils.toDate(list_check_result[0]['ExecutedDateTime'],
                          date_utils.UTC), date_utils.PATTERN_YYYYMMDDHHMM,
        date_utils.UTC)

    check_results = []
    for check_result in list_check_result:
        account_aws = CommonConst.AWS_ACCOUNT_MAIL.format(
            check_result['AWSAccount'][:4], check_result['AWSAccount'][4:8])
        check_result = {
            "accountAWS": account_aws,
            "okCount": check_result['OKCount'],
            "ngCount": check_result['NGCount'],
            "criticalCount": check_result['CriticalCount'],
            "managedCount": check_result['ManagedCount'],
            "errorCount": check_result['ErrorCount']
        }
        check_results.append(check_result)

    template = Template(template_body)
    context = {
        'organizationName': organization_name,
        'projectName': project_name,
        'executedDateTime': executed_date_time,
        'executedDateTimeUTC': executed_date_time_utc,
        'checkResults': check_results
    }
    body = template.render(context)

    # SESで通知メールを送信します。
    list_bcc_addresses = []
    bcc_addresses = []
    for index, destination in enumerate(destinations):
        bcc_addresses.append(destination['MailAddress'])
        if (len(bcc_addresses) >= 50 or len(destinations) == index + 1):
            list_bcc_addresses.append(bcc_addresses)
            bcc_addresses = []

    for index, bcc_addresses in enumerate(list_bcc_addresses):
        try:
            aws_common.send_email(trace_id, config['ses.region'],
                                  config['mail.from'], bcc_addresses,
                                  mail_subject, body)
        except PmError as e:
            msg_err = "通知メール送信に失敗しました。({0}/{1})".format(
                index + 1, len(list_bcc_addresses))
            pm_logger.error(msg_err)
            raise common_utils.write_log_pm_error(e, pm_logger, msg_err)
コード例 #6
0
def send_checkerror_email(trace_id, aws_account, check_history_id,
                          organization_id, project_id, error_code,
                          execute_user_id, region_name, check_code_item,
                          data_body):
    pm_logger = common_utils.begin_logger(trace_id, __name__,
                                          inspect.currentframe())

    # 組織情報を取得する。
    try:
        organization = pm_organizations.get_organization(
            trace_id, organization_id)
    except PmError as e:
        msg_err = "組織情報の取得に失敗しました。OrganizationID={0}".format(organization_id)
        pm_logger.error(msg_err)
        raise common_utils.write_log_pm_error(e, pm_logger, msg_err)

    # プロジェクト情報を取得する。
    try:
        project = pm_projects.query_key(trace_id, project_id)
    except PmError as e:
        msg_err = "プロジェクト情報の取得に失敗しました。ProjectID={0}".format(project_id)
        pm_logger.error(msg_err)
        raise common_utils.write_log_pm_error(e, pm_logger, msg_err)

    # チェック実行ユーザーのメールアドレスと言語を取得します。
    try:
        user_info = aws_common.get_cognito_user_info_by_user_name(
            trace_id, execute_user_id)
    except PmError as e:
        msg_err = "Cognitoから情報取得に失敗しました。"
        pm_logger.error(msg_err)
        raise common_utils.write_log_pm_error(e, pm_logger, msg_err)

    language = jmespath.search("[?Name=='locale'].Value | [0]",
                               user_info['UserAttributes'])
    mail_execute_user = jmespath.search("[?Name=='email'].Value | [0]",
                                        user_info['UserAttributes'])

    # S3から通知メール送信設定ファイルを取得します。
    try:
        config = FileUtils.read_yaml(trace_id, CommonConst.S3_SETTING_BUCKET,
                                     CommonConst.NOTIFY_CONFIG_CIS_RESULT_MAIL)
    except PmError as e:
        msg_err = "通知メール送信設定ファイルの取得に失敗しました。:s3://{0}/{1}".format(
            common_utils.get_environ(CommonConst.S3_SETTING_BUCKET),
            CommonConst.NOTIFY_CONFIG_CIS_RESULT_MAIL)
        pm_logger.error(msg_err)
        raise common_utils.write_log_pm_error(e, pm_logger, msg_err)

    language_mail = CommonConst.LANGUAGE_ENGLISH
    if language in CommonConst.LANGUAGE_SUPPORT:
        language_mail = language

    path_file_template_check_error_notice_mail = config[
        CommonConst.KEY_GET_PATH_FILE_TEMPLATE_CHECK_ERROR_NOTICE_MAIL.format(
            language=language_mail)]
    path_file_message_notice_error_execute_check_security = config[
        CommonConst.KEY_GET_PATH_MESSAGE_NOTICE_ERROR_EXECUTE_CHECK_SECURITY.
        format(language=language_mail)]
    mail_subject_config = config[
        CommonConst.KEY_CHECK_ERROR_NOTICE_MAIL_SUBJECT.format(
            language=language_mail)]

    # エラー通知内容設定ファイルを取得します。
    try:
        messages_notice_error_execute_check_security = FileUtils.read_yaml(
            trace_id, CommonConst.S3_SETTING_BUCKET,
            path_file_message_notice_error_execute_check_security)
    except PmError as e:
        msg_err = "エラー通知内容設定ファイルの取得に失敗しました。:s3://{0}/{1}".format(
            common_utils.get_environ(CommonConst.S3_SETTING_BUCKET),
            path_file_message_notice_error_execute_check_security)
        pm_logger.error(msg_err)
        raise common_utils.write_log_pm_error(e, pm_logger, msg_err)

    # S3から通知メール本文テンプレートファイルを取得します。
    try:
        template_body = FileUtils.read_decode(
            trace_id, CommonConst.S3_SETTING_BUCKET,
            path_file_template_check_error_notice_mail)
    except PmError as e:
        msg_err = "通知メール本文テンプレートファイルの取得に失敗しました。:s3://{0}/{1}".format(
            common_utils.get_environ(CommonConst.S3_SETTING_BUCKET),
            path_file_template_check_error_notice_mail)
        pm_logger.error(msg_err)
        raise common_utils.write_log_pm_error(e, pm_logger, msg_err)

    # に該当する通知内容を取得します。
    content_message = messages_notice_error_execute_check_security[
        CommonConst.KEY_GET_MESSAGE_NOTICE_ERROR_EXECUTE_CHECK_SECURITY.format(
            errorCode=error_code)]
    if data_body is not None:
        content_message = content_message.format_map(data_body)

    # 通知メール件名を作成
    subject_template = Template(mail_subject_config)
    project_name = project['ProjectName']
    mail_subject = subject_template.render(project_name=project_name)
    if check_code_item is not None:
        check_code_item = CommonConst.LIST_CHECK_ITEM_CODE_CONVERT[
            check_code_item]

    template = Template(template_body)
    context = {
        'organizationName': organization['OrganizationName'],
        'projectName': project_name,
        'checkCodeItem': check_code_item,
        'awsAccount': aws_account,
        'regionName': region_name,
        'contentMessage': content_message
    }
    body = template.render(context)

    # SESで通知メールを送信します
    bcc_addresses = [mail_execute_user]

    try:
        aws_common.send_email(trace_id, config['ses.region'],
                              config['mail.from'], bcc_addresses, mail_subject,
                              body)
    except PmError as e:
        msg_err = "通知メール送信に失敗しました。"
        pm_logger.error(msg_err)
        raise common_utils.write_log_pm_error(e, pm_logger, msg_err)
コード例 #7
0
def execute_force_invites(trace_id, body_object, organization_id):
    pm_logger = common_utils.begin_logger(trace_id, __name__,
                                          inspect.currentframe())

    # parse json
    try:
        body_object_json = json.loads(body_object)
    except Exception as e:
        return common_utils.error_exception(MsgConst.ERR_REQUEST_202,
                                            HTTPStatus.BAD_REQUEST, e,
                                            pm_logger, True)

    caller_service_name = common_utils.get_value("callerServiceName",
                                                 body_object_json, None)
    mail_lang = common_utils.get_value("mailLang", body_object_json, None)
    mail_address = common_utils.get_value("mailAddress", body_object_json,
                                          None)
    authority = common_utils.get_value("authority", body_object_json, None)

    # validate param execute invite unregistered
    list_error = validate_param_invite_unregistered_user(
        trace_id, mail_lang, caller_service_name, mail_address, authority)
    if list_error:
        return common_utils.error_validate(MsgConst.ERR_REQUEST_201,
                                           HTTPStatus.UNPROCESSABLE_ENTITY,
                                           list_error, pm_logger)

    # get list cognito users
    try:
        list_users = aws_common.get_cognito_user_pools(trace_id, mail_address)
    except PmError as e:
        return common_utils.error_exception(MsgConst.ERR_COGNITO_501,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)
    if list_users:
        return common_utils.error_common(MsgConst.ERR_302, HTTPStatus.CONFLICT,
                                         pm_logger)

    # regist Cognito UserPools
    temporary_password = ''
    pattern = re.compile(CommonConst.FORMAT_PASSWORD_TEMPORARY)
    while pattern.match(temporary_password) is None:
        temporary_password = common_utils.get_password_temporary(
            CommonConst.NUMBER_CHARACTERS_PASSWORD_TEMPORARY)

    user_attributes = [{"Name": "email", "Value": mail_address}]
    user_name = common_utils.get_uuid4()
    message_action = MessageAction.Suppress
    try:
        aws_common.process_admin_create_user_pools(trace_id, user_name,
                                                   user_attributes,
                                                   temporary_password,
                                                   message_action)
    except PmError as e:
        return common_utils.error_exception(MsgConst.ERR_COGNITO_501,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)

    # enable confirm email
    try:
        user_attributes = [{'Name': 'email_verified', 'Value': 'true'}]
        aws_common.update_cognito_user_attributes(trace_id, user_name,
                                                  user_attributes)
    except PmError as e:
        return common_utils.error_exception(MsgConst.ERR_COGNITO_501,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)

    # get affiliation
    try:
        affiliation = pm_affiliation.get_affiliation(user_name,
                                                     organization_id)
    except PmError as e:
        return common_utils.error_exception(MsgConst.ERR_402,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)
    if affiliation and affiliation["InvitationStatus"] != InvitationStatus.Deny:
        return common_utils.error_common(MsgConst.ERR_302, HTTPStatus.CONFLICT,
                                         pm_logger)

    # get organization
    try:
        organization = pm_organizations.get_organization(
            trace_id, organization_id)
    except PmError as e:
        return common_utils.error_exception(MsgConst.ERR_402,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)
    if len(organization) == 0:
        return common_utils.error_common(MsgConst.ERR_301,
                                         HTTPStatus.NOT_FOUND, pm_logger)

    # create affiliation
    try:
        pm_affiliation.create_affiliation(trace_id, mail_address, user_name,
                                          organization_id, authority,
                                          InvitationStatus.Invited)
    except PmError as e:
        return common_utils.error_exception(MsgConst.ERR_DB_403,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)

    # Get data affiliation
    try:
        affiliation_result = pm_affiliation.get_affiliation(
            user_name, organization_id, True)
    except PmError as e:
        return common_utils.error_exception(MsgConst.ERR_402,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)

    # Get data user_attribute
    try:
        user_attribute = pm_userAttribute.query_key(trace_id)
    except PmError as e:
        return common_utils.error_exception(MsgConst.ERR_402,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)

    # S3から通知メール送信設定ファイルを取得します。
    try:
        config = FileUtils.read_yaml(trace_id, CommonConst.S3_SETTING_BUCKET,
                                     CommonConst.NOTIFY_CONFIG_CIS_RESULT_MAIL)
    except PmError as e:
        pm_logger.error("メール送信設定ファイルの取得に失敗しました。:s3://{0}/{1}".format(
            common_utils.get_environ(CommonConst.S3_SETTING_BUCKET),
            CommonConst.NOTIFY_CONFIG_CIS_RESULT_MAIL))
        return common_utils.error_exception(MsgConst.ERR_S3_702,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)

    # メッセージ本文を作成します。
    path_file_template = config[
        CommonConst.KEY_GET_PATH_FILE_TEMPLATE_USER_INVITE_MAIL.format(
            language=mail_lang, serviceName=caller_service_name)]
    try:
        template_body_mail = FileUtils.read_decode(
            trace_id, CommonConst.S3_SETTING_BUCKET, path_file_template)
    except PmError as e:
        pm_logger.error("招待メール本文テンプレートファイルの取得に失敗しました。:s3://{0}/{1}".format(
            common_utils.get_environ(CommonConst.S3_SETTING_BUCKET),
            path_file_template))
        return common_utils.error_exception(MsgConst.ERR_S3_702,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)

    # SESで通知メールを送信します。
    bcc_addresses = [mail_address]
    user_name_sign_in = common_utils.get_value("UserName", user_attribute,
                                               None)
    if not user_name_sign_in:
        try:
            affiliation_sign_in = pm_affiliation.get_affiliation(
                trace_id, organization_id)
        except PmError as e:
            return common_utils.error_exception(
                MsgConst.ERR_402, HTTPStatus.INTERNAL_SERVER_ERROR, e,
                pm_logger, True)
        user_name_sign_in = common_utils.get_value("MailAddress",
                                                   affiliation_sign_in, None)

    organization_name = common_utils.get_value("OrganizationName",
                                               organization, None)
    time_zone = date_utils.get_time_zone_by_language(mail_lang)
    time_to_live_date = date_utils.get_current_date() + timedelta(days=6)
    time_to_live = date_utils.toString(time_to_live_date,
                                       date_utils.PATTERN_YYYYMMDD_SLASH,
                                       time_zone)
    template_body_mail = Template(template_body_mail)
    context = {
        'mailAddress': mail_address,
        'userName': user_name_sign_in,
        'organizationName': organization_name,
        'temporaryPassword': temporary_password,
        'timeToLive': time_to_live
    }
    body_mail = template_body_mail.render(context)
    mail_subject = config[CommonConst.KEY_MAIL_SUBJECT_USER_INVITE.format(
        language=mail_lang, serviceName=caller_service_name)]
    mail_from = config[CommonConst.KEY_INVITE_MAIL_FROM_SERVICE.format(
        serviceName=caller_service_name)]
    ses_region = config['ses.region']
    try:
        aws_common.send_email(user_name, ses_region, mail_from, bcc_addresses,
                              mail_subject, body_mail)
    except PmError as e:
        pm_logger.error("通知メール送信に失敗しました。")
        return common_utils.error_exception(MsgConst.ERR_SES_801,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)

    response = common_utils.get_response_by_response_body(
        HTTPStatus.CREATED, affiliation_result)

    # return data response
    return common_utils.response(response, pm_logger)