def populate_test_domain(domain, number_of_zones, number_of_subnets_per_zone, number_of_vports_per_subnet): """ Populate a domain with test data Args: domain (vsdk.NUDomain | vsdk.NUDomainTemplate): base domain to populate number_of_zones (int): number of desired zones number_of_subnets_per_zone (int): number of desired subnets per zone number_of_vports_per_subnet (int): number of desired vports per subnet (only available if domain is not a template) """ # check if the domain is a template # if so use children template classes instead of instances is_template = domain.is_template() zone_class = vsdk.NUZoneTemplate if is_template else vsdk.NUZone subnet_class = vsdk.NUSubnetTemplate if is_template else vsdk.NUSubnet # generate a network and subnets network = ipaddress.ip_network(u'10.0.0.0/8') subnets = network.subnets(new_prefix=24) # create zones for i in range(0, number_of_zones): zone = zone_class(name="Zone %d" % i) domain.create_child(zone) domain.add_child(zone) #creates subnets for j in range(0, number_of_subnets_per_zone): # pull a subnet and get information about it subnetwork = subnets.next() ip = "%s" % subnetwork.network_address gw = "%s" % subnetwork.hosts().next() nm = "%s" % subnetwork.netmask subnet = subnet_class(name="Subnet %d %d" % (i, j), address=ip, netmask=nm, gateway=gw) zone.create_child(subnet) zone.add_child(subnet) # if the given domain is a template, we stop if is_template: break # Otherwise we create the VPorts for k in range(0, number_of_vports_per_subnet): vport = vsdk.NUVPort(name="VPort %d-%d-%d" % (i, j, k), type="VM", address_spoofing="INHERITED", multicast="INHERITED") subnet.create_child(vport) subnet.add_child(vport)
def deactivate(self): """ deactivate VM :return: """ try: vm = self.session.user.vms.get_first(filter='UUID== "%s"' % self.vm_uuid) if not vm: LOG.error("VM not found: %s" % self.vm_uuid) for interface in vm.vm_interfaces.get(): if interface.vport_name == self.vport_name: vport = vsdk.NUVPort(id=interface.vport_id) # vport.fetch() vm.delete() vport.delete() except Exception as e: LOG.critical("Error on vm and vport deletion %s" % str(e)) return True
def build_tenant(): if request.method == 'POST': # Get form variable and make them session variable enterprise = request.form.get('enterprise') domain_new = request.form.get('domain') number_of_zones = request.form.get('zones') number_of_subnets_per_zone = request.form.get('subs') number_of_vports_per_subnet = request.form.get('vports') # set variable as integers number_of_zones = int(number_of_zones) number_of_subnets_per_zone = int(number_of_subnets_per_zone) number_of_vports_per_subnet = int(number_of_vports_per_subnet) if (' ' in enterprise): flash('Spaces are not allowed in Tenant name') return render_template('newtenant.html') if (' ' in domain_new): flash('Spaces are not allowed in Domain name') return render_template('newtenant.html') # Copy from an existing 3-Tier domain # Rename to the new domain name domain = nuage_user.domains.get_first( filter='name == "Default Domain"') domain.fetch() job = vsdk.NUJob(command='EXPORT') # Creating export job for the Main VSPK Domain domain.create_child(job) # Printing the export result while True: job.fetch() if job.status == 'SUCCESS': # Copy domain details to new Enterprise enterprise = vsdk.NUEnterprise(name=enterprise) nuage_user.create_child(enterprise) job.result['parameters']['domain'][0]['modifyableAttributes'][ 'name']['value'] = domain_new # Using the export copy of the domain details from above import_job = vsdk.NUJob(command='IMPORT', parameters=job.result) enterprise.create_child(import_job) break if job.status == 'FAILED': return render_template('fail_domain.html', var=enterprise) time.sleep(1) # Verify the import job has finished successfully while True: import_job.fetch() if import_job.status == 'SUCCESS': # get the new domain and provision it dom = nuage_user.domains.get_first(filter="name == '%s'" % domain_new) dom.fetch() # Adjust these numbers as required for differnet use cases is_template = dom.is_template() zone_class = vsdk.NUZoneTemplate if is_template else vsdk.NUZone subnet_class = vsdk.NUSubnetTemplate if is_template else vsdk.NUSubnet # generate a network and subnets network = ipaddress.ip_network(u'10.0.0.0/8') subnets = network.subnets(new_prefix=24) # create zones for i in range(0, number_of_zones): zone = zone_class(name=enterprise.name + "Zone%d" % i) dom.create_child(zone) dom.add_child(zone) #creates subnets for j in range(0, number_of_subnets_per_zone): # pull a subnet and get information about it subnetwork = subnets.next() ip = "%s" % subnetwork.network_address gw = "%s" % subnetwork.hosts().next() nm = "%s" % subnetwork.netmask subnet = subnet_class(name="Subnet%d%d" % (i, j), address=ip, netmask=nm, gateway=gw) zone.create_child(subnet) zone.add_child(subnet) # if the given domain is a template, we stop if is_template: break # Otherwise we create the VPorts for k in range(0, number_of_vports_per_subnet): vport = vsdk.NUVPort(name="VPort%d-%d-%d" % (i, j, k), type="VM", address_spoofing="INHERITED", multicast="INHERITED") subnet.create_child(vport) subnet.add_child(vport) # Now add the default ACCESS Contol Lists for Ingress/egress # Creating the job to begin the policy changes job = vsdk.NUJob(command='BEGIN_POLICY_CHANGES') dom.create_child(job) # wait for the job to finish while True: job.fetch() if job.status == 'SUCCESS': break if job.status == 'FAILED': return render_template('fail_acls.html', domain=domain) break time.sleep(1) # can be done with a while loop # Creating a new Ingress ACL ingressacl = vsdk.NUIngressACLTemplate( name='Middle Ingress ACL', priority_type= 'NONE', # Possible values: TOP, NONE, BOTTOM (domain only accepts NONE) priority=100, default_allow_non_ip=True, default_allow_ip=True, allow_l2_address_spoof=False, active=True) dom.create_child(ingressacl) # Creating a new egressgress ACL # TODO find out what the real element names are egressacl = vsdk.NUEgressACLTemplate( name='Middle Egress ACL', priority_type= 'NONE', # Possible values: TOP, NONE, BOTTOM (domain only accepts NONE) priority=100, default_allow_non_ip=True, default_allow_ip=True, allow_l2_address_spoof=False, active=True) dom.create_child(egressacl) # Creating a new Ingress ACL rule to allow database connectivity # from the Web-Tier Zone to the DB-Tier Zone from_network = dom.zones.get_first(filter='name == "WEBZone2"') to_network = dom.zones.get_first(filter='name == "DBZone2"') db_ingressacl_rule = vsdk.NUIngressACLEntryTemplate( action='FORWARD', description='Allow MySQL DB connections from WebZone2', ether_type='0x0800', location_type='ZONE', location_id=from_network.id, network_type='ZONE', network_id=to_network.id, protocol='6', source_port='*', destination_port='3306', dscp='*') ingressacl.create_child(db_ingressacl_rule) # Applying the changes to the domain job = vsdk.NUJob(command='APPLY_POLICY_CHANGES') dom.create_child(job) break if import_job.status == 'FAILED': return render_template('fail_domain.html', enterprise=enterprise) break time.sleep(1) return render_template('add_tenant_success.html')
def activate(self): """activate a VM """ try: # get enterprise enterprise = self.session.user.enterprises.get_first( filter='name == "%s"' % self.enterprise_name) if enterprise is None: LOG.critical("Enterprise %s not found, exiting" % enterprise) print "can't find enterprise" return False # get domains enterprise.domains.fetch() domain = next( (domain for domain in enterprise.domains if domain.route_distinguisher == self.route_distinguisher and domain.route_target == self.route_target), None) if domain is None: LOG.info("Domain %s not found, creating domain" % self.domain_name) domain_template = enterprise.domain_templates.get_first( filter='name == "%s"' % self.domain_template_name) domain = vsdk.NUDomain(name=self.domain_name, template_id=domain_template.id) enterprise.create_child(domain) # update domain with the right values domain.tunnel_type = self.tunnel_type if hasattr(self, 'route_distinguisher'): domain.route_distinguisher = self.route_distinguisher if hasattr(self, 'route_target'): domain.route_target = self.route_target # domain.back_haul_route_target = '20000:20000' # domain.back_haul_route_distinguisher = '20000:20000' # domain.back_haul_vnid = '25000' domain.save() # get zone zone = domain.zones.get_first(filter='name == "%s"' % self.zone_name) if zone is None: LOG.info("Zone %s not found, creating zone" % self.zone_name) zone = vsdk.NUZone(name=self.zone_name) domain.create_child(zone) zone.subnets.fetch() subnet = next((subnet for subnet in zone.subnets if subnet.address == self.network_address and subnet.netmask == self.netmask), None) # get subnet # subnet = zone.subnets.get_first(filter='address == "%s"' % self.network_address) if subnet is None: LOG.info("Subnet %s not found, creating subnet" % self.subnet_name) subnet = vsdk.NUSubnet(name=self.subnet_name, address=self.network_address, netmask=self.netmask) zone.create_child(subnet) # get vport vport = subnet.vports.get_first(filter='name == "%s"' % self.vport_name) if vport is None: # create vport LOG.info("Vport %s is not found, creating Vport" % self.vport_name) vport = vsdk.NUVPort( name=self.vport_name, address_spoofing='INHERITED', type='VM', description='Automatically created, do not edit.') subnet.create_child(vport) # get vm vm = self.session.user.fetcher_for_rest_name('vm').get( 'uuid=="%s"' % self.vm_uuid) if not vm: LOG.info("VM %s is not found, creating VM" % self.vm_name) vm = vsdk.NUVM(name=self.vm_name, uuid=self.vm_uuid, interfaces=[{ 'name': self.vm_name, 'VPortID': vport.id, 'MAC': self.vm_mac, 'IPAddress': self.vm_ip }]) self.session.user.create_child(vm) except Exception, e: LOG.error("activating vm failed with exception %s" % str(e)) return False
def activate_by_name(self): """activate vm. Uses names to identify domain, subnet, and vm """ # get enterprise enterprise = self.session.user.enterprises.get_first( filter='name == "%s"' % self.enterprise_name) if enterprise is None: LOG.critical("Enterprise %s not found, exiting" % enterprise) print "can't find enterprise" return False # get domains domain = enterprise.domains.get_first(filter='name == "%s"' % self.domain_name) if domain is None: LOG.info("Domain %s not found, creating domain" % self.domain_name) domain = vsdk.NUDomain( name=self.domain_name, route_target=self.route_target, route_distinguisher=self.route_distinguisher, template_id=self.domain_template_id) enterprise.create_child(domain) # get zone zone = domain.zones.get_first(filter='name == "%s"' % self.zone_name) if zone is None: LOG.info("Zone %s not found, creating zone" % self.zone_name) zone = vsdk.NUZone(name=self.zone_name) domain.create_child(zone) # get subnet subnet = zone.subnets.get_first(filter='name == "%s"' % self.subnet_name) if subnet is None: LOG.info("Subnet %s not found, creating subnet" % self.subnet_name) subnet = vsdk.NUSubnet(name=self.subnet_name, address=self.network_address, netmask=self.netmask) zone.create_child(subnet) # get vport vport = subnet.vports.get_first(filter='name == "%s"' % self.vport_name) if vport is None: # create vport LOG.info("Vport %s is not found, creating Vport" % self.vport_name) vport = vsdk.NUVPort( name=self.vport_name, address_spoofing='INHERITED', type='VM', description='Automatically created, do not edit.') subnet.create_child(vport) # get vm vm = self.session.user.vms.get_first(filter='name == "%s"' % self.vm_name) # vm = self.session.user.fetcher_for_rest_name('vm').get('name=="%s"' % self.vm_name) if not vm: LOG.info("VM %s is not found, creating VM" % self.vm_name) vm = vsdk.NUVM(name=self.vm_name, uuid=self.vm_uuid, interfaces=[{ 'name': self.vm_name, 'VPortID': vport.id, 'MAC': self.vm_mac, 'IPAddress': self.vm_ip }]) self.session.user.create_child(vm) return True
def activate(self): """activate a VM """ # get enterprise enterprise = self.session.user.enterprises.get_first( filter='name == "%s"' % self.enterprise_name) if enterprise is None: logging.critical("Enterprise %s not found, exiting" % enterprise) print "can't find enterprise" return False # get domains enterprise.l2_domains.fetch() domain = next( (domain for domain in enterprise.l2_domains if domain.route_distinguisher == self.route_distinguisher and domain.route_target == self.route_target), None) if domain is None: logging.info("Domain %s not found, creating domain" % self.domain_name) l2_domain_template_id = enterprise.l2_domain_templates.get_first( filter='name == "%s"' % self.l2_domain_template_name) domain = vsdk.NUL2Domain(name=self.domain_name, template_id=l2_domain_template_id) enterprise.create_child(domain) # update domain with the right values domain.route_distinguisher = self.route_distinguisher domain.route_target = self.route_target #domain.address=self.vm_ip domain.netmask = self.netmask domain.vn_id = '5000' domain.dhcp_managed = True domain.save() # get vport vport = domain.vports.get_first(filter='name == "%s"' % self.vport_name) if vport is None: # create vport logging.info("Vport %s is not found, creating Vport" % self.vport_name) vport = vsdk.NUVPort( name=self.vport_name, address_spoofing='INHERITED', type='VM', description='Automatically created, do not edit.') domain.create_child(vport) # get vm vm = self.session.user.fetcher_for_rest_name('vm').get('UUID=="%s"' % self.vm_uuid) if not vm: logging.info("VM %s is not found, creating VM" % self.vm_name) vm = vsdk.NUVM(name=self.vm_name, uuid=self.vm_uuid, interfaces=[{ 'name': self.vm_name, 'VPortID': vport.id, 'MAC': self.vm_mac, 'IPAddress': self.vm_ip }]) self.session.user.create_child(vm) return True