def error_cleanup():
    global ir_option
    test_lib.lib_error_cleanup(test_obj_dict)
    ir = res_ops.get_resource(res_ops.IP_RANGE, name = ir1_name)
    if not ir:
        try:
            net_ops.add_ip_range(ir_option)
        except Exception as e:
            test_util.test_warn('Fail to recover [ip range:] %s resource. It will impact later test case.' % ir1_name)
            raise e
def error_cleanup():
    global ir_option
    test_lib.lib_error_cleanup(test_obj_dict)
    ir = res_ops.get_resource(res_ops.IP_RANGE, name=ir1_name)
    if not ir:
        try:
            net_ops.add_ip_range(ir_option)
        except Exception as e:
            test_util.test_warn(
                'Fail to recover [ip range:] %s resource. It will impact later test case.'
                % ir1_name)
            raise e
def test():

    # create l3 network
    global l3, l2

    name = 'mini_network_test'
    test_util.test_dsc('create L2_vlan network mini_l2_network_test')
    zone_uuid = res_ops.query_resource(res_ops.ZONE)[0].uuid
    cluster_uuid = res_ops.query_resource(res_ops.CLUSTER)[0].uuid
    l2 = net_ops.create_l2_vlan('l2_vlan', 'zsn0', zone_uuid, '1998')
    l2_uuid = l2.inventory.uuid
    net_ops.attach_l2(l2_uuid, cluster_uuid)

    type='L3BasicNetwork'
    categry='Private'
    l3=net_ops.create_l3(name, l2_uuid, category=categry, Type=type)

    # add dns to l3 network
    l3_dns = '223.5.5.5'
    net_ops.add_dns_to_l3(l3.uuid, l3_dns)
    test_util.test_dsc('add DNS and IP_Range for L3_flat_network')

    # add ip range to l3 network
    ip_range_option = test_util.IpRangeOption()
    ip_range_option.set_l3_uuid(l3.uuid)
    ip_range_option.set_startIp('192.168.40.2')
    ip_range_option.set_endIp('192.168.40.20')
    ip_range_option.set_gateway('192.168.40.1')
    ip_range_option.set_netmask('255.255.255.0')
    ip_range_option.set_name('ip_range_test')

    net_ops.add_ip_range(ip_range_option)

    #attach network service to l3 network
    cond = res_ops.gen_query_conditions('type', '=', 'flat')
    provider1_uuid = res_ops.query_resource(res_ops.NETWORK_SERVICE_PROVIDER, cond)[0].uuid
    cond = res_ops.gen_query_conditions('type', '=', 'SecurityGroup')
    provider2_uuid = res_ops.query_resource(res_ops.NETWORK_SERVICE_PROVIDER, cond)[0].uuid
    providers = {provider1_uuid:['DHCP','Eip'], provider2_uuid:['SecurityGroup']}
    attach_network_service_to_l3network(l3.uuid, providers)
    test_util.test_dsc('a network with dns, ip range and network services has been created successfully')
    net_ops.delete_l3(l3.uuid)
    net_ops.delete_l2(l2_uuid)
    test_util.test_dsc('delete l3 network after test')
def test():
    global test_obj_dict
    cond_l3 = res_ops.gen_query_conditions('name', '=', os.environ.get('l3PublicNetworkName'))
    l3_uuid = res_ops.query_resource(res_ops.L3_NETWORK, cond_l3)[0].uuid
    ip_range_uuid = res_ops.query_resource(res_ops.L3_NETWORK, cond_l3)[0].ipRanges[0].uuid
    vm = test_stub.create_vm()

    #Get dhcp ip
    dhcp_ip = net_ops.get_l3network_dhcp_ip(l3NetworkUuid=l3_uuid)
    sys_tag = ["staticIp::%s::%s" % (l3_uuid, dhcp_ip)]
    vm.destroy()
    vm.expunge()
    time.sleep(10)
    ip_range=test_lib.lib_get_ip_range_by_l3_uuid(l3NetworkUuid=l3_uuid)
    net_ops.delete_ip_range(ip_range_uuid)
    time.sleep(2)
    ip_range_option = test_util.IpRangeOption()
    ip_range_option.set_l3_uuid(l3_uuid=l3_uuid)
    ip_range_option.set_startIp(startIp=ip_range.startIp)
    ip_range_option.set_endIp(endIp=ip_range.endIp)
    ip_range_option.set_gateway(gateway=ip_range.gateway)
    ip_range_option.set_netmask(netmask=ip_range.netmask)
    ip_range_option.set_name(name=ip_range.name)
    net_ops.add_ip_range(ip_range_option=ip_range_option)
    time.sleep(2)
    vm = test_stub.create_vm(vm_name='vm-dhcp-ip', system_tags=sys_tag)
    test_obj_dict.add_vm(vm)
    time.sleep(60)
    vm_ip = vm.vm.vmNics[0].ip

    cmd = "ping -c 5 %s" % (vm_ip)
    (retcode, output, erroutput) = ssh.execute(cmd, ext_host_ip, "root", ext_host_pwd, True, 22)
    print "retcode is: %s; output is : %s.; erroutput is: %s" % (retcode, output, erroutput)
    vm.destroy()
    vm.expunge()
    if retcode != 0:
        test_util.test_fail('Cannot ping public IP, test Failed.')
