def _values_from_endpoints(self, ns_name, app_name, values, srv_endpoints): """Retrieve updated values from a list of endpoints.""" new_values = values for srv_endpoint in srv_endpoints: jsonpath = srv_endpoint["jsonpath"] parser = jsonpath_parse(jsonpath) jp_data = dict() for match in parser.find(values): jp_data[str(match.full_path)] = match.value if not jp_data: raise ValueError("No match found for jsonpath %s" % jsonpath) timeout = None if "timeout" in srv_endpoint: timeout = srv_endpoint["timeout"] body = {"namespace": ns_name, "app_name": app_name, "values": jp_data} response = self.servicemanager.send_request(srv_endpoint["name"], timeout, **body) # response --> {'values': {'nodeSelector': {'hostname': 'node3'}}} jsonpath = srv_endpoint["jsonpath1"] parser = jsonpath_parse(jsonpath) logging.info("NEW_VALUES %s" % (new_values)) new_jp_data = response["values"] parser.update(new_values, list(new_jp_data.values())[0]) return new_values
def read(context, request): select = request.GET.get('select', None) obj = context.json() if select: expr = jsonpath_parse(select) obj = [match.value for match in expr.find(obj['data'])] return obj
def __init__(self, items): self.items = items self.exprs = [] for path, op, value in items: self.exprs.append(( jsonpath_parse(path), op, value, ))
def json_path(this, data, path): if data is None: raise PybarsError('JSON decode failure!') jsonpath_expr = jsonpath_parse(path) match = jsonpath_expr.find(data) if len(match) < 1: return '' value = match[0].value if value is None: value = 'null' return value
def json_path(data, path): if data is None: raise TemplateSyntaxError('JSON decode failure!', 0) jsonpath_expr = jsonpath_parse(path) match = jsonpath_expr.find(data) if len(match) < 1: return '' value = match[0].value if value is None: value = 'null' return value
def search(context, request): if not context.search_view_enabled: raise HTTPForbidden() query = json.loads(request.GET.get('q', '{}')) if not query: query = None limit = int(request.GET.get('limit', 20)) offset = int(request.GET.get('offset', 0)) order_by = request.GET.get('order_by', None) select = request.GET.get('select', None) if order_by: order_by = order_by.split(':') if len(order_by) == 1: order_by = order_by + ['asc'] if limit > 100: limit = 100 objs = context.search(query, offset=offset, limit=limit, order_by=order_by) objs = [obj.json() for obj in objs] if select: expr = jsonpath_parse(select) results = [] for obj in objs: results.append([match.value for match in expr.find(obj['data'])]) else: results = objs params = {} if select: params['select'] = select if limit: params['limit'] = limit params['offset'] = offset + limit qs = urlencode(params) res = {'results': results, 'q': query} if len(results): res.setdefault('links', []) res['links'].append({ 'rel': 'next', 'href': request.link(context, '+search?%s' % qs) }) if offset > 0: prev_offset = offset - limit if prev_offset < 0: prev_offset = 0 params['offset'] = prev_offset qs = urlencode(params) res.setdefault('links', []) res['links'].append({ 'rel': 'previous', 'href': request.link(context, '+search?%s' % qs) }) return res
def create_app(self, app_name, repochart_name, ns_name, values, **kwargs): """Create a new app.""" app_dir = None try: if '/' in app_name: raise ValueError("no '/' are allowed in the app name") if repochart_name.count("/") != 1: raise ValueError("Charts must come from a repo (repo/chart)") namespace_dir = self._add_ns(ns_name) # no effect if already there app_dir = self._unpack_chart(repochart_name, app_name, namespace_dir) self._write_values(app_name, namespace_dir, values) srv_endpoints = kwargs.get("service_endpoints", None) if self.servicemanager and srv_endpoints: values = self._get_values(app_name, namespace_dir) new_values = self._values_from_endpoints(ns_name, app_name, values, srv_endpoints) for srv_endpoint in srv_endpoints: jsonpath = srv_endpoint["jsonpath2"] parser = jsonpath_parse(jsonpath) parser.update(new_values, self.app_ip_addr) self._write_values(app_name, namespace_dir, new_values) logging.info("NEW_VALUES UPDATED %s" % (new_values)) logging.info("SELECTED NODE %s" % (new_values['nodeSelector']['hostname'])) app_host = os.environ[new_values['nodeSelector']['hostname']] data, app_ips = self.helm_client.install(app_name, repochart_name, chart_dir=namespace_dir, app_host=app_host, namespace=ns_name, create_namespace=True) self.app_ip_addr = app_ips return data except Exception as ex: if app_dir and app_dir.is_dir(): shutil.rmtree(app_dir) self._delete_ns(ns_name) # no effect if there are apps inside raise ValueError(ex)
def _values_from_endpoints(self, values, srv_endpoints): """Retrieve updated values from a list of endpoints.""" new_values = values for srv_endpoint in srv_endpoints: jsonpath = srv_endpoint["jsonpath"] parser = jsonpath_parse(jsonpath) jp_data = dict() for match in parser.find(values): jp_data[str(match.full_path)] = match.value service_name = srv_endpoint["name"] timeout = None if "timeout" in srv_endpoint: timeout = srv_endpoint["timeout"] new_jp_data = self.servicemanager.send_request( service_name, jp_data, timeout) parser.update(new_values, list(new_jp_data.values())[0]) return new_values
def search(context, request): if not context.search_view_enabled: raise HTTPNotFound() qs = request.GET.get('q', '').strip() query = None if qs: searchprovider = context.searchprovider() query = searchprovider.parse_query(qs) limit = int(request.GET.get('limit', 0)) or None offset = int(request.GET.get('offset', 0)) order_by = request.GET.get('order_by', None) select = request.GET.get('select', None) if order_by: order_by = order_by.split(':') if len(order_by) == 1: order_by = order_by + ['asc'] # HACK: +1 to ensure next page links is triggered searchlimit = limit if limit: searchlimit = limit + 1 objs = context.search(query, offset=offset, limit=searchlimit, order_by=order_by, secure=True) # and limit back to actual limit objs = [obj.json() for obj in objs] has_next = False if limit: if len(objs) > limit: has_next = True objs = objs[:limit] if select: expr = jsonpath_parse(select) results = [] for obj in objs: results.append([match.value for match in expr.find(obj['data'])]) else: results = objs params = {} if select: params['select'] = select if limit: params['limit'] = limit if order_by: params['order_by'] = request.GET.get('order_by', '') params['offset'] = offset + (limit or 0) qs = urlencode(params) res = {'results': results, 'q': query} if has_next: res.setdefault('links', []) res['links'].append({ 'rel': 'next', 'href': request.link(context, '+search?%s' % qs) }) if offset > 0: prev_offset = offset - (limit or 0) if prev_offset < 0: prev_offset = 0 params['offset'] = prev_offset qs = urlencode(params) res.setdefault('links', []) res['links'].append({ 'rel': 'previous', 'href': request.link(context, '+search?%s' % qs) }) return res