Example #1
0
def test():
    global my_sns_topic_uuid, email_endpoint_uuid, email_platform_uuid, event_template_uuid, \
        alarm_template_uuid, alarm_uuid_list, test_dict

    smtp_server = 'smtp.zstack.io'
    pop_server = 'pop3.zstack.io'
    smtp_port = 25
    username = '******'
    password = '******'
    email_platform_name = 'Alarm_email'
    email_platform = zwt_ops.create_sns_email_platform(smtp_server, smtp_port,
                                                       email_platform_name,
                                                       username, password)
    email_platform_uuid = email_platform.uuid
    try:
        zwt_ops.validate_sns_email_platform(email_platform_uuid)
    except:
        test_util.test_fail(
            'Validate SNS Email Platform Failed, Email Plarform: %s' %
            email_platform_uuid)
    email_endpoint_uuid = zwt_ops.create_sns_email_endpoint(
        username, 'test_qa', email_platform_uuid).uuid

    my_sns_topic = zwt_ops.create_sns_topic('my_sns_topic')
    my_sns_topic_uuid = my_sns_topic.uuid
    zwt_ops.subscribe_sns_topic(my_sns_topic_uuid, email_endpoint_uuid)

    # create alarm
    namespace = 'ZStack/Image'
    greater_than_or_equal_to = 'GreaterThanOrEqualTo'
    greater_than = 'GreaterThan'
    actions = [{"actionUuid": my_sns_topic_uuid, "actionType": "sns"}]

    period = 10

    threshold_1 = 1
    threshold_3 = 3
    threshold_10 = 10
    threshold_50 = 50

    total_image_count = 'TotalImageCount'
    total_image_count_alarm_uuid = zwt_ops.create_alarm(
        greater_than_or_equal_to,
        period,
        threshold_3,
        namespace,
        total_image_count,
        name='total-count-image',
        repeat_interval=600,
        actions=actions).uuid
    alarm_uuid_list.append(total_image_count_alarm_uuid)

    ready_image_count = 'ReadyImageCount'
    ready_image_count_alarm_uuid = zwt_ops.create_alarm(
        greater_than_or_equal_to,
        period,
        threshold_3,
        namespace,
        ready_image_count,
        name='ready_image_count',
        repeat_interval=600,
        actions=actions).uuid
    alarm_uuid_list.append(ready_image_count_alarm_uuid)

    ready_image_in_percent = 'ReadyImageInPercent'
    ready_image_in_percent_alarm_uuid = zwt_ops.create_alarm(
        greater_than_or_equal_to,
        period,
        threshold_1,
        namespace,
        ready_image_in_percent,
        name='ready_image_in_percent',
        repeat_interval=600,
        actions=actions).uuid
    alarm_uuid_list.append(ready_image_in_percent_alarm_uuid)

    root_volume_template_count = 'RootVolumeTemplateCount'
    root_volume_template_count_alarm_uuid = zwt_ops.create_alarm(
        greater_than_or_equal_to,
        period,
        threshold_3,
        namespace,
        root_volume_template_count,
        name='root_volume_template_count',
        repeat_interval=600,
        actions=actions,
    ).uuid
    alarm_uuid_list.append(root_volume_template_count_alarm_uuid)

    root_volume_template_in_percent = 'RootVolumeTemplateInPercent'
    root_volume_template_in_percent_alarm_uuid = zwt_ops.create_alarm(
        greater_than,
        period,
        threshold_1,
        namespace,
        root_volume_template_in_percent,
        name='root_volume_template_in_percent',
        repeat_interval=600,
        actions=actions).uuid
    alarm_uuid_list.append(root_volume_template_in_percent_alarm_uuid)

    data_volume_template_count = 'DataVolumeTemplateCount'
    data_volume_template_count_alarm_uuid = zwt_ops.create_alarm(
        greater_than_or_equal_to,
        period,
        threshold_3,
        namespace,
        data_volume_template_count,
        name='data_volume_template_count',
        repeat_interval=600,
        actions=actions).uuid
    alarm_uuid_list.append(data_volume_template_count_alarm_uuid)

    data_volume_template_in_percent = 'DataVolumeTemplateInPercent'
    data_volume_template_in_percent_alarm_uuid = zwt_ops.create_alarm(
        greater_than,
        period,
        threshold_1,
        namespace,
        data_volume_template_in_percent,
        name='data_volume_template_in_percent',
        repeat_interval=600,
        actions=actions).uuid
    alarm_uuid_list.append(data_volume_template_in_percent_alarm_uuid)

    iso_count = 'ISOCount'
    iso_count_alarm_uuid = zwt_ops.create_alarm(greater_than_or_equal_to,
                                                period,
                                                threshold_3,
                                                namespace,
                                                iso_count,
                                                name='iso_count',
                                                repeat_interval=600,
                                                actions=actions).uuid
    alarm_uuid_list.append(iso_count_alarm_uuid)

    iso_in_percent = 'ISOInPercent'
    iso_in_percent_alarm_uuid = zwt_ops.create_alarm(greater_than,
                                                     period,
                                                     threshold_1,
                                                     namespace,
                                                     iso_in_percent,
                                                     name='iso_in_percent',
                                                     repeat_interval=600,
                                                     actions=actions).uuid
    alarm_uuid_list.append(iso_in_percent_alarm_uuid)

    # create Image
    image_name = os.environ.get('imageName_s')
    l3_name = os.environ.get('l3VlanNetworkName1')
    vm_name = 'multihost_basic_vm'
    vm = test_stub.create_vm(vm_name, image_name, l3_name)
    test_dict.add_vm(vm)
    volume = test_stub.create_volume()
    test_dict.add_volume(volume)
    volume.attach(vm)
    zone_uuid = vm.get_vm().zoneUuid
    root_volume_uuid = test_lib.lib_get_root_volume_uuid(vm.get_vm())
    bs_uuid_list = test_lib.lib_get_backup_storage_uuid_list_by_zone(zone_uuid)

    image_option = test_util.ImageOption()
    image_option.set_root_volume_uuid(root_volume_uuid)
    image_option.set_format('qcow2')
    image_option.set_backup_storage_uuid_list(bs_uuid_list)
    # image_option.set_mediaType('ISO')

    for i in range(threshold_3):
        image_option.set_name('root_volume_template_for_test_' + str(i))
        root_volume_template = zstack_image_header.ZstackTestImage()
        root_volume_template.set_creation_option(image_option)
        root_volume_template.create()
        test_dict.add_image(root_volume_template)
        iso = test_stub.add_test_minimal_iso("iso_for_test_" + str(i))
        test_dict.add_image(iso)
    time.sleep(30)
    # before change template
    flag = zwt_ops.check_sns_email(pop_server, username, password,
                                   total_image_count,
                                   total_image_count_alarm_uuid)
    if flag != 1:
        test_util.test_fail('No send event email')
    flag = zwt_ops.check_sns_email(pop_server, username, password,
                                   ready_image_count,
                                   ready_image_count_alarm_uuid)
    if flag != 1:
        test_util.test_fail('No send event email')
    flag = zwt_ops.check_sns_email(pop_server, username, password,
                                   ready_image_in_percent,
                                   ready_image_in_percent_alarm_uuid)
    if flag != 1:
        test_util.test_fail('No send event email')
    flag = zwt_ops.check_sns_email(pop_server, username, password,
                                   root_volume_template_count,
                                   root_volume_template_count_alarm_uuid)
    if flag != 1:
        test_util.test_fail('No send event email')
    flag = zwt_ops.check_sns_email(pop_server, username, password,
                                   root_volume_template_in_percent,
                                   root_volume_template_in_percent_alarm_uuid)
    if flag != 1:
        test_util.test_fail('No send event email')
    flag = zwt_ops.check_sns_email(pop_server, username, password, iso_count,
                                   iso_count_alarm_uuid)
    if flag != 1:
        test_util.test_fail('No send event email')
    flag = zwt_ops.check_sns_email(pop_server, username, password,
                                   iso_in_percent, iso_in_percent_alarm_uuid)
    if flag != 1:
        test_util.test_fail('No send event email')

    alarm_keywords = 'TemplateForAlarmOn'

    if zwt_ops.check_keywords_in_email(pop_server, username, password,
                                       alarm_keywords,
                                       total_image_count_alarm_uuid):
        test_util.test_fail('email already exsist before test')
    if zwt_ops.check_keywords_in_email(pop_server, username, password,
                                       alarm_keywords,
                                       ready_image_count_alarm_uuid):
        test_util.test_fail('email already exsist before test')
    if zwt_ops.check_keywords_in_email(pop_server, username, password,
                                       alarm_keywords,
                                       ready_image_in_percent_alarm_uuid):
        test_util.test_fail('email already exsist before test')
    if zwt_ops.check_keywords_in_email(pop_server, username, password,
                                       alarm_keywords,
                                       root_volume_template_count_alarm_uuid):
        test_util.test_fail('email already exsist before test')
    if zwt_ops.check_keywords_in_email(
            pop_server, username, password, alarm_keywords,
            root_volume_template_in_percent_alarm_uuid):
        test_util.test_fail('email already exsist before test')
    if zwt_ops.check_keywords_in_email(pop_server, username, password,
                                       alarm_keywords,
                                       data_volume_template_count_alarm_uuid):
        test_util.test_fail('email already exsist before test')
    if zwt_ops.check_keywords_in_email(
            pop_server, username, password, alarm_keywords,
            data_volume_template_in_percent_alarm_uuid):
        test_util.test_fail('email already exsist before test')
    if zwt_ops.check_keywords_in_email(pop_server, username, password,
                                       alarm_keywords, iso_count_alarm_uuid):
        test_util.test_fail('email already exsist before test')
    if zwt_ops.check_keywords_in_email(pop_server, username, password,
                                       alarm_keywords,
                                       iso_in_percent_alarm_uuid):
        test_util.test_fail('email already exsist before test')

    application_platform_type = 'Email'
    alarm_template_name = 'my-alarm-template'
    alarm_template = '${ALARM_NAME} Change status to ${ALARM_CURRENT_STATUS}' \
                     'ALARM_UUID:${ALARM_UUID}' \
                     'keyword1:ThisWordIsKeyWord' \
                     'keyword2:TemplateForAlarmOn' \
                     '(Using for template changes email check)'
    alarm_template_uuid = zwt_ops.create_sns_text_template(
        alarm_template_name,
        application_platform_type,
        alarm_template,
        default_template=False).uuid

    event_template_name = 'my-event-template'
    event_keywords = 'TemplateForEventHappened'
    event_template = '${EVENT_NAME} IN ${EVENT_NAMESPACE}' \
                     'keyword1:ThisWordIsKeyWord' \
                     'keyword2:TemplateForEventHappened' \
                     'keyword3{PARAM_EVENT_SUBSCRIPTION_UUID}' \
                     '(Using for template changes email check)'
    event_template_uuid = zwt_ops.create_sns_text_template(
        event_template_name,
        application_platform_type,
        event_template,
        default_template=True).uuid

    # test update text template
    zwt_ops.update_sns_text_template(alarm_template_uuid,
                                     description='this is a new description',
                                     default_template=True)

    cond = res_ops.gen_query_conditions('uuid', '=', alarm_template_uuid)
    inv = res_ops.query_resource(res_ops.SNS_TEXT_TEMPLATE, cond)[0]
    if inv.defaultTemplate == False or inv.description != 'this is a new description':
        test_util.test_fail('change template fail')

    for i in range(threshold_3):
        data_volume_template = volume.create_template(
            bs_uuid_list, name="vol_temp_for_volume_test_" + str(i))
        test_dict.add_image(data_volume_template)

    # wait for reboot and send email
    time.sleep(30)
    test_lib.lib_robot_cleanup(test_dict)
    zwt_ops.delete_sns_text_template(alarm_template_uuid)
    zwt_ops.delete_sns_text_template(event_template_uuid)
    for alarm_uuid in alarm_uuid_list:
        zwt_ops.delete_alarm(alarm_uuid)
    zwt_ops.delete_sns_topic(my_sns_topic_uuid)
    zwt_ops.delete_sns_application_endpoint(email_endpoint_uuid)
    zwt_ops.delete_sns_application_platform(email_platform_uuid)

    if zwt_ops.check_keywords_in_email(
            pop_server, username, password, alarm_keywords,
            data_volume_template_count_alarm_uuid
    ) and zwt_ops.check_keywords_in_email(
            pop_server, username, password, alarm_keywords,
            data_volume_template_in_percent_alarm_uuid):
        test_util.test_pass('success check all keywords in the email')
    else:
        test_util.test_fail('cannt check all mail')
def test():
    global email_endpoint_uuid, email_platform_uuid, vm, disk_all_write_bytes_alarm_uuid, my_sns_topic_uuid
    smtp_server = 'smtp.zstack.io'
    pop_server = 'pop3.zstack.io'
    smtp_port = 25
    username = '******'
    password = '******'
    email_platform_name = 'Alarm_email'
    email_platform_uuid = zwt_ops.create_sns_email_platform(
        smtp_server, smtp_port, email_platform_name, username, password).uuid
    zwt_ops.validate_sns_email_platform(email_platform_uuid)
    email_endpoint_uuid = zwt_ops.create_sns_email_endpoint(
        username, 'test_qa', email_platform_uuid).uuid
    my_sns_topic = zwt_ops.create_sns_topic('my_sns_topic')
    my_sns_topic_uuid = my_sns_topic.uuid
    zwt_ops.subscribe_sns_topic(my_sns_topic_uuid, email_endpoint_uuid)

    namespace = 'ZStack/VM'
    less_than = 'LessThan'
    actions = [{"actionUuid": my_sns_topic_uuid, "actionType": "sns"}]
    period = 10
    threshold_3 = 1024 * 1024 * 1024
    disk_all_write_bytes = 'DiskAllWriteBytes'
    disk_all_write_bytes_alarm_uuid = zwt_ops.create_alarm(
        less_than,
        period,
        threshold_3,
        namespace,
        disk_all_write_bytes,
        name='disk_all_write_bytes',
        repeat_interval=20,
        actions=actions).uuid
    image_name = os.environ.get('imageName_s')
    l3_name = os.environ.get('l3VlanNetworkName1')
    vm = test_stub.create_vm('test_vm', image_name, l3_name)

    # wait for send email
    time.sleep(30)
    flag = zwt_ops.check_keywords_in_email(pop_server, username, password,
                                           disk_all_write_bytes,
                                           disk_all_write_bytes_alarm_uuid)
    if not flag:
        test_util.test_fail('cant receive alarm email ')

    zwt_ops.change_alarm_state(disk_all_write_bytes_alarm_uuid, 'disable')

    time.sleep(10)
    time_stamp = zwt_ops.get_boundary_words()
    zwt_ops.send_boundary_email(time_stamp)

    time.sleep(30)
    flag = zwt_ops.check_keywords_in_email(pop_server, username, password,
                                           disk_all_write_bytes,
                                           disk_all_write_bytes_alarm_uuid,
                                           time_stamp)
    if flag:
        test_util.test_fail(
            'the alarm had been changed state to Disabled,we should not receive the email,but we found flag is %s'
            % flag)

    time.sleep(10)
    zwt_ops.change_alarm_state(disk_all_write_bytes_alarm_uuid, 'enable')
    time.sleep(30)

    flag = zwt_ops.check_keywords_in_email(pop_server, username, password,
                                           disk_all_write_bytes,
                                           disk_all_write_bytes_alarm_uuid,
                                           time_stamp)
    if not flag:
        test_util.test_fail(
            'cant receive email when it change state to Enable.')

    vm.clean()
    zwt_ops.delete_alarm(disk_all_write_bytes_alarm_uuid)
    zwt_ops.delete_sns_topic(my_sns_topic_uuid)
    zwt_ops.delete_sns_application_endpoint(email_endpoint_uuid)
    zwt_ops.delete_sns_application_platform(email_platform_uuid)

    test_util.test_pass('success')