def test():
    global ir_option

    vm_creation_option = test_util.VmOption()
    l3_name = os.environ.get('l3VlanNetworkNameMultiRange')
    l3 = res_ops.get_resource(res_ops.L3_NETWORK, name=l3_name)[0]
    vm_creation_option.set_l3_uuids([l3.uuid])

    image_name = os.environ.get('imageName_net')
    image_uuid = test_lib.lib_get_image_by_name(image_name).uuid

    conditions = res_ops.gen_query_conditions('type', '=', 'UserVm')
    instance_offering_uuid = res_ops.query_resource(res_ops.INSTANCE_OFFERING,
                                                    conditions)[0].uuid
    vm_creation_option.set_image_uuid(image_uuid)
    vm_creation_option.set_instance_offering_uuid(instance_offering_uuid)
    vm_creation_option.set_name('multizones_delete_ip_range')

    ir1 = res_ops.get_resource(res_ops.IP_RANGE, name=ir1_name)[0]

    ir_option.set_name(ir1_name)
    ir_option.set_description(ir1.description)
    ir_option.set_netmask(ir1.netmask)
    ir_option.set_gateway(ir1.gateway)
    ir_option.set_l3_uuid(ir1.l3NetworkUuid)
    ir_option.set_startIp(ir1.startIp)
    ir_option.set_endIp(ir1.endIp)

    ir_start_ip = ip_header.IpAddress(ir1.startIp)
    ir_end_ip = ip_header.IpAddress(ir1.endIp)

    #test_util.test_dsc('create vms')
    #vm1 = test_lib.lib_create_vm(vm_creation_option)
    #test_obj_dict.add_vm(vm1)
    #vm1_ip = ip_header.IpAddress(vm1.get_vm().vmNics[0].ip)
    #test_util.test_logger('ip address: %s' % vm1_ip)

    #vm2 = test_lib.lib_create_vm(vm_creation_option)
    #test_obj_dict.add_vm(vm2)
    #vm2_ip = ip_header.IpAddress(vm2.get_vm().vmNics[0].ip)
    #test_util.test_logger('ip address: %s' % vm2_ip)

    #vm3 = test_lib.lib_create_vm(vm_creation_option)
    #test_obj_dict.add_vm(vm3)
    #vm3_ip = ip_header.IpAddress(vm3.get_vm().vmNics[0].ip)
    #test_util.test_logger('ip address: %s' % vm3_ip)

    #vm4 = test_lib.lib_create_vm(vm_creation_option)
    #test_obj_dict.add_vm(vm4)
    #vm4_ip = ip_header.IpAddress(vm4.get_vm().vmNics[0].ip)
    #test_util.test_logger('ip address: %s' % vm4_ip)

    test_util.test_dsc('create vm and make sure vm ip range is in ir1.')
    ip_range_flag = False
    try_times = 0
    while not ip_range_flag and try_times < 10:
        vm1 = test_lib.lib_create_vm(vm_creation_option)
        test_obj_dict.add_vm(vm1)
        vm1_ip = ip_header.IpAddress(vm1.get_vm().vmNics[0].ip)
        test_util.test_logger('ip address: %s' % vm1_ip)
        if vm1_ip <= ir_end_ip and vm1_ip >= ir_start_ip:
            ip_range_flag = True
            break
        vm1.destroy()
        test_obj_dict.rm_vm(vm1)
        try_times += 1

    if not ip_range_flag:
        test_util.test_fail(
            'Can not deply IP address on [l3:] %s in [IP Range:] %s for %s times VMs creation. It might means the VM IP address allocation method has bug.'
            % (l3.uuid, ir1.uuid, try_times))

    test_util.test_dsc('delete ip range')
    net_ops.delete_ip_range(ir1.uuid)

    #vm1_flag = False
    #vm2_flag = False

    #if vm1_ip <= ir_end_ip and vm1_ip >= ir_start_ip:
    #    test_obj_dict.mv_vm(vm1, vm_header.RUNNING, vm_header.STOPPED)
    #    vm1.set_state(vm_header.STOPPED)
    #    vm1.update()
    #    vm1_flag = True

    #vm1.check()

    test_util.test_dsc('start vm with other ip range.')
    vm1.start()

    vm1_new_ip = ip_header.IpAddress(vm1.get_vm().vmNics[0].ip)
    if vm1_ip == vm1_new_ip:
        test_util.test_fail(
            'VM still get origianl [IP:] %s , after delete the IP Range.' %
            vm1_ip)

    if vm1_new_ip <= ir_end_ip and vm1_new_ip >= ir_start_ip:
        test_util.test_fail(
            'VM still get unexpected [IP:] %s , after delete the IP Range: [%s] ~ [%s].'
            % (vm1_ip, ir_start_ip, ir_end_ip))

    #The new start vm's IP address need to refresh (restart network srevice)
    vm1.check()

    net_ops.add_ip_range(ir_option)

    vm1.destroy()
    test_util.test_pass('Delete IP Range Test Success')
