def get_pod_by_az_tenant(context, az_name, tenant_id): pod_bindings = core.query_resource(context, models.PodBinding, [{'key': 'tenant_id', 'comparator': 'eq', 'value': tenant_id}], []) for pod_b in pod_bindings: pod = core.get_resource(context, models.Pod, pod_b['pod_id']) if pod['az_name'] == az_name: return pod, pod['pod_az_name'] # TODO(joehuang): schedule one dynamically in the future filters = [{'key': 'az_name', 'comparator': 'eq', 'value': az_name}] pods = db_api.list_pods(context, filters=filters) for pod in pods: if pod['pod_name'] != '': try: with context.session.begin(): core.create_resource( context, models.PodBinding, {'id': uuidutils.generate_uuid(), 'tenant_id': tenant_id, 'pod_id': pod['pod_id']}) return pod, pod['pod_az_name'] except Exception as e: LOG.error(_LE('Fail to create pod binding: %(exception)s'), {'exception': e}) return None, None return None, None
def get_trunks(self, context, filters=None, fields=None, sorts=None, limit=None, marker=None, page_reverse=False): ret = [] bottom_top_map = {} top_bottom_map = {} t_ctx = t_context.get_context_from_neutron_context(context) route_filters = [{'key': 'resource_type', 'comparator': 'eq', 'value': t_constants.RT_TRUNK}] routes = db_api.list_resource_routings(t_ctx, route_filters) for route in routes: bottom_top_map[route['bottom_id']] = route['top_id'] top_bottom_map[route['top_id']] = route['bottom_id'] if limit: if marker: mappings = db_api.get_bottom_mappings_by_top_id( t_ctx, marker, t_constants.RT_TRUNK) # if mapping exists, we retrieve trunk information # from bottom, otherwise from top if mappings: pod_id = mappings[0][0]['pod_id'] current_pod = db_api.get_pod(t_ctx, pod_id) ret = self._get_trunks_from_pod_with_limit( context, current_pod, bottom_top_map, top_bottom_map, filters, limit, marker) else: ret = self._get_trunks_from_top_with_limit( context, top_bottom_map, filters, limit, marker) else: current_pod = db_api.get_next_bottom_pod(t_ctx) # if current_pod exists, we retrieve trunk information # from bottom, otherwise from top if current_pod: ret = self._get_trunks_from_pod_with_limit( context, current_pod, bottom_top_map, top_bottom_map, filters, limit, None) else: ret = self._get_trunks_from_top_with_limit( context, top_bottom_map, filters, limit, None) else: pods = db_api.list_pods(t_ctx) _filters = self._transform_trunk_filters(filters, top_bottom_map) for pod in pods: if not pod['az_name']: continue client = self._get_client(pod['region_name']) pod_trunks = client.list_trunks(t_ctx, filters=_filters) ret.extend(pod_trunks) ret = self._map_trunks_from_bottom_to_top(ret, bottom_top_map) top_trunks = self._get_trunks_from_top(context, top_bottom_map, filters) ret.extend(top_trunks) return [super(TricircleTrunkPlugin, self)._fields(trunk, fields) for trunk in ret]
def _get_all(self, context): ret = [] pods = db_api.list_pods(context) for pod in pods: if not pod['az_name']: continue client = self._get_client(pod['pod_name']) servers = client.list_servers(context) self._remove_fip_info(servers) ret.extend(servers) return ret
def _update_endpoint_from_keystone(self, cxt, is_internal): """Update the database by querying service endpoint url from Keystone :param cxt: context object :param is_internal: if True, this method utilizes pre-configured admin username and password to apply an new admin token, this happens only when auto_refresh_endpoint is set to True. if False, token in cxt is directly used, users should prepare admin token themselves :return: None """ if is_internal: admin_context = tricircle_context.get_admin_context() self._ensure_token_for_admin(admin_context) endpoint_map = self._get_endpoint_from_keystone(admin_context) else: endpoint_map = self._get_endpoint_from_keystone(cxt) for region in endpoint_map: # use region name to query pod pod_filters = [{'key': 'region_name', 'comparator': 'eq', 'value': region}] pod_list = api.list_pods(cxt, pod_filters) # skip region/pod not registered in cascade service if len(pod_list) != 1: continue for service in endpoint_map[region]: pod_id = pod_list[0]['pod_id'] config_filters = [{'key': 'pod_id', 'comparator': 'eq', 'value': pod_id}, {'key': 'service_type', 'comparator': 'eq', 'value': service}] config_list = api.list_cached_endpoints( cxt, config_filters) if len(config_list) > 1: continue if len(config_list) == 1: config_id = config_list[0]['service_id'] update_dict = { 'service_url': endpoint_map[region][service]} api.update_cached_endpoints( cxt, config_id, update_dict) else: config_dict = { 'service_id': uuidutils.generate_uuid(), 'pod_id': pod_id, 'service_type': service, 'service_url': endpoint_map[region][service] } api.create_cached_endpoints( cxt, config_dict)
def _get_all(self, context, params): filters = [{'key': key, 'comparator': 'eq', 'value': value} for key, value in params.iteritems()] ret = [] pods = db_api.list_pods(context) for pod in pods: if not pod['az_name']: continue client = self._get_client(pod['pod_name']) servers = client.list_servers(context, filters=filters) self._remove_fip_info(servers) ret.extend(servers) return ret
def get_all(self): context = t_context.extract_context_from_environ() if not t_context.is_admin_context(context): pecan.abort(400, _('Admin role required to list pods')) return try: return {'pods': db_api.list_pods(context)} except Exception as e: LOG.error(_LE('Fail to list pod: %(exception)s'), {'exception': e}) pecan.abort(500, _('Fail to list pod')) return
def test_query(self): pod1 = {'pod_id': 'test_pod1_uuid', 'pod_name': 'test_pod1', 'pod_az_name': 'test_pod_az_name1', 'dc_name': 'test_dc_name1', 'az_name': 'test_az1_uuid'} pod2 = {'pod_id': 'test_pod2_uuid', 'pod_name': 'test_pod2', 'pod_az_name': 'test_pod_az_name2', 'dc_name': 'test_dc_name1', 'az_name': 'test_az2_uuid'} api.create_pod(self.context, pod1) api.create_pod(self.context, pod2) filters = [{'key': 'pod_name', 'comparator': 'eq', 'value': 'test_pod2'}] pods = api.list_pods(self.context, filters) self.assertEqual(len(pods), 1) self.assertEqual(pods[0], pod2) filters = [{'key': 'pod_name', 'comparator': 'eq', 'value': 'test_pod3'}] pods = api.list_pods(self.context, filters) self.assertEqual(len(pods), 0)
def get_all(self): context = t_context.extract_context_from_environ() if not policy.enforce(context, policy.ADMIN_API_PODS_LIST): pecan.abort(401, _('Unauthorized to list pods')) return try: return {'pods': db_api.list_pods(context)} except Exception as e: LOG.exception('Failed to list all pods: %(exception)s ', {'exception': e}) pecan.abort(500, _('Failed to list pods')) return
def get_all(self): context = t_context.extract_context_from_environ() if not policy.enforce(context, policy.ADMIN_API_PODS_LIST): pecan.abort(401, _('Unauthorized to list pods')) return try: return {'pods': db_api.list_pods(context)} except Exception as e: LOG.exception(_LE('Failed to list all pods: %(exception)s '), {'exception': e}) pecan.abort(500, _('Failed to list pods')) return
def get_pod_by_az_tenant(context, az_name, tenant_id): pod_bindings = core.query_resource(context, models.PodBinding, [{'key': 'tenant_id', 'comparator': 'eq', 'value': tenant_id}], []) for pod_b in pod_bindings: pod = core.get_resource(context, models.Pod, pod_b['pod_id']) if az_name and pod['az_name'] == az_name: return pod, pod['pod_az_name'] elif az_name == '' and pod['az_name'] != '': # if the az_name is not specified, a defult bottom # pod will be selected return pod, pod['pod_az_name'] else: pass # TODO(joehuang): schedule one dynamically in the future if az_name != '': filters = [{'key': 'az_name', 'comparator': 'eq', 'value': az_name}] else: filters = None # if az_name is valid, select a pod under this az_name # if az_name is '', select the first valid bottom pod. # change to dynamic schedluing in the future pods = db_api.list_pods(context, filters=filters) for pod in pods: if pod['pod_name'] != '' and pod['az_name'] != '': try: with context.session.begin(): core.create_resource( context, models.PodBinding, {'id': uuidutils.generate_uuid(), 'tenant_id': tenant_id, 'pod_id': pod['pod_id'], 'is_binding': True}) return pod, pod['pod_az_name'] except Exception as e: LOG.error(_LE('Fail to create pod binding: %(exception)s'), {'exception': e}) return None, None return None, None
def _ensure_endpoint_set(self, cxt, service): handle = self.service_handle_map[service] if not handle.is_endpoint_url_set(): pod_filters = [{'key': 'region_name', 'comparator': 'eq', 'value': self.region_name}] pod_list = api.list_pods(cxt, pod_filters) if len(pod_list) == 0: raise exceptions.ResourceNotFound(models.Pod, self.region_name) # region_name is unique key, safe to get the first element pod_id = pod_list[0]['pod_id'] config_filters = [ {'key': 'pod_id', 'comparator': 'eq', 'value': pod_id}, {'key': 'service_type', 'comparator': 'eq', 'value': service}] conf_list = self._get_config_with_retry( cxt, config_filters, pod_id, service, cfg.CONF.client.auto_refresh_endpoint) url = conf_list[0]['service_url'] handle.update_endpoint_url(url)
def test_sort(self): pod1 = {'pod_id': 'test_pod1_uuid', 'pod_name': 'test_pod1', 'pod_az_name': 'test_pod_az_name1', 'dc_name': 'test_dc_name1', 'az_name': 'test_az1_uuid'} pod2 = {'pod_id': 'test_pod2_uuid', 'pod_name': 'test_pod2', 'pod_az_name': 'test_pod_az_name2', 'dc_name': 'test_dc_name1', 'az_name': 'test_az2_uuid'} pod3 = {'pod_id': 'test_pod3_uuid', 'pod_name': 'test_pod3', 'pod_az_name': 'test_pod_az_name3', 'dc_name': 'test_dc_name1', 'az_name': 'test_az3_uuid'} pods = [pod1, pod2, pod3] for pod in pods: api.create_pod(self.context, pod) pods = api.list_pods(self.context, sorts=[(models.Pod.pod_id, False)]) self.assertEqual(pods, [pod3, pod2, pod1])
def get_trunks(self, context, filters=None, fields=None, sorts=None, limit=None, marker=None, page_reverse=False): ret = [] bottom_top_map = {} top_bottom_map = {} t_ctx = t_context.get_context_from_neutron_context(context) route_filters = [{ 'key': 'resource_type', 'comparator': 'eq', 'value': t_constants.RT_TRUNK }] routes = db_api.list_resource_routings(t_ctx, route_filters) for route in routes: bottom_top_map[route['bottom_id']] = route['top_id'] top_bottom_map[route['top_id']] = route['bottom_id'] if limit: if marker: mappings = db_api.get_bottom_mappings_by_top_id( t_ctx, marker, t_constants.RT_TRUNK) # if mapping exists, we retrieve trunk information # from bottom, otherwise from top if mappings: pod_id = mappings[0][0]['pod_id'] current_pod = db_api.get_pod(t_ctx, pod_id) ret = self._get_trunks_from_pod_with_limit( context, current_pod, bottom_top_map, top_bottom_map, filters, limit, marker) else: ret = self._get_trunks_from_top_with_limit( context, top_bottom_map, filters, limit, marker) else: current_pod = db_api.get_next_bottom_pod(t_ctx) # if current_pod exists, we retrieve trunk information # from bottom, otherwise from top if current_pod: ret = self._get_trunks_from_pod_with_limit( context, current_pod, bottom_top_map, top_bottom_map, filters, limit, None) else: ret = self._get_trunks_from_top_with_limit( context, top_bottom_map, filters, limit, None) else: pods = db_api.list_pods(t_ctx) _filters = self._transform_trunk_filters(filters, top_bottom_map) for pod in pods: if not pod['az_name']: continue client = self._get_client(pod['region_name']) pod_trunks = client.list_trunks(t_ctx, filters=_filters) ret.extend(pod_trunks) ret = self._map_trunks_from_bottom_to_top(ret, bottom_top_map) top_trunks = self._get_trunks_from_top(context, top_bottom_map, filters) ret.extend(top_trunks) return [db_utils.resource_fields(trunk, fields) for trunk in ret]