def get_network_id_by_cidr(self, context, cidr, project_id): """ Grabs Quantum network UUID based on IPv4 CIDR. """ admin_context = context.elevated() network = db.network_get_by_cidr(admin_context, cidr) if not network: raise Exception(_("No network with fixed_range = %s" % cidr)) return network["uuid"]
def get_network_id_by_cidr(self, context, cidr, project_id): """ Grabs Quantum network UUID based on IPv4 CIDR. """ admin_context = context.elevated() network = db.network_get_by_cidr(admin_context, cidr) if not network: raise Exception(_("No network with fixed_range = %s" % cidr)) return network['uuid']
def _create(self, req, body): context = req.environ['nova.context'] context = context.elevated() print "context!!" print context.to_dict() vals = body['network'] name = vals['name'] size = vals['size'] project_id=str(req.environ['HTTP_X_TENANT_ID']) print FLAGS.network_manager cidr = self.get_new_cidr(context, size) print cidr print"!!!!!!!!!!!!!!!!strat creating" self.create_network(context=context, label=name, fixed_range_v4=cidr, num_networks=1, network_size=size, multi_host=None, vlan_start=None, vpn_start=None, fixed_range_v6=None, gateway=None, gateway_v6=None, bridge=None, bridge_interface=None, dns1=None, dns2=None, project_id=project_id, priority=None, uuid=None, fixed_cidr=None) print cidr db_net = db.network_get_by_cidr(context, cidr) net = dict(db_net.iteritems()) ret_net={} ret_net['network']={'id':net['uuid'],'name':net['label'],'cidr':net['cidr']} return ret_net
def delete_network(self, context, fixed_range, require_disassociated=True): network = db.network_get_by_cidr(context, fixed_range) if require_disassociated and network.project_id is not None: raise ValueError(_("Network must be disassociated from project %s" " before delete" % network.project_id)) db.network_delete_safe(context, network.id)
def create_networks(self, context, cidr, num_networks, network_size, cidr_v6, vlan_start, vpn_start, **kwargs): """Create networks based on parameters.""" # Check that num_networks + vlan_start is not > 4094, fixes lp708025 if num_networks + vlan_start > 4094: raise ValueError( _('The sum between the number of networks and' ' the vlan start cannot be greater' ' than 4094')) fixed_net = IPy.IP(cidr) if fixed_net.len() < num_networks * network_size: raise ValueError( _('The network range is not big enough to fit ' '%(num_networks)s. Network size is %(network_size)s' % locals())) fixed_net_v6 = IPy.IP(cidr_v6) network_size_v6 = 1 << 64 significant_bits_v6 = 64 for index in range(num_networks): vlan = vlan_start + index start = index * network_size start_v6 = index * network_size_v6 significant_bits = 32 - int(math.log(network_size, 2)) cidr = "%s/%s" % (fixed_net[start], significant_bits) project_net = IPy.IP(cidr) net = {} net['cidr'] = cidr net['netmask'] = str(project_net.netmask()) net['gateway'] = str(project_net[1]) net['broadcast'] = str(project_net.broadcast()) net['vpn_private_address'] = str(project_net[2]) net['dhcp_start'] = str(project_net[3]) net['vlan'] = vlan net['bridge'] = 'br%s' % vlan if (FLAGS.use_ipv6): cidr_v6 = "%s/%s" % (fixed_net_v6[start_v6], significant_bits_v6) net['cidr_v6'] = cidr_v6 # NOTE(vish): This makes ports unique accross the cloud, a more # robust solution would be to make them unique per ip net['vpn_public_port'] = vpn_start + index network_ref = None try: network_ref = db.network_get_by_cidr(context, cidr) except exception.NotFound: pass if network_ref is not None: raise ValueError( _('Network with cidr %s already exists' % cidr)) network_ref = self.db.network_create_safe(context, net) if network_ref: self._create_fixed_ips(context, network_ref['id'])
def create_networks(self, context, cidr, num_networks, network_size, cidr_v6, vlan_start, vpn_start, **kwargs): """Create networks based on parameters.""" # Check that num_networks + vlan_start is not > 4094, fixes lp708025 if num_networks + vlan_start > 4094: raise ValueError(_('The sum between the number of networks and' ' the vlan start cannot be greater' ' than 4094')) fixed_net = IPy.IP(cidr) if fixed_net.len() < num_networks * network_size: raise ValueError(_('The network range is not big enough to fit ' '%(num_networks)s. Network size is %(network_size)s' % locals())) fixed_net_v6 = IPy.IP(cidr_v6) network_size_v6 = 1 << 64 significant_bits_v6 = 64 for index in range(num_networks): vlan = vlan_start + index start = index * network_size start_v6 = index * network_size_v6 significant_bits = 32 - int(math.log(network_size, 2)) cidr = "%s/%s" % (fixed_net[start], significant_bits) project_net = IPy.IP(cidr) net = {} net['cidr'] = cidr net['netmask'] = str(project_net.netmask()) net['gateway'] = str(project_net[1]) net['broadcast'] = str(project_net.broadcast()) net['vpn_private_address'] = str(project_net[2]) net['dhcp_start'] = str(project_net[3]) net['vlan'] = vlan net['bridge'] = 'br%s' % vlan if(FLAGS.use_ipv6): cidr_v6 = '%s/%s' % (fixed_net_v6[start_v6], significant_bits_v6) net['cidr_v6'] = cidr_v6 # NOTE(vish): This makes ports unique accross the cloud, a more # robust solution would be to make them unique per ip net['vpn_public_port'] = vpn_start + index network_ref = None try: network_ref = db.network_get_by_cidr(context, cidr) except exception.NotFound: pass if network_ref is not None: raise ValueError(_('Network with cidr %s already exists' % cidr)) network_ref = self.db.network_create_safe(context, net) if network_ref: self._create_fixed_ips(context, network_ref['id'])
def modify(self, fixed_range, project=None, host=None, dis_project=None, dis_host=None): """Associate/Disassociate Network with Project and/or Host arguments: network project host leave any field blank to ignore it """ admin_context = context.get_admin_context() network = db.network_get_by_cidr(admin_context, fixed_range) net = {} # User can choose the following actions each for project and host. # 1) Associate (set not None value given by project/host parameter) # 2) Disassociate (set None by disassociate parameter) # 3) Keep unchanged (project/host key is not added to 'net') if dis_project: net['project_id'] = None if dis_host: net['host'] = None # The --disassociate-X are boolean options, but if they user # mistakenly provides a value, it will be used as a positional argument # and be erroneously interepreted as some other parameter (e.g. # a project instead of host value). The safest thing to do is error-out # with a message indicating that there is probably a problem with # how the disassociate modifications are being used. if dis_project or dis_host: if project or host: error_msg = "ERROR: Unexpected arguments provided. Please " \ "use separate commands." print(error_msg) return (1) db.network_update(admin_context, network['id'], net) return if project: net['project_id'] = project if host: net['host'] = host db.network_update(admin_context, network['id'], net)
def modify(self, fixed_range, project=None, host=None, dis_project=None, dis_host=None): """Associate/Disassociate Network with Project and/or Host arguments: network project host leave any field blank to ignore it """ admin_context = context.get_admin_context() network = db.network_get_by_cidr(admin_context, fixed_range) net = {} #User can choose the following actions each for project and host. #1) Associate (set not None value given by project/host parameter) #2) Disassociate (set None by disassociate parameter) #3) Keep unchanged (project/host key is not added to 'net') if dis_project: net['project_id'] = None if dis_host: net['host'] = None # The --disassociate-X are boolean options, but if they user # mistakenly provides a value, it will be used as a positional argument # and be erroneously interepreted as some other parameter (e.g. # a project instead of host value). The safest thing to do is error-out # with a message indicating that there is probably a problem with # how the disassociate modifications are being used. if dis_project or dis_host: if project or host: error_msg = "ERROR: Unexpected arguments provided. Please " \ "use separate commands." print(error_msg) return(1) db.network_update(admin_context, network['id'], net) return if project: net['project_id'] = project if host: net['host'] = host db.network_update(admin_context, network['id'], net)
def get_by_cidr(cls, context, cidr): db_network = db.network_get_by_cidr(context, cidr) return cls._from_db_object(context, cls(), db_network)