Skip to content

Commit

Permalink
CTS v3 query traces implemented (#487)
Browse files Browse the repository at this point in the history
CTS v3 query traces implemented

Reviewed-by: Vineet Pruthi
  • Loading branch information
RusselSand authored Nov 12, 2024
1 parent c878261 commit 57fe026
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 0 deletions.
7 changes: 7 additions & 0 deletions doc/source/sdk/proxies/cts_v3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ Key Event Operations
.. autoclass:: otcextensions.sdk.ctsv3.v3._proxy.Proxy
:noindex:
:members: create_key_event, update_key_event, delete_key_event, key_events

Trace Operations
^^^^^^^^^^^^^^^^

.. autoclass:: otcextensions.sdk.ctsv3.v3._proxy.Proxy
:noindex:
:members: traces
1 change: 1 addition & 0 deletions doc/source/sdk/resources/cts/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ CTS Resources
v1/trace
v1/tracker
v3/key_event
v3/trace
13 changes: 13 additions & 0 deletions doc/source/sdk/resources/cts/v3/trace.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
otcextensions.sdk.ctsv3.v3.trace
================================

.. automodule:: otcextensions.sdk.ctsv3.v3.trace

The CTS Trace Class
-----------------------

The ``Trace`` class inherits from
:class:`~otcextensions.sdk.sdk_resource.Resource`.

.. autoclass:: otcextensions.sdk.ctsv3.v3.trace.Trace
:members:
25 changes: 25 additions & 0 deletions examples/ctsv3/list_traces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
List Cloud Trace Service Traces
"""

import openstack

openstack.enable_logging(True)
conn = openstack.connect(cloud='otc')
attrs = {
'trace_type': 'system',
'limit': 2
}
traces = list(conn.ctsv3.traces(**attrs))
print(traces)
9 changes: 9 additions & 0 deletions otcextensions/sdk/ctsv3/v3/_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from openstack import proxy

from otcextensions.sdk.ctsv3.v3 import key_event as _key_event
from otcextensions.sdk.ctsv3.v3 import trace as _trace


class Proxy(proxy.Proxy):
Expand Down Expand Up @@ -58,3 +59,11 @@ def key_events(self, notification_type, **attrs):
return self._list(_key_event.KeyEvent,
base_path=base_path,
**attrs)

def traces(self, **attrs):
"""Query traces
:returns: A generator of trace object of a
:class:`~otcextensions.sdk.ctsv3.v3.trace.Trace`
"""
return self._list(_trace.Trace, paginated=False, **attrs)
63 changes: 63 additions & 0 deletions otcextensions/sdk/ctsv3/v3/trace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from openstack import resource


class BaseUserSpec(resource.Resource):
id = resource.Body('id')
name = resource.Body('name')


class UserInfoSpec(resource.Resource):
id = resource.Body('id')
name = resource.Body('name')
domain = resource.Body('domain', type=BaseUserSpec)


class MetaDataSpec(resource.Resource):
count = resource.Body('count', type=int)
marker = resource.Body('marker')


class Trace(resource.Resource):
base_path = '/traces'
resources_key = 'traces'

allow_list = True

_query_mapping = resource.QueryParameters(
'trace_type', 'limit', 'from', 'next', 'to', 'tracker_name',
'service_type', 'user', 'resource_id', 'resource_name',
'resource_type', 'trace_id', 'trace_name', 'trace_rating')

resource_id = resource.Body('resource_id')
trace_name = resource.Body('trace_name')
trace_rating = resource.Body('trace_rating')
trace_type = resource.Body('trace_type')
request = resource.Body('request')
response = resource.Body('response')
code = resource.Body('code')
api_version = resource.Body('api_version')
message = resource.Body('message')
record_time = resource.Body('record_time')
trace_id = resource.Body('trace_id')
time = resource.Body('time')
user = resource.Body('user', type=UserInfoSpec)
service_type = resource.Body('service_type')
resource_type = resource.Body('resource_type')
source_ip = resource.Body('source_ip')
resource_name = resource.Body('resource_name')
request_id = resource.Body('request_id')
location_info = resource.Body('location_info')
endpoint = resource.Body('endpoint')
resource_url = resource.Body('resource_url')
24 changes: 24 additions & 0 deletions otcextensions/tests/functional/sdk/ctsv3/v3/test_traces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from otcextensions.tests.functional.sdk.ctsv3 import TestCtsv3


class TestTraces(TestCtsv3):

def test_01_list_traces(self):
attrs = {
'trace_type': 'system',
'limit': 2
}
traces = list(self.conn.ctsv3.traces(**attrs))
self.assertGreater(len(traces), 0)
9 changes: 9 additions & 0 deletions otcextensions/tests/unit/sdk/ctsv3/v3/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from otcextensions.sdk.ctsv3.v3 import _proxy
from otcextensions.sdk.ctsv3.v3 import key_event as _key_event
from otcextensions.sdk.ctsv3.v3 import trace as _trace

from openstack.tests.unit import test_proxy_base

Expand Down Expand Up @@ -44,3 +45,11 @@ def test_key_event_list(self):
method_kwargs={'notification_type': 'type'},
expected_kwargs={},
)


class TestTraces(TestCTSv3Proxy):
def test_trace_list(self):
self.verify_list(
self.proxy.traces,
_trace.Trace
)
52 changes: 52 additions & 0 deletions otcextensions/tests/unit/sdk/ctsv3/v3/test_traces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from openstack.tests.unit import base
from otcextensions.sdk.ctsv3.v3 import trace

EXAMPLE = {
"time": 1472148708232,
"user": {
"name": "xxx",
"domain": {
"name": "xxx",
"id": "ded649d814464428ba89d04d7955c93e"
}
},
"response": {
"code": "VPC.0514",
"message": "Update port fail."
},
"code": 200,
"service_type": "VPC",
"resource_type": "eip",
"resource_name": "192.144.163.1",
"resource_id": "d502809d-0d1d-41ce-9690-784282142ccc",
"trace_name": "deleteEip",
"trace_rating": "warning",
"trace_type": "ConsoleAction",
"api_version": "2.0",
"record_time": 1481066128032,
"trace_id": "e001ccb9-bc09-11e6-b00b-4b2a61338db6"
}


class TestTrace(base.TestCase):
def test_basic(self):
sot = trace.Trace()
self.assertEqual('/traces', sot.base_path)
self.assertTrue(sot.allow_list)

def test_make_it(self):
sot = trace.Trace(**EXAMPLE)
self.assertEqual(EXAMPLE['time'], sot.time)
self.assertEqual(EXAMPLE['user']['name'], sot.user.name)

0 comments on commit 57fe026

Please sign in to comment.