def test():
    global l2_vxlan_network_uuid,project_uuid,project_operator_uuid,vni_range_uuid,vxlan_pool_uuid,l3_vr_network_uuid

    # create vxlan pool and vni range
    zone_uuid = res_ops.get_resource(res_ops.ZONE)[0].uuid
    cluster_uuid = res_ops.get_resource(res_ops.CLUSTER)[0].uuid
    vxlan_pool_name = 'vxlan_pool_name'

    vxlan_pool_uuid = vxlan_ops.create_l2_vxlan_network_pool(vxlan_pool_name,zone_uuid).uuid
    vxlan_ops.create_vni_range('vni_range',20,40,vxlan_pool_uuid)

    systemTags = ["l2NetworkUuid::%s::clusterUuid::%s::cidr::{172.20.0.1/16}"%(vxlan_pool_uuid,cluster_uuid)]
    net_ops.attach_l2_vxlan_pool(vxlan_pool_uuid,cluster_uuid,systemTags)

    # 1 create project
    project_name = 'test_project7'
    project = iam2_ops.create_iam2_project(project_name)
    project_uuid = project.uuid
    #cond = res_ops.gen_query_conditions("name",'=',"test_project7")
    #linked_account_uuid = res_ops.query_resource(res_ops.ACCOUNT,cond)[0].uuid
    linked_account_uuid = project.linkedAccountUuid

    # 2 create project operator
    project_operator_name = 'username7'
    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
    
    zone_uuid = res_ops.get_resource(res_ops.ZONE)[0].uuid
    attributes = [{"name": "__ProjectRelatedZone__", "value": zone_uuid}]
    iam2_ops.add_attributes_to_iam2_project(project_uuid, attributes)
    # 3 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

    # 4 share vxlan pool to project
    l2vxlan_pools = res_ops.get_resource(res_ops.L2_VXLAN_NETWORK_POOL)
    for l2vxlan_pool in l2vxlan_pools:
        acc_ops.share_resources([linked_account_uuid],[l2vxlan_pool.uuid])
    # 5 create l2 vxlan 
    l2_vxlan_network_uuid = vxlan_ops.create_l2_vxlan_network('l2_vxlan',vxlan_pool_uuid,zone_uuid,session_uuid=project_login_uuid).uuid
    
    
    # 6 use the l2 vxlan to create l3
    # 6.1 create l3_vr_network
    l3_vr_network_uuid = net_ops.create_l3('l3_vr_network',l2_vxlan_network_uuid,session_uuid=project_login_uuid).uuid
    
    cond = res_ops.gen_query_conditions('name','=','virtual-router-vm')
    vr_instance_uuid = res_ops.query_resource(res_ops.INSTANCE_OFFERING,cond)[0].uuid
    tag = "guestL3Network::" + l3_vr_network_uuid
    tag_ops.create_system_tag("InstanceOfferingVO",vr_instance_uuid,tag)
    # add ip range
    ir_option = test_util.IpRangeOption()
    ir_option.set_name('iprange1')
    ir_option.set_description('iprange for vrouter network')
    ir_option.set_netmask('255.255.255.0')
    ir_option.set_gateway('192.168.22.1')
    ir_option.set_l3_uuid(l3_vr_network_uuid)
    ir_option.set_startIp('192.168.22.2')
    ir_option.set_endIp('192.168.22.254')
    
    net_ops.add_ip_range(ir_option)
    # add dns
    AddDnsToL3Network(l3_vr_network_uuid,dns_text,session_uuid = project_login_uuid)
    # add network service
    AttachNetworkServiceToL3Network(l3_vr_network_uuid,allservices,session_uuid = project_login_uuid)
    
    # create vrouter vm 
    test_stub.share_admin_resource_include_vxlan_pool([linked_account_uuid])
    vr_vm = test_stub.create_vr_vm(test_obj_dict, l3_vr_network_uuid,session_uuid=project_login_uuid)
    test_stub.check_resource_not_exist(vr_vm.uuid,res_ops.VM_INSTANCE)
    
    #6 expunge the project and check the l2 vxlan
    iam2_ops.delete_iam2_project(project_uuid)
    iam2_ops.expunge_iam2_project(project_uuid)
    
    try:
        l2_vxlan_network_test_uuid = res_ops.query_resource(res_ops.L2_VXLAN_NETWORK)[0].uuid
    except: 
        
        test_util.test_pass(
            "l2 vxlan  is delete after delete the project " )
    test_util.test_dsc('test l2 l2 cascade delete')

    # 7 check the l3 vrouter network
    try:
        cond = res_ops.gen_query_conditions("name",'=',"l3_vr_network")
        l3_vrouter_network_uuid = res_ops.query_resource(res_ops.L3_NETWORK,cond)[0].uuid
    except:
        
        test_util.test_pass(
            "l3_vrouter network  is deleted after delete the project")
    
    # todo:check the virtual router vm 
    


    # 8 delete 
    vni_range_uuid = res_ops.get_resource(res_ops.VNI_RANGE)[0].uuid
    vxlan_ops.delete_vni_range(vni_range_uuid)
    net_ops.delete_l2(vxlan_pool_uuid)
    iam2_ops.delete_iam2_virtual_id(project_operator_uuid)