def test():
    global email_endpoint_uuid, email_platform_uuid, my_sns_topic_uuid, host_sns_topic_uuid, ps_uuid, hostname, host_management_ip, host_uuid, host_status, bs_uuid, bs_type, bs_status
    bs_cond = res_ops.gen_query_conditions('status', '=', 'Connected')
    bs_list = res_ops.query_resource(res_ops.BACKUP_STORAGE, bs_cond)
    for bss in bs_list:
        if bss.type == res_ops.SFTP_BACKUP_STORAGE:
            bs_uuid = bss.uuid
            bs_type = bss.type
            hostname = bss.hostname
            break
        elif bss.type == res_ops.IMAGE_STORE_BACKUP_STORAGE:
            bs_uuid = bss.uuid
            bs_type = bss.type
            hostname = bss.hostname
            break
        else:
            test_util.test_skip('No match backupStorage,test skip')

    smtp_server = os.environ.get('smtpServer')
    pop_server = os.environ.get('popServer')
    smtp_port = os.environ.get('smtpPort')
    username = os.environ.get('mailUsername')
    password = os.environ.get('mailPassword')
    email_platform_name = 'Alarm_email'
    email_platform = zwt_ops.create_sns_email_platform(smtp_server, smtp_port,
                                                       email_platform_name,
                                                       username, password)
    email_platform_uuid = email_platform.uuid
    try:
        zwt_ops.validate_sns_email_platform(email_platform_uuid)
    except:
        test_util.test_fail(
            'Validate SNS Email Platform Failed, Email Plarform: %s' %
            email_platform_uuid)
    email_endpoint_uuid = zwt_ops.create_sns_email_endpoint(
        username, 'test_qa', email_platform_uuid).uuid

    my_sns_topic = zwt_ops.create_sns_topic('my_sns_topic')
    my_sns_topic_uuid = my_sns_topic.uuid
    zwt_ops.subscribe_sns_topic(my_sns_topic_uuid, email_endpoint_uuid)

    host_sns_topic = zwt_ops.create_sns_topic('host_topic')
    host_sns_topic_uuid = host_sns_topic.uuid
    zwt_ops.subscribe_sns_topic(host_sns_topic_uuid, email_endpoint_uuid)

    ps_actions = [{"actionUuid": my_sns_topic_uuid, "actionType": "sns"}]
    ps_namespace = 'ZStack/PrimaryStorage'
    ps_disconnected = 'PrimaryStorageDisconnected'
    ps_event_sub_uuid = zwt_ops.subscribe_event(ps_namespace, ps_disconnected,
                                                ps_actions).uuid
    event_list.append(ps_event_sub_uuid)

    bs_actions = [{"actionUuid": my_sns_topic_uuid, "actionType": "sns"}]
    bs_namespace = 'ZStack/BackupStorage'
    bs_disconnected = 'BackupStorageDisconnected'
    bs_event_sub_uuid = zwt_ops.subscribe_event(bs_namespace, bs_disconnected,
                                                bs_actions).uuid
    event_list.append(bs_event_sub_uuid)

    host_actions = [{"actionUuid": host_sns_topic_uuid, "actionType": "sns"}]
    host_namespace = 'ZStack/Host'
    host_status_changed = 'HostStatusChanged'
    host_status_labels = [{
        "key": "NewStatus",
        "op": "Equal",
        "value": "Disconnected"
    }]
    host_status_event_sub_uuid = zwt_ops.subscribe_event(
        host_namespace, host_status_changed, host_actions,
        host_status_labels).uuid
    event_list.append(host_status_event_sub_uuid)

    host_disconnected = 'HostDisconnected'
    host_disconnected_event_sub_uuid = zwt_ops.subscribe_event(
        host_namespace, host_disconnected, host_actions).uuid
    event_list.append(host_disconnected_event_sub_uuid)

    if zwt_ops.check_sns_email(pop_server, username, password, ps_disconnected,
                               ps_event_sub_uuid):
        test_util.test_fail('email already exsist before test')
    if zwt_ops.check_sns_email(pop_server, username, password, bs_disconnected,
                               bs_event_sub_uuid):
        test_util.test_fail('email already exsist before test')
    if zwt_ops.check_sns_email(pop_server, username, password,
                               host_status_changed,
                               host_status_event_sub_uuid):
        test_util.test_fail('email already exsist before test')
    if zwt_ops.check_sns_email(pop_server, username, password,
                               host_disconnected,
                               host_disconnected_event_sub_uuid):
        test_util.test_fail('email already exsist before test')

    # Disconnected ps ,bs and host
    zone_uuid = res_ops.query_resource(res_ops.ZONE)[0].uuid
    primary_storage_option = test_util.PrimaryStorageOption()
    primary_storage_option.set_type('nfs')
    primary_storage_option.set_zone_uuid(zone_uuid)
    primary_storage_option.set_name('test_nfs_ps')
    primary_storage_option.set_url('222.222.222.222/nfs/')
    try:
        ps_uuid = ps_ops.create_nfs_primary_storage(
            primary_storage_option).uuid
        ps_ops.reconnect_primary_storage(ps_uuid)
    except:
        pass

    if bs_type == res_ops.IMAGE_STORE_BACKUP_STORAGE:
        bs_ops.update_image_store_backup_storage_info(bs_uuid, 'hostname',
                                                      '222.222.222.222')
        try:
            bs_ops.reconnect_backup_storage(bs_uuid)
        except:
            cond = res_ops.gen_query_conditions('uuid', '=', bs_uuid)
            bs_status = res_ops.query_resource(
                res_ops.IMAGE_STORE_BACKUP_STORAGE, cond)[0].status
    elif bs_type == res_ops.SFTP_BACKUP_STORAGE:
        bs_ops.update_sftp_backup_storage_info(bs_uuid, 'hostname',
                                               '222.222.222.222')
        try:
            bs_ops.reconnect_backup_storage(bs_uuid)
        except:
            cond = res_ops.gen_query_conditions('uuid', '=', bs_uuid)
            bs_status = res_ops.query_resource(res_ops.SFTP_BACKUP_STORAGE,
                                               cond)[0].status

    host_cond = res_ops.gen_query_conditions('status', '=', 'Connected')
    host = res_ops.query_resource_with_num(res_ops.HOST,
                                           host_cond,
                                           start=0,
                                           limit=1)[0]
    host_uuid = host.uuid
    host_management_ip = host.managementIp
    host_ops.update_host(host_uuid, 'managementIp', '222.222.222.222')
    try:
        host_ops.reconnect_host(host_uuid)
    except:
        cond = res_ops.gen_query_conditions('uuid', '=', host_uuid)
        bs_status = res_ops.query_resource(res_ops.HOST, cond)[0].status

    # wait for send email
    time.sleep(60)

    ps_ops.delete_primary_storage(ps_uuid)
    if hostname:
        if bs_type == res_ops.IMAGE_STORE_BACKUP_STORAGE:
            bs_ops.update_image_store_backup_storage_info(
                bs_uuid, 'hostname', hostname)
            bs_ops.reconnect_backup_storage(bs_uuid)
        elif bs_type == res_ops.SFTP_BACKUP_STORAGE:
            bs_ops.update_sftp_backup_storage_info(bs_uuid, 'hostname',
                                                   hostname)
            bs_ops.reconnect_backup_storage(bs_uuid)
    host_ops.update_host(host_uuid, 'managementIp', host_management_ip)
    host_ops.reconnect_host(host_uuid)
    zwt_ops.delete_sns_topic(host_sns_topic_uuid)
    zwt_ops.delete_sns_topic(my_sns_topic_uuid)
    for event_uuid in event_list:
        zwt_ops.unsubscribe_event(event_uuid)
    zwt_ops.delete_sns_application_endpoint(email_endpoint_uuid)
    zwt_ops.delete_sns_application_platform(email_platform_uuid)

    check_1 = zwt_ops.check_sns_email(pop_server, username, password,
                                      ps_disconnected, ps_event_sub_uuid)
    check_2 = zwt_ops.check_sns_email(pop_server, username, password,
                                      bs_disconnected, bs_event_sub_uuid)
    check_3 = zwt_ops.check_sns_email(pop_server, username, password,
                                      host_status_changed,
                                      host_status_event_sub_uuid)
    check_4 = zwt_ops.check_sns_email(pop_server, username, password,
                                      host_disconnected,
                                      host_disconnected_event_sub_uuid)

    if check_1 and check_2 and check_3 and check_4:
        test_util.test_pass(
            'test host ,bs, ps disconnected event with email success!')
    else:
        test_util.test_fail('cannt receive all event mail')
def test():
    global email_platform_uuid, email_endpoint_uuid, dingtalk_endpoint_uuid, http_endpoint_uuid, alarm_uuid, ps_event_sub_uuid, sns_topic_uuid, alarm_template_uuid
    iam2_ops.clean_iam2_enviroment()

    zone_uuid = res_ops.get_resource(res_ops.ZONE)[0].uuid
    # 3 create project
    project_name = 'test_project'
    password = \
        'b109f3bbbc244eb82441917ed06d618b9008dd09b3befd1b5e07394c706a8bb980b1d7785e5976ec049b46df5f1326af5a2ea6d103fd07c95385ffab0cacbc86'
    project = iam2_ops.create_iam2_project(project_name)
    project_uuid = project.uuid
    linked_account_uuid = project.linkedAccountUuid
    attributes = [{"name": "__ProjectRelatedZone__", "value": zone_uuid}]
    iam2_ops.add_attributes_to_iam2_project(project_uuid, attributes)
    test_stub.share_admin_resource_include_vxlan_pool([linked_account_uuid])

    # 4 create projectAdmin  into project
    project_admin_name = 'projectadmin'
    project_admin_uuid = iam2_ops.create_iam2_virtual_id(
        project_admin_name, password).uuid
    iam2_ops.add_iam2_virtual_ids_to_project([project_admin_uuid],
                                             project_uuid)
    attributes = [{"name": "__ProjectAdmin__", "value": project_uuid}]
    iam2_ops.add_attributes_to_iam2_virtual_id(project_admin_uuid, attributes)
    project_admin_session_uuid = iam2_ops.login_iam2_virtual_id(
        project_admin_name, password)
    project_admin_session_uuid = iam2_ops.login_iam2_project(
        project_name, project_admin_session_uuid).uuid

    # 5 create zwatch resource
    smtp_server = os.environ.get('smtpServer')
    smtp_port = os.environ.get('smtpPort')
    email_platform_name = 'Alarm_email'
    email_username = os.environ.get('mailUsername')
    email_password = os.environ.get('mailPassword')
    email_platform_uuid = zwt_ops.create_sns_email_platform(
        smtp_server,
        smtp_port,
        email_platform_name,
        email_username,
        email_password,
        session_uuid=project_admin_session_uuid).uuid

    email_receiver = os.environ.get('mailUsername')
    email_endpoint_uuid = zwt_ops.create_sns_email_endpoint(
        email_receiver,
        'test_email_endpoint',
        email_platform_uuid,
        session_uuid=project_admin_session_uuid).uuid

    url_01 = 'https://oapi.dingtalk.com/robot/send?access_token' \
             '=0be899d4bd0a7629961a5ccd3035dfba30d084b57944897838f1b601006dd153'
    name_01 = 'dingtalkAtPerson'
    dingtalk_endpoint_uuid = zwt_ops.create_sns_dingtalk_endpoint(
        url_01, name_01, at_all=False,
        session_uuid=project_admin_session_uuid).uuid
    http_endpoint_name = 'http'
    url = 'http://localhost:8080/webhook-url'
    http_username = '******'
    http_password = '******'
    http_endpoint_uuid = zwt_ops.create_sns_http_endpoint(
        url,
        http_endpoint_name,
        http_username,
        http_password,
        session_uuid=project_admin_session_uuid).uuid

    sns_topic_uuid = zwt_ops.create_sns_topic(
        'sns_topic_01', session_uuid=project_admin_session_uuid).uuid
    zwt_ops.subscribe_sns_topic(sns_topic_uuid,
                                dingtalk_endpoint_uuid,
                                session_uuid=project_admin_session_uuid)
    zwt_ops.subscribe_sns_topic(sns_topic_uuid,
                                email_endpoint_uuid,
                                session_uuid=project_admin_session_uuid)
    zwt_ops.subscribe_sns_topic(sns_topic_uuid,
                                http_endpoint_uuid,
                                session_uuid=project_admin_session_uuid)

    namespace = 'ZStack/Volume'
    actions = [{"actionUuid": sns_topic_uuid, "actionType": "sns"}]
    comparisonOperator = 'GreaterThanOrEqualTo'
    repeat_interval = 20
    period = 10
    threshold = 5
    metric_name = 'TotalVolumeCount'
    alarm_uuid = zwt_ops.create_alarm(
        comparisonOperator,
        period,
        threshold,
        namespace,
        metric_name,
        actions=actions,
        repeat_interval=repeat_interval,
        session_uuid=project_admin_session_uuid).uuid

    ps_actions = [{"actionUuid": sns_topic_uuid, "actionType": "sns"}]
    ps_namespace = 'ZStack/PrimaryStorage'
    ps_disconnected = 'PrimaryStorageDisconnected'
    ps_event_sub_uuid = zwt_ops.subscribe_event(ps_namespace, ps_disconnected,
                                                ps_actions).uuid

    application_platform_type = 'Email'
    alarm_template_name = 'my-alarm-template'
    alarm_template = '${ALARM_NAME} Change status to ${ALARM_CURRENT_STATUS}' \
                     'ALARM_UUID:${ALARM_UUID}' \
                     'keyword1:ThisWordIsKeyWord' \
                     'keyword2:TemplateForAlarmOn' \
                     '(Using for template changes email check)'
    alarm_template_uuid = zwt_ops.create_sns_text_template(
        alarm_template_name,
        application_platform_type,
        alarm_template,
        default_template=False).uuid

    acc_ops.logout(project_admin_session_uuid)

    # 6 delete project
    iam2_ops.delete_iam2_project(project_uuid)
    iam2_ops.expunge_iam2_project(project_uuid)

    # 7 cascade test
    try:
        test_stub.check_resource_not_exist(email_platform_uuid,
                                           res_ops.SNS_EMAIL_PLATFORM)
    except:
        test_util.test_logger("email platform should not be delete ,success")
    test_stub.check_resource_not_exist(email_endpoint_uuid,
                                       res_ops.SNS_APPLICATION_ENDPOINT)
    test_stub.check_resource_not_exist(dingtalk_endpoint_uuid,
                                       res_ops.SNS_APPLICATION_ENDPOINT)
    test_stub.check_resource_not_exist(http_endpoint_uuid,
                                       res_ops.SNS_APPLICATION_ENDPOINT)
    test_stub.check_resource_not_exist(alarm_uuid, res_ops.ALARM)
    test_stub.check_resource_not_exist(ps_event_sub_uuid,
                                       res_ops.EVENT_SUBSCRIPTION)
    test_stub.check_resource_not_exist(sns_topic_uuid, res_ops.SNS_TOPIC)
    test_stub.check_resource_not_exist(alarm_template_uuid,
                                       res_ops.SNS_TEXT_TEMPLATE)

    zwt_ops.delete_sns_application_platform(email_platform_uuid)

    iam2_ops.clean_iam2_enviroment()
    test_util.test_pass("success test project retired")
