def finish_job(context, job_id, successful, timestamp): status = constants.JS_Success if successful else constants.JS_Fail with context.session.begin(): job_dict = {'status': status, 'timestamp': timestamp, 'extra_id': uuidutils.generate_uuid()} core.update_resource(context, models.Job, job_id, job_dict)
def get_or_create_route(t_ctx, q_ctx, project_id, pod, ele, _type, list_ele_method): # use configuration option later route_expire_threshold = 30 _id = ele['id'] with t_ctx.session.begin(): routes = core.query_resource(t_ctx, models.ResourceRouting, [{ 'key': 'top_id', 'comparator': 'eq', 'value': _id }, { 'key': 'pod_id', 'comparator': 'eq', 'value': pod['pod_id'] }], []) if routes: route = routes[0] if route['bottom_id']: return route, ALL_DONE else: route_time = route['updated_at'] or route['created_at'] current_time = datetime.datetime.utcnow() delta = current_time - route_time if delta.seconds > route_expire_threshold: # NOTE(zhiyuan) cannot directly remove the route, we have # a race here that other worker is updating this route, we # need to check if the corresponding element has been # created by other worker eles = list_ele_method(t_ctx, q_ctx, pod, ele, _type) if eles: route['bottom_id'] = eles[0]['id'] core.update_resource(t_ctx, models.ResourceRouting, route['id'], route) return route, RES_DONE try: core.delete_resource(t_ctx, models.ResourceRouting, route['id']) except db_exc.ResourceNotFound: pass try: # NOTE(zhiyuan) try/except block inside a with block will cause # problem, so move them out of the block and manually handle the # session context t_ctx.session.begin() route = core.create_resource( t_ctx, models.ResourceRouting, { 'top_id': _id, 'pod_id': pod['pod_id'], 'project_id': project_id, 'resource_type': _type }) t_ctx.session.commit() return route, NONE_DONE except db_exc.DBDuplicateEntry: t_ctx.session.rollback() return None, NONE_DONE finally: t_ctx.session.close()
def change_pod_binding(context, pod_binding, pod_id): with context.session.begin(): core.update_resource(context, models.PodBinding, pod_binding['id'], pod_binding) core.create_resource(context, models.PodBinding, {'id': uuidutils.generate_uuid(), 'tenant_id': pod_binding['tenant_id'], 'pod_id': pod_id, 'is_binding': True})
def get_or_create_route(t_ctx, q_ctx, project_id, pod, ele, _type, list_ele_method): # use configuration option later route_expire_threshold = 30 _id = ele['id'] with t_ctx.session.begin(): routes = core.query_resource( t_ctx, models.ResourceRouting, [{'key': 'top_id', 'comparator': 'eq', 'value': _id}, {'key': 'pod_id', 'comparator': 'eq', 'value': pod['pod_id']}, {'key': 'resource_type', 'comparator': 'eq', 'value': _type}], []) if routes: route = routes[0] if route['bottom_id']: return route, ALL_DONE else: route_time = route['updated_at'] or route['created_at'] current_time = datetime.datetime.utcnow() delta = current_time - route_time if delta.seconds > route_expire_threshold: # NOTE(zhiyuan) cannot directly remove the route, we have # a race here that other worker is updating this route, we # need to check if the corresponding element has been # created by other worker eles = list_ele_method(t_ctx, q_ctx, pod, ele, _type) if eles: route['bottom_id'] = eles[0]['id'] core.update_resource(t_ctx, models.ResourceRouting, route['id'], route) return route, RES_DONE try: core.delete_resource(t_ctx, models.ResourceRouting, route['id']) except db_exc.ResourceNotFound: pass try: # NOTE(zhiyuan) try/except block inside a with block will cause # problem, so move them out of the block and manually handle the # session context t_ctx.session.begin() route = core.create_resource(t_ctx, models.ResourceRouting, {'top_id': _id, 'pod_id': pod['pod_id'], 'project_id': project_id, 'resource_type': _type}) t_ctx.session.commit() return route, NONE_DONE except db_exc.DBDuplicateEntry: t_ctx.session.rollback() return None, NONE_DONE finally: t_ctx.session.close()
def get_or_create_element(t_ctx, q_ctx, project_id, pod, ele, _type, body, list_ele_method, create_ele_method): # use configuration option later max_tries = 5 for _ in xrange(max_tries): route, status = get_or_create_route( t_ctx, q_ctx, project_id, pod, ele['id'], _type, list_ele_method) if not route: eventlet.sleep(0) continue if status == RES_DONE or status == ALL_DONE: # in these cases, bottom_id must exist break if status == NONE_DONE: try: ele = create_ele_method(t_ctx, q_ctx, pod, body, _type) except Exception: with t_ctx.session.begin(): try: core.delete_resource(t_ctx, models.ResourceRouting, route['id']) except db_exc.ResourceNotFound: # NOTE(zhiyuan) this is a rare case that other worker # considers the route expires and delete it though it # was just created, maybe caused by out-of-sync time pass raise with t_ctx.session.begin(): # NOTE(zhiyuan) it's safe to update route, the bottom network # has been successfully created, so other worker will not # delete this route route['bottom_id'] = ele['id'] core.update_resource(t_ctx, models.ResourceRouting, route['id'], route) break if not route: raise Exception('Fail to create %s routing entry' % _type) if not route['bottom_id']: raise Exception('Fail to bind top and bottom %s' % _type) # NOTE(zhiyuan) Status being ALL_DONE means that the routing entry is # complete when we retrieve the resource, so we return False to indicate # that we can directly use this resource safely. Status being RES_DONE and # NONE_DONE means that the routing entry is not complete when we retrieve # the resource but we manage to fill the entry finally, so we return True # to indicate that we may leave some work to do. if status == ALL_DONE: return False, route['bottom_id'] else: return True, route['bottom_id']
def get_or_create_element(t_ctx, q_ctx, project_id, pod, ele, _type, body, list_ele_method, create_ele_method): # use configuration option later max_tries = 5 for _ in xrange(max_tries): route, status = get_or_create_route(t_ctx, q_ctx, project_id, pod, ele, _type, list_ele_method) if not route: eventlet.sleep(0) continue if status == RES_DONE or status == ALL_DONE: # in these cases, bottom_id must exist break if status == NONE_DONE: try: new_ele = create_ele_method(t_ctx, q_ctx, pod, body, _type) except Exception: with t_ctx.session.begin(): try: core.delete_resource(t_ctx, models.ResourceRouting, route['id']) except db_exc.ResourceNotFound: # NOTE(zhiyuan) this is a rare case that other worker # considers the route expires and delete it though it # was just created, maybe caused by out-of-sync time pass raise with t_ctx.session.begin(): # NOTE(zhiyuan) it's safe to update route, the bottom network # has been successfully created, so other worker will not # delete this route route['bottom_id'] = new_ele['id'] core.update_resource(t_ctx, models.ResourceRouting, route['id'], route) break if not route: raise Exception('Fail to create %s routing entry' % _type) if not route['bottom_id']: raise Exception('Fail to bind top and bottom %s' % _type) # NOTE(zhiyuan) Status being ALL_DONE means that the routing entry is # complete when we retrieve the resource, so we return False to indicate # that we can directly use this resource safely. Status being RES_DONE and # NONE_DONE means that the routing entry is not complete when we retrieve # the resource but we manage to fill the entry finally, so we return True # to indicate that we may leave some work to do. if status == ALL_DONE: return False, route['bottom_id'] else: return True, route['bottom_id']
def put(self, _id, **kw): context = t_context.extract_context_from_environ() dci = request.context['request_data']['dci'] try: with context.session.begin(): core.get_resource_object(context, models.DCI, _id) connection_updated = core.update_resource( context, models.DCI, _id, dci) return_object = m.SuccessMessage( result={'dci': connection_updated}) return return_object.to_dict() except t_exceptions.ResourceNotFound as e: LOG.exception( 'Failed to update dci: ' 'dci_id %(dci_id)s ,' '%(exception)s ', { 'dci_id': _id, 'exception': e }) return m.DCINotFound(dci_id=_id).to_dict() except Exception as e: LOG.exception('Failed to update dci: ' '%(exception)s ', {'exception': e}) return_object = m.FailureMessage() return return_object.to_dict()
def put(self, _id, **kw): context = t_context.extract_context_from_environ() firewall_bypass = request.context['request_data']['firewall_bypass'] try: with context.session.begin(): core.get_resource_object(context, models.FirewallBypass, _id) firewall_bypass_updated = core.update_resource( context, models.FirewallBypass, _id, firewall_bypass) return_object = m.SuccessMessage( result={'firewall_bypass': firewall_bypass_updated}) return return_object.to_dict() except t_exceptions.ResourceNotFound as e1: LOG.exception( 'Failed to update firewall_bypass : '******'id%(id)s ,' '%(exception)s ', { 'id': _id, 'exception': e1 }) return m.FirewallBypassNotFound(id=_id).to_dict() except Exception as e2: LOG.exception( 'Failed to update firewall_bypass: '******'id: %(id)s,' '%(exception)s ', { 'id': _id, 'exception': e2 }) return_object = m.FailureMessage() return return_object.to_dict()
def test_get_or_create_route_conflict_expire(self): t_pod, b_pod = self._prepare_pod() route, is_own = self.controller._get_or_create_route( self.context, b_pod, 'test_top_id', 'port') # manually set update time to expire the routing entry with self.context.session.begin(): update_time = route['created_at'] - datetime.timedelta(0, 60) core.update_resource(self.context, models.ResourceRouting, route['id'], {'updated_at': update_time}) new_route, is_own = self.controller._get_or_create_route( self.context, b_pod, 'test_top_id', 'port') self.assertTrue(is_own) self.assertEqual('test_top_id', new_route['top_id']) self.assertIsNone(new_route['bottom_id']) self.assertEqual('port', new_route['resource_type']) self.assertEqual(self.project_id, new_route['project_id'])
def put(self, _id, **kw): context = t_context.extract_context_from_environ() if not policy.enforce(context, policy.ADMIN_API_PODS_CREATE): pecan.abort(401, _('Unauthorized to put core_router')) return core_router = request.context['request_data']['core_router'] try: with context.session.begin(): core.get_resource(context, models.CoreRouter, _id) if 'routes' in core_router: self._update_core_router_routes(context, _id, core_router) router_updated = core.update_resource(context, models.CoreRouter, _id, core_router) return_object = m.SuccessMessage( result={'core_router': router_updated}) return return_object.to_dict() except t_exceptions.ResourceNotFound as e: LOG.exception( 'Failed to update core_router : ' 'core_router_id %(core_router_id)s ,' '%(exception)s ', { 'core_router_id': _id, 'exception': e }) return m.CoreRouterNotFound(core_router_id=_id).to_dict() except Exception as e: LOG.exception('Failed to update core_router : ' '%(exception)s ', {'exception': e}) return_object = m.FailureMessage() return return_object.to_dict()
def finish_job(context, job_id, successful, timestamp): status = constants.JS_Success if successful else constants.JS_Fail with context.session.begin(): job_dict = { 'status': status, 'timestamp': timestamp, 'extra_id': uuidutils.generate_uuid() } job = core.update_resource(context, models.AsyncJob, job_id, job_dict) if status == constants.JS_Success: log_dict = { 'id': uuidutils.generate_uuid(), 'type': job['type'], 'project_id': job['project_id'], 'timestamp': timestamp, 'resource_id': job['resource_id'] } context.session.query(models.AsyncJob).filter( sql.and_(models.AsyncJob.type == job['type'], models.AsyncJob.resource_id == job['resource_id'], models.AsyncJob.timestamp <= timestamp)).delete( synchronize_session=False) core.create_resource(context, models.AsyncJobLog, log_dict) else: # sqlite has problem handling "<" operator on timestamp, so we # slide the timestamp a bit and use "<=" timestamp = timestamp - datetime.timedelta(microseconds=1) context.session.query(models.AsyncJob).filter( sql.and_(models.AsyncJob.type == job['type'], models.AsyncJob.resource_id == job['resource_id'], models.AsyncJob.timestamp <= timestamp)).delete( synchronize_session=False)
def test_get_or_create_route_conflict_expire_has_bottom_res(self): t_pod, b_pod = self._prepare_pod() route, is_own = self.controller._get_or_create_route( self.context, b_pod, 'test_top_id', 'port') # manually set update time to expire the routing entry with self.context.session.begin(): update_time = route['created_at'] - datetime.timedelta(0, 60) core.update_resource(self.context, models.ResourceRouting, route['id'], {'updated_at': update_time}) # insert a fake bottom port BOTTOM_PORTS.append({'id': 'test_bottom_id', 'name': 'test_top_id'}) new_route, status = self.controller._get_or_create_route( self.context, b_pod, 'test_top_id', 'port') self.assertEqual(lock_handle.RES_DONE, status) self.assertEqual('test_top_id', new_route['top_id']) self.assertEqual('test_bottom_id', new_route['bottom_id']) self.assertEqual('port', new_route['resource_type']) self.assertEqual(self.project_id, new_route['project_id'])
def get_or_create_element(t_ctx, q_ctx, project_id, pod, ele, _type, body, list_ele_method, create_ele_method): # use configuration option later max_tries = 5 for _ in xrange(max_tries): route, is_new = get_or_create_route( t_ctx, q_ctx, project_id, pod, ele['id'], _type, list_ele_method) if not route: eventlet.sleep(0) continue if not is_new and not route['bottom_id']: eventlet.sleep(0) continue if not is_new and route['bottom_id']: break if is_new: try: ele = create_ele_method(t_ctx, q_ctx, pod, body, _type) except Exception: with t_ctx.session.begin(): try: core.delete_resource(t_ctx, models.ResourceRouting, route['id']) except db_exc.ResourceNotFound: # NOTE(zhiyuan) this is a rare case that other worker # considers the route expires and delete it though it # was just created, maybe caused by out-of-sync time pass raise with t_ctx.session.begin(): # NOTE(zhiyuan) it's safe to update route, the bottom network # has been successfully created, so other worker will not # delete this route route['bottom_id'] = ele['id'] core.update_resource(t_ctx, models.ResourceRouting, route['id'], route) break if not route: raise Exception('Fail to create %s routing entry' % _type) if not route['bottom_id']: raise Exception('Fail to bind top and bottom %s' % _type) return is_new, route['bottom_id']
def _handle_sg_rule_for_new_group(self, context, pod, top_sgs, bottom_sg_ids): client = self._get_client(pod['pod_name']) for i, t_sg in enumerate(top_sgs): b_sg_id = bottom_sg_ids[i] new_b_rules = [] for t_rule in t_sg['security_group_rules']: if t_rule['remote_group_id']: # we do not handle remote group rule for non-default # security group, actually tricircle plugin in neutron # will reject such rule # default security group is not passed with top_sgs so # t_rule will not belong to default security group continue new_b_rules.append( self._construct_bottom_rule(t_rule, b_sg_id)) try: b_sg = client.get_security_groups(context, b_sg_id) for b_rule in b_sg['security_group_rules']: self._safe_delete_security_group_rule( context, client, b_rule['id']) if new_b_rules: rule_body = {'security_group_rules': new_b_rules} self._safe_create_security_group_rule(context, client, rule_body) except Exception: # if we fails when operating bottom security group rule, we # update the security group mapping to set bottom_id to None # and expire the mapping, so next time the security group rule # operations can be redone with context.session.begin(): routes = core.query_resource( context, models.ResourceRouting, [{'key': 'top_id', 'comparator': 'eq', 'value': t_sg['id']}, {'key': 'bottom_id', 'comparator': 'eq', 'value': b_sg_id}], []) update_dict = {'bottom_id': None, 'created_at': constants.expire_time, 'updated_at': constants.expire_time} core.update_resource(context, models.ResourceRouting, routes[0]['id'], update_dict) raise
def finish_job(context, job_id, successful, timestamp): status = constants.JS_Success if successful else constants.JS_Fail retries = 5 for i in range(retries + 1): try: with context.session.begin(): db_test_stub(i) job_dict = {'status': status, 'timestamp': timestamp, 'extra_id': uuidutils.generate_uuid()} job = core.update_resource(context, models.AsyncJob, job_id, job_dict) if status == constants.JS_Success: log_dict = {'id': uuidutils.generate_uuid(), 'type': job['type'], 'project_id': job['project_id'], 'timestamp': timestamp, 'resource_id': job['resource_id']} context.session.query(models.AsyncJob).filter( sql.and_( models.AsyncJob.type == job['type'], models.AsyncJob.resource_id == job['resource_id'], models.AsyncJob.timestamp <= timestamp)).delete( synchronize_session=False) core.create_resource(context, models.AsyncJobLog, log_dict) else: # sqlite has problem handling "<" operator on timestamp, # so we slide the timestamp a bit and use "<=" timestamp = timestamp - datetime.timedelta(microseconds=1) context.session.query(models.AsyncJob).filter( sql.and_( models.AsyncJob.type == job['type'], models.AsyncJob.resource_id == job['resource_id'], models.AsyncJob.timestamp <= timestamp)).delete( synchronize_session=False) except db_exc.DBDeadlock: if i == retries: raise time.sleep(1) continue return
def put(self, _id, **kw): context = t_context.extract_context_from_environ() route_entry = request.context['request_data']['route_entry'] try: with context.session.begin(): core.get_resource_object(context, models.RouteEntry, _id) if 'destination_cidr_list' in route_entry: self._update_route_entry_destination_cidrs( context, _id, route_entry) route_entry_updated = core.update_resource( context, models.RouteEntry, _id, route_entry) return_object = m.SuccessMessage( result={'route_entry': route_entry_updated}) return return_object.to_dict() except t_exceptions.ResourceNotFound as e1: LOG.exception( 'Failed to update route_entry : ' 'id%(id)s ,' '%(exception)s ', { 'id': _id, 'exception': e1 }) return m.RouteEntryNotFound(id=_id).to_dict() except Exception as e2: LOG.exception( 'Failed to update route_entry: ' 'id: %(id)s,' '%(exception)s ', { 'id': _id, 'exception': e2 }) return_object = m.FailureMessage() return return_object.to_dict()
def update_pod(context, pod_id, update_dict): with context.session.begin(): return core.update_resource(context, models.Pod, pod_id, update_dict)
def update_pod_service_configuration(context, config_id, update_dict): with context.session.begin(): return core.update_resource( context, models.PodServiceConfiguration, config_id, update_dict)
def update_site(context, site_id, update_dict): with context.session.begin(): return core.update_resource(context, Site, site_id, update_dict)
def update_fabric(context, fabric_id, update_dict): with context.session.begin(): return core.update_resource(context, models.Fabric, fabric_id, update_dict)
def update_region(context, region_id, update_dict): with context.session.begin(): return core.update_resource(context, models.Region, region_id, update_dict)
def update_firewall_bypass(context, firewall_bypass_id, update_dict): with context.session.begin(): return core.update_resource(context, models.FirewallBypass, firewall_bypass_id, update_dict)
def update_cached_endpoints(context, config_id, update_dict): with context.session.begin(): return core.update_resource(context, models.CachedEndpoint, config_id, update_dict)
def update_resource_routing(context, id, update_dict): with context.session.begin(): return core.update_resource(context, models.ResourceRouting, id, update_dict)
def test_redo_job(self): for job_type in self.all_job_types: job = self._prepare_job_element(job_type) jobs = [ # create an entirely new job { "job": job, "expected_error": 200 }, ] self._test_and_check(jobs) response = self.app.get('/v1.0/jobs') return_job = response.json jobs = return_job['jobs'] # redo a new job for job in jobs: response_1 = self.app.put('/v1.0/jobs/%(id)s' % {'id': job['id']}, expect_errors=True) self.assertEqual(response_1.status_int, 200) response_2 = self.app.put('/v1.0/jobs/123', expect_errors=True) self.assertEqual(response_2.status_int, 404) # redo a running job job_type_3 = constants.JT_NETWORK_UPDATE job_3 = self._prepare_job_element(job_type_3) resource_id_3 = '#'.join([job_3['resource'][resource_id] for resource_type, resource_id in self.job_resource_map[job_type_3]]) job_running_3 = db_api.register_job(self.context, job_3['project_id'], job_type_3, resource_id_3) self.assertEqual(constants.JS_Running, job_running_3['status']) response_3 = self.app.put('/v1.0/jobs/%(id)s' % { 'id': job_running_3['id']}, expect_errors=True) self.assertEqual(response_3.status_int, 400) # redo a failed job job_type_4 = constants.JT_NETWORK_UPDATE job_4 = self._prepare_job_element(job_type_4) job_dict_4 = { "job": job_4, "expected_error": 200 } response_4 = self.app.post_json('/v1.0/jobs', dict(job=job_dict_4['job']), expect_errors=True) return_job_4 = response_4.json self.assertEqual(response_4.status_int, 200) db_api.finish_job(self.context, return_job_4['job']['id'], False, timeutils.utcnow()) job_fail_4 = db_api.get_job(self.context, return_job_4['job']['id']) self.assertEqual(constants.JS_Fail, job_fail_4['status']) response_5 = self.app.put('/v1.0/jobs/%(id)s' % { 'id': return_job_4['job']['id']}, expect_errors=True) self.assertEqual(response_5.status_int, 200) # redo a successful job job_type_6 = constants.JT_NETWORK_UPDATE job_6 = self._prepare_job_element(job_type_6) job_dict_6 = { "job": job_6, "expected_error": 200 } response_6 = self.app.post_json('/v1.0/jobs', dict(job=job_dict_6['job']), expect_errors=True) return_job_6 = response_6.json with self.context.session.begin(): job_dict = {'status': constants.JS_Success, 'timestamp': timeutils.utcnow(), 'extra_id': uuidutils.generate_uuid()} core.update_resource(self.context, models.AsyncJob, return_job_6['job']['id'], job_dict) job_succ_6 = db_api.get_job(self.context, return_job_6['job']['id']) self.assertEqual(constants.JS_Success, job_succ_6['status']) response_7 = self.app.put('/v1.0/jobs/%(id)s' % { 'id': return_job_6['job']['id']}, expect_errors=True) self.assertEqual(response_7.status_int, 400)
def test_delete(self, mock_context): mock_context.return_value = self.context # cover all job types. # each 'for' loop adds one item in job log table, we set count variable # to record dynamic total job entries in job log table. count = 1 for job_type in self.job_resource_map.keys(): job = self._prepare_job_element(job_type) resource_id = '#'.join([job['resource'][resource_id] for resource_type, resource_id in self.job_resource_map[job_type]]) # failure case, only admin can delete the job job_1 = db_api.new_job(self.context, job['project_id'], job_type, resource_id) self.context.is_admin = False res = self.controller.delete(job_1['id']) self._validate_error_code(res, 403) self.context.is_admin = True db_api.delete_job(self.context, job_1['id']) # failure case, job not found res = self.controller.delete(-123) self._validate_error_code(res, 404) # failure case, delete a running job job_2 = db_api.register_job(self.context, job['project_id'], job_type, resource_id) job = db_api.get_job(self.context, job_2['id']) res = self.controller.delete(job_2['id']) self._validate_error_code(res, 400) # finish the job and delete it db_api.finish_job(self.context, job_2['id'], False, timeutils.utcnow()) db_api.delete_job(self.context, job_2['id']) # successful case, delete a successful job. successful job from # job log can't be deleted, here this successful job is from # job table. job_3 = self._prepare_job_element(job_type) resource_id_3 = '#'.join([job_3['resource'][resource_id_3] for resource_type_3, resource_id_3 in self.job_resource_map[job_type]]) job_4 = db_api.new_job(self.context, job_3['project_id'], job_type, resource_id_3) with self.context.session.begin(): job_dict = {'status': constants.JS_Success, 'timestamp': timeutils.utcnow(), 'extra_id': uuidutils.generate_uuid()} core.update_resource(self.context, models.AsyncJob, job_4['id'], job_dict) job_4_succ = db_api.get_job(self.context, job_4['id']) self.controller.delete(job_4['id']) filters_job_4 = [ {'key': 'type', 'comparator': 'eq', 'value': job_4_succ['type']}, {'key': 'status', 'comparator': 'eq', 'value': job_4_succ['status']}, {'key': 'resource_id', 'comparator': 'eq', 'value': job_4_succ['resource_id']}, {'key': 'extra_id', 'comparator': 'eq', 'value': job_4_succ['extra_id']}] self.assertEqual(0, len(db_api.list_jobs(self.context, filters_job_4))) self.assertEqual(count, len(db_api.list_jobs_from_log(self.context))) count = count + 1 # successful case, delete a new job job_5 = db_api.new_job(self.context, job['project_id'], job_type, resource_id) self.controller.delete(job_5['id']) filters_job_5 = [ {'key': 'type', 'comparator': 'eq', 'value': job_5['type']}, {'key': 'status', 'comparator': 'eq', 'value': job_5['status']}, {'key': 'resource_id', 'comparator': 'eq', 'value': job_5['resource_id']}, {'key': 'extra_id', 'comparator': 'eq', 'value': job_5['extra_id']}] self.assertEqual(0, len(db_api.list_jobs(self.context, filters_job_5))) # successful case, delete a failed job job_6 = db_api.new_job(self.context, job['project_id'], job_type, resource_id) db_api.finish_job(self.context, job_6['id'], False, timeutils.utcnow()) job_6_failed = db_api.get_job(self.context, job_6['id']) self.controller.delete(job_6['id']) filters_job_6 = [ {'key': 'type', 'comparator': 'eq', 'value': job_6_failed['type']}, {'key': 'status', 'comparator': 'eq', 'value': job_6_failed['status']}, {'key': 'resource_id', 'comparator': 'eq', 'value': job_6_failed['resource_id']}, {'key': 'extra_id', 'comparator': 'eq', 'value': job_6_failed['extra_id']}] self.assertEqual(0, len(db_api.list_jobs(self.context, filters_job_6)))
def update_site_service_configuration(context, config_id, update_dict): with context.session.begin(): return core.update_resource( context, SiteServiceConfiguration, config_id, update_dict)
def update_core_router(context, core_router_id, update_dict): with context.session.begin(): return core.update_resource(context, models.CoreRouter, core_router_id, update_dict)
def update_dynamic_peering_connection(context, core_router_id, update_dict): with context.session.begin(): return core.update_resource(context, models.DynamicPeeringConnection, core_router_id, update_dict)
def test_put(self, mock_context): mock_context.return_value = self.context # cover all job types for job_type in self.job_resource_map.keys(): job = self._prepare_job_element(job_type) resource_id = '#'.join([job['resource'][resource_id] for resource_type, resource_id in self.job_resource_map[job_type]]) # failure case, only admin can redo the job job_1 = db_api.new_job(self.context, job['project_id'], job_type, resource_id) self.context.is_admin = False res = self.controller.put(job_1['id']) self._validate_error_code(res, 403) self.context.is_admin = True db_api.delete_job(self.context, job_1['id']) # failure case, job not found res = self.controller.put(-123) self._validate_error_code(res, 404) # failure case, redo a running job job_2 = db_api.register_job(self.context, job['project_id'], job_type, resource_id) res = self.controller.put(job_2['id']) self._validate_error_code(res, 400) db_api.finish_job(self.context, job_2['id'], False, timeutils.utcnow()) db_api.delete_job(self.context, job_2['id']) # failure case, redo a successful job job_3 = self._prepare_job_element(job_type) resource_id_3 = '#'.join([job_3['resource'][resource_id_3] for resource_type_3, resource_id_3 in self.job_resource_map[job_type]]) job_4 = db_api.new_job(self.context, job_3['project_id'], job_type, resource_id_3) with self.context.session.begin(): job_dict = {'status': constants.JS_Success, 'timestamp': timeutils.utcnow(), 'extra_id': uuidutils.generate_uuid()} core.update_resource(self.context, models.AsyncJob, job_4['id'], job_dict) res = self.controller.put(job_4['id']) self._validate_error_code(res, 400) db_api.finish_job(self.context, job_4['id'], True, timeutils.utcnow()) # successful case, redo a failed job job_5 = db_api.new_job(self.context, job['project_id'], job_type, resource_id) db_api.finish_job(self.context, job_5['id'], False, timeutils.utcnow()) self.controller.put(job_5['id']) db_api.delete_job(self.context, job_5['id']) # successful case, redo a new job job_6 = db_api.new_job(self.context, job['project_id'], job_type, resource_id) self.controller.put(job_6['id']) db_api.delete_job(self.context, job_6['id'])
def update_firewall_gateway(context, core_router_id, update_dict): with context.session.begin(): return core.update_resource(context, models.FirewallGateway, core_router_id, update_dict)
def test_put(self, mock_context): mock_context.return_value = self.context # cover all job types for job_type in self.job_resource_map.keys(): job = self._prepare_job_element(job_type) resource_id = '#'.join([ job['resource'][resource_id] for resource_type, resource_id in self.job_resource_map[job_type] ]) # failure case, only admin can redo the job job_1 = db_api.new_job(self.context, job['project_id'], job_type, resource_id) self.context.is_admin = False res = self.controller.put(job_1['id']) self._validate_error_code(res, 403) self.context.is_admin = True db_api.delete_job(self.context, job_1['id']) # failure case, job not found res = self.controller.put(-123) self._validate_error_code(res, 404) # failure case, redo a running job job_2 = db_api.register_job(self.context, job['project_id'], job_type, resource_id) res = self.controller.put(job_2['id']) self._validate_error_code(res, 400) db_api.finish_job(self.context, job_2['id'], False, timeutils.utcnow()) db_api.delete_job(self.context, job_2['id']) # failure case, redo a successful job job_3 = self._prepare_job_element(job_type) resource_id_3 = '#'.join([ job_3['resource'][resource_id_3] for resource_type_3, resource_id_3 in self.job_resource_map[job_type] ]) job_4 = db_api.new_job(self.context, job_3['project_id'], job_type, resource_id_3) with self.context.session.begin(): job_dict = { 'status': constants.JS_Success, 'timestamp': timeutils.utcnow(), 'extra_id': uuidutils.generate_uuid() } core.update_resource(self.context, models.AsyncJob, job_4['id'], job_dict) res = self.controller.put(job_4['id']) self._validate_error_code(res, 400) db_api.finish_job(self.context, job_4['id'], True, timeutils.utcnow()) # successful case, redo a failed job job_5 = db_api.new_job(self.context, job['project_id'], job_type, resource_id) db_api.finish_job(self.context, job_5['id'], False, timeutils.utcnow()) self.controller.put(job_5['id']) db_api.delete_job(self.context, job_5['id']) # successful case, redo a new job job_6 = db_api.new_job(self.context, job['project_id'], job_type, resource_id) self.controller.put(job_6['id']) db_api.delete_job(self.context, job_6['id'])
def update_tricircle_resource(context, core_router_id, update_dict): with context.session.begin(): return core.update_resource(context, models.TricircleResource, core_router_id, update_dict)
def update_cached_endpoints(context, config_id, update_dict): with context.session.begin(): return core.update_resource( context, models.CachedEndpoint, config_id, update_dict)
def update_dc(context, dc_id, update_dict): with context.session.begin(): return core.update_resource(context, models.DC, dc_id, update_dict)
def update_route_entry(context, route_entry_id, update_dict): with context.session.begin(): return core.update_resource(context, models.RouteEntry, route_entry_id, update_dict)
def update_pod_service_configuration(context, config_id, update_dict): with context.session.begin(): return core.update_resource(context, models.PodServiceConfiguration, config_id, update_dict)
def test_delete(self, mock_context): mock_context.return_value = self.context # cover all job types. # each 'for' loop adds one item in job log table, we set count variable # to record dynamic total job entries in job log table. count = 1 for job_type in self.job_resource_map.keys(): job = self._prepare_job_element(job_type) resource_id = '#'.join([ job['resource'][resource_id] for resource_type, resource_id in self.job_resource_map[job_type] ]) # failure case, only admin can delete the job job_1 = db_api.new_job(self.context, job['project_id'], job_type, resource_id) self.context.is_admin = False res = self.controller.delete(job_1['id']) self._validate_error_code(res, 403) self.context.is_admin = True db_api.delete_job(self.context, job_1['id']) # failure case, job not found res = self.controller.delete(-123) self._validate_error_code(res, 404) # failure case, delete a running job job_2 = db_api.register_job(self.context, job['project_id'], job_type, resource_id) job = db_api.get_job(self.context, job_2['id']) res = self.controller.delete(job_2['id']) self._validate_error_code(res, 400) # finish the job and delete it db_api.finish_job(self.context, job_2['id'], False, timeutils.utcnow()) db_api.delete_job(self.context, job_2['id']) # successful case, delete a successful job. successful job from # job log can't be deleted, here this successful job is from # job table. job_3 = self._prepare_job_element(job_type) resource_id_3 = '#'.join([ job_3['resource'][resource_id_3] for resource_type_3, resource_id_3 in self.job_resource_map[job_type] ]) job_4 = db_api.new_job(self.context, job_3['project_id'], job_type, resource_id_3) with self.context.session.begin(): job_dict = { 'status': constants.JS_Success, 'timestamp': timeutils.utcnow(), 'extra_id': uuidutils.generate_uuid() } core.update_resource(self.context, models.AsyncJob, job_4['id'], job_dict) job_4_succ = db_api.get_job(self.context, job_4['id']) self.controller.delete(job_4['id']) filters_job_4 = [{ 'key': 'type', 'comparator': 'eq', 'value': job_4_succ['type'] }, { 'key': 'status', 'comparator': 'eq', 'value': job_4_succ['status'] }, { 'key': 'resource_id', 'comparator': 'eq', 'value': job_4_succ['resource_id'] }, { 'key': 'extra_id', 'comparator': 'eq', 'value': job_4_succ['extra_id'] }] self.assertEqual( 0, len(db_api.list_jobs(self.context, filters_job_4))) self.assertEqual(count, len(db_api.list_jobs_from_log(self.context))) count = count + 1 # successful case, delete a new job job_5 = db_api.new_job(self.context, job['project_id'], job_type, resource_id) self.controller.delete(job_5['id']) filters_job_5 = [{ 'key': 'type', 'comparator': 'eq', 'value': job_5['type'] }, { 'key': 'status', 'comparator': 'eq', 'value': job_5['status'] }, { 'key': 'resource_id', 'comparator': 'eq', 'value': job_5['resource_id'] }, { 'key': 'extra_id', 'comparator': 'eq', 'value': job_5['extra_id'] }] self.assertEqual( 0, len(db_api.list_jobs(self.context, filters_job_5))) # successful case, delete a failed job job_6 = db_api.new_job(self.context, job['project_id'], job_type, resource_id) db_api.finish_job(self.context, job_6['id'], False, timeutils.utcnow()) job_6_failed = db_api.get_job(self.context, job_6['id']) self.controller.delete(job_6['id']) filters_job_6 = [{ 'key': 'type', 'comparator': 'eq', 'value': job_6_failed['type'] }, { 'key': 'status', 'comparator': 'eq', 'value': job_6_failed['status'] }, { 'key': 'resource_id', 'comparator': 'eq', 'value': job_6_failed['resource_id'] }, { 'key': 'extra_id', 'comparator': 'eq', 'value': job_6_failed['extra_id'] }] self.assertEqual( 0, len(db_api.list_jobs(self.context, filters_job_6)))