Skip to content

Commit

Permalink
[resotolib/resotoworker/resotometrics][fix] Use new search API endpoi…
Browse files Browse the repository at this point in the history
…nt (#767)

* Update search uri

* Update names

* Rename core file
  • Loading branch information
lloesche authored Apr 6, 2022
1 parent 71abfaf commit 43b1a44
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from resotolib.baseplugin import BaseActionPlugin
from resotolib.core.query import CoreGraph
from resotolib.core.search import CoreGraph
from resotolib.graph import Graph
from resoto_plugin_aws.resources import (
AWSCloudwatchAlarm,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from resotolib.baseplugin import BaseActionPlugin
from resotolib.baseresources import EdgeType
from resotolib.logging import log
from resotolib.core.query import CoreGraph
from resotolib.core.search import CoreGraph
from resotolib.graph import Graph
from resoto_plugin_aws.resources import (
AWSELB,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from resotolib.baseresources import EdgeType
from resotolib.baseplugin import BaseActionPlugin
from resotolib.logging import log
from resotolib.core.query import CoreGraph
from resotolib.core.search import CoreGraph
from resotolib.graph import Graph
from resoto_plugin_aws.resources import (
AWSVPC,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from resotolib.baseplugin import BaseActionPlugin
from resotolib.logging import log
from resotolib.core.query import CoreGraph
from resotolib.core.search import CoreGraph
from resotolib.config import Config
from .config import CleanupExpiredConfig
from typing import Dict
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from copy import deepcopy
from resotolib.baseplugin import BaseActionPlugin
from resotolib.logging import log
from resotolib.core.query import CoreGraph
from resotolib.core.search import CoreGraph
from resotolib.core.model_export import node_from_dict
from resotolib.config import Config
from .config import CleanupUntaggedConfig
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from resotolib.baseplugin import BaseActionPlugin
from resotolib.logging import log
from resotolib.core.query import CoreGraph
from resotolib.core.search import CoreGraph
from resotolib.graph import Graph
from resotolib.baseresources import BaseVolume
from resotolib.config import Config
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from copy import deepcopy
from resotolib.logging import log
from resotolib.core.query import CoreGraph
from resotolib.core.search import CoreGraph
from resotolib.baseplugin import BaseActionPlugin
from resotolib.core.model_export import node_from_dict
from resotolib.config import Config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from resotolib.baseplugin import BaseActionPlugin
from resotolib.config import Config
from .config import TagValidatorConfig
from resotolib.core.query import CoreGraph
from resotolib.core.search import CoreGraph
from resotolib.utils import parse_delta, delta_to_str
from typing import Dict

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(
if tls_data:
self.verify = tls_data.ca_cert_path
self.graph_uri = f"{self.base_uri}/graph/{self.graph_name}"
self.query_uri = f"{self.graph_uri}/query/graph"
self.search_uri = f"{self.graph_uri}/search/graph"

def execute(self, command: str):
log.debug(f"Executing command: {command}")
Expand All @@ -43,14 +43,14 @@ def execute(self, command: str):
execute_endpoint += f"?{query_string}"
return self.post(execute_endpoint, command, headers, verify=self.verify)

def query(self, query: str, edge_type: Optional[EdgeType] = None):
log.debug(f"Sending query {query}")
def search(self, search: str, edge_type: Optional[EdgeType] = None):
log.debug(f"Sending search {search}")
headers = {"Accept": "application/x-ndjson"}
query_endpoint = self.query_uri
search_endpoint = self.search_uri
if edge_type is not None:
query_string = urlencode({"edge_type": edge_type.value})
query_endpoint += f"?{query_string}"
return self.post(query_endpoint, query, headers, verify=self.verify)
search_endpoint += f"?{query_string}"
return self.post(search_endpoint, search, headers, verify=self.verify)

@staticmethod
def post(uri, data, headers, verify: Optional[str] = None):
Expand All @@ -59,7 +59,7 @@ def post(uri, data, headers, verify: Optional[str] = None):
r = requests.post(uri, data=data, headers=headers, stream=True, verify=verify)
if r.status_code != 200:
log.error(r.content.decode())
raise RuntimeError(f"Failed to query graph: {r.content.decode()}")
raise RuntimeError(f"Failed to search graph: {r.content.decode()}")
for line in r.iter_lines():
if not line:
continue
Expand All @@ -70,7 +70,7 @@ def post(uri, data, headers, verify: Optional[str] = None):
log.error(e)
continue

def graph(self, query: str) -> Graph:
def graph(self, search: str) -> Graph:
def process_data_line(data: dict, graph: Graph):
"""Process a single line of resotocore graph data"""

Expand All @@ -95,7 +95,7 @@ def process_data_line(data: dict, graph: Graph):

graph = Graph()
node_mapping = {}
for data in self.query(query):
for data in self.search(search):
try:
process_data_line(data, graph)
except ValueError as e:
Expand Down
16 changes: 8 additions & 8 deletions resotometrics/resotometrics/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
from functools import partial
from resotolib.core.actions import CoreActions
from resotometrics.metrics import Metrics, GraphCollector
from resotometrics.query import (
query,
from resotometrics.search import (
search,
get_labels_from_result,
get_metrics_from_result,
get_label_values_from_result,
Expand Down Expand Up @@ -84,9 +84,9 @@ def main() -> None:

resotocore_graph = Config.resotometrics.graph
graph_uri = f"{resotocore.http_uri}/graph/{resotocore_graph}"
query_uri = f"{graph_uri}/query/aggregate?section=reported"
search_uri = f"{graph_uri}/search/aggregate?section=reported"

message_processor = partial(core_actions_processor, metrics, query_uri, tls_data)
message_processor = partial(core_actions_processor, metrics, search_uri, tls_data)
core_actions = CoreActions(
identifier=ArgumentParser.args.subscriber_id,
resotocore_uri=resotocore.http_uri,
Expand Down Expand Up @@ -124,7 +124,7 @@ def main() -> None:


def core_actions_processor(
metrics: Metrics, query_uri: str, tls_data: TLSData, message: dict
metrics: Metrics, search_uri: str, tls_data: TLSData, message: dict
) -> None:
if not isinstance(message, dict):
log.error(f"Invalid message: {message}")
Expand All @@ -137,7 +137,7 @@ def core_actions_processor(
try:
if message_type == "generate_metrics":
start_time = time.time()
update_metrics(metrics, query_uri, tls_data)
update_metrics(metrics, search_uri, tls_data)
run_time = time.time() - start_time
log.debug(f"Updated metrics for {run_time:.2f} seconds")
else:
Expand All @@ -158,7 +158,7 @@ def core_actions_processor(

@metrics_update_metrics.time()
def update_metrics(
metrics: Metrics, query_uri: str, tls_data: Optional[TLSData] = None
metrics: Metrics, search_uri: str, tls_data: Optional[TLSData] = None
) -> None:
metrics_descriptions = Config.resotometrics.metrics
for _, data in metrics_descriptions.items():
Expand All @@ -176,7 +176,7 @@ def update_metrics(
continue

try:
for result in query(metrics_search, query_uri, tls_data=tls_data):
for result in search(metrics_search, search_uri, tls_data=tls_data):
labels = get_labels_from_result(result)
label_values = get_label_values_from_result(result, labels)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
from typing import Iterator, Optional


def query(
query_str: str, query_uri: str, tls_data: Optional[TLSData] = None
def search(
search_str: str, search_uri: str, tls_data: Optional[TLSData] = None
) -> Iterator:
headers = {"Accept": "application/x-ndjson"}
if ArgumentParser.args.psk:
encode_jwt_to_headers(headers, {}, ArgumentParser.args.psk)

r = requests.post(
query_uri,
data=query_str,
search_uri,
data=search_str,
headers=headers,
stream=True,
verify=getattr(tls_data, "verify", None),
)
if r.status_code != 200:
raise RuntimeError(f"Failed to query graph: {r.content.decode('utf-8')}")
raise RuntimeError(f"Failed to search graph: {r.content.decode('utf-8')}")

for line in r.iter_lines():
if not line:
Expand Down
12 changes: 6 additions & 6 deletions resotoworker/resotoworker/cleanup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from resotolib.logging import log
from resotolib.core.query import CoreGraph
from resotolib.core.search import CoreGraph
from resotolib.core.ca import TLSData
from networkx import DiGraph
from resotolib.graph import Graph
Expand All @@ -22,16 +22,16 @@ def cleanup(tls_data: Optional[TLSData] = None):

cg = CoreGraph(tls_data=tls_data)

query_filter = ""
search_filter = ""
if Config.resotoworker.collector and len(Config.resotoworker.collector) > 0:
clouds = '["' + '", "'.join(Config.resotoworker.collector) + '"]'
query_filter = f"and /ancestors.cloud.reported.id in {clouds} "
query = (
search_filter = f"and /ancestors.cloud.reported.id in {clouds} "
search = (
f"/desired.clean == true and /metadata.cleaned != true"
f" and /metadata.protected!=true {query_filter}<-default,delete[0:]->"
f" and /metadata.protected!=true {search_filter}<-default,delete[0:]->"
)

graph = cg.graph(query)
graph = cg.graph(search)
cleaner = Cleaner(graph)
cleaner.cleanup()
cg.patch_nodes(graph)
Expand Down

0 comments on commit 43b1a44

Please sign in to comment.