python-observabilityclient 0.3.0__tar.gz → 0.5.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/.zuul.yaml +13 -3
  2. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/AUTHORS +3 -0
  3. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/ChangeLog +22 -0
  4. python_observabilityclient-0.5.0/PKG-INFO +83 -0
  5. python_observabilityclient-0.5.0/doc/requirements.txt +3 -0
  6. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/observabilityclient/prometheus_client.py +18 -7
  7. {python-observabilityclient-0.3.0/observabilityclient/v1 → python_observabilityclient-0.5.0/observabilityclient}/rbac.py +58 -52
  8. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/observabilityclient/tests/unit/test_prometheus_client.py +7 -4
  9. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/observabilityclient/tests/unit/test_python_api.py +41 -22
  10. python_observabilityclient-0.5.0/observabilityclient/tests/unit/test_rbac.py +188 -0
  11. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/observabilityclient/tests/unit/test_utils.py +19 -6
  12. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/observabilityclient/utils/metric_utils.py +12 -4
  13. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/observabilityclient/v1/client.py +6 -3
  14. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/observabilityclient/v1/python_api.py +10 -6
  15. python_observabilityclient-0.5.0/python_observabilityclient.egg-info/PKG-INFO +83 -0
  16. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/python_observabilityclient.egg-info/SOURCES.txt +10 -1
  17. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/python_observabilityclient.egg-info/entry_points.txt +0 -1
  18. python_observabilityclient-0.5.0/python_observabilityclient.egg-info/pbr.json +1 -0
  19. python_observabilityclient-0.5.0/python_observabilityclient.egg-info/requires.txt +4 -0
  20. python_observabilityclient-0.5.0/releasenotes/notes/add-root-path-192ac0e6ea99b84f.yaml +9 -0
  21. python_observabilityclient-0.5.0/releasenotes/notes/refactor-rbac-967ed5edcc1ef2a4.yaml +13 -0
  22. python_observabilityclient-0.5.0/releasenotes/notes/remove-py38-acd461c4350f0dca.yaml +5 -0
  23. python_observabilityclient-0.5.0/releasenotes/source/2025.1.rst +6 -0
  24. python_observabilityclient-0.5.0/releasenotes/source/_static/.placeholder +0 -0
  25. python_observabilityclient-0.5.0/releasenotes/source/conf.py +256 -0
  26. python_observabilityclient-0.5.0/releasenotes/source/index.rst +17 -0
  27. python_observabilityclient-0.5.0/releasenotes/source/unreleased.rst +5 -0
  28. python_observabilityclient-0.5.0/requirements.txt +4 -0
  29. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/setup.cfg +2 -22
  30. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/test-requirements.txt +3 -0
  31. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/tox.ini +25 -16
  32. python-observabilityclient-0.3.0/PKG-INFO +0 -81
  33. python-observabilityclient-0.3.0/observabilityclient/tests/unit/test_rbac.py +0 -145
  34. python-observabilityclient-0.3.0/python_observabilityclient.egg-info/PKG-INFO +0 -81
  35. python-observabilityclient-0.3.0/python_observabilityclient.egg-info/pbr.json +0 -1
  36. python-observabilityclient-0.3.0/python_observabilityclient.egg-info/requires.txt +0 -10
  37. python-observabilityclient-0.3.0/requirements.txt +0 -3
  38. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/LICENSE +0 -0
  39. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/README.md +0 -0
  40. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/observabilityclient/__init__.py +0 -0
  41. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/observabilityclient/client.py +0 -0
  42. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/observabilityclient/i18n.py +0 -0
  43. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/observabilityclient/plugin.py +0 -0
  44. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/observabilityclient/tests/functional/__init__.py +0 -0
  45. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/observabilityclient/tests/functional/base.py +0 -0
  46. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/observabilityclient/tests/functional/test_cli.py +0 -0
  47. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/observabilityclient/tests/functional/test_python_api.py +0 -0
  48. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/observabilityclient/tests/unit/__init__.py +0 -0
  49. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/observabilityclient/tests/unit/test_cli.py +0 -0
  50. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/observabilityclient/utils/__init__.py +0 -0
  51. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/observabilityclient/v1/__init__.py +0 -0
  52. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/observabilityclient/v1/base.py +0 -0
  53. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/observabilityclient/v1/cli.py +0 -0
  54. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/pyproject.toml +0 -0
  55. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/python_observabilityclient.egg-info/dependency_links.txt +0 -0
  56. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/python_observabilityclient.egg-info/not-zip-safe +0 -0
  57. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/python_observabilityclient.egg-info/top_level.txt +0 -0
  58. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/setup.py +0 -0
  59. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/tools/fix_ca_bundle.sh +0 -0
  60. {python-observabilityclient-0.3.0 → python_observabilityclient-0.5.0}/tools/install_deps.sh +0 -0
