File: clients.py

package info (click to toggle)
python-tempestconf 3.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 964 kB
  • sloc: python: 4,530; makefile: 18; sh: 9
file content (284 lines) | stat: -rw-r--r-- 11,169 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# Copyright 2016, 2018 Red Hat, Inc.
# All Rights Reserved.
#
# 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 tempest.lib import exceptions
from tempest.lib.services.compute import flavors_client
from tempest.lib.services.compute import hosts_client
from tempest.lib.services.compute import servers_client
from tempest.lib.services.identity.v2 import identity_client
from tempest.lib.services.identity.v2 import roles_client
from tempest.lib.services.identity.v2 import tenants_client
from tempest.lib.services.identity.v2 import users_client
from tempest.lib.services.identity.v3  \
    import identity_client as identity_v3_client
from tempest.lib.services.identity.v3 import projects_client
from tempest.lib.services.identity.v3 import roles_client as roles_v3_client
from tempest.lib.services.identity.v3 import services_client as s_client
from tempest.lib.services.identity.v3 import users_client as users_v3_client
from tempest.lib.services.image.v2 import images_client
from tempest.lib.services.network import networks_client
from tempest.lib.services.network import subnets_client
from tempest.lib.services.object_storage import account_client
try:
    # Since Rocky, volume.v3.services_client is the default
    from tempest.lib.services.volume.v3 import services_client
except ImportError:
    # For backward compatibility
    from tempest.lib.services.volume.v2 import services_client


class ProjectsClient(object):
    """The class is a wrapper for managing projects/tenants.

    It instantiates tempest projects_client and provides methods for creating
    and finding projects (identity version v3)/tenants (identity version v2).
    """
    def __init__(self, auth, catalog_type, identity_region, endpoint_type,
                 identity_version, **default_params):
        self.identity_version = identity_version
        if self.identity_version == "v2":
            self.project_class = tenants_client.TenantsClient
        else:
            self.project_class = projects_client.ProjectsClient
        self.client = self.project_class(auth, catalog_type, identity_region,
                                         endpoint_type, **default_params)

    def get_project_by_name(self, project_name):
        if self.identity_version == "v2":
            projects = self.client.list_tenants()['tenants']
        else:
            projects = self.client.list_projects()['projects']
        for project in projects:
            if project['name'] == project_name:
                return project
        raise exceptions.NotFound(
            'No such project/tenant (%s) in %s' % (project_name, projects))

    def create_project(self, name, description):
        if self.identity_version == "v2":
            self.client.create_tenant(name=name, description=description)
        else:
            self.client.create_project(name=name, description=description)