def test():

    global email_platform_uuid, email_endpoint_uuid, http_endpoint_uuid, sns_topic_uuid

    # create platform
    smtp_server = 'smtp.zstack.io'
    smtp_port = 25
    email_platform_name = 'Alarm_email'
    email_username = '******'
    email_password = '******'
    email_platform = zwt_ops.create_sns_email_platform(smtp_server, smtp_port,
                                                       email_platform_name,
                                                       email_username,
                                                       email_password)
    email_platform_uuid = email_platform.uuid
    cond = res_ops.gen_query_conditions('uuid', '=', email_platform_uuid)
    inv = res_ops.query_resource(res_ops.SNS_EMAIL_PLATFORM, cond)
    if not inv:
        test_util.test_fail('create sns email platform failed')
    try:
        zwt_ops.validate_sns_email_platform(email_platform_uuid)
    except:
        test_util.test_fail(
            'Validate SNS Email Platform Failed, Email Plarform: %s' %
            email_platform_uuid)

    # create endpoint
    email_receiver = '*****@*****.**'
    email_endpoint_name = 'test_qa'
    email_endpoint_uuid = zwt_ops.create_sns_email_endpoint(
        email_receiver, email_endpoint_name, email_platform_uuid).uuid
    cond = res_ops.gen_query_conditions('uuid', '=', email_endpoint_uuid)
    inv = res_ops.query_resource(res_ops.SNS_EMAIL_ENDPOINT, cond)
    if not inv:
        test_util.test_fail('create sns email endpoint failed')
    http_endpoint_name = 'http'
    url = 'http://localhost:8080/webhook-url'
    http_username = '******'
    http_password = '******'
    http_endpoint = zwt_ops.create_sns_http_endpoint(url, http_endpoint_name,
                                                     http_username,
                                                     http_password)
    http_endpoint_uuid = http_endpoint.uuid
    cond = res_ops.gen_query_conditions('uuid', '=', http_endpoint_uuid)
    inv = res_ops.query_resource(res_ops.SNS_HTTP_ENDPOINT, cond)
    if not inv:
        test_util.test_fail('create sns http endpoint failed')

    # create sns topic and query system-in topic
    sns_topic_uuid = zwt_ops.create_sns_topic('sns_topic_01').uuid
    zwt_ops.subscribe_sns_topic(sns_topic_uuid, email_endpoint_uuid)
    cond = res_ops.gen_query_conditions('endpoints.uuid', '=',
                                        email_endpoint_uuid)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC, cond)
    if not inv:
        test_util.test_fail('create and subscribe snstopic failed')
    cond = res_ops.gen_query_conditions('name', '=', 'system-alarm')
    system_alarm_topic = res_ops.query_resource(res_ops.SNS_TOPIC, cond)[0]
    system_alarm_topic_uuid = system_alarm_topic.uuid
    zwt_ops.subscribe_sns_topic(system_alarm_topic_uuid, email_endpoint_uuid)
    cond = res_ops.gen_query_conditions('endpoints.uuid', '=',
                                        email_endpoint_uuid)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC, cond)
    if not inv:
        test_util.test_fail('subscribe system-alarm topic failed')
    cond = res_ops.gen_query_conditions('name', '=', 'api')
    api_topic = res_ops.query_resource(res_ops.SNS_TOPIC, cond)[0]
    api_topic_uuid = api_topic.uuid
    zwt_ops.subscribe_sns_topic(api_topic_uuid, http_endpoint_uuid)
    cond = res_ops.gen_query_conditions('endpointUuid', '=',
                                        http_endpoint_uuid)
    cond = res_ops.gen_query_conditions('topicUuid', '=', api_topic_uuid)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC_SUBSCRIBER, cond)
    if not inv:
        test_util.test_fail('subscribe api topic failed')

    # subscribe event
    namespace = 'ZStack/VM'
    actions = [{"actionUuid": sns_topic_uuid, "actionType": "sns"}]
    labels = [{"key": "NewState", "op": "Equal", "value": "Disconnected"}]
    event_name = 'VMStateChangedOnHost'
    event_sub_uuid = zwt_ops.subscribe_event(namespace, event_name, actions,
                                             labels).uuid
    cond = res_ops.gen_query_conditions('uuid', '=', event_sub_uuid)
    event_subscription = res_ops.query_resource(res_ops.EVENT_SUBSCRIPTION,
                                                cond)
    if not event_subscription:
        test_util.test_fail('Subscribe event failed')

    #update endpoint
    new_name = 'endpointNewName'
    new_description = 'endpoint new description'
    zwt_ops.update_sns_application_endpoint(email_endpoint_uuid, new_name,
                                            new_description)
    cond = res_ops.gen_query_conditions('uuid', '=', email_endpoint_uuid)
    inv = res_ops.query_resource(res_ops.SNS_APPLICATION_ENDPOINT, cond)[0]
    if inv.name != new_name or inv.description != new_description:
        test_util.test_fail('test update email endpoint failed')
    zwt_ops.update_sns_application_endpoint(http_endpoint_uuid, new_name,
                                            new_description)
    cond = res_ops.gen_query_conditions('uuid', '=', http_endpoint_uuid)
    inv = res_ops.query_resource(res_ops.SNS_APPLICATION_ENDPOINT, cond)[0]
    if inv.name != new_name or inv.description != new_description:
        test_util.test_fail('test update http endpoint failed')
    new_name_platform = 'platformNewName'
    new_description_platform = 'platformNewName'
    zwt_ops.update_sns_application_platform(email_platform_uuid,
                                            new_name_platform,
                                            new_description_platform)
    cond = res_ops.gen_query_conditions('uuid', '=', email_platform_uuid)
    inv = res_ops.query_resource(res_ops.SNS_APPLICATION_PLATFORM, cond)[0]
    if inv.name != new_name_platform or inv.description != new_description_platform:
        test_util.test_fail('test update email platform failed')

    #change state
    state_event = 'disable'
    state_result = 'Disabled'
    zwt_ops.change_sns_topic_state(system_alarm_topic_uuid, state_event)
    cond = res_ops.gen_query_conditions('uuid', '=', system_alarm_topic_uuid)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC, cond)[0]
    if inv.state != state_result:
        test_util.test_fail('change system alarm topic state failed')
    zwt_ops.change_sns_topic_state(api_topic_uuid, state_event)
    cond = res_ops.gen_query_conditions('uuid', '=', api_topic_uuid)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC, cond)[0]
    if inv.state != state_result:
        test_util.test_fail('change api topic state failed')
    zwt_ops.change_sns_application_endpoint_state(email_endpoint_uuid,
                                                  state_event)
    cond = res_ops.gen_query_conditions('uuid', '=', email_endpoint_uuid)
    inv = res_ops.query_resource(res_ops.SNS_APPLICATION_ENDPOINT, cond)[0]
    if inv.state != state_result:
        test_util.test_fail('change email endpoint state failed')
    zwt_ops.change_sns_application_endpoint_state(http_endpoint_uuid,
                                                  state_event)
    cond = res_ops.gen_query_conditions('uuid', '=', http_endpoint_uuid)
    inv = res_ops.query_resource(res_ops.SNS_APPLICATION_ENDPOINT, cond)[0]
    if inv.state != state_result:
        test_util.test_fail('change http endpoint state failed')
    zwt_ops.change_sns_application_platform_state(email_platform_uuid,
                                                  state_event)
    cond = res_ops.gen_query_conditions('uuid', '=', email_platform_uuid)
    inv = res_ops.query_resource(res_ops.SNS_APPLICATION_PLATFORM, cond)[0]
    if inv.state != state_result:
        test_util.test_fail('change email platform state failed')

    # test recover and delete
    state_event = 'enable'
    state_result = 'Enabled'
    zwt_ops.change_sns_topic_state(system_alarm_topic_uuid, state_event)
    cond = res_ops.gen_query_conditions('uuid', '=', system_alarm_topic_uuid)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC, cond)[0]
    if inv.state != state_result:
        test_util.test_fail('change system alarm topic state failed')
    zwt_ops.change_sns_topic_state(api_topic_uuid, state_event)
    cond = res_ops.gen_query_conditions('uuid', '=', api_topic_uuid)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC, cond)[0]
    if inv.state != state_result:
        test_util.test_fail('change api topic state failed')
    zwt_ops.unsubscribe_event(event_sub_uuid)
    cond = res_ops.gen_query_conditions('uuid', '=', event_sub_uuid)
    event_subscription = res_ops.query_resource(res_ops.EVENT_SUBSCRIPTION,
                                                cond)
    if event_subscription:
        test_util.test_fail('unsubscribe event failed')
    zwt_ops.unsubscribe_sns_topic(sns_topic_uuid, email_endpoint_uuid)
    cond = res_ops.gen_query_conditions('endpointUuid', '=',
                                        email_endpoint_uuid)
    cond = res_ops.gen_query_conditions('topicUuid', '=', sns_topic_uuid, cond)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC_SUBSCRIBER, cond)
    if inv:
        test_util.test_fail('unsubscribe sns topic failed')
    zwt_ops.unsubscribe_sns_topic(system_alarm_topic_uuid, email_endpoint_uuid)
    cond = res_ops.gen_query_conditions('endpointUuid', '=',
                                        email_endpoint_uuid)
    cond = res_ops.gen_query_conditions('topicUuid', '=',
                                        system_alarm_topic_uuid, cond)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC_SUBSCRIBER, cond)
    if inv:
        test_util.test_fail('unsubscribe system alarm topic failed')
    zwt_ops.unsubscribe_sns_topic(api_topic_uuid, http_endpoint_uuid)
    cond = res_ops.gen_query_conditions('endpointUuid', '=',
                                        http_endpoint_uuid)
    cond = res_ops.gen_query_conditions('topicUuid', '=', api_topic_uuid, cond)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC_SUBSCRIBER, cond)
    if inv:
        test_util.test_fail('unsubscribe api topic failed')
    zwt_ops.delete_sns_topic(sns_topic_uuid)
    cond = res_ops.gen_query_conditions('uuid', '=', sns_topic_uuid)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC, cond)
    if inv:
        test_util.test_fail('delete sns topic failed')
    zwt_ops.delete_sns_application_endpoint(http_endpoint_uuid)
    cond = res_ops.gen_query_conditions('uuid', '=', http_endpoint_uuid)
    inv = res_ops.query_resource(res_ops.SNS_APPLICATION_ENDPOINT, cond)
    if inv:
        test_util.test_fail('delete http endpoint failed')
    zwt_ops.delete_sns_application_endpoint(email_endpoint_uuid)
    cond = res_ops.gen_query_conditions('uuid', '=', email_endpoint_uuid)
    inv = res_ops.query_resource(res_ops.SNS_APPLICATION_ENDPOINT, cond)
    if inv:
        test_util.test_fail('delete email endpoint failed')
    zwt_ops.delete_sns_application_platform(email_platform_uuid)
    cond = res_ops.gen_query_conditions('uuid', '=', email_platform_uuid)
    inv = res_ops.query_resource(res_ops.SNS_APPLICATION_PLATFORM, cond)
    if inv:
        test_util.test_fail('delete email platform failed')

    test_util.test_pass('success test event with email endpoint basic option!')