@@ -8,23 +8,33 @@
8
8
  required-projects:
9
9
  - openstack/python-observabilityclient
10
10
  - openstack/ceilometer
11
- - infrawatch/sg-core
11
+ - openstack-k8s-operators/sg-core
12
+ - openstack/devstack-plugin-prometheus
12
13
  timeout: 4200
13
14
  vars:
14
15
  devstack_localrc:
15
16
  USE_PYTHON3: True
16
17
  PROMETHEUS_SERVICE_SCRAPE_TARGETS: prometheus,sg-core
17
18
  CEILOMETER_BACKEND: sg-core
19
+ PROMETHEUS_CONF_DIR: /opt/stack
20
+ PROMETHEUS_CONFIG_FILE: /opt/stack/prometheus.yml
21
+ PROMETHEUS_CLIENT_CONF_DIR: /etc/openstack
18
22
  devstack_plugins:
19
- sg-core: https://github.com/infrawatch/sg-core
23
+ sg-core: https://github.com/openstack-k8s-operators/sg-core
20
24
  ceilometer: https://opendev.org/openstack/ceilometer
25
+ devstack-plugin-prometheus: https://opendev.org/openstack/devstack-plugin-prometheus
26
+ devstack_services:
27
+ node_exporter: false
28
+ zuul_copy_output:
29
+ /etc/prometheus/prometheus.yml: logs
30
+ /etc/openstack/prometheus.yaml: logs
21
31
 
22
32
  - project:
23
33
  queue: telemetry
24
34
  templates:
25
35
  - openstack-python3-jobs
26
36
  - check-requirements
27
- #- release-notes-jobs-python3
37
+ - release-notes-jobs-python3
28
38
  check:
29
39
  jobs:
30
40
  - telemetry-dsvm-integration:
@@ -1,3 +1,4 @@
1
+ Alfredo Moralejo <amoralej@redhat.com>
1
2
  Chris Sibbitt <csibbitt@redhat.com>
2
3
  Erno Kuvaja <jokke@usr.fi>
3
4
  Ghanshyam Mann <gmann@ghanshyammann.com>
@@ -8,4 +9,6 @@ Leif Madsen <lmadsen@redhat.com>
8
9
  Marihan Girgis mgirgisf@redhat.com
9
10
  Martin Magr <mmagr@redhat.com>
10
11
  Martin Mágr <mmagr@redhat.com>
12
+ OpenStack Release Bot <infra-root@openstack.org>
11
13
  Takashi Kajinami <kajinamit@oss.nttdata.com>
14
+ Victoria Martinez de la Cruz <victoria@redhat.com>
@@ -1,6 +1,28 @@
1
1
  CHANGES
2
2
  =======
3
3
 
