def test_get_create_element_routing_valid(self): pod = self._prepare_pod() resource_id = 'fake_resource_id' _type = 'fake_resource' ele = {'id': resource_id} body = {'name': resource_id} lock_handle.get_or_create_element(self.t_ctx, self.q_ctx, self.project_id, pod, ele, _type, body, list_resource, create_resource) is_new, b_resource_id = lock_handle.get_or_create_element( self.t_ctx, self.q_ctx, self.project_id, pod, ele, _type, body, list_resource, create_resource) self.assertFalse(is_new) self.assertEqual(b_resource_id, RES[0]['id'])
def test_get_create_element_routing_valid(self): pod = self._prepare_pod() resource_id = 'fake_resource_id' _type = 'fake_resource' ele = {'id': resource_id} body = {'name': resource_id} lock_handle.get_or_create_element( self.t_ctx, self.q_ctx, self.project_id, pod, ele, _type, body, list_resource, create_resource) is_new, b_resource_id = lock_handle.get_or_create_element( self.t_ctx, self.q_ctx, self.project_id, pod, ele, _type, body, list_resource, create_resource) self.assertFalse(is_new) self.assertEqual(b_resource_id, RES[0]['id'])
def prepare_bottom_element(self, t_ctx, project_id, pod, ele, _type, body): """Get or create bottom networking resource based on top resource :param t_ctx: tricircle context :param project_id: project id :param pod: dict of bottom pod :param ele: dict of top resource :param _type: type of the resource :param body: request body to create resource :return: boolean value indicating whether the resource is newly created or already exists and id of the resource """ def list_resources(t_ctx_, q_ctx, pod_, ele_, _type_): client = self._get_client(pod_['pod_name']) if _type_ == t_constants.RT_NETWORK: value = utils.get_bottom_network_name(ele_) else: value = ele_['id'] return client.list_resources(_type_, t_ctx_, [{'key': 'name', 'comparator': 'eq', 'value': value}]) def create_resources(t_ctx_, q_ctx, pod_, body_, _type_): client = self._get_client(pod_['pod_name']) return client.create_resources(_type_, t_ctx_, body_) return t_lock.get_or_create_element( t_ctx, None, # we don't need neutron context, so pass None project_id, pod, ele, _type, body, list_resources, create_resources)
def prepare_bottom_element(self, t_ctx, project_id, pod, ele, _type, body): """Get or create bottom networking resource based on top resource :param t_ctx: tricircle context :param project_id: project id :param pod: dict of bottom pod :param ele: dict of top resource :param _type: type of the resource :param body: request body to create resource :return: boolean value indicating whether the resource is newly created or already exists and id of the resource """ def list_resources(t_ctx_, q_ctx, pod_, ele_, _type_): client = self._get_client(pod_['region_name']) if _type_ == t_constants.RT_NS_ROUTER: _type_ = t_constants.RT_ROUTER value = t_constants.ns_router_name % ele_['id'] elif _type_ == t_constants.RT_SD_PORT: _type_ = t_constants.RT_PORT value = t_constants.shadow_port_name % ele_['id'] elif _type_ == t_constants.RT_NETWORK: value = utils.get_bottom_network_name(ele_) elif _type_ == t_constants.RT_SD_PORT: _type_ = t_constants.RT_PORT value = t_constants.shadow_port_name % ele_['id'] else: value = ele_['id'] return client.list_resources(_type_, t_ctx_, [{ 'key': 'name', 'comparator': 'eq', 'value': value }]) def create_resources(t_ctx_, q_ctx, pod_, body_, _type_): if _type_ == t_constants.RT_NS_ROUTER: _type_ = t_constants.RT_ROUTER elif _type_ == t_constants.RT_SD_PORT: _type_ = t_constants.RT_PORT client = self._get_client(pod_['region_name']) if _type_ == t_constants.RT_SD_PORT: _type_ = t_constants.RT_PORT return client.create_resources(_type_, t_ctx_, body_) return t_lock.get_or_create_element( t_ctx, None, # we don't need neutron context, so pass None project_id, pod, ele, _type, body, list_resources, create_resources)
def _prepare_neutron_element(self, context, pod, ele, _type, body): def list_resources(t_ctx, q_ctx, pod_, _id_, _type_): client = self._get_client(pod_['pod_name']) return client.list_resources(_type_, t_ctx, [{'key': 'name', 'comparator': 'eq', 'value': _id_}]) def create_resources(t_ctx, q_ctx, pod_, body_, _type_): client = self._get_client(pod_['pod_name']) return client.create_resources(_type_, t_ctx, body_) return t_lock.get_or_create_element( context, None, # we don't need neutron context, so pass None self.project_id, pod, ele, _type, body, list_resources, create_resources)
def test_get_create_element_routing_expire_resource_missing(self): pod = self._prepare_pod() resource_id = 'fake_resource_id' _type = 'fake_resource' ele = {'id': resource_id} body = {'name': resource_id} routing = api.create_resource_mapping(self.t_ctx, resource_id, None, pod['pod_id'], self.project_id, _type) api.update_resource_routing(self.t_ctx, routing['id'], {'created_at': constants.expire_time, 'updated_at': constants.expire_time}) is_new, b_resource_id = lock_handle.get_or_create_element( self.t_ctx, self.q_ctx, self.project_id, pod, ele, _type, body, list_resource, create_resource) self.assertTrue(is_new) self.assertEqual(b_resource_id, RES[0]['id'])
def _prepare_top_element_by_client(self, t_ctx, q_ctx, project_id, pod, ele, _type, body): def list_resources(t_ctx_, q_ctx_, pod_, ele_, _type_): client = self._get_client() return client.list_resources(_type_, t_ctx_, [{'key': 'name', 'comparator': 'eq', 'value': ele_['id']}]) def create_resources(t_ctx_, q_ctx_, pod_, body_, _type_): client = self._get_client() return client.create_resources(_type_, t_ctx_, body_) assert _type == 'port' # currently only top port is possible to be created via client, other # top resources should be created directly by plugin return t_lock.get_or_create_element( t_ctx, q_ctx, project_id, pod, ele, _type, body, list_resources, create_resources)
def _prepare_neutron_element(self, context, pod, ele, _type, body): def list_resources(t_ctx, q_ctx, pod_, ele_, _type_): client = self._get_client(pod_['pod_name']) if _type_ == constants.RT_NETWORK: value = utils.get_bottom_network_name(ele_) else: value = ele_['id'] return client.list_resources( _type_, t_ctx, [{'key': 'name', 'comparator': 'eq', 'value': value}]) def create_resources(t_ctx, q_ctx, pod_, body_, _type_): client = self._get_client(pod_['pod_name']) return client.create_resources(_type_, t_ctx, body_) return t_lock.get_or_create_element( context, None, # we don't need neutron context, so pass None self.project_id, pod, ele, _type, body, list_resources, create_resources)
def _prepare_top_element_by_call(self, t_ctx, q_ctx, project_id, pod, ele, _type, body): def list_resources(t_ctx_, q_ctx_, pod_, ele_, _type_): return getattr(super(self.call_obj.__class__, self.call_obj), 'get_%ss' % _type_)(q_ctx_, filters={'name': [ele_['id']]}) def create_resources(t_ctx_, q_ctx_, pod_, body_, _type_): if _type_ == t_constants.RT_NETWORK: # for network, we call TricirclePlugin's own create_network to # handle network segment return self.call_obj.create_network(q_ctx_, body_) else: return getattr(super(self.call_obj.__class__, self.call_obj), 'create_%s' % _type_)(q_ctx_, body_) return t_lock.get_or_create_element( t_ctx, q_ctx, project_id, pod, ele, _type, body, list_resources, create_resources)
def test_get_create_element_routing_expire_resource_missing(self): pod = self._prepare_pod() resource_id = 'fake_resource_id' _type = 'fake_resource' ele = {'id': resource_id} body = {'name': resource_id} routing = api.create_resource_mapping(self.t_ctx, resource_id, None, pod['pod_id'], self.project_id, _type) api.update_resource_routing( self.t_ctx, routing['id'], { 'created_at': constants.expire_time, 'updated_at': constants.expire_time }) is_new, b_resource_id = lock_handle.get_or_create_element( self.t_ctx, self.q_ctx, self.project_id, pod, ele, _type, body, list_resource, create_resource) self.assertTrue(is_new) self.assertEqual(b_resource_id, RES[0]['id'])