def test():
    global project_uuid, project_admin_uuid, virtual_id_uuid, project_operator_uuid, plain_user_uuid

    flavor = case_flavor[os.environ.get('CASE_FLAVOR')]
    # 1 create project
    if flavor['target_role'] != 'system_admin':
        project_name = 'test_project'
        project = iam2_ops.create_iam2_project(project_name)
        project_uuid = project.uuid
        project_linked_account_uuid = project.linkedAccountUuid

    if flavor['target_role'] == 'project_admin':
        # 2 create virtual id
        project_admin_name = 'username'
        project_admin_password = '******'
        project_admin_uuid = iam2_ops.create_iam2_virtual_id(project_admin_name, project_admin_password).uuid
        virtual_id_uuid = iam2_ops.create_iam2_virtual_id('usernametwo', 'password').uuid
    
        # 3 create project admin
        iam2_ops.add_iam2_virtual_ids_to_project([project_admin_uuid],project_uuid)
        attributes = [{"name": "__ProjectAdmin__", "value": project_uuid}]
        iam2_ops.add_attributes_to_iam2_virtual_id(project_admin_uuid, attributes)

        # login in project by project admin
        project_admin_session_uuid = iam2_ops.login_iam2_virtual_id(project_admin_name, project_admin_password)
        project_login_uuid = iam2_ops.login_iam2_project(project_name, session_uuid=project_admin_session_uuid).uuid
        # iam2_ops.remove_attributes_from_iam2_virtual_id(virtual_id_uuid, attributes)
    elif flavor['target_role'] == 'project_operator':
        project_operator_name = 'username2'
        project_operator_password = '******'
        attributes = [{"name": "__ProjectOperator__", "value": project_uuid}]
        project_operator_uuid = iam2_ops.create_iam2_virtual_id(project_operator_name,project_operator_password,attributes=attributes).uuid
        virtual_id_uuid = iam2_ops.create_iam2_virtual_id('usernamethree','password').uuid

        # login in project by project operator
        iam2_ops.add_iam2_virtual_ids_to_project([project_operator_uuid],project_uuid)
        project_operator_session_uuid = iam2_ops.login_iam2_virtual_id(project_operator_name,project_operator_password)
        project_login_uuid = iam2_ops.login_iam2_project(project_name,session_uuid=project_operator_session_uuid).uuid
    elif flavor['target_role'] == 'project_member':
	plain_user_name = 'username'
	plain_user_password = '******'
	plain_user_uuid = iam2_ops.create_iam2_virtual_id(plain_user_name, plain_user_password,
	                                                  project_uuid=project_uuid).uuid
	# 3 add virtual id to project
	iam2_ops.add_iam2_virtual_ids_to_project([plain_user_uuid],project_uuid)

	# 4 login in project by plain user
	plain_user_session_uuid = iam2_ops.login_iam2_virtual_id(plain_user_name, plain_user_password)

	# 4 login in project
	#project_inv=iam2_ops.get_iam2_projects_of_virtual_id(plain_user_session_uuid)
	project_login_uuid = iam2_ops.login_iam2_project(project_name, plain_user_session_uuid).uuid
    elif flavor['target_role'] == 'system_admin':
        username = "******"
        password = '******'
        vid_tst_obj = test_vid.ZstackTestVid()
        test_stub.create_system_admin(username, password, vid_tst_obj)
        virtual_id_uuid = vid_tst_obj.get_vid().uuid
        project_login_uuid = acc_ops.login_by_account(username, password)


    # Image related ops: Add, Delete, Expunge, sync image size, Update QGA, delete, expunge
    if flavor['target_role'] == 'project_member':
        statements = [{"effect": "Allow", "actions": ["org.zstack.sns.**"]}, {"effect": "Allow", "actions": ["org.zstack.zwatch.**"]}]
        role_uuid = iam2_ops.create_role('test_role', statements).uuid
        iam2_ops.add_roles_to_iam2_virtual_id([role_uuid], plain_user_uuid)

    # create platform
    smtp_server = os.environ.get('smtpServer')
    smtp_port = os.environ.get('smtpPort')
    email_platform_name = 'Alarm_email'
    email_username = os.environ.get('mailUsername')
    email_password = os.environ.get('mailPassword')
    email_platform = zwt_ops.create_sns_email_platform(smtp_server, smtp_port, email_platform_name, email_username, email_password, session_uuid=project_login_uuid)
    email_platform_uuid = email_platform.uuid
    cond=res_ops.gen_query_conditions('uuid','=',email_platform_uuid)
    inv = res_ops.query_resource(res_ops.SNS_EMAIL_PLATFORM,cond, session_uuid=project_login_uuid)
    if not inv:
        test_util.test_fail('create sns email platform failed')
    try:
        zwt_ops.validate_sns_email_platform(email_platform_uuid)
    except:
        test_util.test_fail('Validate SNS Email Platform Failed, Email Plarform: %s' % email_platform_uuid)

    # create endpoint
    email_receiver = os.environ.get('mailUsername')
    email_endpoint_name = os.environ.get('mailPassword')
    email_endpoint_uuid = zwt_ops.create_sns_email_endpoint(email_receiver, email_endpoint_name, email_platform_uuid, session_uuid=project_login_uuid).uuid
    cond=res_ops.gen_query_conditions('uuid','=',email_endpoint_uuid)
    inv=res_ops.query_resource(res_ops.SNS_EMAIL_ENDPOINT,cond, session_uuid=project_login_uuid)
    if not inv:
        test_util.test_fail('create sns email endpoint failed')
    http_endpoint_name='http'
    url = 'http://localhost:8080/webhook-url'
    http_username='******'
    http_password='******'
    http_endpoint=zwt_ops.create_sns_http_endpoint(url,http_endpoint_name,http_username,http_password, session_uuid=project_login_uuid)
    http_endpoint_uuid=http_endpoint.uuid
    cond=res_ops.gen_query_conditions('uuid','=',http_endpoint_uuid)
    inv=res_ops.query_resource(res_ops.SNS_HTTP_ENDPOINT,cond, session_uuid=project_login_uuid)
    if not inv:
        test_util.test_fail('create sns http endpoint failed')

    # create sns topic and query system-in topic
    sns_topic_uuid = zwt_ops.create_sns_topic('sns_topic_01', session_uuid=project_login_uuid).uuid
    zwt_ops.subscribe_sns_topic(sns_topic_uuid, email_endpoint_uuid, session_uuid=project_login_uuid)
    cond=res_ops.gen_query_conditions('endpoints.uuid','=',email_endpoint_uuid)
    inv=res_ops.query_resource(res_ops.SNS_TOPIC,cond, session_uuid=project_login_uuid)
    if not inv:
        test_util.test_fail('create and subscribe snstopic failed')
    cond = res_ops.gen_query_conditions('name', '=', 'system-alarm')
    system_alarm_topic = res_ops.query_resource(res_ops.SNS_TOPIC, cond)[0]
    system_alarm_topic_uuid=system_alarm_topic.uuid
    if flavor['target_role'] != 'system_admin':
        acc_ops.share_resources([project_linked_account_uuid], [system_alarm_topic_uuid])
    cond = res_ops.gen_query_conditions('name', '=', 'system-alarm')
    system_alarm_topic = res_ops.query_resource(res_ops.SNS_TOPIC, cond)[0]
    system_alarm_topic_uuid=system_alarm_topic.uuid
    zwt_ops.subscribe_sns_topic(system_alarm_topic_uuid, email_endpoint_uuid, session_uuid=project_login_uuid)
    cond=res_ops.gen_query_conditions('endpoints.uuid','=',email_endpoint_uuid)
    inv=res_ops.query_resource(res_ops.SNS_TOPIC,cond, session_uuid=project_login_uuid)
    if not inv:
        test_util.test_fail('subscribe system-alarm topic failed')
    cond = res_ops.gen_query_conditions('name','=','api')
    api_topic= res_ops.query_resource(res_ops.SNS_TOPIC,cond)[0]
    api_topic_uuid=api_topic.uuid
    if flavor['target_role'] != 'system_admin':
        acc_ops.share_resources([project_linked_account_uuid], [api_topic_uuid])
    cond = res_ops.gen_query_conditions('name','=','api')
    api_topic= res_ops.query_resource(res_ops.SNS_TOPIC,cond, session_uuid=project_login_uuid)[0]
    api_topic_uuid=api_topic.uuid
    zwt_ops.subscribe_sns_topic(api_topic_uuid,http_endpoint_uuid, session_uuid=project_login_uuid)
    cond = res_ops.gen_query_conditions('endpointUuid','=',http_endpoint_uuid)
    cond = res_ops.gen_query_conditions('topicUuid','=',api_topic_uuid)
    inv=res_ops.query_resource(res_ops.SNS_TOPIC_SUBSCRIBER,cond, session_uuid=project_login_uuid)
    if not inv:
        test_util.test_fail('subscribe api topic failed')

    # subscribe event
    namespace = 'ZStack/VM'
    actions = [{"actionUuid": sns_topic_uuid, "actionType": "sns"}]
    labels = [{"key": "NewState", "op": "Equal", "value": "Disconnected"}]
    event_name = 'VMStateChangedOnHost'
    event_sub_uuid = zwt_ops.subscribe_event(namespace, event_name, actions, labels, session_uuid=project_login_uuid).uuid
    cond = res_ops.gen_query_conditions('uuid', '=', event_sub_uuid)
    event_subscription = res_ops.query_resource(res_ops.EVENT_SUBSCRIPTION, cond, session_uuid=project_login_uuid)
    if not event_subscription:
        test_util.test_fail('Subscribe event failed')

    #update endpoint
    new_name='endpointNewName'
    new_description='endpoint new description'
    zwt_ops.update_sns_application_endpoint(email_endpoint_uuid,new_name,new_description, session_uuid=project_login_uuid)
    cond= res_ops.gen_query_conditions('uuid','=',email_endpoint_uuid)
    inv =res_ops.query_resource(res_ops.SNS_APPLICATION_ENDPOINT,cond, session_uuid=project_login_uuid)[0]
    if inv.name!=new_name or inv.description!=new_description:
        test_util.test_fail('test update email endpoint failed')
    zwt_ops.update_sns_application_endpoint(http_endpoint_uuid,new_name,new_description, session_uuid=project_login_uuid)
    cond= res_ops.gen_query_conditions('uuid','=',http_endpoint_uuid)
    inv =res_ops.query_resource(res_ops.SNS_APPLICATION_ENDPOINT,cond, session_uuid=project_login_uuid)[0]
    if inv.name!=new_name or inv.description!=new_description:
        test_util.test_fail('test update http endpoint failed')
    new_name_platform='platformNewName'
    new_description_platform='platformNewName'
    zwt_ops.update_sns_application_platform(email_platform_uuid,new_name_platform,new_description_platform, session_uuid=project_login_uuid)
    cond= res_ops.gen_query_conditions('uuid','=',email_platform_uuid)
    inv =res_ops.query_resource(res_ops.SNS_APPLICATION_PLATFORM,cond, session_uuid=project_login_uuid)[0]
    if inv.name!=new_name_platform or inv.description!=new_description_platform:
        test_util.test_fail('test update email platform failed')

    #change state
    state_event = 'disable'
    state_result = 'Disabled'
    zwt_ops.change_sns_topic_state(system_alarm_topic_uuid,state_event, session_uuid=project_login_uuid)
    cond=res_ops.gen_query_conditions('uuid','=',system_alarm_topic_uuid)
    inv=res_ops.query_resource(res_ops.SNS_TOPIC,cond, session_uuid=project_login_uuid)[0]
    if inv.state!=state_result:
        test_util.test_fail('change system alarm topic state failed')
    zwt_ops.change_sns_topic_state(api_topic_uuid, state_event, session_uuid=project_login_uuid)
    cond = res_ops.gen_query_conditions('uuid', '=', api_topic_uuid)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC, cond, session_uuid=project_login_uuid)[0]
    if inv.state != state_result:
        test_util.test_fail('change api topic state failed')
    zwt_ops.change_sns_application_endpoint_state(email_endpoint_uuid,state_event, session_uuid=project_login_uuid)
    cond = res_ops.gen_query_conditions('uuid', '=', email_endpoint_uuid)
    inv = res_ops.query_resource(res_ops.SNS_APPLICATION_ENDPOINT, cond, session_uuid=project_login_uuid)[0]
    if inv.state != state_result:
        test_util.test_fail('change email endpoint state failed')
    zwt_ops.change_sns_application_endpoint_state(http_endpoint_uuid,state_event, session_uuid=project_login_uuid)
    cond = res_ops.gen_query_conditions('uuid', '=', http_endpoint_uuid)
    inv = res_ops.query_resource(res_ops.SNS_APPLICATION_ENDPOINT, cond, session_uuid=project_login_uuid)[0]
    if inv.state != state_result:
        test_util.test_fail('change http endpoint state failed')
    zwt_ops.change_sns_application_platform_state(email_platform_uuid,state_event, session_uuid=project_login_uuid)
    cond = res_ops.gen_query_conditions('uuid', '=', email_platform_uuid)
    inv = res_ops.query_resource(res_ops.SNS_APPLICATION_PLATFORM, cond, session_uuid=project_login_uuid)[0]
    if inv.state != state_result:
        test_util.test_fail('change email platform state failed')

    # test recover and delete
    state_event='enable'
    state_result='Enabled'
    zwt_ops.change_sns_topic_state(system_alarm_topic_uuid,state_event, session_uuid=project_login_uuid)
    cond=res_ops.gen_query_conditions('uuid','=',system_alarm_topic_uuid)
    inv=res_ops.query_resource(res_ops.SNS_TOPIC,cond, session_uuid=project_login_uuid)[0]
    if inv.state!=state_result:
        test_util.test_fail('change system alarm topic state failed')
    zwt_ops.change_sns_topic_state(api_topic_uuid, state_event, session_uuid=project_login_uuid)
    cond = res_ops.gen_query_conditions('uuid', '=', api_topic_uuid)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC, cond, session_uuid=project_login_uuid)[0]
    if inv.state != state_result:
        test_util.test_fail('change api topic state failed')
    zwt_ops.unsubscribe_event(event_sub_uuid, session_uuid=project_login_uuid)
    cond = res_ops.gen_query_conditions('uuid', '=', event_sub_uuid)
    event_subscription = res_ops.query_resource(res_ops.EVENT_SUBSCRIPTION, cond, session_uuid=project_login_uuid)
    if event_subscription:
        test_util.test_fail('unsubscribe event failed')
    zwt_ops.unsubscribe_sns_topic(sns_topic_uuid, email_endpoint_uuid)
    cond =res_ops.gen_query_conditions('endpointUuid','=',email_endpoint_uuid)
    cond=res_ops.gen_query_conditions('topicUuid','=',sns_topic_uuid,cond)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC_SUBSCRIBER,cond, session_uuid=project_login_uuid)
    if inv:
        test_util.test_fail('unsubscribe sns topic failed')
    zwt_ops.unsubscribe_sns_topic(system_alarm_topic_uuid, email_endpoint_uuid, session_uuid=project_login_uuid)
    cond =res_ops.gen_query_conditions('endpointUuid','=',email_endpoint_uuid)
    cond=res_ops.gen_query_conditions('topicUuid','=',system_alarm_topic_uuid,cond)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC_SUBSCRIBER,cond, session_uuid=project_login_uuid)
    if inv:
        test_util.test_fail('unsubscribe system alarm topic failed')
    zwt_ops.unsubscribe_sns_topic(api_topic_uuid, http_endpoint_uuid, session_uuid=project_login_uuid)
    cond =res_ops.gen_query_conditions('endpointUuid','=',http_endpoint_uuid)
    cond=res_ops.gen_query_conditions('topicUuid','=',api_topic_uuid,cond)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC_SUBSCRIBER,cond, session_uuid=project_login_uuid)
    if inv:
        test_util.test_fail('unsubscribe api topic failed')
    zwt_ops.delete_sns_topic(sns_topic_uuid, session_uuid=project_login_uuid)
    cond=res_ops.gen_query_conditions('uuid','=',sns_topic_uuid)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC, cond, session_uuid=project_login_uuid)
    if inv:
        test_util.test_fail('delete sns topic failed')
    zwt_ops.delete_sns_application_endpoint(http_endpoint_uuid, session_uuid=project_login_uuid)
    cond=res_ops.gen_query_conditions('uuid','=',http_endpoint_uuid)
    inv = res_ops.query_resource(res_ops.SNS_APPLICATION_ENDPOINT, cond, session_uuid=project_login_uuid)
    if inv:
        test_util.test_fail('delete http endpoint failed')
    zwt_ops.delete_sns_application_endpoint(email_endpoint_uuid, session_uuid=project_login_uuid)
    cond=res_ops.gen_query_conditions('uuid','=',email_endpoint_uuid)
    inv = res_ops.query_resource(res_ops.SNS_APPLICATION_ENDPOINT, cond, session_uuid=project_login_uuid)
    if inv:
        test_util.test_fail('delete email endpoint failed')
    zwt_ops.delete_sns_application_platform(email_platform_uuid, session_uuid=project_login_uuid)
    cond=res_ops.gen_query_conditions('uuid','=',email_platform_uuid)
    inv = res_ops.query_resource(res_ops.SNS_APPLICATION_PLATFORM, cond, session_uuid=project_login_uuid)
    if inv:
        test_util.test_fail('delete email platform failed')

    # 11 delete
    acc_ops.logout(project_login_uuid)
    if virtual_id_uuid != None:
        iam2_ops.delete_iam2_virtual_id(virtual_id_uuid)
    if project_admin_uuid != None:
        iam2_ops.delete_iam2_virtual_id(project_admin_uuid)
    if project_operator_uuid != None:
        iam2_ops.delete_iam2_virtual_id(project_operator_uuid)
    if plain_user_uuid != None:
        iam2_ops.delete_iam2_virtual_id(plain_user_uuid)

    if flavor['target_role'] != 'system_admin':
        iam2_ops.delete_iam2_project(project_uuid)
        iam2_ops.expunge_iam2_project(project_uuid)

    test_util.test_pass('success test iam2 login in by project admin!')