4
+ 0.5.0
5
+ -----
6
+
7
+ * Add root\_path configuration option
8
+ * Refactor obsC rbac module
9
+ * Enable providing keystone session for Prometheus
10
+ * Additional tests and a fix for rbac code
11
+ * Fix rbac mocking in unit tests
12
+ * Fix observabilityclient-dsvm-functional job
13
+ * Bump hacking
14
+ * Check if the HOME env var is set, otherwise use a default
15
+ * Update master for stable/2025.1
16
+ * Ignore false-positive E231 error
17
+
18
+ 0.4.0
19
+ -----
20
+
21
+ * Fix release note build
22
+ * Remove Python 3.8 support
23
+ * Use requirements.txt solely to manage dependencies
24
+ * Add stestr as test requirement
25
+
4
26
  0.3.0
5
27
  -----
6
28
 
@@ -0,0 +1,83 @@
1
+ Metadata-Version: 2.1
2
+ Name: python-observabilityclient
3
+ Version: 0.5.0
4
+ Summary: OpenStack Observability Client
5
+ Home-page: https://infrawatch.github.io/documentation/
6
+ Author: OpenStack
7
+ Author-email: openstack-discuss@lists.openstack.org
8
+ License: Apache License, Version 2.0
9
+ Classifier: Environment :: Console
10
+ Classifier: Environment :: OpenStack
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Intended Audience :: Information Technology
13
+ Classifier: Intended Audience :: System Administrators
14
+ Classifier: License :: OSI Approved :: Apache Software License
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python
17
+ Classifier: Programming Language :: Python :: Implementation :: CPython
18
+ Classifier: Programming Language :: Python :: 3 :: Only
19
+ Classifier: Programming Language :: Python :: 3
20
+ Classifier: Programming Language :: Python :: 3.9
21
+ Classifier: Programming Language :: Python :: 3.10
22
+ Classifier: Programming Language :: Python :: 3.11
23
+ Classifier: Programming Language :: Python :: 3.12
24
+ Requires-Python: >=3.9
25
+ Description-Content-Type: text/markdown; charset=UTF-8
26
+ License-File: LICENSE
27
+ Requires-Dist: osc-lib>=1.0.1
28
+ Requires-Dist: keystoneauth1>=1.0.0
29
+ Requires-Dist: cliff>=1.14.0
30
+ Requires-Dist: PyYAML>5.1
31
+
32
+ # python-observabilityclient
33
+
34
+ observabilityclient is an OpenStackClient (OSC) plugin implementation that
35
+ implements commands for management of Prometheus.
36
+
37
+ ## Development
38
+
39
+ Install your OpenStack environment and patch your `openstack` client application using python.
40
+
41
+ ```
42
+ # if using standalone, the following commands come after 'sudo dnf install -y python3-tripleoclient'
43
+
44
+ su - stack
45
+
46
+ # clone and install observability client plugin
47
+ git clone https://github.com/infrawatch/python-observabilityclient
48
+ cd python-observabilityclient
49
+ sudo python setup.py install --prefix=/usr
50
+ ```
51
+
52
+ ## Usage
53
+
54
+ Use `openstack metric query somequery` to query for metrics in prometheus.
55
+
56
+ To use the python api do the following:
57
+ ```
58
+ from observabilityclient import client
59
+
60
+ c = client.Client(
61
+ '1', keystone_client.get_session(conf),
62
+ adapter_options={
63
+ 'interface': conf.service_credentials.interface,
64
+ 'region_name': conf.service_credentials.region_name})
65
+ c.query.query("somequery")
66
+ ```
67
+
68
+ ## List of commands
69
+
70
+ openstack metric list - lists all metrics
71
+ openstack metric show - shows current values of a metric
72
+ openstack metric query - queries prometheus and outputs the result
73
+ openstack metric delete - deletes some metrics
74
+ openstack metric snapshot - takes a snapshot of the current data
75
+ openstack metric clean-tombstones - cleans the tsdb tombstones
76
+
77
+ ## List of functions provided by the python library
78
+ c.query.list - lists all metrics
79
+ c.query.show - shows current values of a metric
80
+ c.query.query - queries prometheus and outputs the result
81
+ c.query.delete - deletes some metrics
82
+ c.query.snapshot - takes a snapshot of the current data
83
+ c.query.clean-tombstones - cleans the tsdb tombstones
@@ -0,0 +1,3 @@
1
+ sphinx>=2.1.1 # BSD
2
+ openstackdocstheme>=2.2.1 # Apache-2.0
3
+ reno>=1.6.2 # Apache-2.0
@@ -53,10 +53,18 @@ class PrometheusMetric(object):
53
53
 
