Skip to content

Commit f5134a0

Browse files
committed
feat(natv3): small fix
1 parent 31a76bd commit f5134a0

4 files changed

Lines changed: 134 additions & 5 deletions

File tree

otcextensions/sdk/smn/v2/_proxy.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def topic_attributes(self, topic, **query):
116116
when no resource can be found.
117117
"""
118118
topic = self._get_resource(_topic.Topic, topic)
119-
return self._list(_topic.TopicAttribute, topic_id=topic.id, **query)
119+
return self._list(_topic.TopicAttribute, topic_urn=topic.id, **query)
120120

121121
def update_topic_attribute(self, topic, name="access_policy", **attrs):
122122
"""Update SMN topic attributes
@@ -134,7 +134,7 @@ def update_topic_attribute(self, topic, name="access_policy", **attrs):
134134
when no resource can be found.
135135
"""
136136
topic = self._get_resource(_topic.Topic, topic)
137-
return self._update(_topic.TopicAttribute, id=name, topic_id=topic.id, **attrs)
137+
return self._update(_topic.TopicAttribute, name, topic_urn=topic.id, **attrs)
138138

139139
def delete_topic_attribute(self, topic, name=None):
140140
"""Delete all attributes of a topic
@@ -150,8 +150,8 @@ def delete_topic_attribute(self, topic, name=None):
150150
"""
151151
topic = self._get_resource(_topic.Topic, topic)
152152
if name:
153-
return self._delete(_topic.TopicAttribute, id=name, topic_id=topic.id)
154-
return self._delete(_topic.TopicAttribute, topic_id=topic.id, requires_id=False)
153+
return self._delete(_topic.TopicAttribute, name, topic_urn=topic.id)
154+
return _topic.TopicAttribute().delete_all(self, topic_urn=topic.id)
155155

156156
# ======== Subscription ========
157157
def create_subscription(self, topic, **attrs):

otcextensions/sdk/smn/v2/topic.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# License for the specific language governing permissions and limitations
1111
# under the License.
1212
from openstack import resource
13+
from openstack import exceptions
1314

1415

1516
class Topic(resource.Resource):
@@ -73,4 +74,15 @@ class TopicAttribute(resource.Resource):
7374
#: Unique Request ID
7475
attributes = resource.Body("attributes", type=AttributeSpec)
7576
#: Values of topic attributes
76-
value = resource.Body("value")
77+
attr_value = resource.Body("value")
78+
79+
def delete_all(self, session, topic_urn):
80+
"""Delete all attributes of a topic.
81+
82+
The API supports DELETE without ID which is not
83+
standard REST, so we handle it explicitly.
84+
"""
85+
url = self.base_path % {"topic_urn": topic_urn}
86+
response = session.delete(url)
87+
exceptions.raise_from_response(response)
88+
return response
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
import json
13+
import uuid
14+
15+
import openstack
16+
17+
from otcextensions.tests.functional import base
18+
19+
_logger = openstack._log.setup_logging("openstack")
20+
21+
22+
def _get_access_policy():
23+
return json.dumps({
24+
"Version": "2016-09-07",
25+
"Id": "__default_policy_ID",
26+
"Statement": [
27+
{
28+
"Sid": "__user_pub_0",
29+
"Effect": "Allow",
30+
"Principal": {"CSP": ["*"]},
31+
"Action": ["SMN:Publish", "SMN:QueryTopicDetail"],
32+
"Resource": "*",
33+
}
34+
],
35+
})
36+
37+
38+
class TestTopicAttribute(base.BaseFunctionalTest):
39+
40+
uuid_v4 = uuid.uuid4().hex[:8]
41+
topic_name = "sdk-test-smn-attr-{uuid}".format(uuid=uuid_v4)
42+
topic = None
43+
44+
def setUp(self):
45+
super(TestTopicAttribute, self).setUp()
46+
self.client = self.conn.smn
47+
48+
def tearDown(self):
49+
if self.topic:
50+
try:
51+
self.client.delete_topic(self.topic)
52+
except openstack.exceptions.SDKException as e:
53+
_logger.warning(
54+
"Got exception during clearing resources %s" % e.message
55+
)
56+
super(TestTopicAttribute, self).tearDown()
57+
58+
def _create_topic(self):
59+
try:
60+
self.topic = self.client.create_topic(
61+
name=self.topic_name,
62+
display_name="SDK Test Topic Attribute",
63+
)
64+
except openstack.exceptions.BadRequestException:
65+
self.topic = self.client.find_topic(self.topic_name)
66+
67+
def _set_access_policy(self):
68+
self.client.update_topic_attribute(
69+
self.topic,
70+
name="access_policy",
71+
attr_value=_get_access_policy(),
72+
)
73+
74+
def test_01_update_topic_attribute(self):
75+
self._create_topic()
76+
result = self.client.update_topic_attribute(
77+
self.topic,
78+
name="access_policy",
79+
attr_value=_get_access_policy(),
80+
)
81+
self.assertIsNotNone(result.request_id)
82+
83+
def test_02_list_topic_attributes(self):
84+
self._create_topic()
85+
self._set_access_policy()
86+
attrs = list(self.client.topic_attributes(self.topic))
87+
self.assertGreaterEqual(len(attrs), 1)
88+
89+
def test_03_delete_topic_attribute_by_name(self):
90+
self._create_topic()
91+
self._set_access_policy()
92+
self.client.delete_topic_attribute(
93+
self.topic,
94+
name="access_policy",
95+
)
96+
attrs = list(self.client.topic_attributes(self.topic))
97+
self.assertEqual(len(attrs), 1)
98+
self.assertEqual(attrs[0].attributes.access_policy, "")
99+
100+
def test_04_delete_all_topic_attributes(self):
101+
self._create_topic()
102+
self._set_access_policy()
103+
self.client.delete_topic_attribute(self.topic)
104+
attrs = list(self.client.topic_attributes(self.topic))
105+
self.assertEqual(len(attrs), 1)
106+
self.assertEqual(attrs[0].attributes.access_policy, "")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
fixes:
3+
- |
4+
Fixed ``SMN`` topic attribute operations (``update_topic_attribute``,
5+
``delete_topic_attribute``) which were failing due to incorrect
6+
argument passing to underlying SDK methods. Renamed ``value``
7+
body attribute to ``attr_value`` in ``TopicAttribute`` resource
8+
to avoid naming conflict. Added custom ``delete_all`` method
9+
for deleting all attributes without ID.
10+
- |
11+
Added functional tests for ``SMN`` topic attribute operations.

0 commit comments

Comments
 (0)