def test():
    global l2_vxlan_network_uuid,project_uuid,project_operator_uuid,vni_range_uuid,vxlan_pool_uuid,l3_vpc_network_uuid

    # create vxlan pool and vni range
    zone_uuid = res_ops.get_resource(res_ops.ZONE)[0].uuid
    cluster_uuid = res_ops.get_resource(res_ops.CLUSTER)[0].uuid
    vxlan_pool_name = 'vxlan_pool_name'

    vxlan_pool_uuid = vxlan_ops.create_l2_vxlan_network_pool(vxlan_pool_name,zone_uuid).uuid
    vxlan_ops.create_vni_range('vni_range',20,40,vxlan_pool_uuid)

    systemTags = ["l2NetworkUuid::%s::clusterUuid::%s::cidr::{172.20.0.1/16}"%(vxlan_pool_uuid,cluster_uuid)]
    net_ops.attach_l2_vxlan_pool(vxlan_pool_uuid,cluster_uuid,systemTags)

    # 1 create project
    project_name = 'test_project7'
    project = iam2_ops.create_iam2_project(project_name)
    project_uuid = project.uuid
    cond = res_ops.gen_query_conditions("name",'=',"test_project7")
    linked_account_uuid = res_ops.query_resource(res_ops.ACCOUNT,cond)[0].uuid

    # 2 create project operator
    project_operator_name = 'username7'
    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
    
    zone_uuid = res_ops.get_resource(res_ops.ZONE)[0].uuid
    attributes = [{"name": "__ProjectRelatedZone__", "value": zone_uuid}]
    iam2_ops.add_attributes_to_iam2_project(project_uuid, attributes)
    # 3 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

    # 4 share vxlan pool to project
    l2vxlan_pools = res_ops.query_resource(res_ops.L2_VXLAN_NETWORK_POOL)
    for l2vxlan_pool in l2vxlan_pools:
        acc_ops.share_resources([linked_account_uuid],[l2vxlan_pool.uuid])
    # 5 create l2 vxlan 
    l2_vxlan_network_uuid = vxlan_ops.create_l2_vxlan_network('l2_vxlan',vxlan_pool_uuid,zone_uuid,session_uuid=project_login_uuid).uuid
    
    # 6 use l2 vxlan to create l3 vpc 
    l3_vpc_network = create_l3_vpc('test_vpc',l2_vxlan_network_uuid,project_login_uuid)
    
    l3_vpc_network_uuid = l3_vpc_network.uuid
    # add ip range
    ir_option = test_util.IpRangeOption()
    ir_option.set_name('iprange2')
    ir_option.set_description('iprange for vpc')
    ir_option.set_netmask('255.255.255.0')
    ir_option.set_gateway('192.168.23.1')
    ir_option.set_l3_uuid(l3_vpc_network_uuid)
    ir_option.set_startIp('192.168.23.2')
    ir_option.set_endIp('192.168.23.254')
    
    net_ops.add_ip_range(ir_option)
     
    # add network service
    AttachNetworkServiceToL3Network(l3_vpc_network_uuid,allservices,session_uuid = project_login_uuid)
    
    # share the vr_offering to project and do create vpc router and vpc network
    cond = res_ops.gen_query_conditions("name",'=',"virtual-router-vm")
    vr_offering_uuid = res_ops.query_resource(res_ops.VR_OFFERING,cond)[0].uuid
    acc_ops.share_resources([linked_account_uuid],[vr_offering_uuid])
    vpc_ops.create_vpc_vrouter(name = 'test_vpc_vr', virtualrouter_offering_uuid = vr_offering_uuid,session_uuid = project_login_uuid)
    vpc_vr = test_stub.query_vpc_vrouter('test_vpc_vr')
    vpc_vr.add_nic(l3_vpc_network_uuid)
    
    # 7 expunge the project and check the l2 vxlan
    iam2_ops.delete_iam2_project(project_uuid)
    iam2_ops.expunge_iam2_project(project_uuid)
    try:
        l2_vxlan_network_test_uuid = res_ops.query_resource(res_ops.L2_VXLAN_NETWORK)[0].uuid
    except: 
        
        test_util.test_pass(
            "l2 vxlan  is delete after deleted the project " )
    test_util.test_dsc('test l2 l2 cascade delete')
    
    # 8 check the vpc network and vpc_vr
    try:
        cond = res_ops.gen_query_conditions("name",'=',"test_vpc")
        l3_vpc_network_uuid = res_ops.query_resource(res_ops.L3_NETWORK,cond)[0].uuid
    except:
        
        test_util.test_pass(
            "l3_vpc  is delete after deleted the project")
    
   
    cond = res_ops.gen_query_conditions("name",'=',"test_vpc_vr")
    vpc_vr = res_ops.query_resource(res_ops.VIRTUALROUTER_VM,cond)
    
    if not vpc_vr.inv.state is 'Paused':
        test_util.test_fail(
            "vpc vr [%s] is still exist after delete and expunge the project [%s]" % (vpc_vr.uuid,project_uuid))
   
    # 9 delete 
    vni_range_uuid = res_ops.get_resource(res_ops.VNI_RANGE)[0].uuid
    vxlan_ops.delete_vni_range(vni_range_uuid)
    net_ops.delete_l2(vxlan_pool_uuid)
    iam2_ops.delete_iam2_virtual_id(project_operator_uuid)