54
54
 
55
55
  class PrometheusAPIClient(object):
56
- def __init__(self, host):
56
+ def __init__(self, host, session=None, root_path=""):
57
57
  self._host = host
58
- self._session = requests.Session()
58
+ if not self._host.endswith('/'):
59
+ self._host += '/'
60
+ if session is None:
61
+ self._session = requests.Session()
62
+ else:
63
+ self._session = session
59
64
  self._session.verify = False
65
+ self._root_path = root_path
66
+ if root_path != "" and not self._root_path.endswith('/'):
67
+ self._root_path += '/'
60
68
 
61
69
  def set_ca_cert(self, ca_cert):
62
70
  self._session.verify = ca_cert
@@ -68,11 +76,15 @@ class PrometheusAPIClient(object):
68
76
  def set_basic_auth(self, auth_user, auth_password):
69
77
  self._session.auth = (auth_user, auth_password)
70
78
 
79
+ def _get_url(self, endpoint):
80
+ scheme = 'https' if self._session.verify else 'http'
81
+ return f"{scheme}://{self._host}{self._root_path}api/v1/{endpoint}"
82
+
71
83
  def _get(self, endpoint, params=None):
72
- url = (f"{'https' if self._session.verify else 'http'}://"
73
- f"{self._host}/api/v1/{endpoint}")
84
+ url = self._get_url(endpoint)
74
85
  resp = self._session.get(url, params=params,
75
- headers={'Accept': 'application/json'})
86
+ headers={'Accept': 'application/json',
87
+ 'Accept-Encoding': 'identity'})
76
88
  if resp.status_code != requests.codes.ok:
77
89
  raise PrometheusAPIClientError(resp)
78
90
  decoded = resp.json()
@@ -82,8 +94,7 @@ class PrometheusAPIClient(object):
82
94
  return decoded
83
95
 
84
96
  def _post(self, endpoint, params=None):
85
- url = (f"{'https' if self._session.verify else 'http'}://"
86
- f"{self._host}/api/v1/{endpoint}")
97
+ url = self._get_url(endpoint)
87
98
  resp = self._session.post(url, params=params,
88
99
  headers={'Accept': 'application/json'})
89
100
  if resp.status_code != requests.codes.ok:
@@ -14,32 +14,15 @@
14
14
 
15
15
  import re
16
16
 
17
- from keystoneauth1.exceptions.auth_plugins import MissingAuthPlugin
18
-
19
17
  from observabilityclient.utils.metric_utils import format_labels
20
18
 
21
19
 
22
- class ObservabilityRbacError(Exception):
23
- pass
24
-
25
-
26
- class Rbac(object):
27
- def __init__(self, client, session, disable_rbac=False):
28
- self.client = client
29
- self.session = session
30
- self.disable_rbac = disable_rbac
31
- try:
32
- self.project_id = self.session.get_project_id()
33
- self.default_labels = {
34
- "project": self.project_id
35
- }
36
- self.rbac_init_successful = True
37
- except MissingAuthPlugin:
38
- self.project_id = None
39
- self.default_labels = {
40
- "project": "no-project"
41
- }
42
- self.rbac_init_successful = False
20
+ class PromQLRbac(object):
21
+ def __init__(self, prom_api_client, project_id):
22
+ self.client = prom_api_client
23
+ self.labels = {
24
+ "project": project_id
25
+ }
43
26
 
44
27
  def _find_label_value_end(self, query, start, quote_char):
45
28
  end = start
@@ -71,6 +54,10 @@ class Rbac(object):
71
54
  return end
72
55
 