class ClientManager(object):
    """Manager of various OpenStack API clients.

    Connections to clients are created on-demand, i.e. the client tries to
    connect to the server only when it's being requested.
    """
    def __init__(self, conf, creds):
        """Init method of ClientManager.

        :param conf: TempestConf object
        :param creds: Credentials object
        """
        self.identity_region = creds.identity_region
        self.auth_provider = creds.get_auth_provider()

        default_params = creds.get_ssl_certificate_validation()
        compute_params = self._get_compute_params(conf)
        compute_params.update(default_params)

        catalog_type = conf.get_defaulted('identity', 'catalog_type')

        self.identity = self.get_identity_client(
            creds.identity_version,
            catalog_type,
            default_params)

        self.projects = ProjectsClient(
            self.auth_provider,
            conf.get_defaulted('identity', 'catalog_type'),
            self.identity_region,
            'publicURL',
            creds.identity_version,
            **default_params)

        self.set_roles_client(
            auth=self.auth_provider,
            identity_version=creds.identity_version,
            catalog_type=catalog_type,
            endpoint_type='publicURL',
            default_params=default_params)

        self.hosts_client = hosts_client.HostsClient(
            self.auth_provider,
            conf.get_defaulted('compute', 'catalog_type'),
            self.identity_region,
            **default_params)

        self.accounts = account_client.AccountClient(
            self.auth_provider,
            conf.get_defaulted('object-storage', 'catalog_type'),
            self.identity_region,
            **default_params)

        self.set_users_client(
            auth=self.auth_provider,
            identity_version=creds.identity_version,
            catalog_type=catalog_type,
            endpoint_type='publicURL',
            default_params=default_params)

        self.images = images_client.ImagesClient(
            self.auth_provider,
            conf.get_defaulted('image', 'catalog_type'),
            self.identity_region,
            **default_params)

        self.servers = servers_client.ServersClient(self.auth_provider,
                                                    **compute_params)
        self.flavors = flavors_client.FlavorsClient(self.auth_provider,
                                                    **compute_params)

        self.service_client = s_client.ServicesClient(
            self.auth_provider,
            conf.get_defaulted('identity', 'catalog_type'),
            self.identity_region,
            **default_params)

        self.volume_client = services_client.ServicesClient(
            self.auth_provider,
            conf.get_defaulted('volume', 'catalog_type'),
            self.identity_region,
            **default_params)

        self.networks = networks_client.NetworksClient(
            self.auth_provider,
            conf.get_defaulted('network', 'catalog_type'),
            self.identity_region,
            endpoint_type=conf.get_defaulted('network',
                                             'endpoint_type'),
            **default_params)

        self.subnets = subnets_client.SubnetsClient(
            self.auth_provider,
            conf.get_defaulted('network', 'catalog_type'),
            self.identity_region,
            endpoint_type=conf.get_defaulted('network',
                                             'endpoint_type'),
            **default_params)

        # Set admin project id needed for keystone v3 tests.
        if creds.admin:
            project = self.projects.get_project_by_name(creds.project_name)
            conf.set('auth', 'admin_project_id', project['id'])

    def _get_compute_params(self, conf):
        compute_params = {
            'service': conf.get_defaulted('compute', 'catalog_type'),
            'region': self.identity_region,
            'endpoint_type': conf.get_defaulted('compute', 'endpoint_type')
        }
        return compute_params

    def get_identity_client(self, identity_version, catalog_type,
                            default_params):
        """Obtain identity client.

        :type identity_version: string
        :type catalog_type: string
        :type default_params: dict
        """
        if "v3" in identity_version:
            return identity_v3_client.IdentityClient(
                self.auth_provider,
                catalog_type,
                self.identity_region, endpoint_type='publicURL',
                **default_params)
        else:
            return identity_client.IdentityClient(
                self.auth_provider,
                catalog_type,
                self.identity_region, endpoint_type='publicURL',
                **default_params)

    def get_service_client(self, service_name):
        """Returns name of the service's client.

        :type service_name: string
        :rtype: client object or None when the client doesn't exist
        """
        # TODO(arxcruz): This function is under used, it should return
        # a dictionary of all services for a particular client, for
        # example, we need hosts_client and flavors_client for compute
        # should return {'hosts': self.hosts_client, 'flavors': self.flavors }
        # and so on.
        if service_name == "image":
            return self.images
        elif service_name in ["network", "object-store"]:
            # return whole ClientManager object because NetworkService
            # currently needs to have an access to get_neutron/nova_client
            # methods which are chosen according to neutron presence
            return self
        elif service_name == "compute":
            return self.hosts_client
        elif "volume" in service_name:
            return self.volume_client
        elif service_name == "metering":
            return self.service_client
        elif service_name == "orchestration":
            return {'projects': self.projects,
                    'roles': self.roles,
                    'users': self.users,
                    'networks': self.networks,
                    'subnets': self.subnets}
        else:
            return None

    def set_users_client(self, auth, identity_version, catalog_type,
                         endpoint_type, default_params):
        """Sets users client.

        :param auth: auth provider
        :type auth: auth.KeystoneV2AuthProvider (or V3)
        :type identity_version: string
        :type catalog_type: string
        :type endpoint_type: string
        :type default_params: dict
        """
        users_class = users_client.UsersClient
        if "v3" in identity_version:
            users_class = users_v3_client.UsersClient

        self.users = users_class(
            auth,
            catalog_type,
            self.identity_region,
            endpoint_type=endpoint_type,
            **default_params)

    def set_roles_client(self, auth, identity_version, catalog_type,
                         endpoint_type, default_params):
        """Sets roles client.

        :param auth: auth provider
        :type auth: auth.KeystoneV2AuthProvider (or V3)
        :type identity_version: string
        :type catalog_type: string
        :type endpoint_type: string
        :type default_params: dict
        """
        roles_class = roles_client.RolesClient
        if "v3" in identity_version:
            roles_class = roles_v3_client.RolesClient

        self.roles = roles_class(
            auth,
            catalog_type,
            self.identity_region,
            endpoint_type=endpoint_type,
            **default_params)