def test():
	global email_endpoint_uuid, email_platform_uuid, vm, disk_all_write_bytes_alarm_uuid, my_sns_topic_uuid
	smtp_server = os.environ.get('smtpServer')
	pop_server = os.environ.get('popServer')
	smtp_port = os.environ.get('smtpPort')
	username = os.environ.get('mailUsername')
	password = os.environ.get('mailPassword')
	email_platform_name = 'Alarm_email'
	email_platform_uuid = zwt_ops.create_sns_email_platform(smtp_server, smtp_port,
	                                                        email_platform_name, username, password).uuid
	zwt_ops.validate_sns_email_platform(email_platform_uuid)
	email_endpoint_uuid = zwt_ops.create_sns_email_endpoint(username, 'test_qa',
	                                                        email_platform_uuid).uuid
	my_sns_topic = zwt_ops.create_sns_topic('my_sns_topic')
	my_sns_topic_uuid = my_sns_topic.uuid
	zwt_ops.subscribe_sns_topic(my_sns_topic_uuid, email_endpoint_uuid)

	namespace = 'ZStack/VM'
	less_than = 'LessThan'
	actions = [{"actionUuid": my_sns_topic_uuid, "actionType": "sns"}]
	period = 10
	threshold_3 = 1024 * 1024 * 1024
	disk_all_write_bytes = 'DiskAllWriteBytes'
	disk_all_write_bytes_alarm_uuid = zwt_ops.create_alarm(less_than, period,
	                                                       threshold_3, namespace,
	                                                       disk_all_write_bytes,
	                                                       name='disk_all_write_bytes',
	                                                       repeat_interval=15,
	                                                       actions=actions).uuid
	image_name = os.environ.get('imageName_s')
	l3_name = os.environ.get('l3PublicNetworkName')
	vm = test_stub.create_vm('test_vm', image_name, l3_name)

	# wait for send email
	time.sleep(60)
	flag = zwt_ops.check_keywords_in_email(pop_server, username, password, disk_all_write_bytes,
	                                       disk_all_write_bytes_alarm_uuid)
	if not flag:
		test_util.test_fail('cant receive alarm email ')

	zwt_ops.change_alarm_state(disk_all_write_bytes_alarm_uuid, 'disable')

	time.sleep(10)
	time_stamp = zwt_ops.get_boundary_words()
	zwt_ops.send_boundary_email(time_stamp,smtp_server,username,password,username)

	time.sleep(30)
	flag = zwt_ops.check_keywords_in_email(pop_server, username, password, disk_all_write_bytes,
	                                       disk_all_write_bytes_alarm_uuid, time_stamp)
	if flag:
		test_util.test_fail(
			'the alarm had been changed state to Disabled,we should not receive the email,but we found flag is %s' %
			flag)

	time.sleep(10)
	zwt_ops.change_alarm_state(disk_all_write_bytes_alarm_uuid, 'enable')
	time.sleep(30)

	flag = zwt_ops.check_keywords_in_email(pop_server, username, password, disk_all_write_bytes,
	                                       disk_all_write_bytes_alarm_uuid, time_stamp)
	if not flag:
		test_util.test_fail('cant receive email when it change state to Enable.')

	vm.clean()
	zwt_ops.delete_alarm(disk_all_write_bytes_alarm_uuid)
	zwt_ops.delete_sns_topic(my_sns_topic_uuid)
	zwt_ops.delete_sns_application_endpoint(email_endpoint_uuid)
	zwt_ops.delete_sns_application_platform(email_platform_uuid)

	test_util.test_pass('success')
def test():

    global email_platform_uuid, email_endpoint_uuid,http_endpoint_uuid, sns_topic_uuid

    # create platform
    smtp_server = os.environ.get('smtpServer')
    smtp_port = os.environ.get('smtpPort')
    email_platform_name = 'Alarm_email'
    email_username = os.environ.get('mailUsername')
    email_password = os.environ.get('mailPassword')
    email_platform = zwt_ops.create_sns_email_platform(smtp_server, smtp_port, email_platform_name, email_username, email_password)
    email_platform_uuid = email_platform.uuid
    cond=res_ops.gen_query_conditions('uuid','=',email_platform_uuid)
    inv = res_ops.query_resource(res_ops.SNS_EMAIL_PLATFORM,cond)
    if not inv:
        test_util.test_fail('create sns email platform failed')
    try:
        zwt_ops.validate_sns_email_platform(email_platform_uuid)
    except:
        test_util.test_fail('Validate SNS Email Platform Failed, Email Plarform: %s' % email_platform_uuid)

    # create endpoint
    email_receiver = os.environ.get('mailUsername')
    email_endpoint_name = os.environ.get('mailPassword')
    email_endpoint_uuid = zwt_ops.create_sns_email_endpoint(email_receiver, email_endpoint_name, email_platform_uuid).uuid
    cond=res_ops.gen_query_conditions('uuid','=',email_endpoint_uuid)
    inv=res_ops.query_resource(res_ops.SNS_EMAIL_ENDPOINT,cond)
    if not inv:
        test_util.test_fail('create sns email endpoint failed')
    http_endpoint_name='http'
    url = 'http://localhost:8080/webhook-url'
    http_username='******'
    http_password='******'
    http_endpoint=zwt_ops.create_sns_http_endpoint(url,http_endpoint_name,http_username,http_password)
    http_endpoint_uuid=http_endpoint.uuid
    cond=res_ops.gen_query_conditions('uuid','=',http_endpoint_uuid)
    inv=res_ops.query_resource(res_ops.SNS_HTTP_ENDPOINT,cond)
    if not inv:
        test_util.test_fail('create sns http endpoint failed')

    # create sns topic and query system-in topic
    sns_topic_uuid = zwt_ops.create_sns_topic('sns_topic_01').uuid
    zwt_ops.subscribe_sns_topic(sns_topic_uuid, email_endpoint_uuid)
    cond=res_ops.gen_query_conditions('endpoints.uuid','=',email_endpoint_uuid)
    inv=res_ops.query_resource(res_ops.SNS_TOPIC,cond)
    if not inv:
        test_util.test_fail('create and subscribe snstopic failed')
    cond = res_ops.gen_query_conditions('name', '=', 'system-alarm')
    system_alarm_topic = res_ops.query_resource(res_ops.SNS_TOPIC, cond)[0]
    system_alarm_topic_uuid=system_alarm_topic.uuid
    zwt_ops.subscribe_sns_topic(system_alarm_topic_uuid, email_endpoint_uuid)
    cond=res_ops.gen_query_conditions('endpoints.uuid','=',email_endpoint_uuid)
    inv=res_ops.query_resource(res_ops.SNS_TOPIC,cond)
    if not inv:
        test_util.test_fail('subscribe system-alarm topic failed')
    cond = res_ops.gen_query_conditions('name','=','api')
    api_topic= res_ops.query_resource(res_ops.SNS_TOPIC,cond)[0]
    api_topic_uuid=api_topic.uuid
    zwt_ops.subscribe_sns_topic(api_topic_uuid,http_endpoint_uuid)
    cond = res_ops.gen_query_conditions('endpointUuid','=',http_endpoint_uuid)
    cond = res_ops.gen_query_conditions('topicUuid','=',api_topic_uuid)
    inv=res_ops.query_resource(res_ops.SNS_TOPIC_SUBSCRIBER,cond)
    if not inv:
        test_util.test_fail('subscribe api topic failed')

    # subscribe event
    namespace = 'ZStack/VM'
    actions = [{"actionUuid": sns_topic_uuid, "actionType": "sns"}]
    labels = [{"key": "NewState", "op": "Equal", "value": "Disconnected"}]
    event_name = 'VMStateChangedOnHost'
    event_sub_uuid = zwt_ops.subscribe_event(namespace, event_name, actions, labels).uuid
    cond = res_ops.gen_query_conditions('uuid', '=', event_sub_uuid)
    event_subscription = res_ops.query_resource(res_ops.EVENT_SUBSCRIPTION, cond)
    if not event_subscription:
        test_util.test_fail('Subscribe event failed')

    #update endpoint
    new_name='endpointNewName'
    new_description='endpoint new description'
    zwt_ops.update_sns_application_endpoint(email_endpoint_uuid,new_name,new_description)
    cond= res_ops.gen_query_conditions('uuid','=',email_endpoint_uuid)
    inv =res_ops.query_resource(res_ops.SNS_APPLICATION_ENDPOINT,cond)[0]
    if inv.name!=new_name or inv.description!=new_description:
        test_util.test_fail('test update email endpoint failed')
    zwt_ops.update_sns_application_endpoint(http_endpoint_uuid,new_name,new_description)
    cond= res_ops.gen_query_conditions('uuid','=',http_endpoint_uuid)
    inv =res_ops.query_resource(res_ops.SNS_APPLICATION_ENDPOINT,cond)[0]
    if inv.name!=new_name or inv.description!=new_description:
        test_util.test_fail('test update http endpoint failed')
    new_name_platform='platformNewName'
    new_description_platform='platformNewName'
    zwt_ops.update_sns_application_platform(email_platform_uuid,new_name_platform,new_description_platform)
    cond= res_ops.gen_query_conditions('uuid','=',email_platform_uuid)
    inv =res_ops.query_resource(res_ops.SNS_APPLICATION_PLATFORM,cond)[0]
    if inv.name!=new_name_platform or inv.description!=new_description_platform:
        test_util.test_fail('test update email platform failed')

    #change state
    state_event = 'disable'
    state_result = 'Disabled'
    zwt_ops.change_sns_topic_state(system_alarm_topic_uuid,state_event)
    cond=res_ops.gen_query_conditions('uuid','=',system_alarm_topic_uuid)
    inv=res_ops.query_resource(res_ops.SNS_TOPIC,cond)[0]
    if inv.state!=state_result:
        test_util.test_fail('change system alarm topic state failed')
    zwt_ops.change_sns_topic_state(api_topic_uuid, state_event)
    cond = res_ops.gen_query_conditions('uuid', '=', api_topic_uuid)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC, cond)[0]
    if inv.state != state_result:
        test_util.test_fail('change api topic state failed')
    zwt_ops.change_sns_application_endpoint_state(email_endpoint_uuid,state_event)
    cond = res_ops.gen_query_conditions('uuid', '=', email_endpoint_uuid)
    inv = res_ops.query_resource(res_ops.SNS_APPLICATION_ENDPOINT, cond)[0]
    if inv.state != state_result:
        test_util.test_fail('change email endpoint state failed')
    zwt_ops.change_sns_application_endpoint_state(http_endpoint_uuid,state_event)
    cond = res_ops.gen_query_conditions('uuid', '=', http_endpoint_uuid)
    inv = res_ops.query_resource(res_ops.SNS_APPLICATION_ENDPOINT, cond)[0]
    if inv.state != state_result:
        test_util.test_fail('change http endpoint state failed')
    zwt_ops.change_sns_application_platform_state(email_platform_uuid,state_event)
    cond = res_ops.gen_query_conditions('uuid', '=', email_platform_uuid)
    inv = res_ops.query_resource(res_ops.SNS_APPLICATION_PLATFORM, cond)[0]
    if inv.state != state_result:
        test_util.test_fail('change email platform state failed')

    # test recover and delete
    state_event='enable'
    state_result='Enabled'
    zwt_ops.change_sns_topic_state(system_alarm_topic_uuid,state_event)
    cond=res_ops.gen_query_conditions('uuid','=',system_alarm_topic_uuid)
    inv=res_ops.query_resource(res_ops.SNS_TOPIC,cond)[0]
    if inv.state!=state_result:
        test_util.test_fail('change system alarm topic state failed')
    zwt_ops.change_sns_topic_state(api_topic_uuid, state_event)
    cond = res_ops.gen_query_conditions('uuid', '=', api_topic_uuid)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC, cond)[0]
    if inv.state != state_result:
        test_util.test_fail('change api topic state failed')
    zwt_ops.unsubscribe_event(event_sub_uuid)
    cond = res_ops.gen_query_conditions('uuid', '=', event_sub_uuid)
    event_subscription = res_ops.query_resource(res_ops.EVENT_SUBSCRIPTION, cond)
    if event_subscription:
        test_util.test_fail('unsubscribe event failed')
    zwt_ops.unsubscribe_sns_topic(sns_topic_uuid, email_endpoint_uuid)
    cond =res_ops.gen_query_conditions('endpointUuid','=',email_endpoint_uuid)
    cond=res_ops.gen_query_conditions('topicUuid','=',sns_topic_uuid,cond)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC_SUBSCRIBER,cond)
    if inv:
        test_util.test_fail('unsubscribe sns topic failed')
    zwt_ops.unsubscribe_sns_topic(system_alarm_topic_uuid, email_endpoint_uuid)
    cond =res_ops.gen_query_conditions('endpointUuid','=',email_endpoint_uuid)
    cond=res_ops.gen_query_conditions('topicUuid','=',system_alarm_topic_uuid,cond)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC_SUBSCRIBER,cond)
    if inv:
        test_util.test_fail('unsubscribe system alarm topic failed')
    zwt_ops.unsubscribe_sns_topic(api_topic_uuid, http_endpoint_uuid)
    cond =res_ops.gen_query_conditions('endpointUuid','=',http_endpoint_uuid)
    cond=res_ops.gen_query_conditions('topicUuid','=',api_topic_uuid,cond)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC_SUBSCRIBER,cond)
    if inv:
        test_util.test_fail('unsubscribe api topic failed')
    zwt_ops.delete_sns_topic(sns_topic_uuid)
    cond=res_ops.gen_query_conditions('uuid','=',sns_topic_uuid)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC, cond)
    if inv:
        test_util.test_fail('delete sns topic failed')
    zwt_ops.delete_sns_application_endpoint(http_endpoint_uuid)
    cond=res_ops.gen_query_conditions('uuid','=',http_endpoint_uuid)
    inv = res_ops.query_resource(res_ops.SNS_APPLICATION_ENDPOINT, cond)
    if inv:
        test_util.test_fail('delete http endpoint failed')
    zwt_ops.delete_sns_application_endpoint(email_endpoint_uuid)
    cond=res_ops.gen_query_conditions('uuid','=',email_endpoint_uuid)
    inv = res_ops.query_resource(res_ops.SNS_APPLICATION_ENDPOINT, cond)
    if inv:
        test_util.test_fail('delete email endpoint failed')
    zwt_ops.delete_sns_application_platform(email_platform_uuid)
    cond=res_ops.gen_query_conditions('uuid','=',email_platform_uuid)
    inv = res_ops.query_resource(res_ops.SNS_APPLICATION_PLATFORM, cond)
    if inv:
        test_util.test_fail('delete email platform failed')

    test_util.test_pass('success test event with email endpoint basic option!')
