File: existing.py

package info (click to toggle)
rally-openstack 3.0.0-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,928 kB
  • sloc: python: 53,131; sh: 262; makefile: 38
file content (401 lines) | stat: -rw-r--r-- 15,697 bytes parent folder | download | duplicates (3)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# 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.

import copy
import json
import traceback

from rally.common import cfg
from rally.common import logging
from rally.env import platform
from rally_openstack.common import osclients


LOG = logging.getLogger(__name__)

CONF = cfg.CONF


@platform.configure(name="existing", platform="openstack")
class OpenStack(platform.Platform):
    """Default plugin for OpenStack platform

    It may be used to test any existing OpenStack API compatible cloud.
    """
    VERSION_SCHEMA = {
        "anyOf": [
            {"type": "string", "description": "a string-like version."},
            {"type": "number", "description": "a number-like version."}
        ]
    }

    CONFIG_SCHEMA = {
        "type": "object",
        "definitions": {
            "user": {
                "type": "object",
                "properties": {
                    "username": {"type": "string"},
                    "password": {"type": "string"},
                    "project_name": {"type": "string"},
                    "tenant_name": {"type": "string"},
                    "domain_name": {"type": "string"},
                    "user_domain_name": {"type": "string"},
                    "project_domain_name": {"type": "string"},
                },
                "additionalProperties": False,
                "anyOf": [
                    {
                        "description": "Keystone V2.0 (old-style)",
                        "required": ["username", "password", "tenant_name"]
                    },
                    {
                        "description": "Keystone V3.0 (modern terms)",
                        "required": ["username", "password", "project_name"]
                    }
                ]
            },
            "api_info": {
                "type": "object",
                "patternProperties": {
                    "^(?!neutron)([a-z]+)$": {
                        "type": "object",
                        "properties": {
                            "version": VERSION_SCHEMA,
                            "service_type": {"type": "string"}
                        },
                        "minProperties": 1,
                        "additionalProperties": False
                    },
                    "^neutron$": {
                        "type": "object",
                        "properties": {
                            "version": VERSION_SCHEMA,
                            "service_type": {"type": "string"},
                            "pre_newton": {
                                "type": "boolean",
                                "description": "Whether Neutron API is older "
                                               "then OpenStack Newton or not. "
                                               "Based on this option, some "
                                               "external fields for "
                                               "identifying resources can be "
                                               "applied."
                            }
                        },
                        "minProperties": 1,
                        "additionalProperties": False
                    }
                },
                "additionalProperties": False
            }
        },
        "properties": {
            "auth_url": {"type": "string"},
            "region_name": {"type": "string"},
            "endpoint": {"type": ["string", "null"]},
            "endpoint_type": {"enum": ["public", "internal", "admin", None]},
            "https_insecure": {"type": "boolean"},
            "https_cacert": {"type": "string"},
            "https_cert": {"type": "string"},
            "https_key": {"type": "string"},
            "profiler_hmac_key": {"type": ["string", "null"]},
            "profiler_conn_str": {"type": ["string", "null"]},
            "admin": {"$ref": "#/definitions/user"},
            "users": {
                "type": "array",
                "items": {"$ref": "#/definitions/user"},
                "minItems": 1
            },
            "api_info": {"$ref": "#/definitions/api_info"}
        },
        "anyOf": [
            {
                "description": "The case when the admin is specified and the "
                               "users can be created via 'users@openstack' "
                               "context or 'existing_users' will be used.",
                "required": ["admin", "auth_url"]},
            {
                "description": "The case when the only existing users are "
                               "specified.",
                "required": ["users", "auth_url"]}
        ],
        "additionalProperties": False
    }

    def create(self):
        defaults = {
            "region_name": None,
            "endpoint_type": None,
            "domain_name": None,
            "user_domain_name": cfg.CONF.openstack.user_domain,
            "project_domain_name": cfg.CONF.openstack.project_domain,
            "https_insecure": False,
            "https_cacert": None
        }

        """Converts creds of real OpenStack to internal presentation."""
        new_data = copy.deepcopy(self.spec)
        if "endpoint" in new_data:
            LOG.warning("endpoint is deprecated and not used.")
            del new_data["endpoint"]
        admin = new_data.pop("admin", None)
        users = new_data.pop("users", [])
        api_info = new_data.pop("api_info", None)

        if admin:
            if "project_name" in admin:
                admin["tenant_name"] = admin.pop("project_name")
            admin.update(new_data)
            for k, v in defaults.items():
                admin.setdefault(k, v)
        for user in users:
            if "project_name" in user:
                user["tenant_name"] = user.pop("project_name")
            user.update(new_data)
            for k, v in defaults.items():
                user.setdefault(k, v)
        platform_data = {"admin": admin, "users": users}
        if api_info:
            platform_data["api_info"] = api_info
        return platform_data, {}

    def destroy(self):
        # NOTE(boris-42): No action need to be performed.
        pass

    def cleanup(self, task_uuid=None):
        return {
            "message": "Coming soon!",
            "discovered": 0,
            "deleted": 0,
            "failed": 0,
            "resources": {},
            "errors": []
        }

    def check_health(self):
        """Check whatever platform is alive."""

        users_to_check = self.platform_data["users"]
        if self.platform_data["admin"]:
            users_to_check.append(self.platform_data["admin"])
        clients = None
        for user in users_to_check:
            user["api_info"] = self.platform_data.get("api_info", {})
            try:
                clients = osclients.Clients(user)
                if self.platform_data["admin"] == user:
                    clients.verified_keystone()
                else:
                    clients.keystone()
            except osclients.exceptions.RallyException as e:
                # all rally native exceptions should provide user-friendly
                # messages
                return {"available": False,
                        "message": e.format_message(),
                        "traceback": traceback.format_exc()}
            except Exception:
                d = copy.deepcopy(user)
                d["password"] = "***"
                if logging.is_debug():
                    LOG.exception("Something unexpected had happened while "
                                  "validating OpenStack credentials.")
                if self.platform_data["admin"] == user:
                    user_role = "admin"
                else:
                    user_role = "user"
                return {
                    "available": False,
                    "message": (
                        "Bad %s creds: \n%s"
                        % (user_role,
                           json.dumps(d, indent=2, sort_keys=True))),
                    "traceback": traceback.format_exc()
                }

        for name in self.platform_data.get("api_info", {}):
            if name == "keystone":
                continue
            if not hasattr(clients, name):
                return {
                    "available": False,
                    "message": ("There is no OSClient plugin '%s' for"
                                " communicating with OpenStack API."
                                % name)}
            client = getattr(clients, name)
            try:
                client.validate_version(client.choose_version())
                client.create_client()
            except osclients.exceptions.RallyException as e:
                return {
                    "available": False,
                    "message": ("Invalid setting for '%(client)s':"
                                " %(error)s") % {
                        "client": name, "error": e.format_message()}
                }
            except Exception:
                return {
                    "available": False,
                    "message": ("Can not create '%(client)s' with"
                                " %(version)s version.") % {
                        "client": name,
                        "version": client.choose_version()},
                    "traceback": traceback.format_exc()
                }

        return {"available": True}

    def info(self):
        """Return information about cloud as dict."""
        active_user = (self.platform_data["admin"]
                       or self.platform_data["users"][0])
        services = []
        for stype, name in osclients.Clients(active_user).services().items():
            if name == "__unknown__":
                # `__unknown__` name misleads, let's just not include it...
                services.append({"type": stype})
            else:
                services.append({"type": stype, "name": name})

        return {
            "info": {
                "services": sorted(services, key=lambda x: x["type"])
            }
        }

    def _get_validation_context(self):
        return {"users@openstack": {}}

    @classmethod
    def create_spec_from_sys_environ(cls, sys_environ):
        """Create a spec based on system environment.

        * OS_AUTH_URL - The auth url for OpenStack cluster. Supported both
          versioned and unversioned urls.

        * OS_USERNAME - A user name with admin role to use.

        * OS_PASSWORD - A password for selected user.

        * OS_PROJECT_NAME - Project name to scope to

        * OS_TENANT_NAME - Project name to scope to (an alternative for
          $OS_PROJECT_NAME)

        * OS_USER_DOMAIN_NAME - User domain name (in case of Keystone V3)

        * OS_PROJECT_DOMAIN_NAME - Domain name containing project (in case of
          Keystone V3)

        * OS_ENDPOINT_TYPE - Type of endpoint. Valid endpoint types: admin,
          public, internal

        * OS_INTERFACE - Type of endpoint (an alternative for OS_ENDPOINT_TYPE)

        * OS_REGION_NAME - Authentication region name

        * OS_CACERT - A path to CA certificate bundle file

        * OS_CERT - A path to Client certificate bundle file

        * OS_KEY - A path to Client certificate key file

        * OS_INSECURE - Disable server certificate verification

        * OSPROFILER_HMAC_KEY - HMAC key to use for encrypting context while
          using osprofiler

        * OSPROFILER_CONN_STR - A connection string for OSProfiler collector
          to grep profiling results while building html task reports

        """

        from oslo_utils import strutils

        required_env_vars = ["OS_AUTH_URL", "OS_USERNAME", "OS_PASSWORD"]
        missing_env_vars = [v for v in required_env_vars if
                            v not in sys_environ]
        if missing_env_vars:
            return {"available": False,
                    "message": "The following variable(s) are missed: %s" %
                               missing_env_vars}
        tenant_name = sys_environ.get("OS_PROJECT_NAME",
                                      sys_environ.get("OS_TENANT_NAME"))
        if tenant_name is None:
            return {"available": False,
                    "message": "One of OS_PROJECT_NAME or OS_TENANT_NAME "
                               "should be specified."}

        endpoint_type = sys_environ.get("OS_ENDPOINT_TYPE",
                                        sys_environ.get("OS_INTERFACE"))
        if endpoint_type and "URL" in endpoint_type:
            endpoint_type = endpoint_type.replace("URL", "")

        spec = {
            "auth_url": sys_environ["OS_AUTH_URL"],
            "admin": {
                "username": sys_environ["OS_USERNAME"],
                "password": sys_environ["OS_PASSWORD"],
                "tenant_name": tenant_name
            },
            "endpoint_type": endpoint_type,
            "region_name": sys_environ.get("OS_REGION_NAME", ""),
            "https_cacert": sys_environ.get("OS_CACERT", ""),
            "https_cert": sys_environ.get("OS_CERT", ""),
            "https_key": sys_environ.get("OS_KEY", ""),
            "https_insecure": strutils.bool_from_string(
                sys_environ.get("OS_INSECURE")),
            "profiler_hmac_key": sys_environ.get("OSPROFILER_HMAC_KEY"),
            "profiler_conn_str": sys_environ.get("OSPROFILER_CONN_STR"),
            "api_info": {
                "keystone": {
                    "version": 2,
                    "service_type": "identity"
                }
            }
        }

        user_domain_name = sys_environ.get("OS_USER_DOMAIN_NAME")
        project_domain_name = sys_environ.get("OS_PROJECT_DOMAIN_NAME")
        identity_api_version = sys_environ.get(
            "OS_IDENTITY_API_VERSION", sys_environ.get("IDENTITY_API_VERSION"))
        if (identity_api_version == "3"
                or (identity_api_version is None
                    and (user_domain_name or project_domain_name))):
            # it is Keystone v3 and it has another config scheme
            spec["admin"]["project_name"] = spec["admin"].pop("tenant_name")
            spec["admin"]["user_domain_name"] = user_domain_name or "Default"
            project_domain_name = project_domain_name or "Default"
            spec["admin"]["project_domain_name"] = project_domain_name
            spec["api_info"] = {
                "keystone": {
                    "version": 3,
                    "service_type": "identityv3"
                }
            }

        return {"spec": spec, "available": True, "message": "Available"}

    @classmethod
    def _get_doc(cls):
        doc = cls.__doc__.strip()

        env_vars_docs = cls.create_spec_from_sys_environ.__doc__
        env_vars_description = "\n".join(
            line for line in env_vars_docs.split("\n")[1:]
        )
        doc += (f"\n **The following environment variables are expected for "
                f"creation a Rally environment using sustem environment "
                f"variables**\n{env_vars_description}")
        return doc