Esempio n. 1
0
def test():
    test_util.test_dsc("Test Resource template Apis")

    resource_stack_option = test_util.ResourceStackOption()
    resource_stack_option.set_name("test")
    templateContent = '''
{
    "ZStackTemplateFormatVersion": "2018-06-18",
    "Description": "test",
    "Parameters": {
        "TestStringBasicEcho": {
             "Type": "String",
             "DefaultValue":"testonly"
        }
    },
    "Resources": {
        "InstanceOffering": {
            "Type": "ZStack::Resource::InstanceOffering",
            "Properties": {
                "name": "8cpu-8g",
                "cpuNum": 8,
                "memorySize": 8589934592
            }
        }
    },
    "Outputs": {
        "InstanceOffering": {
            "Value": {
                "Ref": "InstanceOffering"
            }
        }
    }
}
'''

    parameter = '''
{
    "TestStringBasicEcho": "Just a string Possiple"
}
'''
    resource_stack_option.set_templateContent(templateContent)
    resource_stack_option.set_parameters(parameter)

    preview_resource_stack = resource_stack_ops.preview_resource_stack(
        resource_stack_option)

    resource_stack = resource_stack_ops.create_resource_stack(
        resource_stack_option)

    cond = res_ops.gen_query_conditions('uuid', '=', resource_stack.uuid)
    resource_stack_queried = res_ops.query_resource(res_ops.RESOURCE_STACK,
                                                    cond)

    if len(resource_stack_queried) == 0:
        test_util.test_fail("Fail to query stack template")
    #Add a template via text.

    resource = resource_stack_ops.get_resource_from_resource_stack(
        resource_stack.uuid)
    print resource
    if resource == None:
        test_util.test_fail("Fail to get resource from resource_stack")

    print resource_stack.uuid
    cond = res_ops.gen_query_conditions('stackUuid', '=', resource_stack.uuid)
    event = res_ops.query_event_from_resource_stack(cond)
    print event
    if event == None:
        test_util.test_fail("Fail to get event from resource_stack")

    resource_stack_ops.delete_resource_stack(resource_stack.uuid)

    cond = res_ops.gen_query_conditions('uuid', '=', resource_stack.uuid)
    resource_stack_queried = res_ops.query_resource(res_ops.RESOURCE_STACK,
                                                    cond)
    if len(resource_stack_queried) != 0:
        test_util.test_fail("Fail to query stack template")

    test_util.test_pass('Create Stack Template Test Success')
def test():
    test_util.test_dsc("Test Resource template Apis")

    cond = res_ops.gen_query_conditions('status', '=', 'Connected')
    cond = res_ops.gen_query_conditions('state', '=', 'Enabled', cond)
    bs_queried = res_ops.query_resource(res_ops.BACKUP_STORAGE, cond)

    cond = res_ops.gen_query_conditions("category", '=', "Public")
    l3_queried = res_ops.query_resource(res_ops.L3_NETWORK, cond)
    if len(l3_queried) == 0:
        cond = res_ops.gen_query_conditions("category", '=', "Private")
        l3_queried = res_ops.query_resource(res_ops.L3_NETWORK, cond)

    resource_stack_option = test_util.ResourceStackOption()
    resource_stack_option.set_name("Create_VM")
    templateContent = '''
{
    "ZStackTemplateFormatVersion": "2018-06-18",
    "Description": "Just create a flat network & VM",
    "Parameters": {
        "L3NetworkUuid":{
            "Type": "String",
            "Label": "三层网络"
        },
        "BackupStorageUuid":{
            "Type": "CommaDelimitedList",
            "Label": "镜像服务器"
        }
    },
    "Resources": {
        "VmInstance": {
            "Type": "ZStack::Resource::VmInstance",
            "Properties": {
                "name": "VM-STACK",
                "instanceOfferingUuid": {"Fn::GetAtt":["InstanceOffering","uuid"]},
                "imageUuid":{"Fn::GetAtt":["Image","uuid"]},
                "l3NetworkUuids":[{"Ref":"L3NetworkUuid"}]
            }
        },
        "InstanceOffering": {
            "Type": "ZStack::Resource::InstanceOffering",
            "Properties": {
                "name":"1G-1CPU-STACK",
                "description":"测试创建计算规格",
                "cpuNum": 1,
                "memorySize": 1073741824
            }
        },
        "Image": {
            "Type": "ZStack::Resource::Image",
            "Properties": {
                "name": "IMAGE-STACK",
                "backupStorageUuids": {"Ref":"BackupStorageUuid"},
                "url":"file:///opt/zstack-dvd/zstack-image-1.4.qcow2",
                "format": "qcow2"
            }
        }
    },
    "Outputs": {
        "VmInstance": {
            "Value": {
                "Ref": "VmInstance"
            }
        }
    }
}
'''
    #1.create resource stack 5 times with the same name
    parameter = '{"L3NetworkUuid":"%s","BackupStorageUuid":"%s"}' % (
        l3_queried[0].uuid, bs_queried[0].uuid)
    for i in range(5):
        resource_stack_option.set_templateContent(templateContent)
        resource_stack_option.set_parameters(parameter)
        preview_resource_stack = resource_stack_ops.preview_resource_stack(
            resource_stack_option)
        resource_stack = resource_stack_ops.create_resource_stack(
            resource_stack_option)

    #2.query resource stack
    cond = res_ops.gen_query_conditions('name', '=', 'Create_VM')
    resource_stack_queried = res_ops.query_resource(res_ops.RESOURCE_STACK,
                                                    cond)

    cond = res_ops.gen_query_conditions('name', '=', 'VM-STACK')
    vm_queried = res_ops.query_resource(res_ops.VM_INSTANCE, cond)

    cond = res_ops.gen_query_conditions('name', '=', '1G-1CPU-STACK')
    instance_offering_queried = res_ops.query_resource(
        res_ops.INSTANCE_OFFERING, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'IMAGE-STACK')
    image_queried = res_ops.query_resource(res_ops.IMAGE, cond)

    test_util.test_logger(len(image_queried))

    if len(resource_stack_queried) != 5:
        test_util.test_fail("Fail to query 5 resource stacks")

    for i in range(5):
        if resource_stack_queried[i].status == 'Created':
            if len(vm_queried) != 5 or len(
                    instance_offering_queried) != 5 or len(image_queried) != 5:
                test_util.test_fail(
                    "Fail to create all resource when resource stack status is Created"
                )

    #5.delete resource stack
    for i in range(5):
        resource_stack_ops.delete_resource_stack(
            resource_stack_queried[i].uuid)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_VM')
    resource_stack_queried = res_ops.query_resource(res_ops.RESOURCE_STACK,
                                                    cond)

    cond = res_ops.gen_query_conditions('name', '=', 'VM-STACK')
    vm_queried = res_ops.query_resource(res_ops.VM_INSTANCE, cond)

    cond = res_ops.gen_query_conditions('name', '=', '1G-1CPU-STACK')
    instance_offering_queried = res_ops.query_resource(
        res_ops.INSTANCE_OFFERING, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'IMAGE-STACK')
    image_queried = res_ops.query_resource(res_ops.IMAGE, cond)

    if len(resource_stack_queried) != 0:
        test_util.test_fail(
            "Fail to delete all resource stack and there are still %s resource stacks not deleted."
        ) % len(resource_stack_queried)
    elif len(vm_queried) != 0 or len(instance_offering_queried) != 0 or len(
            image_queried) != 0:
        test_util.test_fail(
            "Fail to delete resource when resource stack is deleted and there are still %s vm, %s instance offering, %s image not deleted."
        ) % (len(vm_queried), len(instance_offering_queried),
             len(image_queried))

    test_util.test_pass(
        'Create Resource Stack With The Same Name Test Success')