def test():
    global my_sns_topic_uuid, email_endpoint_uuid, email_platform_uuid, event_template_uuid, \
        alarm_template_uuid, alarm_uuid_list, test_dict

    smtp_server = os.environ.get('smtpServer')
    pop_server = os.environ.get('popServer')
    smtp_port = os.environ.get('smtpPort')
    username = os.environ.get('mailUsername')
    password = os.environ.get('mailPassword')
    email_platform_name = 'Alarm_email'
    email_platform = zwt_ops.create_sns_email_platform(smtp_server, smtp_port,
                                                         email_platform_name, username, password)
    email_platform_uuid = email_platform.uuid
    try:
        zwt_ops.validate_sns_email_platform(email_platform_uuid)
    except:
        test_util.test_fail(
            'Validate SNS Email Platform Failed, Email Plarform: %s' % email_platform_uuid)
    email_endpoint_uuid = zwt_ops.create_sns_email_endpoint(username, 'test_qa',
                                                              email_platform_uuid).uuid

    my_sns_topic = zwt_ops.create_sns_topic('my_sns_topic')
    my_sns_topic_uuid = my_sns_topic.uuid
    zwt_ops.subscribe_sns_topic(my_sns_topic_uuid, email_endpoint_uuid)

    # create alarm
    namespace = 'ZStack/Image'
    greater_than_or_equal_to = 'GreaterThanOrEqualTo'
    greater_than = 'GreaterThan'
    actions = [{"actionUuid": my_sns_topic_uuid, "actionType": "sns"}]

    period = 10

    threshold_1 = 1
    threshold_3 = 3
    threshold_10 = 10
    threshold_50 = 50

    total_image_count = 'TotalImageCount'
    total_image_count_alarm_uuid = zwt_ops.create_alarm(greater_than_or_equal_to, period,
                                                          threshold_3, namespace,
                                                          total_image_count,
                                                          name='total-count-image',
                                                          repeat_interval=600,
                                                          actions=actions).uuid
    alarm_uuid_list.append(total_image_count_alarm_uuid)

    ready_image_count = 'ReadyImageCount'
    ready_image_count_alarm_uuid = zwt_ops.create_alarm(greater_than_or_equal_to, period,
                                                          threshold_3, namespace,
                                                          ready_image_count,
                                                          name='ready_image_count',
                                                          repeat_interval=600,
                                                          actions=actions).uuid
    alarm_uuid_list.append(ready_image_count_alarm_uuid)

    ready_image_in_percent = 'ReadyImageInPercent'
    ready_image_in_percent_alarm_uuid = zwt_ops.create_alarm(greater_than_or_equal_to, period,
                                                               threshold_1, namespace,
                                                               ready_image_in_percent,
                                                               name='ready_image_in_percent',
                                                               repeat_interval=600,
                                                               actions=actions).uuid
    alarm_uuid_list.append(ready_image_in_percent_alarm_uuid)

    root_volume_template_count = 'RootVolumeTemplateCount'
    root_volume_template_count_alarm_uuid = zwt_ops.create_alarm(greater_than_or_equal_to,
                                                                   period,
                                                                   threshold_3, namespace,
                                                                   root_volume_template_count,
                                                                   name='root_volume_template_count',
                                                                   repeat_interval=600,
                                                                   actions=actions, ).uuid
    alarm_uuid_list.append(root_volume_template_count_alarm_uuid)

    root_volume_template_in_percent = 'RootVolumeTemplateInPercent'
    root_volume_template_in_percent_alarm_uuid = zwt_ops.create_alarm(greater_than, period,
                                                                        threshold_1, namespace,
                                                                        root_volume_template_in_percent,
                                                                        name='root_volume_template_in_percent',
                                                                        repeat_interval=600,
                                                                        actions=actions).uuid
    alarm_uuid_list.append(root_volume_template_in_percent_alarm_uuid)

    data_volume_template_count = 'DataVolumeTemplateCount'
    data_volume_template_count_alarm_uuid = zwt_ops.create_alarm(greater_than_or_equal_to,
                                                                   period,
                                                                   threshold_3, namespace,
                                                                   data_volume_template_count,
                                                                   name='data_volume_template_count',
                                                                   repeat_interval=600,
                                                                   actions=actions).uuid
    alarm_uuid_list.append(data_volume_template_count_alarm_uuid)

    data_volume_template_in_percent = 'DataVolumeTemplateInPercent'
    data_volume_template_in_percent_alarm_uuid = zwt_ops.create_alarm(greater_than, period,
                                                                        threshold_1, namespace,
                                                                        data_volume_template_in_percent,
                                                                        name='data_volume_template_in_percent',
                                                                        repeat_interval=600,
                                                                        actions=actions).uuid
    alarm_uuid_list.append(data_volume_template_in_percent_alarm_uuid)

    iso_count = 'ISOCount'
    iso_count_alarm_uuid = zwt_ops.create_alarm(greater_than_or_equal_to, period, threshold_3,
                                                  namespace, iso_count, name='iso_count',
                                                  repeat_interval=600, actions=actions).uuid
    alarm_uuid_list.append(iso_count_alarm_uuid)

    iso_in_percent = 'ISOInPercent'
    iso_in_percent_alarm_uuid = zwt_ops.create_alarm(greater_than, period, threshold_1,
                                                       namespace,
                                                       iso_in_percent, name='iso_in_percent',
                                                       repeat_interval=600, actions=actions).uuid
    alarm_uuid_list.append(iso_in_percent_alarm_uuid)

    # create Image
    image_name = os.environ.get('imageName_s')
    l3_name = os.environ.get('l3VlanNetworkName1')
    vm_name='multihost_basic_vm'
    vm = test_stub.create_vm(vm_name,image_name,l3_name)
    test_dict.add_vm(vm)
    volume = test_stub.create_volume()
    test_dict.add_volume(volume)
    volume.attach(vm)
    zone_uuid = vm.get_vm().zoneUuid
    root_volume_uuid = test_lib.lib_get_root_volume_uuid(vm.get_vm())
    bs_uuid_list = test_lib.lib_get_backup_storage_uuid_list_by_zone(zone_uuid)

    image_option = test_util.ImageOption()
    image_option.set_root_volume_uuid(root_volume_uuid)
    image_option.set_format('qcow2')
    image_option.set_backup_storage_uuid_list(bs_uuid_list)
    # image_option.set_mediaType('ISO')

    for i in range(threshold_3):
        image_option.set_name('root_volume_template_for_test_' + str(i))
        root_volume_template = zstack_image_header.ZstackTestImage()
        root_volume_template.set_creation_option(image_option)
        root_volume_template.create()
        test_dict.add_image(root_volume_template)
        iso=test_stub.add_test_minimal_iso("iso_for_test_"+str(i))
        test_dict.add_image(iso)
    time.sleep(30)
    # before change template
    flag = zwt_ops.check_sns_email(pop_server, username, password, total_image_count,
                                     total_image_count_alarm_uuid)
    if flag != 1:
        test_util.test_fail('No send event email')
    flag = zwt_ops.check_sns_email(pop_server, username, password, ready_image_count,
                                     ready_image_count_alarm_uuid)
    if flag != 1:
        test_util.test_fail('No send event email')
    flag = zwt_ops.check_sns_email(pop_server, username, password, ready_image_in_percent,
                                     ready_image_in_percent_alarm_uuid)
    if flag != 1:
        test_util.test_fail('No send event email')
    flag = zwt_ops.check_sns_email(pop_server, username, password, root_volume_template_count,
                                     root_volume_template_count_alarm_uuid)
    if flag != 1:
        test_util.test_fail('No send event email')
    flag = zwt_ops.check_sns_email(pop_server, username, password,
                                     root_volume_template_in_percent,
                                     root_volume_template_in_percent_alarm_uuid)
    if flag != 1:
        test_util.test_fail('No send event email')
    flag = zwt_ops.check_sns_email(pop_server, username, password, iso_count,
                                     iso_count_alarm_uuid)
    if flag != 1:
        test_util.test_fail('No send event email')
    flag = zwt_ops.check_sns_email(pop_server, username, password, iso_in_percent,
                                     iso_in_percent_alarm_uuid)
    if flag != 1:
        test_util.test_fail('No send event email')

    alarm_keywords = 'TemplateForAlarmOn'

    if zwt_ops.check_keywords_in_email(pop_server, username, password, alarm_keywords,
                                         total_image_count_alarm_uuid):
        test_util.test_fail('email already exsist before test')
    if zwt_ops.check_keywords_in_email(pop_server, username, password, alarm_keywords,
                                         ready_image_count_alarm_uuid):
        test_util.test_fail('email already exsist before test')
    if zwt_ops.check_keywords_in_email(pop_server, username, password, alarm_keywords,
                                         ready_image_in_percent_alarm_uuid):
        test_util.test_fail('email already exsist before test')
    if zwt_ops.check_keywords_in_email(pop_server, username, password, alarm_keywords,
                                         root_volume_template_count_alarm_uuid):
        test_util.test_fail('email already exsist before test')
    if zwt_ops.check_keywords_in_email(pop_server, username, password, alarm_keywords,
                                         root_volume_template_in_percent_alarm_uuid):
        test_util.test_fail('email already exsist before test')
    if zwt_ops.check_keywords_in_email(pop_server, username, password, alarm_keywords,
                                         data_volume_template_count_alarm_uuid):
        test_util.test_fail('email already exsist before test')
    if zwt_ops.check_keywords_in_email(pop_server, username, password, alarm_keywords,
                                         data_volume_template_in_percent_alarm_uuid):
        test_util.test_fail('email already exsist before test')
    if zwt_ops.check_keywords_in_email(pop_server, username, password, alarm_keywords,
                                         iso_count_alarm_uuid):
        test_util.test_fail('email already exsist before test')
    if zwt_ops.check_keywords_in_email(pop_server, username, password, alarm_keywords,
                                         iso_in_percent_alarm_uuid):
        test_util.test_fail('email already exsist before test')

    application_platform_type = 'Email'
    alarm_template_name = 'my-alarm-template'
    alarm_template = '${ALARM_NAME} Change status to ${ALARM_CURRENT_STATUS}' \
                     'ALARM_UUID:${ALARM_UUID}' \
                     'keyword1:ThisWordIsKeyWord' \
                     'keyword2:TemplateForAlarmOn' \
                     '(Using for template changes email check)'
    alarm_template_uuid = zwt_ops.create_sns_text_template(alarm_template_name,
                                                             application_platform_type,
                                                             alarm_template,
                                                             default_template=False).uuid

    event_template_name = 'my-event-template'
    event_keywords = 'TemplateForEventHappened'
    event_template = '${EVENT_NAME} IN ${EVENT_NAMESPACE}' \
                     'keyword1:ThisWordIsKeyWord' \
                     'keyword2:TemplateForEventHappened' \
                     'keyword3{PARAM_EVENT_SUBSCRIPTION_UUID}' \
                     '(Using for template changes email check)'
    event_template_uuid = zwt_ops.create_sns_text_template(event_template_name,
                                                             application_platform_type,
                                                             event_template,
                                                             default_template=True).uuid

    # test update text template
    zwt_ops.update_sns_text_template(alarm_template_uuid, description='this is a new description',
                                       default_template=True)

    cond = res_ops.gen_query_conditions('uuid', '=', alarm_template_uuid)
    inv = res_ops.query_resource(res_ops.SNS_TEXT_TEMPLATE, cond)[0]
    if inv.defaultTemplate == False or inv.description != 'this is a new description':
        test_util.test_fail('change template fail')

    for i in range(threshold_3):
        data_volume_template = volume.create_template(bs_uuid_list,
                                                      name="vol_temp_for_volume_test_" + str(i))
        test_dict.add_image(data_volume_template)

    # wait for reboot and send email
    time.sleep(30)
    test_lib.lib_robot_cleanup(test_dict)
    zwt_ops.delete_sns_text_template(alarm_template_uuid)
    zwt_ops.delete_sns_text_template(event_template_uuid)
    for alarm_uuid in alarm_uuid_list:
        zwt_ops.delete_alarm(alarm_uuid)
    zwt_ops.delete_sns_topic(my_sns_topic_uuid)
    zwt_ops.delete_sns_application_endpoint(email_endpoint_uuid)
    zwt_ops.delete_sns_application_platform(email_platform_uuid)

    if zwt_ops.check_keywords_in_email(pop_server, username, password, alarm_keywords,
                                         data_volume_template_count_alarm_uuid) and zwt_ops.check_keywords_in_email(
        pop_server, username, password, alarm_keywords,
        data_volume_template_in_percent_alarm_uuid):
        test_util.test_pass('success check all keywords in the email')
    else:
        test_util.test_fail('cannt check all mail')
