| 1 |
# Copyright 2016, 2018 Red Hat, Inc. |
| 2 |
# All Rights Reserved. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
|
| 16 |
from tempest.lib import exceptions |
| 17 |
from tempest.lib.services.compute import flavors_client |
| 18 |
from tempest.lib.services.compute import hosts_client |
| 19 |
from tempest.lib.services.compute import servers_client |
| 20 |
from tempest.lib.services.identity.v2 import identity_client |
| 21 |
from tempest.lib.services.identity.v2 import roles_client |
| 22 |
from tempest.lib.services.identity.v2 import tenants_client |
| 23 |
from tempest.lib.services.identity.v2 import users_client |
| 24 |
from tempest.lib.services.identity.v3 \ |
| 25 |
import identity_client as identity_v3_client |
| 26 |
from tempest.lib.services.identity.v3 import projects_client |
| 27 |
from tempest.lib.services.identity.v3 import roles_client as roles_v3_client |
| 28 |
from tempest.lib.services.identity.v3 import services_client as s_client |
| 29 |
from tempest.lib.services.identity.v3 import users_client as users_v3_client |
| 30 |
from tempest.lib.services.image.v2 import images_client |
| 31 |
from tempest.lib.services.network import networks_client |
| 32 |
from tempest.lib.services.network import subnets_client |
| 33 |
from tempest.lib.services.object_storage import account_client |
| 34 |
try: |
| 35 |
# Since Rocky, volume.v3.services_client is the default |
| 36 |
from tempest.lib.services.volume.v3 import services_client |
| 37 |
except ImportError: |
| 38 |
# For backward compatibility |
| 39 |
from tempest.lib.services.volume.v2 import services_client |
| 40 |
|
| 41 |
|
| 42 |
class ProjectsClient(object): |
| 43 |
"""The class is a wrapper for managing projects/tenants. |
| 44 |
|
| 45 |
It instantiates tempest projects_client and provides methods for creating |
| 46 |
and finding projects (identity version v3)/tenants (identity version v2). |
| 47 |
""" |
| 48 |
def __init__(self, auth, catalog_type, identity_region, endpoint_type, |
| 49 |
identity_version, **default_params): |
| 50 |
self.identity_version = identity_version |
| 51 |
if self.identity_version == "v2": |
| 52 |
self.project_class = tenants_client.TenantsClient |
| 53 |
else: |
| 54 |
self.project_class = projects_client.ProjectsClient |
| 55 |
self.client = self.project_class(auth, catalog_type, identity_region, |
| 56 |
endpoint_type, **default_params) |
| 57 |
|
| 58 |
def get_project_by_name(self, project_name): |
| 59 |
if self.identity_version == "v2": |
| 60 |
projects = self.client.list_tenants()['tenants'] |
| 61 |
else: |
| 62 |
projects = self.client.list_projects()['projects'] |
| 63 |
for project in projects: |
| 64 |
if project['name'] == project_name: |
| 65 |
return project |
| 66 |
raise exceptions.NotFound( |
| 67 |
'No such project/tenant (%s) in %s' % (project_name, projects)) |
| 68 |
|
| 69 |
def create_project(self, name, description): |
| 70 |
if self.identity_version == "v2": |
| 71 |
self.client.create_tenant(name=name, description=description) |
| 72 |
else: |
| 73 |
self.client.create_project(name=name, description=description) |
| 74 |
|
| 75 |
|
| 76 |
class ClientManager(object): |
| 77 |
"""Manager of various OpenStack API clients. |
| 78 |
|
| 79 |
Connections to clients are created on-demand, i.e. the client tries to |
| 80 |
connect to the server only when it's being requested. |
| 81 |
""" |
| 82 |
def __init__(self, conf, creds): |
| 83 |
"""Init method of ClientManager. |
| 84 |
|
| 85 |
:param conf: TempestConf object |
| 86 |
:param creds: Credentials object |
| 87 |
""" |
| 88 |
self.identity_region = creds.identity_region |
| 89 |
self.auth_provider = creds.get_auth_provider() |
| 90 |
|
| 91 |
default_params = creds.get_ssl_certificate_validation() |
| 92 |
compute_params = self._get_compute_params(conf) |
| 93 |
compute_params.update(default_params) |
| 94 |
|
| 95 |
catalog_type = conf.get_defaulted('identity', 'catalog_type') |
| 96 |
|
| 97 |
self.identity = self.get_identity_client( |
| 98 |
creds.identity_version, |
| 99 |
catalog_type, |
| 100 |
default_params) |
| 101 |
|
| 102 |
self.projects = ProjectsClient( |
| 103 |
self.auth_provider, |
| 104 |
conf.get_defaulted('identity', 'catalog_type'), |
| 105 |
self.identity_region, |
| 106 |
'publicURL', |
| 107 |
creds.identity_version, |
| 108 |
**default_params) |
| 109 |
|
| 110 |
self.set_roles_client( |
| 111 |
auth=self.auth_provider, |
| 112 |
identity_version=creds.identity_version, |
| 113 |
catalog_type=catalog_type, |
| 114 |
endpoint_type='publicURL', |
| 115 |
default_params=default_params) |
| 116 |
|
| 117 |
self.hosts_client = hosts_client.HostsClient( |
| 118 |
self.auth_provider, |
| 119 |
conf.get_defaulted('compute', 'catalog_type'), |
| 120 |
self.identity_region, |
| 121 |
**default_params) |
| 122 |
|
| 123 |
self.accounts = account_client.AccountClient( |
| 124 |
self.auth_provider, |
| 125 |
conf.get_defaulted('object-storage', 'catalog_type'), |
| 126 |
self.identity_region, |
| 127 |
**default_params) |
| 128 |
|
| 129 |
self.set_users_client( |
| 130 |
auth=self.auth_provider, |
| 131 |
identity_version=creds.identity_version, |
| 132 |
catalog_type=catalog_type, |
| 133 |
endpoint_type='publicURL', |
| 134 |
default_params=default_params) |
| 135 |
|
| 136 |
self.images = images_client.ImagesClient( |
| 137 |
self.auth_provider, |
| 138 |
conf.get_defaulted('image', 'catalog_type'), |
| 139 |
self.identity_region, |
| 140 |
**default_params) |
| 141 |
|
| 142 |
self.servers = servers_client.ServersClient(self.auth_provider, |
| 143 |
**compute_params) |
| 144 |
self.flavors = flavors_client.FlavorsClient(self.auth_provider, |
| 145 |
**compute_params) |
| 146 |
|
| 147 |
self.service_client = s_client.ServicesClient( |
| 148 |
self.auth_provider, |
| 149 |
conf.get_defaulted('identity', 'catalog_type'), |
| 150 |
self.identity_region, |
| 151 |
**default_params) |
| 152 |
|
| 153 |
self.volume_client = services_client.ServicesClient( |
| 154 |
self.auth_provider, |
| 155 |
conf.get_defaulted('volume', 'catalog_type'), |
| 156 |
self.identity_region, |
| 157 |
**default_params) |
| 158 |
|
| 159 |
self.networks = networks_client.NetworksClient( |
| 160 |
self.auth_provider, |
| 161 |
conf.get_defaulted('network', 'catalog_type'), |
| 162 |
self.identity_region, |
| 163 |
endpoint_type=conf.get_defaulted('network', |
| 164 |
'endpoint_type'), |
| 165 |
**default_params) |
| 166 |
|
| 167 |
self.subnets = subnets_client.SubnetsClient( |
| 168 |
self.auth_provider, |
| 169 |
conf.get_defaulted('network', 'catalog_type'), |
| 170 |
self.identity_region, |
| 171 |
endpoint_type=conf.get_defaulted('network', |
| 172 |
'endpoint_type'), |
| 173 |
**default_params) |
| 174 |
|
| 175 |
# Set admin project id needed for keystone v3 tests. |
| 176 |
if creds.admin: |
| 177 |
project = self.projects.get_project_by_name(creds.project_name) |
| 178 |
conf.set('auth', 'admin_project_id', project['id']) |
| 179 |
|
| 180 |
def _get_compute_params(self, conf): |
| 181 |
compute_params = { |
| 182 |
'service': conf.get_defaulted('compute', 'catalog_type'), |
| 183 |
'region': self.identity_region, |
| 184 |
'endpoint_type': conf.get_defaulted('compute', 'endpoint_type') |
| 185 |
} |
| 186 |
return compute_params |
| 187 |
|
| 188 |
def get_identity_client(self, identity_version, catalog_type, |
| 189 |
default_params): |
| 190 |
"""Obtain identity client. |
| 191 |
|
| 192 |
:type identity_version: string |
| 193 |
:type catalog_type: string |
| 194 |
:type default_params: dict |
| 195 |
""" |
| 196 |
if "v3" in identity_version: |
| 197 |
return identity_v3_client.IdentityClient( |
| 198 |
self.auth_provider, |
| 199 |
catalog_type, |
| 200 |
self.identity_region, endpoint_type='publicURL', |
| 201 |
**default_params) |
| 202 |
else: |
| 203 |
return identity_client.IdentityClient( |
| 204 |
self.auth_provider, |
| 205 |
catalog_type, |
| 206 |
self.identity_region, endpoint_type='publicURL', |
| 207 |
**default_params) |
| 208 |
|
| 209 |
def get_service_client(self, service_name): |
| 210 |
"""Returns name of the service's client. |
| 211 |
|
| 212 |
:type service_name: string |
| 213 |
:rtype: client object or None when the client doesn't exist |
| 214 |
""" |
| 215 |
# TODO(arxcruz): This function is under used, it should return |
| 216 |
# a dictionary of all services for a particular client, for |
| 217 |
# example, we need hosts_client and flavors_client for compute |
| 218 |
# should return {'hosts': self.hosts_client, 'flavors': self.flavors } |
| 219 |
# and so on. |
| 220 |
if service_name == "image": |
| 221 |
return self.images |
| 222 |
elif service_name in ["network", "object-store"]: |
| 223 |
# return whole ClientManager object because NetworkService |
| 224 |
# currently needs to have an access to get_neutron/nova_client |
| 225 |
# methods which are chosen according to neutron presence |
| 226 |
return self |
| 227 |
elif service_name == "compute": |
| 228 |
return self.hosts_client |
| 229 |
elif "volume" in service_name: |
| 230 |
return self.volume_client |
| 231 |
elif service_name == "metering": |
| 232 |
return self.service_client |
| 233 |
elif service_name == "orchestration": |
| 234 |
return {'projects': self.projects, |
| 235 |
'roles': self.roles, |
| 236 |
'users': self.users, |
| 237 |
'networks': self.networks, |
| 238 |
'subnets': self.subnets} |
| 239 |
else: |
| 240 |
return None |
| 241 |
|
| 242 |
def set_users_client(self, auth, identity_version, catalog_type, |
| 243 |
endpoint_type, default_params): |
| 244 |
"""Sets users client. |
| 245 |
|
| 246 |
:param auth: auth provider |
| 247 |
:type auth: auth.KeystoneV2AuthProvider (or V3) |
| 248 |
:type identity_version: string |
| 249 |
:type catalog_type: string |
| 250 |
:type endpoint_type: string |
| 251 |
:type default_params: dict |
| 252 |
""" |
| 253 |
users_class = users_client.UsersClient |
| 254 |
if "v3" in identity_version: |
| 255 |
users_class = users_v3_client.UsersClient |
| 256 |
|
| 257 |
self.users = users_class( |
| 258 |
auth, |
| 259 |
catalog_type, |
| 260 |
self.identity_region, |
| 261 |
endpoint_type=endpoint_type, |
| 262 |
**default_params) |
| 263 |
|
| 264 |
def set_roles_client(self, auth, identity_version, catalog_type, |
| 265 |
endpoint_type, default_params): |
| 266 |
"""Sets roles client. |
| 267 |
|
| 268 |
:param auth: auth provider |
| 269 |
:type auth: auth.KeystoneV2AuthProvider (or V3) |
| 270 |
:type identity_version: string |
| 271 |
:type catalog_type: string |
| 272 |
:type endpoint_type: string |
| 273 |
:type default_params: dict |
| 274 |
""" |
| 275 |
roles_class = roles_client.RolesClient |
| 276 |
if "v3" in identity_version: |
| 277 |
roles_class = roles_v3_client.RolesClient |
| 278 |
|
| 279 |
self.roles = roles_class( |
| 280 |
auth, |
| 281 |
catalog_type, |
| 282 |
self.identity_region, |
| 283 |
endpoint_type=endpoint_type, |
| 284 |
**default_params) |