Browse Source

Merge "Introduce keystoneauth1.loading.adapter"

Jenkins 1 year ago
parent
commit
6cbd95bb8d

+ 6
- 0
keystoneauth1/adapter.py View File

@@ -237,6 +237,7 @@ class Adapter(object):
237 237
     def delete(self, url, **kwargs):
238 238
         return self.request(url, 'DELETE', **kwargs)
239 239
 
240
+    # TODO(efried): Move this to loading.adapter.Adapter
240 241
     @classmethod
241 242
     def register_argparse_arguments(cls, parser, service_type=None):
242 243
         """Attach arguments to a given argparse Parser for Adapters.
@@ -286,6 +287,7 @@ class Adapter(object):
286 287
             default=os.environ.get('OS_API_VERSION', None),
287 288
             help='Which version of the service API to use')
288 289
 
290
+    # TODO(efried): Move this to loading.adapter.Adapter
289 291
     @classmethod
290 292
     def register_service_argparse_arguments(cls, parser, service_type):
291 293
         """Attach arguments to a given argparse Parser for Adapters.
@@ -387,9 +389,13 @@ class LegacyJsonAdapter(Adapter):
387 389
         return resp, body
388 390
 
389 391
 
392
+# TODO(efried): Deprecate this in favor of
393
+#               loading.adapter.register_argparse_arguments
390 394
 def register_adapter_argparse_arguments(*args, **kwargs):
391 395
     return Adapter.register_argparse_arguments(*args, **kwargs)
392 396
 
393 397
 
398
+# TODO(efried): Deprecate this in favor of
399
+#               loading.adapter.register_service_argparse_arguments
394 400
 def register_service_adapter_argparse_arguments(*args, **kwargs):
395 401
     return Adapter.register_service_argparse_arguments(*args, **kwargs)

+ 15
- 0
keystoneauth1/loading/__init__.py View File

@@ -10,6 +10,7 @@
10 10
 # License for the specific language governing permissions and limitations
11 11
 # under the License.
12 12
 
13
+from keystoneauth1.loading import adapter
13 14
 from keystoneauth1.loading.base import *  # noqa
14 15
 from keystoneauth1.loading import cli
15 16
 from keystoneauth1.loading import conf
@@ -32,6 +33,13 @@ register_session_conf_options = session.register_conf_options
32 33
 load_session_from_conf_options = session.load_from_conf_options
33 34
 get_session_conf_options = session.get_conf_options
34 35
 
36
+register_adapter_argparse_arguments = adapter.register_argparse_arguments
37
+register_service_adapter_argparse_arguments = (
38
+    adapter.register_service_argparse_arguments)
39
+register_adapter_conf_options = adapter.register_conf_options
40
+load_adapter_from_conf_options = adapter.load_from_conf_options
41
+get_adapter_conf_options = adapter.get_conf_options
42
+
35 43
 