73
56
  def _find_label_section_end(self, query, start):
57
+ if query[start:].startswith('{}'):
58
+ # We have an empty label section, without any
59
+ # label pair
60
+ return start + 1
74
61
  nearest_curly_brace_pos = None
75
62
  while nearest_curly_brace_pos != -1:
76
63
  pair_end = self._find_label_pair_end(query, start)
@@ -84,23 +71,26 @@ class Rbac(object):
84
71
  # and the "=" or "~" is a part of the next section.
85
72
  return nearest_curly_brace_pos
86
73
  start = pair_end
74
+ # TODO(tkajinam): We should probably raise an exception here because
75
+ # this indicates illegal format without closing } .
87
76
  return -1
88
77
 
89
- def enrich_query(self, query, disable_rbac=False):
90
- """Add rbac labels to queries.
78
+ def _insert_labels(self, query, location, comma=False, braces=False):
79
+ comma_str = ", " if comma else ""
80
+ formatted_labels = format_labels(self.labels)
81
+ labels_str = f"{{{formatted_labels}}}" if braces else formatted_labels
82
+ return (f"{query[:location]}{comma_str}"
83
+ f"{labels_str}"
84
+ f"{query[location:]}")
85
+
86
+ def modify_query(self, query):
87
+ """Add rbac labels to a query.
91
88
 
92
- :param query: The query to enrich
89
+ :param query: The query to modify
93
90
  :type query: str
94
- :param disable_rbac: Disables rbac injection if set to True
95
- :type disable_rbac: boolean
96
91
  """
97
- # TODO(jwysogla): This should be properly tested
98
- if disable_rbac:
99
- return query
100
- labels = self.default_labels
101
-
102
92
  # We need to get all metric names, no matter the rbac
103
- metric_names = self.client.query.list(disable_rbac=False)
93
+ metric_names = self.client.label_values("__name__")
104
94
 
105
95
  # We need to detect the locations of metric names
106
96
  # inside the query
@@ -117,35 +107,51 @@ class Rbac(object):
117
107
  name_end_locations.append(potential_name.end())
118
108
 
119
109
  name_end_locations = sorted(name_end_locations, reverse=True)
110
+ if len(name_end_locations) == 0:
111
+ name_end_locations = [0]
120
112
  for name_end_location in name_end_locations:
121
113
  if (name_end_location < len(query) and
122
114
  query[name_end_location] == "{"):
123
- # There already are some labels
124
- labels_end = self._find_label_section_end(query,
125
- name_end_location)
126
- query = (f"{query[:labels_end]}, "
127
- f"{format_labels(labels)}"
128
- f"{query[labels_end:]}")
115
+ # There is already a label section
116
+ labels_end = self._find_label_section_end(
117
+ query,
118
+ name_end_location
119
+ )
120
+ if labels_end == name_end_location + 1:
121
+ query = self._insert_labels(
122
+ query,
123
+ labels_end,
124
+ comma=False,
125
+ braces=False
126
+ )
127
+ else:
128
+ query = self._insert_labels(
129
+ query,
130
+ labels_end,
131
+ comma=True,
132
+ braces=False
133
+ )
129
134
  else:
130
- query = (f"{query[:name_end_location]}"
131
- f"{{{format_labels(labels)}}}"
132
- f"{query[name_end_location:]}")
135
+ query = self._insert_labels(
136
+ query,
137
+ name_end_location,
138
+ comma=False,
139
+ braces=True
140
+ )
133
141
  return query
134
142
 
135
- def append_rbac(self, query, disable_rbac=False):
143
+ def append_rbac_labels(self, query):
136
144
  """Append rbac labels to queries.
137
145
 
138
- It's a simplified and faster version of enrich_query(). This just
146
+ It's a simplified and faster version of modify_query(). This just
139
147
  appends the labels at the end of the query string. For proper handling
140
148
  of complex queries, where metric names might occure elsewhere than