def test():
    global l2_vxlan_network_uuid, project_uuid, project_operator_uuid, vni_range_uuid, vxlan_pool_uuid, l3_vr_network_uuid

    # create vxlan pool and vni range
    zone_uuid = res_ops.get_resource(res_ops.ZONE)[0].uuid
    cluster_uuid = res_ops.get_resource(res_ops.CLUSTER)[0].uuid
    vxlan_pool_name = 'vxlan_pool_name'

    vxlan_pool_uuid = vxlan_ops.create_l2_vxlan_network_pool(
        vxlan_pool_name, zone_uuid).uuid
    vxlan_ops.create_vni_range('vni_range', 20, 40, vxlan_pool_uuid)

    systemTags = [
        "l2NetworkUuid::%s::clusterUuid::%s::cidr::{172.20.0.1/16}" %
        (vxlan_pool_uuid, cluster_uuid)
    ]
    net_ops.attach_l2_vxlan_pool(vxlan_pool_uuid, cluster_uuid, systemTags)

    # 1 create project
    project_name = 'test_project7'
    project = iam2_ops.create_iam2_project(project_name)
    project_uuid = project.uuid
    cond = res_ops.gen_query_conditions("name", '=', "test_project7")
    linked_account_uuid = res_ops.query_resource(res_ops.ACCOUNT, cond)[0].uuid

    # 2 create project operator
    project_operator_name = 'username7'
    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

    zone_uuid = res_ops.get_resource(res_ops.ZONE)[0].uuid
    attributes = [{"name": "__ProjectRelatedZone__", "value": zone_uuid}]
    iam2_ops.add_attributes_to_iam2_project(project_uuid, attributes)
    # 3 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

    # 4 share vxlan pool to project
    l2vxlan_pools = res_ops.get_resource(res_ops.L2_VXLAN_NETWORK_POOL)
    for l2vxlan_pool in l2vxlan_pools:
        acc_ops.share_resources([linked_account_uuid], [l2vxlan_pool.uuid])
    # 5 create l2 vxlan
    l2_vxlan_network_uuid = vxlan_ops.create_l2_vxlan_network(
        'l2_vxlan',
        vxlan_pool_uuid,
        zone_uuid,
        session_uuid=project_login_uuid).uuid

    # 6 use the l2 vxlan to create l3
    # 6.1 create l3_vr_network
    l3_vr_network_uuid = net_ops.create_l3(
        'l3_vr_network',
        l2_vxlan_network_uuid,
        session_uuid=project_login_uuid).uuid

    cond = res_ops.gen_query_conditions('name', '=', 'virtual-router-vm')
    vr_instance_uuid = res_ops.query_resource(res_ops.INSTANCE_OFFERING,
                                              cond)[0].uuid
    tag = "guestL3Network::" + l3_vr_network_uuid
    tag_ops.create_system_tag("InstanceOfferingVO", vr_instance_uuid, tag)
    # add ip range
    ir_option = test_util.IpRangeOption()
    ir_option.set_name('iprange1')
    ir_option.set_description('iprange for vrouter network')
    ir_option.set_netmask('255.255.255.0')
    ir_option.set_gateway('192.168.22.1')
    ir_option.set_l3_uuid(l3_vr_network_uuid)
    ir_option.set_startIp('192.168.22.2')
    ir_option.set_endIp('192.168.22.254')

    net_ops.add_ip_range(ir_option)
    # add dns
    AddDnsToL3Network(l3_vr_network_uuid,
                      dns_text,
                      session_uuid=project_login_uuid)
    # add network service
    AttachNetworkServiceToL3Network(l3_vr_network_uuid,
                                    allservices,
                                    session_uuid=project_login_uuid)

    # create vrouter vm
    test_stub.share_admin_resource_include_vxlan_pool([linked_account_uuid])
    vr_vm = test_stub.create_vr_vm(test_obj_dict,
                                   l3_vr_network_uuid,
                                   session_uuid=project_login_uuid)
    test_stub.check_resource_not_exist(vr_vm.uuid, res_ops.VM_INSTANCE)

    #6 expunge the project and check the l2 vxlan
    iam2_ops.delete_iam2_project(project_uuid)
    iam2_ops.expunge_iam2_project(project_uuid)

    try:
        l2_vxlan_network_test_uuid = res_ops.query_resource(
            res_ops.L2_VXLAN_NETWORK)[0].uuid
    except:

        test_util.test_pass("l2 vxlan  is delete after delete the project ")
    test_util.test_dsc('test l2 l2 cascade delete')

    # 7 check the l3 vrouter network
    try:
        cond = res_ops.gen_query_conditions("name", '=', "l3_vr_network")
        l3_vrouter_network_uuid = res_ops.query_resource(
            res_ops.L3_NETWORK, cond)[0].uuid
    except:

        test_util.test_pass(
            "l3_vrouter network  is deleted after delete the project")

    # todo:check the virtual router vm

    # 8 delete
    vni_range_uuid = res_ops.get_resource(res_ops.VNI_RANGE)[0].uuid
    vxlan_ops.delete_vni_range(vni_range_uuid)
    net_ops.delete_l2(vxlan_pool_uuid)
    iam2_ops.delete_iam2_virtual_id(project_operator_uuid)