36 44
 __all__ = (
37 45
     # loading.base
@@ -65,6 +73,13 @@ __all__ = (
65 73
     'load_session_from_conf_options',
66 74
     'get_session_conf_options',
67 75
 
76
+    # adapter
77
+    'register_adapter_argparse_arguments',
78
+    'register_service_adapter_argparse_arguments',
79
+    'register_adapter_conf_options',
80
+    'load_adapter_from_conf_options',
81
+    'get_adapter_conf_options',
82
+
68 83
     # loading.opts
69 84
     'Opt',
70 85
 )

+ 131
- 0
keystoneauth1/loading/adapter.py View File

@@ -0,0 +1,131 @@
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
+
13
+from keystoneauth1 import adapter
14
+from keystoneauth1.loading import _utils
15
+from keystoneauth1.loading import base
16
+
17
+
18
+__all__ = ('register_argparse_arguments',
19
+           'register_service_argparse_arguments',
20
+           'register_conf_options',
21
+           'load_from_conf_options',
22
+           'get_conf_options')
23
+
24
+
25
+class Adapter(base.BaseLoader):
26
+
27
+    @property
28
+    def plugin_class(self):
29
+        return adapter.Adapter
30
+
31
+    def get_options(self):
32
+        return []
33
+
34
+    @staticmethod
35
+    def get_conf_options():
36
+        """Get oslo_config options that are needed for a :py:class:`.Adapter`.
37
+
38
+        These may be useful without being registered for config file generation
39
+        or to manipulate the options before registering them yourself.
40
+
41
+        The options that are set are:
42
+            :service_type:      The default service_type for URL discovery.
43
+            :service_name:      The default service_name for URL discovery.
44
+            :interface:         The default interface for URL discovery.
45
+            :region_name:       The default region_name for URL discovery.
46
+            :endpoint_override: Always use this endpoint URL for requests
47
+                                for this client.
48
+
49
+        :returns: A list of oslo_config options.
50
+        """
51
+        cfg = _utils.get_oslo_config()
52
+
53
+        return [cfg.StrOpt('service-type',
54
+                           help='The default service_type for endpoint URL '
55
+                                'discovery.'),
56
+                cfg.StrOpt('service-name',
57
+                           help='The default service_name for endpoint URL '
58
+                                'discovery.'),
59
+                cfg.StrOpt('interface',
60
+                           help='The default interface for endpoint URL '
61
+                                'discovery.'),
62
+                cfg.StrOpt('region-name',
63
+                           help='The default region_name for endpoint URL '
64
+                                'discovery.'),
65
+                cfg.StrOpt('endpoint-override',
66
+                           help='Always use this endpoint URL for requests '
67
+                                'for this client.'),
68
+                ]
69
+
70
+    def register_conf_options(self, conf, group):
71
+        """Register the oslo_config options that are needed for an Adapter.
72
+
73
+        The options that are set are:
74
+            :service_type:      The default service_type for URL discovery.
75
+            :service_name:      The default service_name for URL discovery.
76
+            :interface:         The default interface for URL discovery.
77
+            :region_name:       The default region_name for URL discovery.
78
+            :endpoint_override: Always use this endpoint URL for requests
79
+                                for this client.
80
+
81
+        :param oslo_config.Cfg conf: config object to register with.
82
+        :param string group: The ini group to register options in.
83
+        :returns: The list of options that was registered.
84
+        """
85
+        opts = self.get_conf_options()
86
+        conf.register_group(_utils.get_oslo_config().OptGroup(group))
87
+        conf.register_opts(opts, group=group)
88
+        return opts
89
+
90
+    def load_from_conf_options(self, conf, group, **kwargs):
91
+        """Create an Adapter object from an oslo_config object.
92
+
93
+        The options must have been previously registered with
94
+        register_conf_options.
95
+
96
+        :param oslo_config.Cfg conf: config object to register with.
97
+        :param string group: The ini group to register options in.
98
+        :param dict kwargs: Additional parameters to pass to Adapter
99
+                            construction.
100
+        :returns: A new Adapter object.
101
+        :rtype: :py:class:`.Adapter`
102
+        """
103
+        c = conf[group]
104
+
105
+        kwargs.setdefault('service_type', c.service_type)
106
+        kwargs.setdefault('service_name', c.service_name)
107
+        kwargs.setdefault('interface', c.interface)
108
+        kwargs.setdefault('region_name', c.region_name)
109
+        kwargs.setdefault('endpoint_override', c.endpoint_override)
110
+
111
+        return self.load_from_options(**kwargs)
112
+
113
+
114
+def register_argparse_arguments(*args, **kwargs):
115
+    return adapter.register_adapter_argparse_arguments(*args, **kwargs)
116
+
117
+
118
+def register_service_argparse_arguments(*args, **kwargs):
119
+    return adapter.register_service_adapter_argparse_arguments(*args, **kwargs)
120
+
121
+
122
+def register_conf_options(*args, **kwargs):
123
+    return Adapter().register_conf_options(*args, **kwargs)
124
+
125
+
126
+def load_from_conf_options(*args, **kwargs):
127
+    return Adapter().load_from_conf_options(*args, **kwargs)
128
+
129
+
130
+def get_conf_options():
131
+    return Adapter.get_conf_options()

+ 51
- 0
keystoneauth1/tests/unit/loading/test_adapter.py View File

@@ -0,0 +1,51 @@
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
+
13
+from oslo_config import cfg
14
+from oslo_config import fixture as config
15
+
16
+from keystoneauth1 import loading
17
+from keystoneauth1.tests.unit.loading import utils
18
+
19
+
20
+class ConfLoadingTests(utils.TestCase):
21
+
22
+    GROUP = 'adaptergroup'
23
+
24
+    def setUp(self):
25
+        super(ConfLoadingTests, self).setUp()
26
+
27
+        self.conf_fx = self.useFixture(config.Config())
28
+        loading.register_adapter_conf_options(self.conf_fx.conf, self.GROUP)
29
+
30
+    def test_load(self):
31
+        self.conf_fx.config(
32
+            service_type='type', service_name='name', interface='iface',
33
+            region_name='region', endpoint_override='endpoint',
34
+            group=self.GROUP)
35
+        adap = loading.load_adapter_from_conf_options(
36
+            self.conf_fx.conf, self.GROUP, session='session', auth='auth')
37
+        self.assertEqual('type', adap.service_type)
38
+        self.assertEqual('name', adap.service_name)
39
+        self.assertEqual('iface', adap.interface)
40
+        self.assertEqual('region', adap.region_name)
41
+        self.assertEqual('endpoint', adap.endpoint_override)
42
+        self.assertEqual('session', adap.session)
43
+        self.assertEqual('auth', adap.auth)
44
+
45
+    def test_get_conf_options(self):
46
+        opts = loading.get_adapter_conf_options()
47
+        for opt in opts:
48
+            self.assertTrue(isinstance(opt, cfg.StrOpt))
49
+        self.assertEqual({'service-type', 'service-name', 'interface',
50
+                          'region-name', 'endpoint-override'},
51
+                         {opt.name for opt in opts})

+ 3
- 4
keystoneauth1/tests/unit/loading/test_cli.py View File

@@ -16,7 +16,6 @@ import uuid
16 16
 import fixtures
17 17
 import mock
18 18
 
19
-from keystoneauth1 import adapter
20 19
 from keystoneauth1 import loading
21 20
 from keystoneauth1.loading import cli
22 21
 from keystoneauth1.tests.unit.loading import utils
@@ -201,7 +200,7 @@ class CliTests(utils.TestCase):
201 200
     def test_adapter_service_type(self):
202 201
         argv = ['--os-service-type', 'compute']
203 202
 
204
-        adapter.Adapter.register_argparse_arguments(self.p, 'compute')
203
+        loading.register_adapter_argparse_arguments(self.p, 'compute')
205 204
 
206 205
         opts = self.p.parse_args(argv)
207 206
         self.assertEqual('compute', opts.os_service_type)
@@ -210,8 +209,8 @@ class CliTests(utils.TestCase):
210 209
     def test_adapter_service_type_per_service(self):
211 210
         argv = ['--os-compute-service-type', 'weirdness']
212 211
 
213
-        adapter.Adapter.register_argparse_arguments(self.p, 'compute')
214
-        adapter.Adapter.register_service_argparse_arguments(self.p, 'compute')
212
+        loading.register_adapter_argparse_arguments(self.p, 'compute')
213
+        loading.register_service_adapter_argparse_arguments(self.p, 'compute')
215 214
 
216 215
         opts = self.p.parse_args(argv)
217 216
         self.assertEqual('compute', opts.os_service_type)

Loading…
Cancel
Save