def error_cleanup():
    global email_endpoint_uuid,email_platform_uuid,my_sns_topic_uuid,host_sns_topic_uuid,ps_uuid,hostname,host_management_ip,host_status,bs_type,bs_status,event_list
    if host_status == 'Disconnected':
        host_ops.update_host(host_uuid, 'managementIp', host_management_ip)
        host_ops.reconnect_host(host_uuid)
    if bs_status == 'Disconnected':
        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)
    if ps_uuid:
        ps_ops.delete_primary_storage(ps_uuid)
    if host_sns_topic_uuid:
        zwt_ops.delete_sns_topic(host_sns_topic_uuid)
    if my_sns_topic_uuid:
        zwt_ops.delete_sns_topic(my_sns_topic_uuid)
    if event_list:
        for event_uuid in event_list:
            zwt_ops.unsubscribe_event(event_uuid)
    if email_endpoint_uuid:
        zwt_ops.delete_sns_application_endpoint(email_endpoint_uuid)
    if email_platform_uuid:
        zwt_ops.delete_sns_application_platform(email_platform_uuid)
def error_cleanup():
    global email_endpoint_uuid, email_platform_uuid, my_sns_topic_uuid, host_sns_topic_uuid, ps_uuid, hostname, host_management_ip, host_status, bs_type, bs_status, event_list
    if host_status == 'Disconnected':
        host_ops.update_host(host_uuid, 'managementIp', host_management_ip)
        host_ops.reconnect_host(host_uuid)
    if bs_status == 'Disconnected':
        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)
    if ps_uuid:
        ps_ops.delete_primary_storage(ps_uuid)
    if host_sns_topic_uuid:
        zwt_ops.delete_sns_topic(host_sns_topic_uuid)
    if my_sns_topic_uuid:
        zwt_ops.delete_sns_topic(my_sns_topic_uuid)
    if event_list:
        for event_uuid in event_list:
            zwt_ops.unsubscribe_event(event_uuid)
    if email_endpoint_uuid:
        zwt_ops.delete_sns_application_endpoint(email_endpoint_uuid)
    if email_platform_uuid:
        zwt_ops.delete_sns_application_platform(email_platform_uuid)
def error_cleanup():
    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()
    if email_platform_uuid:
        zwt_ops.delete_sns_application_platform(email_platform_uuid)
    if email_endpoint_uuid:
        zwt_ops.delete_sns_application_endpoint(email_endpoint_uuid)
    if dingtalk_endpoint_uuid:
        zwt_ops.delete_sns_application_endpoint(dingtalk_endpoint_uuid)
    if http_endpoint_uuid:
        zwt_ops.delete_sns_application_endpoint(http_endpoint_uuid)
    if alarm_uuid:
        zwt_ops.delete_alarm(alarm_uuid)
    if ps_event_sub_uuid:
        zwt_ops.unsubscribe_event(ps_event_sub_uuid)
    if sns_topic_uuid:
        zwt_ops.delete_sns_topic(sns_topic_uuid)
    if alarm_template_uuid:
        zwt_ops.delete_sns_text_template(alarm_template_uuid)
def error_cleanup():
    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()
    if email_platform_uuid:
        zwt_ops.delete_sns_application_platform(email_platform_uuid)
    if email_endpoint_uuid:
        zwt_ops.delete_sns_application_endpoint(email_endpoint_uuid)
    if dingtalk_endpoint_uuid:
        zwt_ops.delete_sns_application_endpoint(dingtalk_endpoint_uuid)
    if http_endpoint_uuid:
        zwt_ops.delete_sns_application_endpoint(http_endpoint_uuid)
    if alarm_uuid:
        zwt_ops.delete_alarm(alarm_uuid)
    if ps_event_sub_uuid:
        zwt_ops.unsubscribe_event(ps_event_sub_uuid)
    if sns_topic_uuid:
        zwt_ops.delete_sns_topic(sns_topic_uuid)
    if alarm_template_uuid:
        zwt_ops.delete_sns_text_template(alarm_template_uuid)
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, 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_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 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')