def test():
    global ir_option

    vm_creation_option = test_util.VmOption()
    l3_name = os.environ.get('l3VlanNetworkNameMultiRange')
    l3 = res_ops.get_resource(res_ops.L3_NETWORK, name = l3_name)[0]
    vm_creation_option.set_l3_uuids([l3.uuid])

    image_name = os.environ.get('imageName_net')
    image_uuid = test_lib.lib_get_image_by_name(image_name).uuid

    conditions = res_ops.gen_query_conditions('type', '=', 'UserVm')
    instance_offering_uuid = res_ops.query_resource(res_ops.INSTANCE_OFFERING, conditions)[0].uuid
    vm_creation_option.set_image_uuid(image_uuid)
    vm_creation_option.set_instance_offering_uuid(instance_offering_uuid)
    vm_creation_option.set_name('multizones_delete_ip_range')

    ir1 = res_ops.get_resource(res_ops.IP_RANGE, name = ir1_name)[0]

    ir_option.set_name(ir1_name)
    ir_option.set_description(ir1.description)
    ir_option.set_netmask(ir1.netmask)
    ir_option.set_gateway(ir1.gateway)
    ir_option.set_l3_uuid(ir1.l3NetworkUuid)
    ir_option.set_startIp(ir1.startIp)
    ir_option.set_endIp(ir1.endIp)

    ir_start_ip = ip_header.IpAddress(ir1.startIp)
    ir_end_ip = ip_header.IpAddress(ir1.endIp)

    #test_util.test_dsc('create vms')
    #vm1 = test_lib.lib_create_vm(vm_creation_option)
    #test_obj_dict.add_vm(vm1)
    #vm1_ip = ip_header.IpAddress(vm1.get_vm().vmNics[0].ip)
    #test_util.test_logger('ip address: %s' % vm1_ip)

    #vm2 = test_lib.lib_create_vm(vm_creation_option)
    #test_obj_dict.add_vm(vm2)
    #vm2_ip = ip_header.IpAddress(vm2.get_vm().vmNics[0].ip)
    #test_util.test_logger('ip address: %s' % vm2_ip)

    #vm3 = test_lib.lib_create_vm(vm_creation_option)
    #test_obj_dict.add_vm(vm3)
    #vm3_ip = ip_header.IpAddress(vm3.get_vm().vmNics[0].ip)
    #test_util.test_logger('ip address: %s' % vm3_ip)

    #vm4 = test_lib.lib_create_vm(vm_creation_option)
    #test_obj_dict.add_vm(vm4)
    #vm4_ip = ip_header.IpAddress(vm4.get_vm().vmNics[0].ip)
    #test_util.test_logger('ip address: %s' % vm4_ip)

    test_util.test_dsc('create vm and make sure vm ip range is in ir1.')
    ip_range_flag = False
    try_times = 0
    while not ip_range_flag and try_times < 10:
        vm1 = test_lib.lib_create_vm(vm_creation_option)
        test_obj_dict.add_vm(vm1)
        vm1_ip = ip_header.IpAddress(vm1.get_vm().vmNics[0].ip)
        test_util.test_logger('ip address: %s' % vm1_ip)
        if vm1_ip <= ir_end_ip and vm1_ip >= ir_start_ip:
            ip_range_flag = True
            break
        vm1.destroy()
        test_obj_dict.rm_vm(vm1)
        try_times += 1

    if not ip_range_flag:
        test_util.test_fail('Can not deply IP address on [l3:] %s in [IP Range:] %s for %s times VMs creation. It might means the VM IP address allocation method has bug.' % (l3.uuid, ir1.uuid, try_times))

    test_util.test_dsc('delete ip range')
    net_ops.delete_ip_range(ir1.uuid)

    #vm1_flag = False
    #vm2_flag = False

    #if vm1_ip <= ir_end_ip and vm1_ip >= ir_start_ip:
    #    test_obj_dict.mv_vm(vm1, vm_header.RUNNING, vm_header.STOPPED)
    #    vm1.set_state(vm_header.STOPPED)
    #    vm1.update()
    #    vm1_flag = True

    #vm1.check()

    test_util.test_dsc('start vm with other ip range.')
    vm1.start()

    vm1_new_ip = ip_header.IpAddress(vm1.get_vm().vmNics[0].ip)
    if vm1_ip == vm1_new_ip:
        test_util.test_fail('VM still get origianl [IP:] %s , after delete the IP Range.' % vm1_ip)

    if vm1_new_ip <= ir_end_ip and vm1_new_ip >= ir_start_ip:
        test_util.test_fail('VM still get unexpected [IP:] %s , after delete the IP Range: [%s] ~ [%s].' % (vm1_ip, ir_start_ip, ir_end_ip))

    #The new start vm's IP address need to refresh (restart network srevice)
    vm1.check()

    net_ops.add_ip_range(ir_option)

    vm1.destroy()
    test_util.test_pass('Delete IP Range Test Success')
