Browse Source

Add transport_url property to AMQP adapter

To support oslo.messaging switch to use of transport_url, provide
a formatted transport_url attribute on the AMQP relation adapter.

Change-Id: I02513dbef01f7fde7db65702d2ad64d2aa72ed0f
Closes-Bug: 1817672
changes/79/639279/1
James Page 3 months ago
parent
commit
93f4831605
2 changed files with 27 additions and 1 deletions
  1. 17
    0
      charms_openstack/adapters.py
  2. 10
    1
      unit_tests/test_charms_openstack_adapters.py

+ 17
- 0
charms_openstack/adapters.py View File

@@ -206,6 +206,23 @@ class RabbitMQRelationAdapter(OpenStackRelationAdapter):
206 206
         return '/var/lib/charm/{}/rabbit-client-ca.pem'.format(
207 207
             hookenv.service_name())
208 208
 
209
+    @property
210
+    def transport_url(self):
211
+        """
212
+        oslo.messaging formatted transport URL
213
+
214
+        :returns: oslo.messaging formatted transport URL
215
+        :rtype: string
216
+        """
217
+        hosts = self.relation.rabbitmq_hosts()
218
+        transport_url_hosts = ','.join([
219
+            "{}:{}@{}:5672".format(self.username,
220
+                                   self.password,
221
+                                   ch_ip.format_ipv6_addr(host_) or host_)
222
+            for host_ in hosts
223
+        ])
224
+        return "rabbit://{}/{}".format(transport_url_hosts, self.vhost)
225
+
209 226
 
210 227
 class PeerHARelationAdapter(OpenStackRelationAdapter):
211 228
     """

+ 10
- 1
unit_tests/test_charms_openstack_adapters.py View File

@@ -124,7 +124,7 @@ class TestMemcacheRelationAdapter(unittest.TestCase):
124 124
 
125 125
 class FakeRabbitMQRelation():
126 126
 
127
-    auto_accessors = ['vip', 'private_address']
127
+    auto_accessors = ['vip', 'private_address', 'password']
128 128
     relation_name = 'amqp'
129 129
 
130 130
     def __init__(self, vip=None):
@@ -145,11 +145,15 @@ class FakeRabbitMQRelation():
145 145
     def username(self):
146 146
         return 'fakename'
147 147
 
148
+    def password(self):
149
+        return 'password'
150
+
148 151
 
149 152
 class TestRabbitMQRelationAdapter(unittest.TestCase):
150 153
 
151 154
     def test_class(self):
152 155
         fake = FakeRabbitMQRelation(None)
156
+        adapters.ch_ip.format_ipv6_addr.side_effect = lambda x: x
153 157
         mq = adapters.RabbitMQRelationAdapter(fake)
154 158
         self.assertEqual(mq.vhost, 'vhost')
155 159
         self.assertEqual(mq.username, 'fakename')
@@ -158,6 +162,11 @@ class TestRabbitMQRelationAdapter(unittest.TestCase):
158 162
         # fake._vip = 'vip1'
159 163
         # self.assertEqual(mq.host, 'vip1')
160 164
         self.assertEqual(mq.hosts, 'host1,host2')
165
+        self.assertEqual(
166
+            mq.transport_url,
167
+            'rabbit://fakename:password@host1:5672,'
168
+            'fakename:password@host2:5672/vhost'
169
+        )
161 170
 
162 171
 
163 172
 class FakeAPIConfigAdapter():

Loading…
Cancel
Save