def create()

in tools/marvin/marvin/lib/base.py [0:0]


    def create(cls, apiclient, services, templateid=None, accountid=None,
               domainid=None, zoneid=None, networkids=None,
               serviceofferingid=None, securitygroupids=None,
               projectid=None, startvm=None, diskofferingid=None,
               affinitygroupnames=None, affinitygroupids=None, group=None,
               hostid=None, clusterid=None, keypair=None, ipaddress=None, mode='default',
               method='GET', hypervisor=None, customcpunumber=None,
               customcpuspeed=None, custommemory=None, rootdisksize=None,
               rootdiskcontroller=None, vpcid=None, macaddress=None, datadisktemplate_diskoffering_list={},
               properties=None, nicnetworklist=None, bootmode=None, boottype=None, dynamicscalingenabled=None,
               userdataid=None, userdatadetails=None, extraconfig=None, size=None, overridediskofferingid=None):
        """Create the instance"""

        cmd = deployVirtualMachine.deployVirtualMachineCmd()

        if serviceofferingid:
            cmd.serviceofferingid = serviceofferingid
        elif "serviceoffering" in services:
            cmd.serviceofferingid = services["serviceoffering"]

        if overridediskofferingid:
            cmd.overridediskofferingid = overridediskofferingid

        if zoneid:
            cmd.zoneid = zoneid
        elif "zoneid" in services:
            cmd.zoneid = services["zoneid"]

        if hypervisor:
            cmd.hypervisor = hypervisor

        if "displayname" in services:
            cmd.displayname = services["displayname"]

        if "name" in services:
            cmd.name = services["name"]

        if accountid:
            cmd.account = accountid
        elif "account" in services:
            cmd.account = services["account"]

        if domainid:
            cmd.domainid = domainid
        elif "domainid" in services:
            cmd.domainid = services["domainid"]

        if networkids:
            cmd.networkids = networkids
            allow_egress = False
        elif "networkids" in services:
            cmd.networkids = services["networkids"]
            allow_egress = False
        else:
            # When no networkids are passed, network
            # is created using the "defaultOfferingWithSourceNAT"
            # which has an egress policy of DENY. But guests in tests
            # need access to test network connectivity
            allow_egress = True

        if templateid:
            cmd.templateid = templateid
        elif "template" in services:
            cmd.templateid = services["template"]

        if diskofferingid:
            cmd.diskofferingid = diskofferingid
        elif "diskoffering" in services:
            cmd.diskofferingid = services["diskoffering"]

        if keypair:
            cmd.keypair = keypair
        elif "keypair" in services:
            cmd.keypair = services["keypair"]

        if ipaddress:
            cmd.ipaddress = ipaddress
        elif "ipaddress" in services:
            cmd.ipaddress = services["ipaddress"]

        if securitygroupids:
            cmd.securitygroupids = [str(sg_id) for sg_id in securitygroupids]

        if "affinitygroupnames" in services:
            cmd.affinitygroupnames = services["affinitygroupnames"]
        elif affinitygroupnames:
            cmd.affinitygroupnames = affinitygroupnames

        if affinitygroupids:
            cmd.affinitygroupids = affinitygroupids

        if projectid:
            cmd.projectid = projectid

        if startvm is not None:
            cmd.startvm = startvm

        if hostid:
            cmd.hostid = hostid

        if clusterid:
            cmd.clusterid = clusterid

        if "userdata" in services:
            cmd.userdata = base64.urlsafe_b64encode(services["userdata"].encode()).decode()

        if userdataid is not None:
            cmd.userdataid = userdataid

        if userdatadetails is not None:
            cmd.userdatadetails = userdatadetails

        if "dhcpoptionsnetworklist" in services:
            cmd.dhcpoptionsnetworklist = services["dhcpoptionsnetworklist"]

        if dynamicscalingenabled is not None:
            cmd.dynamicscalingenabled = dynamicscalingenabled

        cmd.details = [{}]

        if customcpunumber:
            cmd.details[0]["cpuNumber"] = customcpunumber

        if customcpuspeed:
            cmd.details[0]["cpuSpeed"] = customcpuspeed

        if custommemory:
            cmd.details[0]["memory"] = custommemory

        if not rootdisksize is None and rootdisksize >= 0:
            cmd.details[0]["rootdisksize"] = rootdisksize

        if rootdiskcontroller:
            cmd.details[0]["rootDiskController"] = rootdiskcontroller

        if size:
            cmd.size = size
        elif "size" in services:
            cmd.size = services["size"]

        if group:
            cmd.group = group

        cmd.datadisktemplatetodiskofferinglist = []
        for datadisktemplate, diskoffering in list(datadisktemplate_diskoffering_list.items()):
            cmd.datadisktemplatetodiskofferinglist.append({
                'datadisktemplateid': datadisktemplate,
                'diskofferingid': diskoffering
            })

        # program default access to ssh
        if mode.lower() == 'basic':
            cls.ssh_access_group(apiclient, cmd)

        if macaddress:
            cmd.macaddress = macaddress
        elif macaddress in services:
            cmd.macaddress = services["macaddress"]

        if properties:
            cmd.properties = properties

        if nicnetworklist:
            cmd.nicnetworklist = nicnetworklist

        if bootmode:
            cmd.bootmode = bootmode

        if boottype:
            cmd.boottype = boottype

        if extraconfig:
            cmd.extraconfig = extraconfig

        virtual_machine = apiclient.deployVirtualMachine(cmd, method=method)

        if 'password' in list(virtual_machine.__dict__.keys()):
            if virtual_machine.password:
                services['password'] = virtual_machine.password

        virtual_machine.ssh_ip = virtual_machine.nic[0].ipaddress
        if startvm is False:
            virtual_machine.public_ip = virtual_machine.nic[0].ipaddress
            return VirtualMachine(virtual_machine.__dict__, services)

        # program ssh access over NAT via PF
        retries = 5
        interval = 30
        while retries > 0:
            time.sleep(interval)
            try:
                if mode.lower() == 'advanced':
                    cls.access_ssh_over_nat(
                        apiclient,
                        services,
                        virtual_machine,
                        allow_egress=allow_egress,
                        networkid=cmd.networkids[0] if cmd.networkids else None,
                        vpcid=vpcid)
                elif mode.lower() == 'basic':
                    if virtual_machine.publicip is not None:
                        # EIP/ELB (netscaler) enabled zone
                        vm_ssh_ip = virtual_machine.publicip
                    else:
                        # regular basic zone with security group
                        vm_ssh_ip = virtual_machine.nic[0].ipaddress
                    virtual_machine.ssh_ip = vm_ssh_ip
                    virtual_machine.public_ip = vm_ssh_ip
                break
            except Exception as e:
                if retries >= 0:
                    retries = retries - 1
                    continue
                raise Exception(
                    "The following exception appeared while programming ssh access - %s" % e)

        return VirtualMachine(virtual_machine.__dict__, services)