def test():
    global l2_vxlan_network_uuid,project_uuid,project_operator_uuid,vni_range_uuid,vxlan_pool_uuid,l3_vpc_network_uuid

    # create vxlan pool and vni range
    zone_uuid = res_ops.get_resource(res_ops.ZONE)[0].uuid
    cluster_uuid = res_ops.get_resource(res_ops.CLUSTER)[0].uuid
    vxlan_pool_name = 'vxlan_pool_name'

    vxlan_pool_uuid = vxlan_ops.create_l2_vxlan_network_pool(vxlan_pool_name,zone_uuid).uuid
    vxlan_ops.create_vni_range('vni_range',20,40,vxlan_pool_uuid)

    systemTags = ["l2NetworkUuid::%s::clusterUuid::%s::cidr::{172.20.0.1/16}"%(vxlan_pool_uuid,cluster_uuid)]
    net_ops.attach_l2_vxlan_pool(vxlan_pool_uuid,cluster_uuid,systemTags)

    # 1 create project
    project_name = 'test_project7'
    project = iam2_ops.create_iam2_project(project_name)
    project_uuid = project.uuid
    #cond = res_ops.gen_query_conditions("name",'=',"test_project7")
    #linked_account_uuid = res_ops.query_resource(res_ops.ACCOUNT,cond)[0].uuid
    linked_account_uuid = project.linkedAccountUuid

    # 2 create project operator
    project_operator_name = 'username7'
    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
    
    zone_uuid = res_ops.get_resource(res_ops.ZONE)[0].uuid
    attributes = [{"name": "__ProjectRelatedZone__", "value": zone_uuid}]
    iam2_ops.add_attributes_to_iam2_project(project_uuid, attributes)
    # 3 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

    # 4 share vxlan pool to project
    l2vxlan_pools = res_ops.query_resource(res_ops.L2_VXLAN_NETWORK_POOL)
    for l2vxlan_pool in l2vxlan_pools:
        acc_ops.share_resources([linked_account_uuid],[l2vxlan_pool.uuid])
    # 5 create l2 vxlan 
    l2_vxlan_network_uuid = vxlan_ops.create_l2_vxlan_network('l2_vxlan',vxlan_pool_uuid,zone_uuid,session_uuid=project_login_uuid).uuid
    
    # 6 use l2 vxlan to create l3 vpc 
    l3_vpc_network = create_l3_vpc('test_vpc',l2_vxlan_network_uuid,project_login_uuid)
    
    l3_vpc_network_uuid = l3_vpc_network.uuid
    # add ip range
    ir_option = test_util.IpRangeOption()
    ir_option.set_name('iprange2')
    ir_option.set_description('iprange for vpc')
    ir_option.set_netmask('255.255.255.0')
    ir_option.set_gateway('192.168.23.1')
    ir_option.set_l3_uuid(l3_vpc_network_uuid)
    ir_option.set_startIp('192.168.23.2')
    ir_option.set_endIp('192.168.23.254')
    
    net_ops.add_ip_range(ir_option)
     
    # add network service
    AttachNetworkServiceToL3Network(l3_vpc_network_uuid,allservices,session_uuid = project_login_uuid)
    
    # share the vr_offering to project and do create vpc router and vpc network
    cond = res_ops.gen_query_conditions("name",'=',"virtual-router-vm")
    vr_offering_uuid = res_ops.query_resource(res_ops.VR_OFFERING,cond)[0].uuid
    acc_ops.share_resources([linked_account_uuid],[vr_offering_uuid])
    vpc_ops.create_vpc_vrouter(name = 'test_vpc_vr', virtualrouter_offering_uuid = vr_offering_uuid,session_uuid = project_login_uuid)
    vpc_vr = test_stub.query_vpc_vrouter('test_vpc_vr')
    vpc_vr.add_nic(l3_vpc_network_uuid)
    
    # 7 expunge the project and check the l2 vxlan
    iam2_ops.delete_iam2_project(project_uuid)
    iam2_ops.expunge_iam2_project(project_uuid)
    try:
        l2_vxlan_network_test_uuid = res_ops.query_resource(res_ops.L2_VXLAN_NETWORK)[0].uuid
    except: 
        
        test_util.test_pass(
            "l2 vxlan  is delete after deleted the project " )
    test_util.test_dsc('test l2 l2 cascade delete')
    
    # 8 check the vpc network and vpc_vr
    try:
        cond = res_ops.gen_query_conditions("name",'=',"test_vpc")
        l3_vpc_network_uuid = res_ops.query_resource(res_ops.L3_NETWORK,cond)[0].uuid
    except:
        
        test_util.test_pass(
            "l3_vpc  is delete after deleted the project")
    
   
    cond = res_ops.gen_query_conditions("name",'=',"test_vpc_vr")
    vpc_vr = res_ops.query_resource(res_ops.VIRTUALROUTER_VM,cond)
    
    if not vpc_vr.inv.state is 'Paused':
        test_util.test_fail(
            "vpc vr [%s] is still exist after delete and expunge the project [%s]" % (vpc_vr.uuid,project_uuid))
   
    # 9 delete 
    vni_range_uuid = res_ops.get_resource(res_ops.VNI_RANGE)[0].uuid
    vxlan_ops.delete_vni_range(vni_range_uuid)
    net_ops.delete_l2(vxlan_pool_uuid)
    iam2_ops.delete_iam2_virtual_id(project_operator_uuid)