def test():
    global project_uuid, project_admin_uuid, virtual_id_uuid, project_operator_uuid, plain_user_uuid

    flavor = case_flavor[os.environ.get('CASE_FLAVOR')]
    # 1 create project
    project_name = 'test_project'
    project = iam2_ops.create_iam2_project(project_name)
    project_uuid = project.uuid
    project_linked_account_uuid = project.linkedAccountUuid

    if flavor['target_role'] == 'project_admin':
        # 2 create virtual id
        project_admin_name = 'username'
        project_admin_password = '******'
        project_admin_uuid = iam2_ops.create_iam2_virtual_id(project_admin_name, project_admin_password).uuid
        virtual_id_uuid = iam2_ops.create_iam2_virtual_id('usernametwo', 'password').uuid
    
        # 3 create project admin
        iam2_ops.add_iam2_virtual_ids_to_project([project_admin_uuid],project_uuid)
        attributes = [{"name": "__ProjectAdmin__", "value": project_uuid}]
        iam2_ops.add_attributes_to_iam2_virtual_id(project_admin_uuid, attributes)

        # login in project by project admin
        project_admin_session_uuid = iam2_ops.login_iam2_virtual_id(project_admin_name, project_admin_password)
        project_login_uuid = iam2_ops.login_iam2_project(project_name, session_uuid=project_admin_session_uuid).uuid
        # iam2_ops.remove_attributes_from_iam2_virtual_id(virtual_id_uuid, attributes)
    elif flavor['target_role'] == 'project_operator':
        project_operator_name = 'username2'
        project_operator_password = '******'
        attributes = [{"name": "__ProjectOperator__", "value": project_uuid}]
        project_operator_uuid = iam2_ops.create_iam2_virtual_id(project_operator_name,project_operator_password,attributes=attributes).uuid
        virtual_id_uuid = iam2_ops.create_iam2_virtual_id('usernamethree','password').uuid

        # login in project by project operator
        iam2_ops.add_iam2_virtual_ids_to_project([project_operator_uuid],project_uuid)
        project_operator_session_uuid = iam2_ops.login_iam2_virtual_id(project_operator_name,project_operator_password)
        project_login_uuid = iam2_ops.login_iam2_project(project_name,session_uuid=project_operator_session_uuid).uuid
    elif flavor['target_role'] == 'project_member':
	plain_user_name = 'username'
	plain_user_password = '******'
	plain_user_uuid = iam2_ops.create_iam2_virtual_id(plain_user_name, plain_user_password,
	                                                  project_uuid=project_uuid).uuid
	# 3 add virtual id to project
	iam2_ops.add_iam2_virtual_ids_to_project([plain_user_uuid],project_uuid)

	# 4 login in project by plain user
	plain_user_session_uuid = iam2_ops.login_iam2_virtual_id(plain_user_name, plain_user_password)

	# 4 login in project
	#project_inv=iam2_ops.get_iam2_projects_of_virtual_id(plain_user_session_uuid)
	project_login_uuid = iam2_ops.login_iam2_project(project_name, plain_user_session_uuid).uuid


    # Image related ops: Add, Delete, Expunge, sync image size, Update QGA, delete, expunge
    if flavor['target_role'] == 'project_member':
        statements = [{"effect": "Allow", "actions": ["org.zstack.sns.**"]}, {"effect": "Allow", "actions": ["org.zstack.zwatch.**"]}]
        role_uuid = iam2_ops.create_role('test_role', statements).uuid
        iam2_ops.add_roles_to_iam2_virtual_id([role_uuid], plain_user_uuid)

    # create platform
    smtp_server = os.environ.get('smtpServer')
    smtp_port = os.environ.get('smtpPort')
    email_platform_name = 'Alarm_email'
    email_username = os.environ.get('mailUsername')
    email_password = os.environ.get('mailPassword')
    email_platform = zwt_ops.create_sns_email_platform(smtp_server, smtp_port, email_platform_name, email_username, email_password, session_uuid=project_login_uuid)
    email_platform_uuid = email_platform.uuid
    cond=res_ops.gen_query_conditions('uuid','=',email_platform_uuid)
    inv = res_ops.query_resource(res_ops.SNS_EMAIL_PLATFORM,cond, session_uuid=project_login_uuid)
    if not inv:
        test_util.test_fail('create sns email platform failed')
    try:
        zwt_ops.validate_sns_email_platform(email_platform_uuid)
    except:
        test_util.test_fail('Validate SNS Email Platform Failed, Email Plarform: %s' % email_platform_uuid)

    # create endpoint
    email_receiver = os.environ.get('mailUsername')
    email_endpoint_name = os.environ.get('mailPassword')
    email_endpoint_uuid = zwt_ops.create_sns_email_endpoint(email_receiver, email_endpoint_name, email_platform_uuid, session_uuid=project_login_uuid).uuid
    cond=res_ops.gen_query_conditions('uuid','=',email_endpoint_uuid)
    inv=res_ops.query_resource(res_ops.SNS_EMAIL_ENDPOINT,cond, session_uuid=project_login_uuid)
    if not inv:
        test_util.test_fail('create sns email endpoint failed')
    http_endpoint_name='http'
    url = 'http://localhost:8080/webhook-url'
    http_username='******'
    http_password='******'
    http_endpoint=zwt_ops.create_sns_http_endpoint(url,http_endpoint_name,http_username,http_password, session_uuid=project_login_uuid)
    http_endpoint_uuid=http_endpoint.uuid
    cond=res_ops.gen_query_conditions('uuid','=',http_endpoint_uuid)
    inv=res_ops.query_resource(res_ops.SNS_HTTP_ENDPOINT,cond, session_uuid=project_login_uuid)
    if not inv:
        test_util.test_fail('create sns http endpoint failed')

    # create sns topic and query system-in topic
    sns_topic_uuid = zwt_ops.create_sns_topic('sns_topic_01', session_uuid=project_login_uuid).uuid
    zwt_ops.subscribe_sns_topic(sns_topic_uuid, email_endpoint_uuid, session_uuid=project_login_uuid)
    cond=res_ops.gen_query_conditions('endpoints.uuid','=',email_endpoint_uuid)
    inv=res_ops.query_resource(res_ops.SNS_TOPIC,cond, session_uuid=project_login_uuid)
    if not inv:
        test_util.test_fail('create and subscribe snstopic failed')
    cond = res_ops.gen_query_conditions('name', '=', 'system-alarm')
    system_alarm_topic = res_ops.query_resource(res_ops.SNS_TOPIC, cond)[0]
    system_alarm_topic_uuid=system_alarm_topic.uuid
    acc_ops.share_resources([project_linked_account_uuid], [system_alarm_topic_uuid])
    cond = res_ops.gen_query_conditions('name', '=', 'system-alarm')
    system_alarm_topic = res_ops.query_resource(res_ops.SNS_TOPIC, cond)[0]
    system_alarm_topic_uuid=system_alarm_topic.uuid
    zwt_ops.subscribe_sns_topic(system_alarm_topic_uuid, email_endpoint_uuid, session_uuid=project_login_uuid)
    cond=res_ops.gen_query_conditions('endpoints.uuid','=',email_endpoint_uuid)
    inv=res_ops.query_resource(res_ops.SNS_TOPIC,cond, session_uuid=project_login_uuid)
    if not inv:
        test_util.test_fail('subscribe system-alarm topic failed')
    cond = res_ops.gen_query_conditions('name','=','api')
    api_topic= res_ops.query_resource(res_ops.SNS_TOPIC,cond)[0]
    api_topic_uuid=api_topic.uuid
    acc_ops.share_resources([project_linked_account_uuid], [api_topic_uuid])
    cond = res_ops.gen_query_conditions('name','=','api')
    api_topic= res_ops.query_resource(res_ops.SNS_TOPIC,cond, session_uuid=project_login_uuid)[0]
    api_topic_uuid=api_topic.uuid
    zwt_ops.subscribe_sns_topic(api_topic_uuid,http_endpoint_uuid, session_uuid=project_login_uuid)
    cond = res_ops.gen_query_conditions('endpointUuid','=',http_endpoint_uuid)
    cond = res_ops.gen_query_conditions('topicUuid','=',api_topic_uuid)
    inv=res_ops.query_resource(res_ops.SNS_TOPIC_SUBSCRIBER,cond, session_uuid=project_login_uuid)
    if not inv:
        test_util.test_fail('subscribe api topic failed')

    # subscribe event
    namespace = 'ZStack/VM'
    actions = [{"actionUuid": sns_topic_uuid, "actionType": "sns"}]
    labels = [{"key": "NewState", "op": "Equal", "value": "Disconnected"}]
    event_name = 'VMStateChangedOnHost'
    event_sub_uuid = zwt_ops.subscribe_event(namespace, event_name, actions, labels, session_uuid=project_login_uuid).uuid
    cond = res_ops.gen_query_conditions('uuid', '=', event_sub_uuid)
    event_subscription = res_ops.query_resource(res_ops.EVENT_SUBSCRIPTION, cond, session_uuid=project_login_uuid)
    if not event_subscription:
        test_util.test_fail('Subscribe event failed')

    #update endpoint
    new_name='endpointNewName'
    new_description='endpoint new description'
    zwt_ops.update_sns_application_endpoint(email_endpoint_uuid,new_name,new_description, session_uuid=project_login_uuid)
    cond= res_ops.gen_query_conditions('uuid','=',email_endpoint_uuid)
    inv =res_ops.query_resource(res_ops.SNS_APPLICATION_ENDPOINT,cond, session_uuid=project_login_uuid)[0]
    if inv.name!=new_name or inv.description!=new_description:
        test_util.test_fail('test update email endpoint failed')
    zwt_ops.update_sns_application_endpoint(http_endpoint_uuid,new_name,new_description, session_uuid=project_login_uuid)
    cond= res_ops.gen_query_conditions('uuid','=',http_endpoint_uuid)
    inv =res_ops.query_resource(res_ops.SNS_APPLICATION_ENDPOINT,cond, session_uuid=project_login_uuid)[0]
    if inv.name!=new_name or inv.description!=new_description:
        test_util.test_fail('test update http endpoint failed')
    new_name_platform='platformNewName'
    new_description_platform='platformNewName'
    zwt_ops.update_sns_application_platform(email_platform_uuid,new_name_platform,new_description_platform, session_uuid=project_login_uuid)
    cond= res_ops.gen_query_conditions('uuid','=',email_platform_uuid)
    inv =res_ops.query_resource(res_ops.SNS_APPLICATION_PLATFORM,cond, session_uuid=project_login_uuid)[0]
    if inv.name!=new_name_platform or inv.description!=new_description_platform:
        test_util.test_fail('test update email platform failed')

    #change state
    state_event = 'disable'
    state_result = 'Disabled'
    zwt_ops.change_sns_topic_state(system_alarm_topic_uuid,state_event, session_uuid=project_login_uuid)
    cond=res_ops.gen_query_conditions('uuid','=',system_alarm_topic_uuid)
    inv=res_ops.query_resource(res_ops.SNS_TOPIC,cond, session_uuid=project_login_uuid)[0]
    if inv.state!=state_result:
        test_util.test_fail('change system alarm topic state failed')
    zwt_ops.change_sns_topic_state(api_topic_uuid, state_event, session_uuid=project_login_uuid)
    cond = res_ops.gen_query_conditions('uuid', '=', api_topic_uuid)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC, cond, session_uuid=project_login_uuid)[0]
    if inv.state != state_result:
        test_util.test_fail('change api topic state failed')
    zwt_ops.change_sns_application_endpoint_state(email_endpoint_uuid,state_event, session_uuid=project_login_uuid)
    cond = res_ops.gen_query_conditions('uuid', '=', email_endpoint_uuid)
    inv = res_ops.query_resource(res_ops.SNS_APPLICATION_ENDPOINT, cond, session_uuid=project_login_uuid)[0]
    if inv.state != state_result:
        test_util.test_fail('change email endpoint state failed')
    zwt_ops.change_sns_application_endpoint_state(http_endpoint_uuid,state_event, session_uuid=project_login_uuid)
    cond = res_ops.gen_query_conditions('uuid', '=', http_endpoint_uuid)
    inv = res_ops.query_resource(res_ops.SNS_APPLICATION_ENDPOINT, cond, session_uuid=project_login_uuid)[0]
    if inv.state != state_result:
        test_util.test_fail('change http endpoint state failed')
    zwt_ops.change_sns_application_platform_state(email_platform_uuid,state_event, session_uuid=project_login_uuid)
    cond = res_ops.gen_query_conditions('uuid', '=', email_platform_uuid)
    inv = res_ops.query_resource(res_ops.SNS_APPLICATION_PLATFORM, cond, session_uuid=project_login_uuid)[0]
    if inv.state != state_result:
        test_util.test_fail('change email platform state failed')

    # test recover and delete
    state_event='enable'
    state_result='Enabled'
    zwt_ops.change_sns_topic_state(system_alarm_topic_uuid,state_event, session_uuid=project_login_uuid)
    cond=res_ops.gen_query_conditions('uuid','=',system_alarm_topic_uuid)
    inv=res_ops.query_resource(res_ops.SNS_TOPIC,cond, session_uuid=project_login_uuid)[0]
    if inv.state!=state_result:
        test_util.test_fail('change system alarm topic state failed')
    zwt_ops.change_sns_topic_state(api_topic_uuid, state_event, session_uuid=project_login_uuid)
    cond = res_ops.gen_query_conditions('uuid', '=', api_topic_uuid)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC, cond, session_uuid=project_login_uuid)[0]
    if inv.state != state_result:
        test_util.test_fail('change api topic state failed')
    zwt_ops.unsubscribe_event(event_sub_uuid, session_uuid=project_login_uuid)
    cond = res_ops.gen_query_conditions('uuid', '=', event_sub_uuid)
    event_subscription = res_ops.query_resource(res_ops.EVENT_SUBSCRIPTION, cond, session_uuid=project_login_uuid)
    if event_subscription:
        test_util.test_fail('unsubscribe event failed')
    zwt_ops.unsubscribe_sns_topic(sns_topic_uuid, email_endpoint_uuid)
    cond =res_ops.gen_query_conditions('endpointUuid','=',email_endpoint_uuid)
    cond=res_ops.gen_query_conditions('topicUuid','=',sns_topic_uuid,cond)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC_SUBSCRIBER,cond, session_uuid=project_login_uuid)
    if inv:
        test_util.test_fail('unsubscribe sns topic failed')
    zwt_ops.unsubscribe_sns_topic(system_alarm_topic_uuid, email_endpoint_uuid, session_uuid=project_login_uuid)
    cond =res_ops.gen_query_conditions('endpointUuid','=',email_endpoint_uuid)
    cond=res_ops.gen_query_conditions('topicUuid','=',system_alarm_topic_uuid,cond)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC_SUBSCRIBER,cond, session_uuid=project_login_uuid)
    if inv:
        test_util.test_fail('unsubscribe system alarm topic failed')
    zwt_ops.unsubscribe_sns_topic(api_topic_uuid, http_endpoint_uuid, session_uuid=project_login_uuid)
    cond =res_ops.gen_query_conditions('endpointUuid','=',http_endpoint_uuid)
    cond=res_ops.gen_query_conditions('topicUuid','=',api_topic_uuid,cond)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC_SUBSCRIBER,cond, session_uuid=project_login_uuid)
    if inv:
        test_util.test_fail('unsubscribe api topic failed')
    zwt_ops.delete_sns_topic(sns_topic_uuid, session_uuid=project_login_uuid)
    cond=res_ops.gen_query_conditions('uuid','=',sns_topic_uuid)
    inv = res_ops.query_resource(res_ops.SNS_TOPIC, cond, session_uuid=project_login_uuid)
    if inv:
        test_util.test_fail('delete sns topic failed')
    zwt_ops.delete_sns_application_endpoint(http_endpoint_uuid, session_uuid=project_login_uuid)
    cond=res_ops.gen_query_conditions('uuid','=',http_endpoint_uuid)
    inv = res_ops.query_resource(res_ops.SNS_APPLICATION_ENDPOINT, cond, session_uuid=project_login_uuid)
    if inv:
        test_util.test_fail('delete http endpoint failed')
    zwt_ops.delete_sns_application_endpoint(email_endpoint_uuid, session_uuid=project_login_uuid)
    cond=res_ops.gen_query_conditions('uuid','=',email_endpoint_uuid)
    inv = res_ops.query_resource(res_ops.SNS_APPLICATION_ENDPOINT, cond, session_uuid=project_login_uuid)
    if inv:
        test_util.test_fail('delete email endpoint failed')
    zwt_ops.delete_sns_application_platform(email_platform_uuid, session_uuid=project_login_uuid)
    cond=res_ops.gen_query_conditions('uuid','=',email_platform_uuid)
    inv = res_ops.query_resource(res_ops.SNS_APPLICATION_PLATFORM, cond, session_uuid=project_login_uuid)
    if inv:
        test_util.test_fail('delete email platform failed')

    # 11 delete
    acc_ops.logout(project_login_uuid)
    if virtual_id_uuid != None:
        iam2_ops.delete_iam2_virtual_id(virtual_id_uuid)
    if project_admin_uuid != None:
        iam2_ops.delete_iam2_virtual_id(project_admin_uuid)
    if project_operator_uuid != None:
        iam2_ops.delete_iam2_virtual_id(project_operator_uuid)
    if plain_user_uuid != None:
        iam2_ops.delete_iam2_virtual_id(plain_user_uuid)

    iam2_ops.delete_iam2_project(project_uuid)
    iam2_ops.expunge_iam2_project(project_uuid)

    test_util.test_pass('success test iam2 login in by project admin!')