141
- just at the end, please use the enrich_query() function.
149
+ just at the end, please use the modify_query() function.
142
150
 
143
151
  :param query: The query to append to
144
152
  :type query: str
145
- :param disable_rbac: Disables rbac injection if set to True
146
- :type disable_rbac: boolean
147
153
  """
148
- labels = self.default_labels
149
- if disable_rbac:
150
- return query
151
- return f"{query}{{{format_labels(labels)}}}"
154
+ if any(c in query for c in "{}"):
155
+ return self.modify_query(query)
156
+ else:
157
+ return f"{query}{{{format_labels(self.labels)}}}"
@@ -85,7 +85,8 @@ class PrometheusAPIClientTestBase(testtools.TestCase):
85
85
  class PrometheusAPIClientTest(PrometheusAPIClientTestBase):
86
86
  def test_get(self):
87
87
  url = "test"
88
- expected_url = "http://localhost:9090/api/v1/test"
88
+ root_path = "root_path"
89
+ expected_url = f"http://localhost:9090/{root_path}/api/v1/{url}"
89
90
 
90
91
  params = {"query": "ceilometer_image_size{publisher='localhost'}"}
91
92
  expected_params = params
@@ -93,12 +94,14 @@ class PrometheusAPIClientTest(PrometheusAPIClientTestBase):
93
94
  return_value = self.GoodResponse()
94
95
  with mock.patch.object(requests.Session, 'get',
95
96
  return_value=return_value) as m:
96
- c = client.PrometheusAPIClient("localhost:9090")
97
+ c = client.PrometheusAPIClient("localhost:9090",
98
+ root_path=root_path)
97
99
  c._get(url, params)
98
100
 
99
101
  m.assert_called_with(expected_url,
100
102
  params=expected_params,
101
- headers={'Accept': 'application/json'})
103
+ headers={'Accept': 'application/json',
104
+ 'Accept-Encoding': 'identity'})
102
105
 
103
106
  def test_get_error(self):
104
107
  url = "test"
@@ -120,7 +123,7 @@ class PrometheusAPIClientTest(PrometheusAPIClientTestBase):
120
123
 
121
124
  def test_post(self):
122
125
  url = "test"
123
- expected_url = "http://localhost:9090/api/v1/test"
126
+ expected_url = f"http://localhost:9090/api/v1/{url}"
124
127
 
125
128
  params = {"query": "ceilometer_image_size{publisher='localhost'}"}
126
129
  expected_params = params
@@ -17,11 +17,11 @@ from unittest import mock
17
17
  import testtools
18
18
 
19
19
  from observabilityclient import prometheus_client
20
+ from observabilityclient import rbac
20
21
  from observabilityclient.tests.unit.test_prometheus_client import (
21
22
  MetricListMatcher
22
23
  )
23
24
  from observabilityclient.v1 import python_api
24
- from observabilityclient.v1 import rbac
25
25
 
26
26
 
27
27
  class QueryManagerTest(testtools.TestCase):
@@ -31,9 +31,8 @@ class QueryManagerTest(testtools.TestCase):
31
31
  prom_client = prometheus_client.PrometheusAPIClient("somehost")
32
32
  self.client.prometheus_client = prom_client
33
33
 
34
- self.rbac = mock.Mock(wraps=rbac.Rbac(self.client, mock.Mock()))
35
- self.rbac.default_labels = {'project': 'project_id'}
36
- self.rbac.rbac_init_succesful = True
34
+ self.rbac = mock.Mock(wraps=rbac.PromQLRbac(prom_client, mock.Mock()))
35
+ self.rbac.labels = {'project': 'project_id'}
37
36
 
38
37
  self.manager = python_api.QueryManager(self.client)
39
38
 
@@ -41,15 +40,32 @@ class QueryManagerTest(testtools.TestCase):
41
40
  self.client.query = self.manager
42
41
 
43
42
  def test_list(self):
44
- returned_by_prom = {'data': ['metric1', 'test42', 'abc2']}
43
+ returned_by_prom_rbac = {
44
+ 'data': [
45
+ {
46
+ '__name__': 'metric1',
47
+ 'label1': 'foo'
48
+ },
49
+ {
50
+ '__name__': 'test42',
51
+ 'anotherlabel': 'bar'
52
+ },
53
+ {
54
+ '__name__': 'abc2',
55
+ }
56
+ ]
57
+ }
58
+ returned_by_prom_no_rbac = {'data': ['metric1', 'test42', 'abc2']}
45
59
  expected = ['abc2', 'metric1', 'test42']
46
60
 
47
61
  with mock.patch.object(prometheus_client.PrometheusAPIClient, '_get',
48
- return_value=returned_by_prom):
62
+ return_value=returned_by_prom_rbac):
49
63
  ret1 = self.manager.list()
50
- ret2 = self.manager.list(disable_rbac=True)
51
-
52
64
  self.assertEqual(expected, ret1)
65
+
66
+ with mock.patch.object(prometheus_client.PrometheusAPIClient, '_get',
67
+ return_value=returned_by_prom_no_rbac):
68
+ ret2 = self.manager.list(disable_rbac=True)
53
69
  self.assertEqual(expected, ret2)
54
70
 
55
71
  def test_show(self):
@@ -65,21 +81,23 @@ class QueryManagerTest(testtools.TestCase):
65
81
  }
66
82
  expected = [prometheus_client.PrometheusMetric(returned_by_prom)]
67
83
  expected_matcher = MetricListMatcher(expected)
84
+
68
85
  with mock.patch.object(prometheus_client.PrometheusAPIClient, '_get',
69
86
  return_value=returned_by_prom):
70
- ret1 = self.manager.show(query)
71
- self.rbac.append_rbac.assert_called_with(query,
72
- disable_rbac=False)
73
-
74
87
  ret2 = self.manager.show(query, disable_rbac=True)
75
- self.rbac.append_rbac.assert_called_with(query,
76
- disable_rbac=True)
88
+ self.rbac.append_rbac_labels.assert_not_called()
89
+
90
+ with mock.patch.object(prometheus_client.PrometheusAPIClient, '_get',
91
+ return_value=returned_by_prom):
92
+ ret1 = self.manager.show(query)
93
+ self.rbac.append_rbac_labels.assert_called_with(query)
77
94
 
78
95
  self.assertThat(ret1, expected_matcher)
79
96
  self.assertThat(ret2, expected_matcher)
80
97
 
81
98
  def test_query(self):
82
- query = 'some_metric'
99
+ queried_metric_name = 'some_metric'
100
+ query = queried_metric_name
83
101
  returned_by_prom = {
84
102
  'data': {
85
103
  'resultType': 'non-vector'
@@ -92,14 +110,15 @@ class QueryManagerTest(testtools.TestCase):
92
110
  expected = [prometheus_client.PrometheusMetric(returned_by_prom)]
93
111
  expected_matcher = MetricListMatcher(expected)
94
112
  with mock.patch.object(prometheus_client.PrometheusAPIClient, '_get',
95
- return_value=returned_by_prom):
96
- ret1 = self.manager.query(query)
97
- self.rbac.enrich_query.assert_called_with(query,
98
- disable_rbac=False)
113
+ return_value=returned_by_prom), \
114
+ mock.patch.object(python_api.QueryManager, 'list',
115
+ return_value=queried_metric_name):
116
+
117
+ ret1 = self.manager.query(query, disable_rbac=True)
118
+ self.rbac.modify_query.assert_not_called()
99
119
 
100
- ret2 = self.manager.query(query, disable_rbac=True)
101
- self.rbac.enrich_query.assert_called_with(query,
102
- disable_rbac=True)
120
+ ret2 = self.manager.query(query)
121
+ self.rbac.modify_query.assert_called_with(query)
103
122
 
104
123
  self.assertThat(ret1, expected_matcher)
105
124
  self.assertThat(ret2, expected_matcher)