def create_segment(self, context, segment): """Create a segment.""" segment = segment['segment'] segment_id = segment.get('id') or uuidutils.generate_uuid() with context.session.begin(subtransactions=True): network_id = segment['network_id'] # FIXME couldn't use constants because of a circular import problem physical_network = segment['physical_network'] if physical_network == constants.ATTR_NOT_SPECIFIED: physical_network = None network_type = segment['network_type'] segmentation_id = segment['segmentation_id'] if segmentation_id == constants.ATTR_NOT_SPECIFIED: segmentation_id = None args = { 'id': segment_id, 'network_id': network_id, db.PHYSICAL_NETWORK: physical_network, db.NETWORK_TYPE: network_type, db.SEGMENTATION_ID: segmentation_id } new_segment = db.NetworkSegment(**args) context.session.add(new_segment) return self._make_segment_dict(new_segment)
def create_segment(self, context, segment): """Create a segment.""" segment = segment['segment'] segment_id = segment.get('id') or uuidutils.generate_uuid() with context.session.begin(subtransactions=True): network_id = segment['network_id'] physical_network = segment[extension.PHYSICAL_NETWORK] if physical_network == constants.ATTR_NOT_SPECIFIED: physical_network = None network_type = segment[extension.NETWORK_TYPE] segmentation_id = segment[extension.SEGMENTATION_ID] if segmentation_id == constants.ATTR_NOT_SPECIFIED: segmentation_id = None args = {'id': segment_id, 'network_id': network_id, db.PHYSICAL_NETWORK: physical_network, db.NETWORK_TYPE: network_type, db.SEGMENTATION_ID: segmentation_id} new_segment = db.NetworkSegment(**args) try: context.session.add(new_segment) context.session.flush([new_segment]) except db_exc.DBReferenceError: raise n_exc.NetworkNotFound(net_id=network_id) registry.notify(resources.SEGMENT, events.PRECOMMIT_CREATE, self, context=context, segment=new_segment) return self._make_segment_dict(new_segment)
def _create_segment_db(self, context, segment_id, segment): with context.session.begin(subtransactions=True): network_id = segment['network_id'] physical_network = segment[extension.PHYSICAL_NETWORK] if physical_network == constants.ATTR_NOT_SPECIFIED: physical_network = None network_type = segment[extension.NETWORK_TYPE] segmentation_id = segment[extension.SEGMENTATION_ID] if segmentation_id == constants.ATTR_NOT_SPECIFIED: segmentation_id = None name = segment['name'] if name == constants.ATTR_NOT_SPECIFIED: name = None description = segment['description'] if description == constants.ATTR_NOT_SPECIFIED: description = None args = { 'id': segment_id, 'network_id': network_id, 'name': name, 'description': description, db.PHYSICAL_NETWORK: physical_network, db.NETWORK_TYPE: network_type, db.SEGMENTATION_ID: segmentation_id } # Calculate the index of segment segment_index = 0 segments = self.get_segments(context, filters={'network_id': [network_id]}, fields=['segment_index'], sorts=[('segment_index', True)]) if segments: # NOTE(xiaohhui): The new index is the last index + 1, this # may casue discontinuous segment_index. But segment_index # can functionally work as the order index for segments. segment_index = (segments[-1].get('segment_index') + 1) args['segment_index'] = segment_index new_segment = db.NetworkSegment(**args) context.session.add(new_segment) # Do some preliminary operations before commiting the segment to db registry.notify(resources.SEGMENT, events.PRECOMMIT_CREATE, self, context=context, segment=new_segment) return new_segment