def test():
    global email_endpoint_uuid,email_platform_uuid,my_sns_topic_uuid,host_sns_topic_uuid,ps_uuid,hostname,host_management_ip,host_uuid,host_status,bs_uuid,bs_type,bs_status
    bs_cond = res_ops.gen_query_conditions('status', '=', 'Connected')
    bs_list = res_ops.query_resource(res_ops.BACKUP_STORAGE, bs_cond)
    for bss in bs_list:
        if bss.type ==res_ops.SFTP_BACKUP_STORAGE:
            bs_uuid =bss.uuid
            bs_type =bss.type
            hostname = bss.hostname
            break
        elif bss.type == res_ops.IMAGE_STORE_BACKUP_STORAGE:
            bs_uuid=bss.uuid
            bs_type=bss.type
            hostname = bss.hostname
            break
        else:
            test_util.test_skip('No match backupStorage,test skip')

    smtp_server = os.environ.get('smtpServer')
    pop_server = os.environ.get('popServer')
    smtp_port = os.environ.get('smtpPort')
    username = os.environ.get('mailUsername')
    password = os.environ.get('mailPassword')
    email_platform_name='Alarm_email'
    email_platform = zwt_ops.create_sns_email_platform(smtp_server, smtp_port,email_platform_name , username, password)
    email_platform_uuid = email_platform.uuid
    try:
        zwt_ops.validate_sns_email_platform(email_platform_uuid)
    except:
        test_util.test_fail('Validate SNS Email Platform Failed, Email Plarform: %s' % email_platform_uuid)
    email_endpoint_uuid = zwt_ops.create_sns_email_endpoint(username, 'test_qa', email_platform_uuid).uuid

    my_sns_topic = zwt_ops.create_sns_topic('my_sns_topic')
    my_sns_topic_uuid = my_sns_topic.uuid
    zwt_ops.subscribe_sns_topic(my_sns_topic_uuid, email_endpoint_uuid)

    host_sns_topic = zwt_ops.create_sns_topic('host_topic')
    host_sns_topic_uuid = host_sns_topic.uuid
    zwt_ops.subscribe_sns_topic(host_sns_topic_uuid, email_endpoint_uuid)

    ps_actions = [{"actionUuid": my_sns_topic_uuid, "actionType": "sns"}]
    ps_namespace = 'ZStack/PrimaryStorage'
    ps_disconnected = 'PrimaryStorageDisconnected'
    ps_event_sub_uuid = zwt_ops.subscribe_event(ps_namespace, ps_disconnected, ps_actions).uuid
    event_list.append(ps_event_sub_uuid)

    bs_actions = [{"actionUuid": my_sns_topic_uuid, "actionType": "sns"}]
    bs_namespace = 'ZStack/BackupStorage'
    bs_disconnected = 'BackupStorageDisconnected'
    bs_event_sub_uuid = zwt_ops.subscribe_event(bs_namespace, bs_disconnected, bs_actions).uuid
    event_list.append(bs_event_sub_uuid)

    host_actions = [{"actionUuid": host_sns_topic_uuid, "actionType": "sns"}]
    host_namespace = 'ZStack/Host'
    host_status_changed = 'HostStatusChanged'
    host_status_labels = [{"key": "NewStatus", "op": "Equal", "value": "Disconnected"}]
    host_status_event_sub_uuid = zwt_ops.subscribe_event(host_namespace, host_status_changed, host_actions, host_status_labels).uuid
    event_list.append(host_status_event_sub_uuid)

    host_disconnected = 'HostDisconnected'
    host_disconnected_event_sub_uuid = zwt_ops.subscribe_event(host_namespace, host_disconnected, host_actions).uuid
    event_list.append(host_disconnected_event_sub_uuid)

    if zwt_ops.check_sns_email(pop_server, username, password, ps_disconnected, ps_event_sub_uuid):
        test_util.test_fail('email already exsist before test')
    if zwt_ops.check_sns_email(pop_server, username, password, bs_disconnected, bs_event_sub_uuid):
        test_util.test_fail('email already exsist before test')
    if zwt_ops.check_sns_email(pop_server, username, password, host_status_changed, host_status_event_sub_uuid):
        test_util.test_fail('email already exsist before test')
    if zwt_ops.check_sns_email(pop_server, username, password, host_disconnected, host_disconnected_event_sub_uuid):
        test_util.test_fail('email already exsist before test')

    # Disconnected ps ,bs and host
    zone_uuid = res_ops.query_resource(res_ops.ZONE)[0].uuid
    primary_storage_option = test_util.PrimaryStorageOption()
    primary_storage_option.set_type('nfs')
    primary_storage_option.set_zone_uuid(zone_uuid)
    primary_storage_option.set_name('test_nfs_ps')
    primary_storage_option.set_url('222.222.222.222/nfs/')
    try:
        ps_uuid = ps_ops.create_nfs_primary_storage(primary_storage_option).uuid
        ps_ops.reconnect_primary_storage(ps_uuid)
    except:
        pass

    if bs_type == res_ops.IMAGE_STORE_BACKUP_STORAGE:
        bs_ops.update_image_store_backup_storage_info(bs_uuid, 'hostname', '222.222.222.222')
        try:
            bs_ops.reconnect_backup_storage(bs_uuid)
        except:
            cond = res_ops.gen_query_conditions('uuid', '=', bs_uuid)
            bs_status = res_ops.query_resource(res_ops.IMAGE_STORE_BACKUP_STORAGE, cond)[0].status
    elif bs_type == res_ops.SFTP_BACKUP_STORAGE:
        bs_ops.update_sftp_backup_storage_info(bs_uuid,'hostname','222.222.222.222')
        try:
            bs_ops.reconnect_backup_storage(bs_uuid)
        except:
            cond=res_ops.gen_query_conditions('uuid','=',bs_uuid)
            bs_status=res_ops.query_resource(res_ops.SFTP_BACKUP_STORAGE,cond)[0].status

    host_cond = res_ops.gen_query_conditions('status', '=', 'Connected')
    host = res_ops.query_resource_with_num(res_ops.HOST, host_cond, start=0, limit=1)[0]
    host_uuid = host.uuid
    host_management_ip = host.managementIp
    host_ops.update_host(host_uuid, 'managementIp', '222.222.222.222')
    try:
        host_ops.reconnect_host(host_uuid)
    except:
        cond = res_ops.gen_query_conditions('uuid', '=', host_uuid)
        bs_status = res_ops.query_resource(res_ops.HOST, cond)[0].status

    # wait for send email
    time.sleep(120)

    ps_ops.delete_primary_storage(ps_uuid)
    if hostname:
        if bs_type == res_ops.IMAGE_STORE_BACKUP_STORAGE:
            bs_ops.update_image_store_backup_storage_info(bs_uuid, 'hostname', hostname)
            bs_ops.reconnect_backup_storage(bs_uuid)
        elif bs_type == res_ops.SFTP_BACKUP_STORAGE:
            bs_ops.update_sftp_backup_storage_info(bs_uuid, 'hostname', hostname)
            bs_ops.reconnect_backup_storage(bs_uuid)
    host_ops.update_host(host_uuid, 'managementIp', host_management_ip)
    host_ops.reconnect_host(host_uuid)
    zwt_ops.delete_sns_topic(host_sns_topic_uuid)
    zwt_ops.delete_sns_topic(my_sns_topic_uuid)
    for event_uuid in event_list:
        zwt_ops.unsubscribe_event(event_uuid)
    zwt_ops.delete_sns_application_endpoint(email_endpoint_uuid)
    zwt_ops.delete_sns_application_platform(email_platform_uuid)

    check_1 = zwt_ops.check_sns_email(pop_server, username, password, ps_disconnected, ps_event_sub_uuid)
    check_2 = zwt_ops.check_sns_email(pop_server, username, password, bs_disconnected, bs_event_sub_uuid)
    check_3 = zwt_ops.check_sns_email(pop_server, username, password, host_status_changed, host_status_event_sub_uuid)
    check_4 = zwt_ops.check_sns_email(pop_server, username, password, host_disconnected, host_disconnected_event_sub_uuid)

    if check_1 and check_2 and check_3 and check_4:
        test_util.test_pass('test host ,bs, ps disconnected event with email success!')
    else:
        test_util.test_fail('cannt receive all event mail')
def test():
    global email_platform_uuid,email_endpoint_uuid,dingtalk_endpoint_uuid,http_endpoint_uuid,alarm_uuid,ps_event_sub_uuid,sns_topic_uuid,alarm_template_uuid
    iam2_ops.clean_iam2_enviroment()

    zone_uuid = res_ops.get_resource(res_ops.ZONE)[0].uuid
    # 3 create project
    project_name = 'test_project'
    password = \
        'b109f3bbbc244eb82441917ed06d618b9008dd09b3befd1b5e07394c706a8bb980b1d7785e5976ec049b46df5f1326af5a2ea6d103fd07c95385ffab0cacbc86'
    project = iam2_ops.create_iam2_project(project_name)
    project_uuid = project.uuid
    linked_account_uuid = project.linkedAccountUuid
    attributes = [{"name": "__ProjectRelatedZone__", "value": zone_uuid}]
    iam2_ops.add_attributes_to_iam2_project(project_uuid, attributes)
    test_stub.share_admin_resource_include_vxlan_pool([linked_account_uuid])

    # 4 create projectAdmin  into project
    project_admin_name = 'projectadmin'
    project_admin_uuid = iam2_ops.create_iam2_virtual_id(project_admin_name, password).uuid
    iam2_ops.add_iam2_virtual_ids_to_project([project_admin_uuid], project_uuid)
    attributes = [{"name": "__ProjectAdmin__", "value": project_uuid}]
    iam2_ops.add_attributes_to_iam2_virtual_id(project_admin_uuid, attributes)
    project_admin_session_uuid = iam2_ops.login_iam2_virtual_id(project_admin_name,password)
    project_admin_session_uuid = iam2_ops.login_iam2_project(project_name,project_admin_session_uuid).uuid

    # 5 create zwatch resource
    smtp_server = os.environ.get('smtpServer')
    smtp_port = os.environ.get('smtpPort')
    email_platform_name = 'Alarm_email'
    email_username = os.environ.get('mailUsername')
    email_password = os.environ.get('mailPassword')
    email_platform_uuid = zwt_ops.create_sns_email_platform(smtp_server, smtp_port, email_platform_name, email_username, email_password,session_uuid=project_admin_session_uuid).uuid

    email_receiver = os.environ.get('mailUsername')
    email_endpoint_uuid = zwt_ops.create_sns_email_endpoint(email_receiver, 'test_email_endpoint', email_platform_uuid,session_uuid=project_admin_session_uuid).uuid

    url_01 = 'https://oapi.dingtalk.com/robot/send?access_token' \
             '=0be899d4bd0a7629961a5ccd3035dfba30d084b57944897838f1b601006dd153'
    name_01 = 'dingtalkAtPerson'
    dingtalk_endpoint_uuid = zwt_ops.create_sns_dingtalk_endpoint(url_01, name_01, at_all=False,session_uuid=project_admin_session_uuid).uuid
    http_endpoint_name = 'http'
    url = 'http://localhost:8080/webhook-url'
    http_username = '******'
    http_password = '******'
    http_endpoint_uuid = zwt_ops.create_sns_http_endpoint(url, http_endpoint_name, http_username, http_password,session_uuid=project_admin_session_uuid).uuid

    sns_topic_uuid = zwt_ops.create_sns_topic('sns_topic_01',session_uuid=project_admin_session_uuid).uuid
    zwt_ops.subscribe_sns_topic(sns_topic_uuid, dingtalk_endpoint_uuid,session_uuid=project_admin_session_uuid)
    zwt_ops.subscribe_sns_topic(sns_topic_uuid, email_endpoint_uuid,session_uuid=project_admin_session_uuid)
    zwt_ops.subscribe_sns_topic(sns_topic_uuid, http_endpoint_uuid,session_uuid=project_admin_session_uuid)

    namespace = 'ZStack/Volume'
    actions = [{"actionUuid": sns_topic_uuid, "actionType": "sns"}]
    comparisonOperator = 'GreaterThanOrEqualTo'
    repeat_interval = 20
    period = 10
    threshold = 5
    metric_name = 'TotalVolumeCount'
    alarm_uuid = zwt_ops.create_alarm(comparisonOperator, period, threshold, namespace, metric_name, actions=actions,repeat_interval=repeat_interval,session_uuid=project_admin_session_uuid).uuid

    ps_actions = [{"actionUuid": sns_topic_uuid, "actionType": "sns"}]
    ps_namespace = 'ZStack/PrimaryStorage'
    ps_disconnected = 'PrimaryStorageDisconnected'
    ps_event_sub_uuid = zwt_ops.subscribe_event(ps_namespace, ps_disconnected, ps_actions, session_uuid=project_admin_session_uuid).uuid

    application_platform_type = 'Email'
    alarm_template_name = 'my-alarm-template'
    alarm_template = '${ALARM_NAME} Change status to ${ALARM_CURRENT_STATUS}' \
                     'ALARM_UUID:${ALARM_UUID}' \
                     'keyword1:ThisWordIsKeyWord' \
                     'keyword2:TemplateForAlarmOn' \
                     '(Using for template changes email check)'
    alarm_template_uuid = zwt_ops.create_sns_text_template(alarm_template_name,
                                                             application_platform_type,
                                                             alarm_template,
                                                             default_template=False, session_uuid=project_admin_session_uuid).uuid

    acc_ops.logout(project_admin_session_uuid)

    # 6 delete project
    iam2_ops.delete_iam2_project(project_uuid)
    iam2_ops.expunge_iam2_project(project_uuid)

    # 7 cascade test
    try:
        test_stub.check_resource_not_exist(email_platform_uuid,res_ops.SNS_EMAIL_PLATFORM)
    except:
        test_util.test_logger("email platform should not be delete ,success")
    test_stub.check_resource_not_exist(email_endpoint_uuid,res_ops.SNS_APPLICATION_ENDPOINT)
    test_stub.check_resource_not_exist(dingtalk_endpoint_uuid,res_ops.SNS_APPLICATION_ENDPOINT)
    test_stub.check_resource_not_exist(http_endpoint_uuid,res_ops.SNS_APPLICATION_ENDPOINT)
    test_stub.check_resource_not_exist(alarm_uuid,res_ops.ALARM)
    test_stub.check_resource_not_exist(ps_event_sub_uuid,res_ops.EVENT_SUBSCRIPTION)
    test_stub.check_resource_not_exist(sns_topic_uuid,res_ops.SNS_TOPIC)
    test_stub.check_resource_not_exist(alarm_template_uuid,res_ops.SNS_TEXT_TEMPLATE)

    zwt_ops.delete_sns_application_platform(email_platform_uuid)

    iam2_ops.clean_iam2_enviroment()
    test_util.test_pass("success test project retired")