def test():
    test_util.test_dsc("Test Resource template Apis")

    cond = res_ops.gen_query_conditions('status', '=', 'Ready')
    cond = res_ops.gen_query_conditions('state', '=', 'Enabled', cond)
    cond = res_ops.gen_query_conditions('system', '=', 'false', cond)
    image_queried = res_ops.query_resource(res_ops.IMAGE, cond)

    cond = res_ops.gen_query_conditions("category", '=', "Public")
    l3_pub_queried = res_ops.query_resource(res_ops.L3_NETWORK, cond)

    cond = res_ops.gen_query_conditions("category", '=', "Private")
    cond = res_ops.gen_query_conditions('networkServices.networkServiceType',
                                        '=', 'EIP')
    l3_pri_queried = res_ops.query_resource(res_ops.L3_NETWORK, cond)

    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('type', '=', 'UserVm', cond)
    instance_offering_queried = res_ops.query_resource(
        res_ops.INSTANCE_OFFERING, cond)

    resource_stack_option = test_util.ResourceStackOption()
    resource_stack_option.set_name("Create_STACK")
    resource_stack_option.set_rollback("true")
    templateContent = '''
{
	"ZStackTemplateFormatVersion": "2018-06-18",
	"Description": "Just create a VM with eip",
	"Parameters": {
		"InstanceOfferingUuid": {
			"Type": "String",
			"Label": "vm instance offering"
		},
		"ImageUuid":{
			"Type": "String"
		},
		"PrivateNetworkUuid":{
			"Type": "String"
		},
		"PublicNetworkUuid":{
			"Type": "String"
		}
	},
	"Resources": {
		"VmInstance": {
			"Type": "ZStack::Resource::VmInstance",
			"Properties": {
				"name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"},"VM"]]},
				"instanceOfferingUuid": {"Ref":"InstanceOfferingUuid"},
				"imageUuid":{"Ref":"ImageUuid"},
				"l3NetworkUuids":[{"Ref":"PrivateNetworkUuid"}]
			}
		},
		"VIP": {
			"Type": "ZStack::Resource::Vip",
			"Properties": {
				"name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"},"VIP"]]},
				"l3NetworkUuid":{"Ref":"PublicNetworkUuid"}
			}
		},
		"EIP":{
			"Type": "ZStack::Resource::Eip",
			"Properties": {
				"name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"},"EIP"]]},
				"vipUuid":{"Fn::GetAtt":["VIP","uuid"]},
				"vmNicUuid":{"Fn::GetAtt":[{"Fn::Select":[0,{"Fn::GetAtt":["VmInstance","vmNics"]}]},"uuid"]}
			}
		}
	},
	"Outputs": {
		"VmInstance": {
			"Value": {
				"Ref": "VmInstance"
			}
		}
	}
}
'''
    #1.create resource stack
    test_util.test_logger(
        '{"PrivateNetworkUuid":"%s","PublicNetworkUuid":"%s","ImageUuid":"%s","InstanceOfferingUuid":"%s"}'
        % (l3_pri_queried[0].uuid, l3_pub_queried[0].uuid,
           image_queried[0].uuid, instance_offering_queried[0].uuid))
    parameter = '{"PrivateNetworkUuid":"%s","PublicNetworkUuid":"%s","ImageUuid":"%s","InstanceOfferingUuid":"%s"}' % (
        l3_pri_queried[0].uuid, l3_pub_queried[0].uuid, image_queried[0].uuid,
        instance_offering_queried[0].uuid)

    resource_stack_option.set_templateContent(templateContent)
    resource_stack_option.set_parameters(parameter)
    preview_resource_stack = resource_stack_ops.preview_resource_stack(
        resource_stack_option)
    resource_stack = resource_stack_ops.create_resource_stack(
        resource_stack_option)

    #2.query resource stack
    cond = res_ops.gen_query_conditions('uuid', '=', resource_stack.uuid)
    resource_stack_queried = res_ops.query_resource(res_ops.RESOURCE_STACK,
                                                    cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-VM')
    vm_queried = res_ops.query_resource(res_ops.VM_INSTANCE, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-VIP')
    vip_queried = res_ops.query_resource(res_ops.VIP, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-EIP')
    eip_queried = res_ops.query_resource(res_ops.EIP, cond)

    if len(resource_stack_queried) == 0:
        test_util.test_fail("Fail to query resource stack")

    if resource_stack_queried[0].status == 'Created':
        if len(vm_queried) == 0 or len(vip_queried) == 0 or len(
                eip_queried) == 0:
            test_util.test_fail(
                "Fail to create all resource when resource stack status is Created"
            )
    elif len(vm_queried) != 0 or len(vip_queried) != 0 or len(
            eip_queried) != 0:
        test_util.test_fail(
            "Fail to delete all resource when resource stack status is Rollbacked or Deleted"
        )

    #3.get resource from resource stack
    resource = resource_stack_ops.get_resource_from_resource_stack(
        resource_stack.uuid)
    if resource == None or len(resource) != 3:
        test_util.test_fail("Fail to get resource from resource_stack")

    #4.query event from resource stack
    cond = res_ops.gen_query_conditions('stackUuid', '=', resource_stack.uuid)
    event = res_ops.query_event_from_resource_stack(cond)
    if event == None or len(event) != 6:
        test_util.test_fail("Fail to get event from resource_stack")

    #5.delete resource stack
    resource_stack_ops.delete_resource_stack(resource_stack.uuid)

    cond = res_ops.gen_query_conditions('uuid', '=', resource_stack.uuid)
    resource_stack_queried = res_ops.query_resource(res_ops.RESOURCE_STACK,
                                                    cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-VM')
    vm_queried = res_ops.query_resource(res_ops.VM_INSTANCE, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-VIP')
    vip_queried = res_ops.query_resource(res_ops.VIP, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-EIP')
    eip_queried = res_ops.query_resource(res_ops.EIP, cond)

    if len(resource_stack_queried) != 0:
        test_util.test_fail("Fail to delete resource stack")
    elif len(vm_queried) != 0 or len(vip_queried) != 0 or len(
            eip_queried) != 0:
        test_util.test_fail(
            "Fail to delete resource when resource stack is deleted")

    test_util.test_pass('Create Resource Stack Test Success')
def test():
    test_util.test_dsc("Test Resource template Apis")

    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('status', '=', 'Connected', cond)
    bs_queried = res_ops.query_resource(res_ops.BACKUP_STORAGE, cond)

    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    zone_queried = res_ops.query_resource(res_ops.ZONE, cond)

    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cluster_queried = res_ops.query_resource(res_ops.CLUSTER, cond)

    resource_stack_option = test_util.ResourceStackOption()
    resource_stack_option.set_name("Create_STACK")
    resource_stack_option.set_rollback("true")
    templateContent = '''
{
    "ZStackTemplateFormatVersion": "2018-06-18",
    "Description": "本示例会创建一个云路由云主机,需要用户提供下面正确的数据i\n公有网络Uuid\n私有网络Uuid: 如果只有公有网络,则把公有网络当作管理网即可.\n",
    "Parameters": {
        "VrouterImageUrl": {
            "Type": "String",
            "Description":"云路由镜像链接地址",
            "DefaultValue": "http://cdn.zstack.io/product_downloads/vrouter/2.3/zstack-vrouter-2.3.2.qcow2"
        },
        "VmImageUrl": {
            "Type": "String",
            "Description":"测试云主机镜像,请确定ZStack 可以下载下面链接的镜像",
            "DefaultValue": "http://cdn.zstack.io/zstack_repo/latest/zstack-image-1.4.qcow2"
        },
        "BackupStorage":{
            "Type": "CommaDelimitedList",
            "Description":"镜像服务器Uuid",
            "DefaultValue": "a61d132738494a11a7cb8fb95a7d00b0"
        },
        "ZoneUuid":{
            "Type": "String",
            "Description":"区域Uuid",
            "DefaultValue": "7c01dc3e9f594b4aac731e5af9cc6653"
        },
        "ClusterUuid":{
            "Type": "String",
            "Description":"集群Uuid",
            "DefaultValue": "9c57b47e547f47569a227e03985203c1"
        },
        "physicalInterface":{
            "Type": "String",
            "DefaultValue":"eth0"
        },
        "StartIp":{
            "Type": "String",
            "DefaultValue":"192.168.20.2"
        },
        "EndIp":{
            "Type": "String",
            "DefaultValue":"192.168.20.200"
        },
        "Netmask":{
            "Type": "String",
            "DefaultValue":"255.255.0.0"
        },
        "Gateway":{
            "Type": "String",
            "DefaultValue":"192.168.0.1"
        },
        "networkCidr":{
            "Type":"String",
            "DefaultValue":"172.20.0.1/8"
        }
    },
    "Resources": {
        "InstanceOffering":{
            "Type":"ZStack::Resource::InstanceOffering",
            "Properties":{
                "name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"}, "1cpu","256M"]]},
                "cpuNum": 1,
                "memorySize" : 268435456
            }
        },
        "L2NoVlanNetwork":{
            "Type":"ZStack::Resource::L2NoVlanNetwork",
            "Properties":{
                "name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"}, "L2NoVlanNetwork"]]},
                "zoneUuid":{"Ref":"ZoneUuid"},
                "physicalInterface":{"Ref":"physicalInterface"}
            }
        },
        "L2VlanNetwork":{
            "Type":"ZStack::Resource::L2VlanNetwork",
            "Properties":{
                "name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"}, "L2VlanNetwork"]]},
                "zoneUuid":{"Ref":"ZoneUuid"},
                "physicalInterface":{"Ref":"physicalInterface"},
                "vlan":1101
            }
        },
        "PublicNetwork":{
            "Type":"ZStack::Resource::L3Network",
            "Properties":{
                "name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"}, "Public-Network"]]},
                "l2NetworkUuid":{"Fn::GetAtt":["L2NoVlanNetwork","uuid"]},
                "category":"Public",
                "type":"L3BasicNetwork"
            }
        },
        "AddIpRange":{
            "Type":"ZStack::Action::AddIpRange",
            "Properties":{
                "name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"}, "iprange"]]},
                "l3NetworkUuid":{"Fn::GetAtt":["PublicNetwork","uuid"]},
                "startIp":{"Ref":"StartIp"},
                "endIp":{"Ref":"EndIp"},
                "netmask":{"Ref":"Netmask"},
                "gateway":{"Ref":"Gateway"}
            }
        },
        "VrouterImage": {
            "Type": "ZStack::Resource::Image",
            "Properties": {
                "name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"}, "Vrouter-Image"]]},
                "url": {"Ref":"VrouterImageUrl"},
                "system": true,
                "format": "qcow2",
                "backupStorageUuids":{"Ref":"BackupStorage"}
            }
        },
        "VMImage": {
            "Type": "ZStack::Resource::Image",
            "Properties": {
                "name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"}, "VmImage"]]},
                "url": {"Ref":"VmImageUrl"},
                "format": "qcow2",
                "backupStorageUuids":{"Ref":"BackupStorage"}
            }
        },
        "PrivateNetwork":{
            "Type":"ZStack::Resource::L3Network",
            "Properties":{
                "name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"}, "Private-Network"]]},
                "l2NetworkUuid":{"Fn::GetAtt":["L2VlanNetwork","uuid"]},
                "category":"Private",
                "type":"L3BasicNetwork",
                "systemTags":["networkservices::VRouter"]
            }
        },
        "AddIpRangeByNetworkCidr" :{
            "Type":"ZStack::Action::AddIpRangeByNetworkCidr",
            "Properties":{
                "name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"}, "iprange"]]},
                "l3NetworkUuid":{"Fn::GetAtt":["PrivateNetwork","uuid"]},
                "networkCidr":{"Ref":"networkCidr"}
            }
        },
        "VirtualRouterOffering":{
            "Type":"ZStack::Resource::VirtualRouterOffering",
            "Properties":{
                "name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"}, "Vrouter-Offering"]]},
                "zoneUuid":{"Ref":"ZoneUuid"},
                "managementNetworkUuid":{"Fn::GetAtt":["PublicNetwork","uuid"]},
                "publicNetworkUuid":{"Fn::GetAtt":["PublicNetwork","uuid"]},
                "imageUuid":{"Fn::GetAtt":["VrouterImage", "uuid"]},
                "cpuNum":2,
                "memorySize":2147483648
            }
        },
        "SystemTag":{
            "Type":"ZStack::Resource::SystemTag",
            "Properties":{
                "resourceUuid":{"Fn::GetAtt":["VirtualRouterOffering","uuid"]},
                "resourceType":"InstanceOfferingVO",
                "tag":{"Fn::Join": ["::", ["guestL3Network", {"Fn::GetAtt":["PrivateNetwork","uuid"]}]]}
            }
        },
        "AttachNoVlanNetworkToCluster":{
            "Type":"ZStack::Action::AttachL2NetworkToCluster",
            "Properties":{
                "l2NetworkUuid":{"Fn::GetAtt":["L2NoVlanNetwork","uuid"]},
                "clusterUuid":{"Ref":"ClusterUuid"},
                "systemTags":[{"Fn::Join":["::",["l2NoVlanNetworkUuid",{"Fn::GetAtt":["L2NoVlanNetwork","uuid"]},"clusterUuid",{"Ref":"ClusterUuid"}]]}]
            }
        },
        "AttachVlanNetworkToCluster":{
            "Type":"ZStack::Action::AttachL2NetworkToCluster",
            "Properties":{
                "l2NetworkUuid":{"Fn::GetAtt":["L2VlanNetwork","uuid"]},
                "clusterUuid":{"Ref":"ClusterUuid"},
                "systemTags":[{"Fn::Join":["::",["l2VlanNetworkUuid",{"Fn::GetAtt":["L2VlanNetwork","uuid"]},"clusterUuid",{"Ref":"ClusterUuid"}]]}]
            }
        },
        "TestVm":{
            "Type":"ZStack::Resource::VmInstance",
            "Properties":{
                "name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"}, "TestVm"]]},
                "instanceOfferingUuid": {"Fn::GetAtt":["InstanceOffering","uuid"]},
                "l3NetworkUuids": [{"Fn::GetAtt":["PrivateNetwork","uuid"]}],
                "imageUuid": {"Fn::GetAtt":["VMImage", "uuid"]}
            }
        }
    },
    "Outputs": {
        "vyos": {
            "Value": {
                "Ref": "TestVm"
            }
        }
    }
}
'''
    #1.create resource stack
    parameter = '{"BackupStorage":"%s","ZoneUuid":"%s","ClusterUuid":"%s"}' % (
        bs_queried[0].uuid, zone_queried[0].uuid, cluster_queried[0].uuid)
    resource_stack_option.set_templateContent(templateContent)
    resource_stack_option.set_parameters(parameter)
    preview_resource_stack = resource_stack_ops.preview_resource_stack(
        resource_stack_option)
    resource_stack = resource_stack_ops.create_resource_stack(
        resource_stack_option)

    #2.query resource stack
    cond = res_ops.gen_query_conditions('uuid', '=', resource_stack.uuid)
    resource_stack_queried = res_ops.query_resource(res_ops.RESOURCE_STACK,
                                                    cond)

    cond = res_ops.gen_query_conditions('name', '=',
                                        'Create_STACK-Vrouter-Image')
    vr_image_queried = res_ops.query_resource(res_ops.IMAGE, cond)

    cond = res_ops.gen_query_conditions('name', '=',
                                        'Create_STACK-Vrouter-Offering')
    vr_offering_queried = res_ops.query_resource(res_ops.INSTANCE_OFFERING,
                                                 cond)

    cond = res_ops.gen_query_conditions('applianceVmType', '=', 'vrouter')
    vrouter_queried = res_ops.query_resource(res_ops.VIRTUALROUTER_VM, cond)

    cond = res_ops.gen_query_conditions('name', '=',
                                        'Create_STACK-L2VlanNetwork')
    l2_vlan_queried = res_ops.query_resource(res_ops.L2_VLAN_NETWORK, cond)

    cond = res_ops.gen_query_conditions('name', '=',
                                        'Create_STACK-L2NoVlanNetwork')
    l2_queried = res_ops.query_resource(res_ops.L2_NETWORK, cond)

    if len(resource_stack_queried) == 0:
        test_util.test_fail("Fail to query resource stack")

    if resource_stack_queried[0].status == 'Created':
        if len(vr_image_queried) == 0 or len(vr_offering_queried) == 0 or len(
                vrouter_queried) == 0 or len(l2_queried) == 0 or len(
                    l2_vlan_queried) == 0:
            test_util.test_fail(
                "Fail to create all resource when resource stack status is Created"
            )

    #3.get resource from resource stack
    resource = resource_stack_ops.get_resource_from_resource_stack(
        resource_stack.uuid)
    if resource == None or len(resource) != 9:
        test_util.test_fail("Fail to get resource from resource_stack")

    #4.query event from resource stack
    cond = res_ops.gen_query_conditions('stackUuid', '=', resource_stack.uuid)
    event = res_ops.query_event_from_resource_stack(cond)
    if event == None or len(event) != 26:
        test_util.test_fail("Fail to get event from resource_stack")

    #5.delete resource stack
    resource_stack_ops.delete_resource_stack(resource_stack.uuid)

    cond = res_ops.gen_query_conditions('uuid', '=', resource_stack.uuid)
    resource_stack_queried = res_ops.query_resource(res_ops.RESOURCE_STACK,
                                                    cond)

    cond = res_ops.gen_query_conditions('name', '=',
                                        'Create_STACK-Vrouter-Image')
    vr_image_queried = res_ops.query_resource(res_ops.IMAGE, cond)

    cond = res_ops.gen_query_conditions('name', '=',
                                        'Create_STACK-Vrouter-Offering')
    vr_offering_queried = res_ops.query_resource(res_ops.INSTANCE_OFFERING,
                                                 cond)

    cond = res_ops.gen_query_conditions('applianceVmType', '=', 'vrouter')
    vrouter_queried = res_ops.query_resource(res_ops.VIRTUALROUTER_VM, cond)

    cond = res_ops.gen_query_conditions('name', '=',
                                        'Create_STACK-L2NoVlanNetwork')
    l2_queried = res_ops.query_resource(res_ops.L2_NETWORK, cond)

    cond = res_ops.gen_query_conditions('name', '=',
                                        'Create_STACK-L2VlanNetwork')
    l2_vlan_queried = res_ops.query_resource(res_ops.L2_VLAN_NETWORK, cond)

    if len(resource_stack_queried) != 0:
        test_util.test_fail("Fail to delete resource stack")
    elif len(vr_image_queried) != 0 or len(vr_offering_queried) != 0 or len(
            vrouter_queried) != 0 or len(l2_queried) != 0 or len(
                l2_vlan_queried) != 0:
        test_util.test_fail(
            "Fail to delete resource when resource stack is deleted")

    test_util.test_pass('Create Resource Stack Test Success')
def test():
    test_util.test_dsc("Test Resource template Apis")

    cond = res_ops.gen_query_conditions('status', '=', 'Ready')
    cond = res_ops.gen_query_conditions('state', '=', 'Enabled', cond)
    cond = res_ops.gen_query_conditions('system', '=', 'false', cond)
    image_queried = res_ops.query_resource(res_ops.IMAGE, cond)

    cond = res_ops.gen_query_conditions("category", '=', "Public")
    l3_queried = res_ops.query_resource(res_ops.L3_NETWORK, cond)
    if len(l3_queried) == 0:
        cond = res_ops.gen_query_conditions("category", '=', "Private")
        l3_queried = res_ops.query_resource(res_ops.L3_NETWORK, cond)

    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('type', '=', 'UserVm', cond)
    instance_offering_queried = res_ops.query_resource(
        res_ops.INSTANCE_OFFERING, cond)

    stack_template_option = test_util.StackTemplateOption()
    stack_template_option.set_name("template")
    resource_stack_option = test_util.ResourceStackOption()
    resource_stack_option.set_name("Stack_test")
    templateContent = '''
{
    "ZStackTemplateFormatVersion": "2018-06-18",
    "Description": "Just create a flat network & VM",
    "Parameters": {
        "L3NetworkUuid":{
            "Type": "String",
            "Label": "三层网络"
        },
        "ImageUuid":{
            "Type": "String",
            "Label": "镜像"
        },
        "InstanceOfferingUuid":{
            "Type": "String",
            "Label": "计算规格"
        }
    },
    "Resources": {
        "VmInstance": {
            "Type": "ZStack::Resource::VmInstance",
            "Properties": {
                "name": "VM-STACK",
                "instanceOfferingUuid": {"Ref":"InstanceOfferingUuid"},
                "imageUuid":{"Ref":"ImageUuid"},
                "l3NetworkUuids":[{"Ref":"L3NetworkUuid"}]
            }
        }
    },
    "Outputs": {
        "VmInstance": {
            "Value": {
                "Ref": "VmInstance"
            }
        }
    }
}
'''
    #1.Add template which state is false
    stack_template_option.set_templateContent(templateContent)
    stack_template = stack_template_ops.add_stack_template(
        stack_template_option)
    cond = res_ops.gen_query_conditions('uuid', '=', stack_template.uuid)
    stack_template_queried = res_ops.query_resource(res_ops.STACK_TEMPLATE,
                                                    cond)
    if len(stack_template_queried) == 0:
        test_util.test_fail("Fail to query stack template")

    stack_template_option.set_state('false')
    new_stack_template = stack_template_ops.update_stack_template(
        stack_template.uuid, stack_template_option)

    #2.Create a resource stack based on this template
    parameter = '{"L3NetworkUuid":"%s","ImageUuid":"%s","InstanceOfferingUuid":"%s"}' % (
        l3_queried[0].uuid, image_queried[0].uuid,
        instance_offering_queried[0].uuid)
    resource_stack_option.set_template_uuid(new_stack_template.uuid)
    resource_stack_option.set_parameters(parameter)
    #preview_resource_stack = resource_stack_ops.preview_resource_stack(resource_stack_option)

    try:
        resource_stack = resource_stack_ops.create_resource_stack(
            resource_stack_option)
        test_util.test_fail(
            'Cannot create stack when stack template is disabled.')
    except:
        pass

    #3.Qeury this stack
    cond = res_ops.gen_query_conditions('name', '=', 'Stack_test')
    resource_stack_queried = res_ops.query_resource(res_ops.RESOURCE_STACK,
                                                    cond)
    if len(resource_stack_queried) != 0:
        test_util.test_fail('Resource stack should not be existed.')

    stack_template_option.set_state('true')
    lastest_stack_template = stack_template_ops.update_stack_template(
        stack_template.uuid, stack_template_option)
    test_util.test_logger(lastest_stack_template.uuid)

    #4.Set template state true
    resource_stack_option.set_template_uuid(lastest_stack_template.uuid)
    resource_stack_option.set_parameters(parameter)
    resource_stack_option.set_name("Stack_test")

    resource_stack = resource_stack_ops.create_resource_stack(
        resource_stack_option)
    cond = res_ops.gen_query_conditions('name', '=', 'Stack_test')
    resource_stack_queried = res_ops.query_resource(res_ops.RESOURCE_STACK,
                                                    cond)
    if len(resource_stack_queried) == 0:
        test_util.test_fail('Failed to create resource stack.')

    cond = res_ops.gen_query_conditions('name', '=', 'VM-STACK')
    vm_queried = res_ops.query_resource(res_ops.VM_INSTANCE, cond)

    if resource_stack_queried[0].status == 'Created':
        if len(vm_queried) == 0:
            test_util.test_fail(
                "Fail to create all resource when resource stack status is Created"
            )

    #5.delete resource stack
    resource_stack_ops.delete_resource_stack(resource_stack.uuid)

    cond = res_ops.gen_query_conditions('uuid', '=', resource_stack.uuid)
    resource_stack_queried = res_ops.query_resource(res_ops.RESOURCE_STACK,
                                                    cond)

    cond = res_ops.gen_query_conditions('name', '=', 'VM-STACK')
    vm_queried = res_ops.query_resource(res_ops.VM_INSTANCE, cond)

    if len(resource_stack_queried) != 0:
        test_util.test_fail("Fail to delete resource stack")
    elif len(vm_queried) != 0:
        test_util.test_fail(
            "Fail to delete resource when resource stack is deleted")
    #6.delete stack template
    stack_template_ops.delete_stack_template(lastest_stack_template.uuid)
    stack_template_queried = res_ops.query_resource(res_ops.STACK_TEMPLATE,
                                                    cond)
    if len(stack_template_queried) != 0:
        test_util.test_fail("Fail to query stack template")

    test_util.test_pass('Create Resource Stack Test Success')
Esempio n. 6
0
def test():
    test_util.test_dsc("Test Resource template Apis")
    
    cond = res_ops.gen_query_conditions('status', '=', 'Ready')
    cond = res_ops.gen_query_conditions('state', '=', 'Enabled', cond)
    cond = res_ops.gen_query_conditions('system', '=', 'false', cond)
    image_queried = res_ops.query_resource(res_ops.IMAGE, cond)

    cond = res_ops.gen_query_conditions("category", '=', "Private")
    l3_pri_queried = res_ops.query_resource(res_ops.L3_NETWORK, cond)

    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('type', '=', 'UserVm', cond)
    instance_offering_queried = res_ops.query_resource(res_ops.INSTANCE_OFFERING, cond)  

    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('status', '=', 'Connected', cond)
    bs_queried = res_ops.query_resource(res_ops.BACKUP_STORAGE, cond)

    resource_stack_option = test_util.ResourceStackOption()
    resource_stack_option.set_name("Create_STACK")
    resource_stack_option.set_rollback("true")
    templateContent = '''
{
    "ZStackTemplateFormatVersion": "2018-06-18",
    "Description": "本示例将创建一个云盘,并将云盘加载到云主机上(基于本地存储), 创建示例前提环境:\n计算规格,镜像,云盘规格,私有网络,可用物理机",
    "Parameters": {
        "InstanceOfferingUuid": {
            "Type": "String",
            "Label": "计算规格",
            "Description": "The instance offering uuid"
        },
        "ImageUuid":{
            "Type": "String",
            "Label": "镜像",
            "Description": "The Image uuid for creating VmInstance, Please choose an image not iso"
        },
        "PrivateNetworkUuid":{
            "Type": "String",
            "Label": "私有网络",
            "Description" : "The private network uuid for creating VmInstance"
        },
        "BackupStorage":{
            "Type": "CommaDelimitedList",
            "Label": "镜像服务器",
            "Description":"镜像服务器Uuid"
        }
    },
    "Resources": {
        "VmInstance": {
            "Type": "ZStack::Resource::VmInstance",
            "Properties": {
                "name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"},"VM"]]},
                "instanceOfferingUuid": {"Ref":"InstanceOfferingUuid"},
                "imageUuid":{"Ref":"ImageUuid"},
                "l3NetworkUuids":[{"Ref":"PrivateNetworkUuid"}]
            }
        },
        "DataVolume": {
            "Type": "ZStack::Resource::DataVolume",
            "Properties": {
                "name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"},"Data-Volume"]]},
                "diskOfferingUuid":{"Fn::GetAtt":["DiskOffering","uuid"]}
            }
        },
        "RootVolumeTemplate": {
            "Type": "ZStack::Resource::RootVolumeTemplate",
            "Properties": {
                "name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"},"Root-Volume-Template"]]},
                "backupStorageUuids": {"Ref":"BackupStorage"},
                "rootVolumeUuid": {"Fn::GetAtt":["VmInstance","rootVolumeUuid"]}
            }
        },
        "DiskOffering": {
            "Type":"ZStack::Resource::DiskOffering",
            "Properties": {
                "name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"},"Volume-Offering-1G"]]},
                "diskSize": 1073741824
            }
        },
        "AffinityGroup": {
            "Type": "ZStack::Resource::AffinityGroup",
            "Properties": {
                "name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"},"AffinityGroup"]]},
                "policy": "antiSoft"
            }
        },
        "AddVmToAffinityGroup": {
            "Type": "ZStack::Action::AddVmToAffinityGroup",
            "Properties": {
                "uuid":{"Fn::GetAtt":["VmInstance","uuid"]},
                "affinityGroupUuid":{"Fn::GetAtt":["AffinityGroup","uuid"]}
            }
        },
        "AttachDataVolumeToVm": {
            "Type": "ZStack::Action::AttachDataVolumeToVm",
            "Properties": {
                "vmInstanceUuid": {"Fn::GetAtt":["VmInstance","uuid"]},
                "volumeUuid": {"Fn::GetAtt":["DataVolume","uuid"]}
            }
        },
        "DataVolumeTemplate": {
            "Type": "ZStack::Resource::DataVolumeTemplate",
            "Properties": {
                "name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"},"Data-Volume-Template"]]},
                "backupStorageUuids": {"Ref":"BackupStorage"},
                "volumeUuid": {"Fn::GetAtt":["DataVolume","uuid"]}
            }
        }
    },
    "Outputs": {
        "VmInstance": {
            "Value": {
                "Ref": "VmInstance"
            }
        }
    }
}

'''
    #1.create resource stack 
    #test_util.test_logger('{"PrivateNetworkUuid":"%s","ImageUuid":"%s","InstanceOfferingUuid":"%s"}' % (l3_pri_queried[0].uuid, image_queried[0].uuid, instance_offering_queried[0].uuid))
    parameter = '{"PrivateNetworkUuid":"%s","ImageUuid":"%s","InstanceOfferingUuid":"%s","BackupStorage":"%s"}' % (l3_pri_queried[0].uuid, image_queried[0].uuid, instance_offering_queried[0].uuid, bs_queried[0].uuid)
    
    resource_stack_option.set_templateContent(templateContent)
    resource_stack_option.set_parameters(parameter)
    preview_resource_stack = resource_stack_ops.preview_resource_stack(resource_stack_option)
    resource_stack = resource_stack_ops.create_resource_stack(resource_stack_option)
    

    #2.query resource stack
    cond = res_ops.gen_query_conditions('uuid', '=', resource_stack.uuid)
    resource_stack_queried = res_ops.query_resource(res_ops.RESOURCE_STACK, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-VM')
    vm_queried = res_ops.query_resource(res_ops.VM_INSTANCE, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-Data-Volume')
    data_volume_queried = res_ops.query_resource(res_ops.VOLUME, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-Volume-Offering-1G')
    volume_offering_queried = res_ops.query_resource(res_ops.DISK_OFFERING, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-Data-Volume-Template')
    dv_template_queried = res_ops.query_resource(res_ops.IMAGE, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-Root-Volume-Template')
    rv_template_queried = res_ops.query_resource(res_ops.IMAGE, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-AffinityGroup')
    affinity_group_queried = res_ops.query_resource(res_ops.AFFINITY_GROUP, cond)

    if len(resource_stack_queried) == 0:
        test_util.test_fail("Fail to query resource stack")

    if resource_stack_queried[0].status == 'Created':
        if len(vm_queried) == 0 or len(data_volume_queried) == 0 or len(volume_offering_queried) == 0 or len(dv_template_queried) == 0 or len(rv_template_queried) == 0 or len(affinity_group_queried) == 0:
            test_util.test_fail("Fail to create all resource when resource stack status is Created")  
    
    #3.get resource from resource stack
    resource = resource_stack_ops.get_resource_from_resource_stack(resource_stack.uuid)
    if resource == None or len(resource) != 6:
        test_util.test_fail("Fail to get resource from resource_stack")
    
    #4.query event from resource stack
    cond = res_ops.gen_query_conditions('stackUuid', '=', resource_stack.uuid)
    event = res_ops.query_event_from_resource_stack(cond)
    if event == None or len(event) != 16:
        test_util.test_fail("Fail to get event from resource_stack")

    #5.delete resource stack
    resource_stack_ops.delete_resource_stack(resource_stack.uuid)

    cond = res_ops.gen_query_conditions('uuid', '=', resource_stack.uuid)
    resource_stack_queried = res_ops.query_resource(res_ops.RESOURCE_STACK, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-VM')
    vm_queried = res_ops.query_resource(res_ops.VM_INSTANCE, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-Data-Volume')
    data_volume_queried = res_ops.query_resource(res_ops.VOLUME, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-Volume-Offering-1G')
    volume_offering_queried = res_ops.query_resource(res_ops.DISK_OFFERING, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-Data-Volume-Template')
    dv_template_queried = res_ops.query_resource(res_ops.IMAGE, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-Root-Volume-Template')
    rv_template_queried = res_ops.query_resource(res_ops.IMAGE, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-AffinityGroup')
    affinity_group_queried = res_ops.query_resource(res_ops.AFFINITY_GROUP, cond)

    if len(resource_stack_queried) != 0 :
        test_util.test_fail("Fail to delete resource stack") 
    elif len(vm_queried) != 0 or len(data_volume_queried) != 0 or len(volume_offering_queried) != 0 or len(dv_template_queried) != 0 or len(rv_template_queried) != 0 or len(affinity_group_queried) != 0:
        test_util.test_fail("Fail to delete resource when resource stack is deleted")   
   

    test_util.test_pass('Create Resource Stack Test Success')
def test():
    test_util.test_dsc("Test Resource template Apis")

    cond = res_ops.gen_query_conditions("category", '=', "Public")
    l3_queried = res_ops.query_resource(res_ops.L3_NETWORK, cond)
    if len(l3_queried) == 0:
        cond = res_ops.gen_query_conditions("category", '=', "Private")
        l3_queried = res_ops.query_resource(res_ops.L3_NETWORK, cond)

    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('type', '=', 'UserVm', cond)
    instance_offering_queried = res_ops.query_resource(res_ops.INSTANCE_OFFERING, cond)

    cond = res_ops.gen_query_conditions('status', '=', 'Ready')
    cond = res_ops.gen_query_conditions('state', '=', 'Enabled', cond)
    cond = res_ops.gen_query_conditions('system', '=', 'false', cond)
    image_queried = res_ops.query_resource(res_ops.IMAGE, cond)

    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('status', '=', 'Connected', cond)
    ps_queried = res_ops.query_resource(res_ops.PRIMARY_STORAGE, cond)

    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('status', '=', 'Connected', cond)
    host_queried = res_ops.query_resource(res_ops.HOST, cond)

    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('status', '=', 'Connected', cond)
    bs_queried = res_ops.query_resource(res_ops.BACKUP_STORAGE, cond)

    volume_image_url = os.environ.get('imageUrl_s')
    resource_stack_option = test_util.ResourceStackOption()
    resource_stack_option.set_name("Create_STACK")
    resource_stack_option.set_rollback("true")
    templateContent = '''
{
    "ZStackTemplateFormatVersion": "2018-06-18",
    "Description": "",
    "Parameters": {
        "InstanceOfferingUuid":{
            "Type": "String",
            "Label": "计算规格"
        },
        "ImageUuid":{
            "Type": "String",
            "Label": "云主机镜像"        
        },
        "PrivateNetworkUuid": {
            "Type": "String",
            "Label": "私有网络"
        },
        "PrimaryStorageUuid": {
            "Type": "String",
            "Label": "主存储"
        },
        "HostUuid": {
            "Type": "String",
            "Label": "物理机"
        },
        "BackupStorageUuid": {
            "Type": "CommaDelimitedList",
            "Label": "镜像服务器"
        },
        "VolumeImageUrl": {
            "Type": "String",
            "Label": "云盘镜像链接地址",
			"Description":"测试盘镜像,请确定ZStack 可以下载下面链接的镜像"
        }
    },
    "Resources": {
        "VM": {
            "Type": "ZStack::Resource::VmInstance",
            "Properties": {
                "name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"},"VM"]]},
                "description": "",
                "instanceOfferingUuid": {"Ref":"InstanceOfferingUuid"},
                "imageUuid": {"Ref":"ImageUuid"},
                "l3NetworkUuids": [
                    {"Ref":"PrivateNetworkUuid"}
                ],
                "defaultL3NetworkUuid": {"Ref":"PrivateNetworkUuid"},
                "dataDiskOfferingUuids": [],
                "systemTags": [
                    "usbRedirect::false",
                    "vmConsoleMode::vnc"
                ]
            }
        },
        "VolumeImage": {
            "Type": "ZStack::Resource::Image",
            "Properties": {
                "name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"},"IMAGE"]]},
                "url": {"Ref":"VolumeImageUrl"},
		"format": "qcow2",
		"mediaType": "DataVolumeTemplate",
		"backupStorageUuids":{"Ref":"BackupStorageUuid"}
            }
        },
        "VOLUME": {
            "Type": "ZStack::Resource::DataVolumeFromVolumeTemplate",
            "Properties": {
                "name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"},"VOLUME"]]},
                "systemTags": [
                    "capability::virtio-scsi"
                ],
                "imageUuid": {"Fn::GetAtt":["VolumeImage","uuid"]},
                "primaryStorageUuid": {"Ref":"PrimaryStorageUuid"},
                "hostUuid": {"Ref":"HostUuid"}
            }
        },
        "AttachDataVolumeToVm": {
            "Type": "ZStack::Action::AttachDataVolumeToVm",
            "Properties": {
                "volumeUuid": {
                    "Fn::GetAtt": [
                        "VOLUME",
                        "uuid"
                    ]
                },
                "vmInstanceUuid": {
                    "Fn::GetAtt": [
                        "VM",
                        "uuid"
                    ]
                }
            }
        }
    }
}
'''
    #1.create resource stack
    parameter = '{"InstanceOfferingUuid":"%s","PrivateNetworkUuid":"%s","PrimaryStorageUuid":"%s","BackupStorageUuid":"%s","HostUuid":"%s","VolumeImageUrl":"%s","ImageUuid":"%s"}' % (instance_offering_queried[0].uuid, l3_queried[0].uuid, ps_queried[0].uuid, bs_queried[0].uuid, host_queried[0].uuid, volume_image_url, image_queried[0].uuid)
    resource_stack_option.set_templateContent(templateContent)
    resource_stack_option.set_parameters(parameter)
    preview_resource_stack = resource_stack_ops.preview_resource_stack(resource_stack_option)
    resource_stack = resource_stack_ops.create_resource_stack(resource_stack_option)

    #2.query resource stack
    cond = res_ops.gen_query_conditions('uuid', '=', resource_stack.uuid)
    resource_stack_queried = res_ops.query_resource(res_ops.RESOURCE_STACK, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-VM')
    vm_queried = res_ops.query_resource(res_ops.VM_INSTANCE, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-IMAGE')
    image_queried = res_ops.query_resource(res_ops.IMAGE, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-VOLUME')
    volume_queried = res_ops.query_resource(res_ops.VOLUME, cond)

    if len(resource_stack_queried) == 0:
        test_util.test_fail("Fail to query resource stack")
    if resource_stack_queried[0].status == 'Created':
        if len(vm_queried) == 0 or len(image_queried) == 0 or len(volume_queried) == 0:
            test_util.test_fail("Fail to create all resource when resource stack status is Created")

    #3.get resource from resource stack
    resource = resource_stack_ops.get_resource_from_resource_stack(resource_stack.uuid)
    if resource == None or len(resource) != 3:
        test_util.test_fail("Fail to get resource from resource_stack")

    #4.query event from resource stack
    cond = res_ops.gen_query_conditions('stackUuid', '=', resource_stack.uuid)
    event = res_ops.query_event_from_resource_stack(cond)
    if event == None or len(event) != 8:
        test_util.test_fail("Fail to get event from resource_stack")

    #5.delete resource stack
    resource_stack_ops.delete_resource_stack(resource_stack.uuid)

    cond = res_ops.gen_query_conditions('uuid', '=', resource_stack.uuid)
    resource_stack_queried = res_ops.query_resource(res_ops.RESOURCE_STACK, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-VM')
    vm_queried = res_ops.query_resource(res_ops.VM_INSTANCE, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-IMAGE')
    image_queried = res_ops.query_resource(res_ops.IMAGE, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-VOLUME')
    cond = res_ops.gen_query_conditions('status', '=', 'Ready',cond)
    volume_queried = res_ops.query_resource(res_ops.VOLUME, cond)

    if len(resource_stack_queried) != 0 :
        test_util.test_fail("Fail to delete resource stack") 
    if len(vm_queried) != 0 or len(image_queried) != 0 or len(volume_queried) != 0:
        test_util.test_fail("Fail to delete resource when resource stack is deleted")   
   

    test_util.test_pass('Create Resource Stack Test Success')
Esempio n. 8
0
def test():
    test_util.test_dsc("Test Resource template Apis")

    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    zone_queried = res_ops.query_resource(res_ops.ZONE, cond)

    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cluster_queried = res_ops.query_resource(res_ops.CLUSTER, cond)

    resource_stack_option = test_util.ResourceStackOption()
    resource_stack_option.set_name("Create_STACK")
    resource_stack_option.set_rollback("true")
    templateContent = '''
{
    "ZStackTemplateFormatVersion": "2018-06-18",
    "Description": "",
    "Parameters": {
        "ZoneUuid": {
            "Label":"区域",
            "Type": "String",
            "DefaultValue":""
        },
        "ClusterUuid": {
            "Label": "集群",
            "Type": "String",
            "DefaultValue": ""
        }
    },    
    "Resources": {
        "L2_NETWORK": {
            "Type": "ZStack::Resource::L2VlanNetwork",
            "Properties": {
                "name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"}, "L2Network"]]},
                "type": "vlan",
                "zoneUuid": {"Ref":"ZoneUuid"},
                "physicalInterface": "eth0",
                "vlan": 66
            }
        },
        "AttachL2NetworkToCluster-L2_NETWORK": {
            "Type": "ZStack::Action::AttachL2NetworkToCluster",
            "Properties": {
                "l2NetworkUuid": {
                    "Fn::GetAtt": [
                        "L2_NETWORK",
                        "uuid"
                    ]
                },
                "clusterUuid": {"Ref":"ClusterUuid"}
            }
        },
        "PUBLIC_NETWORK": {
            "Type": "ZStack::Resource::L3Network",
            "Properties": {
                "name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"}, "PublicNetwork"]]},
                "type": "L3BasicNetwork",
                "l2NetworkUuid": {
                    "Fn::GetAtt": [
                        "L2_NETWORK",
                        "uuid"
                    ]
                },
                "system": false,
                "category": "Public",
                "systemTags": [
                    "networkservices::Public"
                ]
            },
            "DependsOn": [
                "AttachL2NetworkToCluster-L2_NETWORK"
            ]
        },
        "AddIpRangeByNetworkCidr-PUBLIC_NETWORK": {
            "Type": "ZStack::Action::AddIpRangeByNetworkCidr",
            "Properties": {
                "name": "Iprange",
                "networkCidr": "10.100.88.0/8",
                "l3NetworkUuid": {
                    "Fn::GetAtt": [
                        "PUBLIC_NETWORK",
                        "uuid"
                    ]
                }
            }
        }
    }
}
'''
    parameter = '{"ZoneUuid":"%s","ClusterUuid":"%s"}' % (zone_queried[0].uuid, cluster_queried[0].uuid)
    
    resource_stack_option.set_templateContent(templateContent)
    resource_stack_option.set_parameters(parameter)
    preview_resource_stack = resource_stack_ops.preview_resource_stack(resource_stack_option)
    resource_stack = resource_stack_ops.create_resource_stack(resource_stack_option)

    cond = res_ops.gen_query_conditions('uuid', '=', resource_stack.uuid)
    resource_stack_queried = res_ops.query_resource(res_ops.RESOURCE_STACK, cond)
    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-PublicNetwork')
    public_network_queried = res_ops.query_resource(res_ops.L3_NETWORK, cond)
    cond = res_ops.gen_query_conditions('l3NetworkUuid', '=', public_network_queried[0].uuid)
    networkservices_queried = res_ops.query_resource(res_ops.NETWORK_SERVICE_PROVIDER_L3_REF, cond)
    if resource_stack_queried[0].status == 'Created':
    	if len(networkservices_queried) == 0:
    	    test_util.test_fail("Failed to attach networkservices to l3 network.")
        elif 'Userdata' not in [service.networkServiceType for service in public_network_queried[0].networkServices]:
            test_util.test_fail("Userdata services has not been added successfully.")
        elif 'DHCP' not in [service.networkServiceType for service in public_network_queried[0].networkServices]:
            test_util.test_fail("DHCP services has not been added successfully.")
        elif 'SecurityGroup' not in [service.networkServiceType for service in public_network_queried[0].networkServices]:
            test_util.test_fail("SecurityGroup services has not been added successfully.")
 
    test_util.test_pass('Create Resource Stack Test Success')
Esempio n. 9
0
def test():
    test_util.test_dsc("Test Resource template Apis")
    
    cond = res_ops.gen_query_conditions('status', '=', 'Ready')
    cond = res_ops.gen_query_conditions('state', '=', 'Enabled', cond)
    cond = res_ops.gen_query_conditions('system', '=', 'false', cond)
    image_queried = res_ops.query_resource(res_ops.IMAGE, cond)

    cond = res_ops.gen_query_conditions("category", '=', "Private")
    cond = res_ops.gen_query_conditions('networkServices.networkServiceType', '=', 'SecurityGroup')
    l3_pri_queried = res_ops.query_resource(res_ops.L3_NETWORK, cond)

    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('type', '=', 'UserVm', cond)
    instance_offering_queried = res_ops.query_resource(res_ops.INSTANCE_OFFERING, cond)  

    resource_stack_option = test_util.ResourceStackOption()
    resource_stack_option.set_name("Create_STACK-SG")
    resource_stack_option.set_rollback("true")
    templateContent = '''
{
	"ZStackTemplateFormatVersion": "2018-06-18",
	"Description": "创建安全组示例:\n本示例会创建一个简单的安全组,并绑定到指定云主机上。创建示例前提环境:\n计算规格,镜像,私有网络,请确保网络的正确性,否则示例可能会创建失败。",
	"Parameters": {
		"InstanceOfferingUuid": {
			"Type": "String",
            "Label": "计算规格",
			"Description": "The instance offering uuid"
		},
		"ImageUuid":{
			"Type": "String",
            "Label": "镜像",
			"Description": "The Image uuid for creating VmInstance, Please choose an image not iso"
		},
		"PrivateNetworkUuid":{
			"Type": "String",
            "Label": "私有网络",
			"Description" : "The private network uuid for creating VmInstance"
		}
	},
	"Resources": {
		"VmInstance": {
			"Type": "ZStack::Resource::VmInstance",
			"Properties": {
				"name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"},"VM"]]},
				"instanceOfferingUuid": {"Ref":"InstanceOfferingUuid"},
				"imageUuid":{"Ref":"ImageUuid"},
				"l3NetworkUuids":[{"Ref":"PrivateNetworkUuid"}]
			}
		},
		"SecurityGroup": {
			"Type": "ZStack::Resource::SecurityGroup",
			"Properties": {
				"name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"},"SG"]]}
			}
		},
		"SecurityGroupRule":{
			"Type": "ZStack::Resource::SecurityGroupRule",
			"Properties": {
				"securityGroupUuid": {"Fn::GetAtt":["SecurityGroup","uuid"]},
				"rules":[{"type":"Ingress","startPort":22,"endPort":22,"protocol":"TCP","allowedCidr":"0.0.0.0/0"}]
			}
		},
		"AddVmNicToSecurityGroup": {
			"Type": "ZStack::Action::AddVmNicToSecurityGroup",
			"Properties": {
				"securityGroupUuid": {"Fn::GetAtt":["SecurityGroup","uuid"]},
				"vmNicUuids":[{"Fn::GetAtt":[{"Fn::Select":[0,{"Fn::GetAtt":["VmInstance","vmNics"]}]},"uuid"]}]
			},
			"DependsOn":[{"Ref":"AttachSecurityGroupToL3Network"}]
		},
		"AttachSecurityGroupToL3Network": {
			"Type": "ZStack::Action::AttachSecurityGroupToL3Network",
			"Properties": {
				"securityGroupUuid": {"Fn::GetAtt":["SecurityGroup","uuid"]},
				"l3NetworkUuid":{"Ref":"PrivateNetworkUuid"}
			}
		}
	},
	"Outputs": {
		"VmInstance": {
			"Value": {
				"Ref": "VmInstance"
			}
		}
	}
}
'''
    #1.create resource stack 
    #test_util.test_logger('{"PrivateNetworkUuid":"%s","ImageUuid":"%s","InstanceOfferingUuid":"%s"}' % (l3_pri_queried[0].uuid, image_queried[0].uuid, instance_offering_queried[0].uuid))
    parameter = '{"PrivateNetworkUuid":"%s","ImageUuid":"%s","InstanceOfferingUuid":"%s"}' % (l3_pri_queried[0].uuid, image_queried[0].uuid, instance_offering_queried[0].uuid)
    
    resource_stack_option.set_templateContent(templateContent)
    resource_stack_option.set_parameters(parameter)
    preview_resource_stack = resource_stack_ops.preview_resource_stack(resource_stack_option)
    resource_stack = resource_stack_ops.create_resource_stack(resource_stack_option)
    

    #2.query resource stack
    cond = res_ops.gen_query_conditions('uuid', '=', resource_stack.uuid)
    resource_stack_queried = res_ops.query_resource(res_ops.RESOURCE_STACK, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-SG-VM')
    vm_queried = res_ops.query_resource(res_ops.VM_INSTANCE, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-SG-SG')
    sg_queried = res_ops.query_resource(res_ops.SECURITY_GROUP, cond)

    if len(resource_stack_queried) == 0:
        test_util.test_fail("Fail to query resource stack")

    if resource_stack_queried[0].status == 'Created':
        if len(vm_queried) == 0 or len(sg_queried) == 0 :
            test_util.test_fail("Fail to create all resource when resource stack status is Created")
    elif len(vm_queried) != 0 or len(sg_queried) != 0 :
        test_util.test_fail("Fail to delete all resource when resource stack status is Rollbacked or Deleted")
    
    
    #3.get resource from resource stack
    resource = resource_stack_ops.get_resource_from_resource_stack(resource_stack.uuid)
    if resource == None or len(resource) != 3:
        test_util.test_fail("Fail to get resource from resource_stack")
    
    #4.query event from resource stack
    cond = res_ops.gen_query_conditions('stackUuid', '=', resource_stack.uuid)
    event = res_ops.query_event_from_resource_stack(cond)
    if event == None or len(event) != 10:
        test_util.test_fail("Fail to get event from resource_stack")

    #5.delete resource stack
    resource_stack_ops.delete_resource_stack(resource_stack.uuid)

    cond = res_ops.gen_query_conditions('uuid', '=', resource_stack.uuid)
    resource_stack_queried = res_ops.query_resource(res_ops.RESOURCE_STACK, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-SG-VM')
    vm_queried = res_ops.query_resource(res_ops.VM_INSTANCE, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-SG-SG')
    sg_queried = res_ops.query_resource(res_ops.SECURITY_GROUP, cond)

    if len(resource_stack_queried) != 0 :
        test_util.test_fail("Fail to delete resource stack") 
    elif len(vm_queried) != 0 or len(sg_queried) != 0:
        test_util.test_fail("Fail to delete resource when resource stack is deleted")   
   

    test_util.test_pass('Create Resource Stack Test Success')
Esempio n. 10
0
def test():
    test_util.test_dsc("Test Resource template Apis")
    
    cond = res_ops.gen_query_conditions("category", '=', "Public")
    l3_pub_queried = res_ops.query_resource(res_ops.L3_NETWORK, cond)

    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('status', '=', 'Connected', cond)
    bs_queried = res_ops.query_resource(res_ops.BACKUP_STORAGE, cond)

    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    zone_queried = res_ops.query_resource(res_ops.ZONE, cond)

    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cluster_queried = res_ops.query_resource(res_ops.CLUSTER, cond)

    resource_stack_option = test_util.ResourceStackOption()
    resource_stack_option.set_name("Create_STACK-VPC")
    resource_stack_option.set_rollback("true")
    templateContent = '''
{
	"ZStackTemplateFormatVersion": "2018-06-18",
	"Description": "本示例会创建一个简单的VPC网络,需要用户提供下面正确的数据\n公有网络Uuid\n管理网络Uuid: 如果只有公有网络,则把公有网络当作管理网即可.\nVxlan网络的VTEP Cider",
	"Parameters": {
		"VrouterImageUrl": {
			"Type": "String",
            "Label":"云路由镜像链接地址",
			"Description":"云路由镜像链接地址",
			"DefaultValue": "http://cdn.zstack.io/product_downloads/vrouter/2.3/zstack-vrouter-2.3.2.qcow2"
		},
		"VmImageUrl": {
			"Type": "String",
            "Label": "云主机镜像链接地址",
			"Description":"测试云主机镜像,请确定ZStack 可以下载下面链接的镜像",
			"DefaultValue": "http://cdn.zstack.io/zstack_repo/latest/zstack-image-1.4.qcow2"
		},
		"BackupStorage":{
			"Type": "CommaDelimitedList",
            "Label": "镜像服务器",
			"Description":"镜像服务器Uuid"
		},
		"ManagementNetworkUuid":{
			"Type": "String",
            "Label": "管理网络",
			"Description":"管理网络Uuid,如果只有公有网络填入公有网络Uuid即可"
		},
		"PublicNetworkUuid":{
			"Type": "String",
            "Label": "公有网络",
			"Description":"公有网络Uuid"
		},
		"ZoneUuid":{
			"Type": "String",
            "Label": "区域",
			"Description":"区域Uuid"
		},
		"ClusterUuid":{
			"Type": "String",
            "Label": "集群",
			"Description":"集群Uuid"
		},
		"Cidr":{
			"Type": "String",
			"Description":"VTEP Cider",
			"DefaultValue":"{172.20.0.1/8}"
		},
		"Vni":{
			"Type": "Number",
			"DefaultValue":222
		},
		"StartVni":{
			"Type": "Number",
			"DefaultValue":100
		},
		"EndVni":{
			"Type": "Number",
			"DefaultValue":300
		},
		"StartIp":{
			"Type": "String",
			"DefaultValue":"192.168.20.2"
		},
		"EndIp":{
			"Type": "String",
			"DefaultValue":"192.168.20.200"
		},
		"Netmask":{
			"Type": "String",
			"DefaultValue":"255.255.255.0"
		},
		"Gateway":{
			"Type": "String",
			"DefaultValue":"192.168.20.1"
		}
	},
	"Resources": {
		"VrouterImage": {
			"Type": "ZStack::Resource::Image",
			"Properties": {
				"name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"},"Vrouter-Image"]]},
				"url": {"Ref":"VrouterImageUrl"},
				"system": true,
				"format": "qcow2",
				"backupStorageUuids":{"Ref":"BackupStorage"}
			}
		},
		"VMImage": {
			"Type": "ZStack::Resource::Image",
			"Properties": {
				"name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"}, "VmImage"]]},
				"url": {"Ref":"VmImageUrl"},
				"format": "qcow2",
				"backupStorageUuids":{"Ref":"BackupStorage"}
			}
		},
		"VirtualRouterOffering":{
			"Type":"ZStack::Resource::VirtualRouterOffering",
			"Properties":{
				"name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"}, "Vrouter-Offering"]]},
				"zoneUuid":{"Ref":"ZoneUuid"},
				"managementNetworkUuid":{"Ref":"ManagementNetworkUuid"},
				"publicNetworkUuid":{"Ref":"PublicNetworkUuid"},
				"imageUuid":{"Fn::GetAtt":["VrouterImage", "uuid"]},
				"cpuNum":1,
				"memorySize":1073741824
			}
		},
		"VpcVRouter":{
			"Type":"ZStack::Resource::VpcVRouter",
			"Properties":{
				"name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"}, "VPC-Router"]]},
				"virtualRouterOfferingUuid":{"Fn::GetAtt":["VirtualRouterOffering","uuid"]}
			}
		},
		"L2VxlanNetworkPool":{
			"Type":"ZStack::Resource::L2VxlanNetworkPool",
			"Properties":{
				"name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"}, "L2VxlanNetworkPool"]]},
				"zoneUuid":{"Ref":"ZoneUuid"}
			}
		},
		"VniRange":{
			"Type":"ZStack::Resource::VniRange",
			"Properties":{
				"name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"}, "VniRange"]]},
				"startVni":{"Ref":"StartVni"},
				"endVni":{"Ref":"EndVni"},
				"l2NetworkUuid":{"Fn::GetAtt":["L2VxlanNetworkPool","uuid"]}
			}
		},
		"L2VxlanNetwork":{
			"Type":"ZStack::Resource::L2VxlanNetwork",
			"Properties":{
				"name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"}, "L2VxlanNetwork"]]},
				"poolUuid":{"Fn::GetAtt":["L2VxlanNetworkPool","uuid"]},
				"zoneUuid":{"Ref":"ZoneUuid"},
				"vni":{"Ref":"Vni"}
			}
		},
		"VpcL3Network":{
			"Type":"ZStack::Resource::L3Network",
			"Properties":{
				"name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"}, "VPC-Network"]]},
				"l2NetworkUuid":{"Fn::GetAtt":["L2VxlanNetwork","uuid"]},
				"category":"Private",
				"type":"L3VpcNetwork",
				"systemTags":["networkservices::VRouter"]
			}
		},
		"InstanceOffering":{
			"Type":"ZStack::Resource::InstanceOffering",
			"Properties":{
				"name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"}, "1cpu","1G"]]},
				"cpuNum": 1,
				"memorySize" : 1073741824
			}
		},

		"AttachL3ToVm":{
			"Type":"ZStack::Action::AttachL3NetworkToVm",
			"Properties":{
				"vmInstanceUuid": {"Fn::GetAtt":["VpcVRouter","uuid"]},
				"l3NetworkUuid":{"Fn::GetAtt":["VpcL3Network","uuid"]}
			},
			"DependsOn":[{"Ref":"AddIpRange"}]
		},
		"AddIpRange" :{
			"Type":"ZStack::Action::AddIpRange",
			"Properties":{
				"name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"}, "iprange"]]},
				"l3NetworkUuid":{"Fn::GetAtt":["VpcL3Network","uuid"]},
				"startIp":{"Ref":"StartIp"},
				"endIp":{"Ref":"EndIp"},
				"netmask":{"Ref":"Netmask"},
				"gateway":{"Ref":"Gateway"}
			}
		},
		"AttachL2NetworkToCluster":{
			"Type":"ZStack::Action::AttachL2NetworkToCluster",
			"Properties":{
				"l2NetworkUuid":{"Fn::GetAtt":["L2VxlanNetworkPool","uuid"]},
				"clusterUuid":{"Ref":"ClusterUuid"},
				"systemTags":[{"Fn::Join":["::",["l2NetworkUuid",{"Fn::GetAtt":["L2VxlanNetwork","uuid"]},"clusterUuid",{"Ref":"ClusterUuid"},"cidr",{"Ref":"Cidr"}]]}]
			}
		},
		"TestVm":{
			"Type":"ZStack::Resource::VmInstance",
			"Properties":{
				"name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"}, "TestVm"]]},
				"instanceOfferingUuid": {"Fn::GetAtt":["InstanceOffering","uuid"]},
				"l3NetworkUuids": [{"Fn::GetAtt":["VpcL3Network","uuid"]}],
				"imageUuid": {"Fn::GetAtt":["VMImage", "uuid"]}
			},
			"DependsOn":[{"Ref":"AttachL3ToVm"}]
		}
	},
	"Outputs": {
		"vpc": {
			"Value": {
				"Ref": "VpcL3Network"
			}
		}
	}
}

'''
    #1.create resource stack 
    #test_util.test_logger('{"PrivateNetworkUuid":"%s","ImageUuid":"%s","InstanceOfferingUuid":"%s"}' % (l3_pri_queried[0].uuid, image_queried[0].uuid, instance_offering_queried[0].uuid))
    parameter = '{"PublicNetworkUuid":"%s","ManagementNetworkUuid":"%s","BackupStorage":"%s","ZoneUuid":"%s","ClusterUuid":"%s"}' % (l3_pub_queried[0].uuid, l3_pub_queried[0].uuid, bs_queried[0].uuid, zone_queried[0].uuid, cluster_queried[0].uuid)
    
    resource_stack_option.set_templateContent(templateContent)
    resource_stack_option.set_parameters(parameter)
    preview_resource_stack = resource_stack_ops.preview_resource_stack(resource_stack_option)
    resource_stack = resource_stack_ops.create_resource_stack(resource_stack_option)
    

    #2.query resource stack
    cond = res_ops.gen_query_conditions('uuid', '=', resource_stack.uuid)
    resource_stack_queried = res_ops.query_resource(res_ops.RESOURCE_STACK, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-VPC-Vrouter-Image')
    vr_image_queried = res_ops.query_resource(res_ops.IMAGE, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-VPC-Vrouter-Offering')
    vr_offering_queried = res_ops.query_resource(res_ops.INSTANCE_OFFERING, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-VPC-VPC-Router')
    vpc_router_queried = res_ops.query_resource(res_ops.VIRTUALROUTER_VM, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-VPC-L2VxlanNetwotkPool')
    l2_vxlan_pool_queried = res_ops.query_resource(res_ops.L2_VXLAN_NETWORK_POOL, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-VPC-L2VxlanNetwotk')
    l2_vxlan_queried = res_ops.query_resource(res_ops.L2_VXLAN_NETWORK, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-VPC-VniRange')
    vni_range_queried = res_ops.query_resource(res_ops.VNI_RANGE, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-VPC-VPC-Network')
    vpc_network_queried = res_ops.query_resource(res_ops.L3_NETWORK, cond)

    if len(resource_stack_queried) == 0:
        test_util.test_fail("Fail to query resource stack")

    if resource_stack_queried[0].status == 'Created':
        if len(vr_image_queried) == 0 or len(vr_offering_queried) == 0 or len(vpc_router_queried) == 0 or len(l2_vxlan_pool_queried) == 0 or len(l2_vxlan_queried) == 0 or len(vni_range_queried) == 0 or len(vpc_network_queried) == 0:
            test_util.test_fail("Fail to create all resource when resource stack status is Created")
    
    
    
    #3.get resource from resource stack
    resource = resource_stack_ops.get_resource_from_resource_stack(resource_stack.uuid)
    if resource == None or len(resource) != 10:
        test_util.test_fail("Fail to get resource from resource_stack")
    
    #4.query event from resource stack
    cond = res_ops.gen_query_conditions('stackUuid', '=', resource_stack.uuid)
    event = res_ops.query_event_from_resource_stack(cond)
    if event == None or len(event) != 26:
        test_util.test_fail("Fail to get event from resource_stack")

    #5.delete resource stack
    resource_stack_ops.delete_resource_stack(resource_stack.uuid)

    cond = res_ops.gen_query_conditions('uuid', '=', resource_stack.uuid)
    resource_stack_queried = res_ops.query_resource(res_ops.RESOURCE_STACK, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-VPC-Vrouter-Image')
    vr_image_queried = res_ops.query_resource(res_ops.IMAGE, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-VPC-Vrouter-Offering')
    vr_offering_queried = res_ops.query_resource(res_ops.INSTANCE_OFFERING, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-VPC-VPC-Router')
    vpc_router_queried = res_ops.query_resource(res_ops.VIRTUALROUTER_VM, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-VPC-L2VxlanNetwotkPool')
    l2_vxlan_pool_queried = res_ops.query_resource(res_ops.L2_VXLAN_NETWORK_POOL, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-VPC-L2VxlanNetwotk')
    l2_vxlan_queried = res_ops.query_resource(res_ops.L2_VXLAN_NETWORK, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-VPC-VniRange')
    vni_range_queried = res_ops.query_resource(res_ops.VNI_RANGE, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-VPC-VPC-Network')
    vpc_network_queried = res_ops.query_resource(res_ops.L3_NETWORK, cond)

    if len(resource_stack_queried) != 0 :
        test_util.test_fail("Fail to delete resource stack") 
    elif len(vr_image_queried) != 0 or len(vr_offering_queried) != 0 or len(vpc_router_queried) != 0 or len(l2_vxlan_pool_queried) != 0 or len(l2_vxlan_queried) != 0 or len(vni_range_queried) != 0 or len(vpc_network_queried) != 0:
        test_util.test_fail("Fail to delete resource when resource stack is deleted")   
   

    test_util.test_pass('Create Resource Stack Test Success')
def test():
    test_util.test_dsc("Test Resource template Apis")

    cond = res_ops.gen_query_conditions('status', '=', 'Ready')
    cond = res_ops.gen_query_conditions('state', '=', 'Enabled', cond)
    cond = res_ops.gen_query_conditions('system', '=', 'false', cond)
    image_queried = res_ops.query_resource(res_ops.IMAGE, cond)

    cond = res_ops.gen_query_conditions("category", '=', "Public")
    l3_pub_queried = res_ops.query_resource(res_ops.L3_NETWORK, cond)

    cond = res_ops.gen_query_conditions("category", '=', "Private")
    cond = res_ops.gen_query_conditions('networkServices.networkServiceType',
                                        '=', 'EIP')
    l3_pri_queried = res_ops.query_resource(res_ops.L3_NETWORK, cond)

    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('type', '=', 'UserVm', cond)
    instance_offering_queried = res_ops.query_resource(
        res_ops.INSTANCE_OFFERING, cond)

    templateContent = '''
{
    "ZStackTemplateFormatVersion": "2018-06-18",
    "Description": "Just create a VM with eip",
    "Parameters": {
        "InstanceOfferingUuid": {
            "Type": "String",
            "Label": "vm instance offering"
        },
        "ImageUuid":{
            "Type": "String"
        },
        "PrivateNetworkUuid":{
            "Type": "String"
        },
        "PublicNetworkUuid":{
            "Type": "String"
        }
    },
    "Resources": {
        "VmInstance": {
            "Type": "ZStack::Resource::VmInstance",
            "Properties": {
                "name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"},"VM"]]},
                "instanceOfferingUuid": {"Ref":"InstanceOfferingUuid"},
                "imageUuid":{"Ref":"ImageUuid"},
                "l3NetworkUuids":[{"Ref":"PrivateNetworkUuid"}]
            }
        },
        "VIP": {
            "Type": "ZStack::Resource::Vip",
            "Properties": {
                "name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"},"VIP"]]},
                "l3NetworkUuid":{"Ref":"PublicNetworkUuid"}
            }
        },
        "EIP":{
            "Type": "ZStack::Resource::Eip",
            "Properties": {
                "vipUuid":{"Fn::GetAtt":["VIP","uuid"]}
            }
        }
    },
    "Outputs": {
        "VmInstance": {
            "Value": {
                "Ref": "VmInstance"
            }
        }
    }
}
'''
    parameter = '{"PrivateNetworkUuid":"%s","PublicNetworkUuid":"%s","ImageUuid":"%s","InstanceOfferingUuid":"%s"}' % (
        l3_pri_queried[0].uuid, l3_pub_queried[0].uuid, image_queried[0].uuid,
        instance_offering_queried[0].uuid)

    #1.create resource stack when rollback
    resource_stack_option_rollback = test_util.ResourceStackOption()
    resource_stack_option_rollback.set_name("Create_STACK-Rollback")
    resource_stack_option_rollback.set_rollback("true")

    resource_stack_option_rollback.set_templateContent(templateContent)
    resource_stack_option_rollback.set_parameters(parameter)
    preview_resource_stack = resource_stack_ops.preview_resource_stack(
        resource_stack_option_rollback)
    resource_stack_rollback = resource_stack_ops.create_resource_stack(
        resource_stack_option_rollback)

    #2.get resource from resource stack when rollback
    resource_rollback = resource_stack_ops.get_resource_from_resource_stack(
        resource_stack_rollback.uuid)
    if len(resource_rollback) != 0:
        test_util.test_fail("There still has resources when rollback")

    cond = res_ops.gen_query_conditions('uuid', '=',
                                        resource_stack_rollback.uuid)
    resource_stack_rollback_queried = res_ops.query_resource(
        res_ops.RESOURCE_STACK, cond)

    cond = res_ops.gen_query_conditions('name', '=',
                                        'Create_STACK-Rollback-VM')
    vm_rollback_queried = res_ops.query_resource(res_ops.VM_INSTANCE, cond)

    cond = res_ops.gen_query_conditions('name', '=',
                                        'Create_STACK-Rollback-VIP')
    vip_rollback_queried = res_ops.query_resource(res_ops.VIP, cond)

    cond = res_ops.gen_query_conditions('name', '=',
                                        'Create_STACK-Rollback-EIP')
    eip_rollback_queried = res_ops.query_resource(res_ops.EIP, cond)

    if len(vm_rollback_queried) != 0 or len(vip_rollback_queried) != 0 or len(
            eip_rollback_queried) != 0:
        test_util.test_fail("Fail to delete resources when rollback")

    #3.create resource stack when not rollback
    resource_stack_option_norollback = test_util.ResourceStackOption()
    resource_stack_option_norollback.set_name("Create_STACK")
    resource_stack_option_norollback.set_rollback("false")

    resource_stack_option_norollback.set_templateContent(templateContent)
    resource_stack_option_norollback.set_parameters(parameter)
    preview_resource_stack = resource_stack_ops.preview_resource_stack(
        resource_stack_option_norollback)
    try:
        resource_stack_norollback = resource_stack_ops.create_resource_stack(
            resource_stack_option_norollback)
    except:
        pass
    else:
        test_util.test_fail('Resource Stack Rollback Test Success')

    #4.get resource from resource stack when not rollback
    resource_stack_ops.delete_resource_stack(resource_stack_rollback.uuid)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK')
    resource_stack_norollback = res_ops.query_resource(res_ops.RESOURCE_STACK,
                                                       cond)

    cond = res_ops.gen_query_conditions('uuid', '=',
                                        resource_stack_norollback[0].uuid)
    resource_stack_norollback_queried = res_ops.query_resource(
        res_ops.RESOURCE_STACK, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-VM')
    vm_norollback_queried = res_ops.query_resource(res_ops.VM_INSTANCE, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-VIP')
    vip_norollback_queried = res_ops.query_resource(res_ops.VIP, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-EIP')
    eip_norollback_queried = res_ops.query_resource(res_ops.EIP, cond)

    if len(resource_stack_norollback_queried) == 0:
        test_util.test_fail("Fail to delete resource stack")

    if resource_stack_norollback_queried[0].status == 'Failed':
        if len(vm_norollback_queried) == 0 or len(vip_norollback_queried) == 0:
            test_util.test_fail("Resources were deleted when not rollback")
    else:
        test_util.test_fail("Resource Stack status is not Failed")

    resource_norollback = resource_stack_ops.get_resource_from_resource_stack(
        resource_stack_norollback[0].uuid)
    if len(resource_norollback) == 0:
        test_util.test_fail("There has no resources when not rollback")

    test_util.test_pass('Resource Stack Rollback Test Success')
def test():
    test_util.test_dsc("Test Resource template Apis")

    cond = res_ops.gen_query_conditions('status', '=', 'Ready')
    cond = res_ops.gen_query_conditions('state', '=', 'Enabled', cond)
    cond = res_ops.gen_query_conditions('system', '=', 'false', cond)
    image_queried = res_ops.query_resource(res_ops.IMAGE, cond)

    cond = res_ops.gen_query_conditions("category", '=', "Public")
    l3_pub_queried = res_ops.query_resource(res_ops.L3_NETWORK, cond)

    cond = res_ops.gen_query_conditions("category", '=', "Private")
    cond = res_ops.gen_query_conditions('networkServices.networkServiceType',
                                        '=', 'LoadBalancer')
    l3_pri_queried = res_ops.query_resource(res_ops.L3_NETWORK, cond)

    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('type', '=', 'UserVm', cond)
    instance_offering_queried = res_ops.query_resource(
        res_ops.INSTANCE_OFFERING, cond)

    resource_stack_option = test_util.ResourceStackOption()
    resource_stack_option.set_name("Create_STACK")
    templateContent = '''
{
    "ZStackTemplateFormatVersion": "2018-06-18",
    "Description": "",
    "Parameters": {
        "PrivateNetworkUuid": {
            "Type": "String",
            "Label": "私有网络"
        },
        "PublicNetworkUuid":{
            "Type": "String",
            "Label": "公有网络"
        },
        "InstanceOfferingUuid": {
            "Type": "String",
            "Label": "计算规格"
        },
        "ImageUuid": {
            "Type": "String",
            "Label": "镜像"
        }
    },
    "Resources": {
        "VM": {
            "Type": "ZStack::Resource::VmInstance",
            "Properties": {
                "name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"},"VM"]]},
                "description": "",
                "instanceOfferingUuid": {"Ref":"InstanceOfferingUuid"},
                "imageUuid": {"Ref":"ImageUuid"},
                "l3NetworkUuids": [
                    {"Ref":"PrivateNetworkUuid"}
                ],
                "defaultL3NetworkUuid": {"Ref":"PrivateNetworkUuid"},
                "dataDiskOfferingUuids": [],
                "systemTags": [
                    "usbRedirect::false",
                    "vmConsoleMode::vnc"
                ]
            }
        },
        "LOAD_BALANCER": {
            "Type": "ZStack::Resource::LoadBalancer",
            "Properties": {
                "name": {"Fn::Join":["-",[{"Ref":"ZStack::StackName"},"LB"]]},
                "vipUuid": {
                    "Fn::GetAtt": [
                        "LOAD_BALANCER",
                        "uuid"
                    ]
                }
            }
        },
        "VIP-LOAD_BALANCER": {
            "Type": "ZStack::Resource::Vip",
            "Properties": {
                "name": "{"Fn::Join":["-",[{"Ref":"ZStack::StackName"},"VIP"]]}",
                "l3NetworkUuid": {"Ref":"PublicNetworkUuid"}
            }
        },
        "LISTENER": {
            "Type": "ZStack::Resource::LoadBalancerListener",
            "Properties": {
                "name": "{"Fn::Join":["-",[{"Ref":"ZStack::StackName"},"LBL"]]}",
                "protocol": "tcp",
                "loadBalancerPort": 22,
                "instancePort": 22,
                "loadBalancerUuid": {
                    "Fn::GetAtt": [
                        "LOAD_BALANCER",
                        "uuid"
                    ]
                },
                "systemTags": [
                    "balancerAlgorithm::roundrobin",
                    "connectionIdleTimeout::60",
                    "healthyThreshold::2",
                    "unhealthyThreshold::2",
                    "healthCheckInterval::5",
                    "maxConnection::5000"
                ]
            }
        },
        "AddVmNicToLoadBalancer": {
            "Type": "ZStack::Action::AddVmNicToLoadBalancer",
            "Properties": {
                "vmNicUuids": [
                    {
                        "Fn::GetAtt": [
                            {
                                "Fn::Select": [
                                    0,
                                    {
                                        "Fn::GetAtt": [
                                            "VM",
                                            "vmNics"
                                        ]
                                    }
                                ]
                            },
                            "uuid"
                        ]
                    }
                ],
                "listenerUuid": {
                    "Fn::GetAtt": [
                        "LISTENER",
                        "uuid"
                    ]
                }
            }
        }
    }
}
'''
    #1.create resource stack
    parameter = '{"PrivateNetworkUuid":"%s","PublicNetworkUuid":"%s","ImageUuid":"%s","InstanceOfferingUuid":"%s"}' % (
        l3_pri_queried[0].uuid, l3_pub_queried[0].uuid, image_queried[0].uuid,
        instance_offering_queried[0].uuid)
    resource_stack_option.set_templateContent(templateContent)
    resource_stack_option.set_parameters(parameter)
    try:
        preview_resource_stack = resource_stack_ops.preview_resource_stack(
            resource_stack_option)
    except:
        pass
    else:
        test_util.test_fail(
            'Preview resource stack successfully with wrong parameter.')

    try:
        resource_stack = resource_stack_ops.create_resource_stack(
            resource_stack_option)
    except:
        pass
    else:
        test_util.test_fail(
            'Create resource stack successfully with wrong parameter.')

    #2.query resource stack
    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK')
    resource_stack_queried = res_ops.query_resource(res_ops.RESOURCE_STACK,
                                                    cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-VM')
    vm_queried = res_ops.query_resource(res_ops.VM_INSTANCE, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-VIP')
    vip_queried = res_ops.query_resource(res_ops.VIP, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-LB')
    lb_queried = res_ops.query_resource(res_ops.LOAD_BALANCER, cond)

    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-LBL')
    lbl_queried = res_ops.query_resource(res_ops.LOAD_BALANCER_LISTENER, cond)

    if len(resource_stack_queried) != 0:
        if len(vm_queried) == 0 or len(vip_queried) == 0 or len(
                lb_queried) == 0 or len(lbl_queried) == 0:
            test_util.test_fail("Fail to delete resources.")
def test():
    test_util.test_dsc("Test Resource Stack Apis")

    cond = res_ops.gen_query_conditions('status', '=', 'Ready')
    cond = res_ops.gen_query_conditions('state', '=', 'Enabled', cond)
    cond = res_ops.gen_query_conditions('system', '=', 'false', cond)
    image_queried = res_ops.query_resource(res_ops.IMAGE, cond)

    cond = res_ops.gen_query_conditions("category", '=', "Public")
    l3_queried = res_ops.query_resource(res_ops.L3_NETWORK, cond)
    if len(l3_queried) == 0:
        cond = res_ops.gen_query_conditions("category", '=', "Private")
        l3_queried = res_ops.query_resource(res_ops.L3_NETWORK, cond)

    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('type', '=', 'UserVm', cond)
    instance_offering_queried = res_ops.query_resource(
        res_ops.INSTANCE_OFFERING, cond)

    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('status', '=', 'Connected', cond)
    ps_queried = res_ops.query_resource(res_ops.PRIMARY_STORAGE, cond)

    resource_stack_option = test_util.ResourceStackOption()
    resource_stack_option.set_name("Restart_Stack")
    templateContent = '''
{
    "ZStackTemplateFormatVersion": "2018-06-18",
    "Description": "Just create a flat network & VM",
    "Parameters": {
        "L3NetworkUuid":{
            "Type": "String",
            "Label": "三层网络",
            "DefaultValue": "testuuid"
        },
        "ImageUuid":{
            "Type": "String",
            "Label": "镜像"
        },
        "InstanceOfferingUuid":{
            "Type": "String",
            "Label": "计算规格"
        }
    },
    "Resources": {
        "VmInstance": {
            "Type": "ZStack::Resource::VmInstance",
            "Properties": {
                "name": "VM-STACK",
                "instanceOfferingUuid": {"Ref":"InstanceOfferingUuid"},
                "imageUuid":{"Ref":"ImageUuid"},
                "l3NetworkUuids":[{"Ref":"L3NetworkUuid"}]
            }
        }
    },
    "Outputs": {
        "VmInstance": {
            "Value": {
                "Ref": "VmInstance"
            }
        }
    }
}
'''
    #1.create resource stack
    ps_ops.change_primary_storage_state(ps_queried[0].uuid, 'disable')
    parameter = '{"ImageUuid":"%s","InstanceOfferingUuid":"%s","L3NetworkUuid":"%s"}' % (
        image_queried[0].uuid, instance_offering_queried[0].uuid,
        l3_queried[0].uuid)
    resource_stack_option.set_templateContent(templateContent)
    resource_stack_option.set_parameters(parameter)
    #preview_resource_stack = resource_stack_ops.preview_resource_stack(resource_stack_option)
    try:
        resource_stack = resource_stack_ops.create_resource_stack(
            resource_stack_option)
        test_util.test_fail(
            'This resource stack cannot be created successfully')
    except:
        pass

    #2.query resource stack
    cond = res_ops.gen_query_conditions('name', '=', 'Restart_Stack')
    resource_stack_queried = res_ops.query_resource(res_ops.RESOURCE_STACK,
                                                    cond)

    cond = res_ops.gen_query_conditions('name', '=', 'VM-STACK')
    vm_queried = res_ops.query_resource(res_ops.VM_INSTANCE, cond)

    if len(resource_stack_queried) == 0:
        test_util.test_fail("Fail to query resource stack")
    elif resource_stack_queried[0].status == 'Created':
        test_util.test_fail('The status of resource stack cannot be created')
    else:
        if len(vm_queried) != 0:
            test_util.test_fail(
                'Vm cannot be created when resource stack is Rollbacked or Failed'
            )

    #3.restart resource stack
    ps_ops.change_primary_storage_state(ps_queried[0].uuid, 'enable')
    resource_stack_ops.restart_resource_stack(resource_stack_queried[0].uuid)

    cond = res_ops.gen_query_conditions('uuid', '=',
                                        resource_stack_queried[0].uuid)
    resource_stack_queried = res_ops.query_resource(res_ops.RESOURCE_STACK,
                                                    cond)

    cond = res_ops.gen_query_conditions('name', '=', 'VM-STACK')
    vm_queried = res_ops.query_resource(res_ops.VM_INSTANCE, cond)

    if len(resource_stack_queried) == 0:
        test_util.test_fail("Fail to query resource stack")
    elif resource_stack_queried[0].status != 'Created':
        test_util.test_fail('The status of resource stack should be created')
    else:
        if len(vm_queried) == 0:
            test_util.test_fail(
                'Vm should be created when resource stack is Created')

    try:
        resource_stack_ops.restart_resource_stack(
            resource_stack_queried[0].uuid)
        test_util.test_fail(
            'Resource stack cannot restart when status of stack is created')
    except:
        pass

    #4.delete resource stack
    resource_stack_ops.delete_resource_stack(resource_stack_queried[0].uuid)

    cond = res_ops.gen_query_conditions('uuid', '=',
                                        resource_stack_queried[0].uuid)
    resource_stack_queried = res_ops.query_resource(res_ops.RESOURCE_STACK,
                                                    cond)

    cond = res_ops.gen_query_conditions('name', '=', 'VM-STACK')
    vm_queried = res_ops.query_resource(res_ops.VM_INSTANCE, cond)

    if len(resource_stack_queried) != 0:
        test_util.test_fail("Fail to delete resource stack")
    elif len(vm_queried) != 0:
        test_util.test_fail(
            "Fail to delete resource when resource stack is deleted")

    test_util.test_pass('Restart Resource Stack Test Success')
def test():
    test_stub = test_lib.lib_get_test_stub()
    test_obj_dict1 = test_state.TestStateDict()
    test_obj_dict2 = test_state.TestStateDict()
    global mevoco1_ip
    global mevoco2_ip
    global ipsec1
    global ipsec2
    global templateContent
    mevoco1_ip = os.environ['ZSTACK_BUILT_IN_HTTP_SERVER_IP']
    mevoco2_ip = os.environ['secondZStackMnIp']
    test_util.test_dsc('Create test vip in mevoco1')
    cond = res_ops.gen_query_conditions("category", '=', "Public")
    l3_pub1_queried = res_ops.query_resource(res_ops.L3_NETWORK, cond)
    cond = res_ops.gen_query_conditions("name", '=',
                                        os.environ.get('l3VlanNetworkName1'))
    l3_pri1_queried = res_ops.query_resource(res_ops.L3_NETWORK, cond)
    vm1 = test_stub.create_vlan_vm(os.environ.get('l3VlanNetworkName1'))
    vip1 = test_stub.create_vip('ipsec1_vip', l3_pub1_queried[0].uuid)
    cond = res_ops.gen_query_conditions('uuid', '=', l3_pri1_queried[0].uuid)
    first_zstack_cidrs = res_ops.query_resource(
        res_ops.L3_NETWORK, cond)[0].ipRanges[0].networkCidr
    os.environ['ZSTACK_BUILT_IN_HTTP_SERVER_IP'] = mevoco2_ip
    test_util.test_dsc('Create test vip in mevoco2')
    cond = res_ops.gen_query_conditions("category", '=', "Public")
    l3_pub2_queried = res_ops.query_resource(res_ops.L3_NETWORK, cond)
    cond = res_ops.gen_query_conditions(
        "name", '=', os.environ.get('l3VlanDNATNetworkName'))
    l3_pri2_queried = res_ops.query_resource(res_ops.L3_NETWORK, cond)
    vm2 = test_stub.create_vlan_vm(os.environ.get('l3VlanDNATNetworkName'))
    vip2 = test_stub.create_vip('ipsec2_vip', l3_pub2_queried[0].uuid)
    cond = res_ops.gen_query_conditions('uuid', '=', l3_pri2_queried[0].uuid)
    second_zstack_cidrs = res_ops.query_resource(
        res_ops.L3_NETWORK, cond)[0].ipRanges[0].networkCidr

    templateContent = '''
{
	"ZStackTemplateFormatVersion": "2018-06-18",
        "Description": "本示例会创建一个简单的IPsec通道,需要用户提供下面正确的数据\n已有的虚拟IP地址,\n本地子网Uuid,远端IP,远端CIDR,认证密钥",
	"Parameters": {
	    "VipUuid":{
                "Type": "String",
                "Label": "虚拟IP",
	        "Description":"已有的虚拟IP的Uuid"
	    },
	    "PrivateNetworkUuid":{
		"Type": "String",
                "Label": "本地网络",
		"Description":"本地网络Uuid"
	    },
	    "PeerAddress": {
		"Type": "String",
		"Description":"远端IP"
	    },
	    "PeerCidrs":{
		"Type": "CommaDelimitedList",
		"Description":"远端 Cidr"
	    },
	    "AuthKey":{
		"Type": "String",
		"DefaultValue":"Test1234"
	    }
	},
	"Resources": {
            "IPsecConnection":{
                "Type": "ZStack::Resource::IPsecConnection",
                "Properties": {
                    "name": "IPsec-STACK",
                    "vipUuid": {"Ref": "VipUuid"},
                    "l3NetworkUuid": {"Ref":"PrivateNetworkUuid"},
                    "peerAddress": {"Ref":"PeerAddress"},
                    "peerCidrs": {"Ref":"PeerCidrs"},
                    "authKey": {"Ref":"AuthKey"}
                }
            }
	},
	"Outputs": {
	    "IPsecConnection": {
	        "Value": {
		"Ref": "IPsecConnection"
		}
	    }
	}
}

'''
    #1.create resource stack
    os.environ['ZSTACK_BUILT_IN_HTTP_SERVER_IP'] = mevoco1_ip
    test_util.test_dsc('Create ipsec in mevoco1')
    resource_stack1_option = test_util.ResourceStackOption()
    resource_stack1_option.set_name("Create_STACK-IPSEC1")
    resource_stack1_option.set_rollback("true")
    print('aooo = %s is %s') % ([second_zstack_cidrs
                                 ], type([second_zstack_cidrs]))
    parameter1 = '{"VipUuid":"%s","PrivateNetworkUuid":"%s","PeerAddress":"%s","PeerCidrs":"%s"}' % (
        vip1.get_vip().uuid, l3_pri1_queried[0].uuid, vip2.get_vip().ip,
        second_zstack_cidrs)
    resource_stack1_option.set_templateContent(templateContent)
    resource_stack1_option.set_parameters(parameter1)
    preview_resource_stack1 = resource_stack_ops.preview_resource_stack(
        resource_stack1_option)
    resource_stack1 = resource_stack_ops.create_resource_stack(
        resource_stack1_option)

    os.environ['ZSTACK_BUILT_IN_HTTP_SERVER_IP'] = mevoco2_ip
    test_util.test_dsc('Create ipsec in mevoco2')
    resource_stack2_option = test_util.ResourceStackOption()
    resource_stack2_option.set_name("Create_STACK-IPSEC2")
    resource_stack2_option.set_rollback("true")
    parameter2 = '{"VipUuid":"%s","PrivateNetworkUuid":"%s","PeerAddress":"%s","PeerCidrs":"%s"}' % (
        vip2.get_vip().uuid, l3_pri2_queried[0].uuid, vip1.get_vip().ip,
        first_zstack_cidrs)
    resource_stack2_option.set_templateContent(templateContent)
    resource_stack2_option.set_parameters(parameter2)
    preview_resource_stack2 = resource_stack_ops.preview_resource_stack(
        resource_stack2_option)
    resource_stack2 = resource_stack_ops.create_resource_stack(
        resource_stack2_option)

    #2.query resource stack
    os.environ['ZSTACK_BUILT_IN_HTTP_SERVER_IP'] = mevoco1_ip
    test_util.test_dsc('Query resource stack in mevoco1')
    cond = res_ops.gen_query_conditions('uuid', '=', resource_stack1.uuid)
    resource_stack1_queried = res_ops.query_resource(res_ops.RESOURCE_STACK,
                                                     cond)

    cond = res_ops.gen_query_conditions('name', '=', 'IPsec-STACK')
    ipsec1_queried = res_ops.query_resource(res_ops.IPSEC_CONNECTION, cond)

    if len(resource_stack1_queried) == 0:
        test_util.test_fail("Fail to query resource stack")

    if resource_stack1_queried[0].status == 'Created':
        if len(ipsec1_queried) == 0:
            test_util.test_fail(
                "Fail to create ipsec connection when resource stack status is Created"
            )

    os.environ['ZSTACK_BUILT_IN_HTTP_SERVER_IP'] = mevoco2_ip
    test_util.test_dsc('Query resource stack in mevoco2')
    cond = res_ops.gen_query_conditions('uuid', '=', resource_stack2.uuid)
    resource_stack2_queried = res_ops.query_resource(res_ops.RESOURCE_STACK,
                                                     cond)

    cond = res_ops.gen_query_conditions('name', '=', 'IPsec-STACK')
    ipsec2_queried = res_ops.query_resource(res_ops.IPSEC_CONNECTION, cond)

    if len(resource_stack2_queried) == 0:
        test_util.test_fail("Fail to query resource stack")

    if resource_stack2_queried[0].status == 'Created':
        if len(ipsec2_queried) == 0:
            test_util.test_fail(
                "Fail to create ipsec connection when resource stack status is Created"
            )

    #3.get resource from resource stack
    os.environ['ZSTACK_BUILT_IN_HTTP_SERVER_IP'] = mevoco1_ip
    test_util.test_dsc('Get resource from resource stack in mevoco1')
    resource1 = resource_stack_ops.get_resource_from_resource_stack(
        resource_stack1.uuid)
    if resource1 == None or len(resource1) != 1:
        test_util.test_fail("Fail to get resource from resource_stack")

    os.environ['ZSTACK_BUILT_IN_HTTP_SERVER_IP'] = mevoco2_ip
    test_util.test_dsc('Get resource from resource stack in mevoco2')
    resource2 = resource_stack_ops.get_resource_from_resource_stack(
        resource_stack2.uuid)
    if resource2 == None or len(resource1) != 1:
        test_util.test_fail("Fail to get resource from resource_stack")

    #4.query event from resource stack
    os.environ['ZSTACK_BUILT_IN_HTTP_SERVER_IP'] = mevoco1_ip
    test_util.test_dsc('Get resource from resource stack in mevoco1')
    cond = res_ops.gen_query_conditions('stackUuid', '=', resource_stack1.uuid)
    event1 = res_ops.query_event_from_resource_stack(cond)
    if event1 == None or len(event1) != 2:
        test_util.test_fail("Fail to get event from resource_stack")

    os.environ['ZSTACK_BUILT_IN_HTTP_SERVER_IP'] = mevoco2_ip
    test_util.test_dsc('Get resource from resource stack in mevoco2')
    cond = res_ops.gen_query_conditions('stackUuid', '=', resource_stack2.uuid)
    event2 = res_ops.query_event_from_resource_stack(cond)
    if event2 == None or len(event2) != 2:
        test_util.test_fail("Fail to get event from resource_stack")

    #5.delete resource stack
    os.environ['ZSTACK_BUILT_IN_HTTP_SERVER_IP'] = mevoco1_ip
    test_util.test_dsc('Delete resource stack in mevoco1')
    resource_stack_ops.delete_resource_stack(resource_stack1.uuid)
    cond = res_ops.gen_query_conditions('uuid', '=', resource_stack1.uuid)
    resource_stack1_queried = res_ops.query_resource(res_ops.RESOURCE_STACK,
                                                     cond)
    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-IPSEC1')
    ipsec1_queried = res_ops.query_resource(res_ops.IPSEC_CONNECTION, cond)
    if len(resource_stack1_queried) != 0:
        test_util.test_fail("Fail to delete resource stack")
    elif len(ipsec1_queried) != 0:
        test_util.test_fail(
            "Fail to delete ipsec connection when resource stack is deleted")

    os.environ['ZSTACK_BUILT_IN_HTTP_SERVER_IP'] = mevoco2_ip
    test_util.test_dsc('Delete resource stack in mevoco2')
    resource_stack_ops.delete_resource_stack(resource_stack2.uuid)
    cond = res_ops.gen_query_conditions('uuid', '=', resource_stack2.uuid)
    resource_stack2_queried = res_ops.query_resource(res_ops.RESOURCE_STACK,
                                                     cond)
    cond = res_ops.gen_query_conditions('name', '=', 'Create_STACK-IPSEC2')
    ipsec2_queried = res_ops.query_resource(res_ops.IPSEC_CONNECTION, cond)
    if len(resource_stack2_queried) != 0:
        test_util.test_fail("Fail to delete resource stack")
    elif len(ipsec2_queried) != 0:
        test_util.test_fail(
            "Fail to delete ipsec connection when resource stack is deleted")

    test_util.test_pass('Create IPsec Resource Stack Test Success')