| 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_log import log as logging |
| 14 |
from tempest.lib import exceptions |
| 15 |
|
| 16 |
from magnum_tempest_plugin.common import client |
| 17 |
from magnum_tempest_plugin.common import utils |
| 18 |
from magnum_tempest_plugin.tests.api.v1.models import cluster_id_model |
| 19 |
from magnum_tempest_plugin.tests.api.v1.models import cluster_model |
| 20 |
|
| 21 |
|
| 22 |
class ClusterClient(client.MagnumClient): |
| 23 |
"""Encapsulates REST calls and maps JSON to/from models""" |
| 24 |
|
| 25 |
LOG = logging.getLogger(__name__) |
| 26 |
|
| 27 |
@classmethod |
| 28 |
def clusters_uri(cls, filters=None): |
| 29 |
"""Construct clusters uri with optional filters |
| 30 |
|
| 31 |
:param filters: Optional k:v dict that's converted to url query |
| 32 |
:returns: url string |
| 33 |
""" |
| 34 |
|
| 35 |
url = "/clusters" |
| 36 |
if filters: |
| 37 |
url = cls.add_filters(url, filters) |
| 38 |
return url |
| 39 |
|
| 40 |
@classmethod |
| 41 |
def cluster_uri(cls, cluster_id): |
| 42 |
"""Construct cluster uri |
| 43 |
|
| 44 |
:param cluster_id: cluster uuid or name |
| 45 |
:returns: url string |
| 46 |
""" |
| 47 |
|
| 48 |
return "{0}/{1}".format(cls.clusters_uri(), cluster_id) |
| 49 |
|
| 50 |
def list_clusters(self, filters=None, **kwargs): |
| 51 |
"""Makes GET /clusters request and returns ClusterCollection |
| 52 |
|
| 53 |
Abstracts REST call to return all clusters |
| 54 |
|
| 55 |
:param filters: Optional k:v dict that's converted to url query |
| 56 |
:returns: response object and ClusterCollection object |
| 57 |
""" |
| 58 |
|
| 59 |
resp, body = self.get(self.clusters_uri(filters), **kwargs) |
| 60 |
return self.deserialize(resp, body, cluster_model.ClusterCollection) |
| 61 |
|
| 62 |
def get_cluster(self, cluster_id, **kwargs): |
| 63 |
"""Makes GET /cluster request and returns ClusterEntity |
| 64 |
|
| 65 |
Abstracts REST call to return a single cluster based on uuid or name |
| 66 |
|
| 67 |
:param cluster_id: cluster uuid or name |
| 68 |
:returns: response object and ClusterCollection object |
| 69 |
""" |
| 70 |
|
| 71 |
resp, body = self.get(self.cluster_uri(cluster_id)) |
| 72 |
return self.deserialize(resp, body, cluster_model.ClusterEntity) |
| 73 |
|
| 74 |
def post_cluster(self, model, **kwargs): |
| 75 |
"""Makes POST /cluster request and returns ClusterIdEntity |
| 76 |
|
| 77 |
Abstracts REST call to create new cluster |
| 78 |
|
| 79 |
:param model: ClusterEntity |
| 80 |
:returns: response object and ClusterIdEntity object |
| 81 |
""" |
| 82 |
|
| 83 |
resp, body = self.post( |
| 84 |
self.clusters_uri(), |
| 85 |
body=model.to_json(), **kwargs) |
| 86 |
return self.deserialize(resp, body, cluster_id_model.ClusterIdEntity) |
| 87 |
|
| 88 |
def patch_cluster(self, cluster_id, clusterpatch_listmodel, **kwargs): |
| 89 |
"""Makes PATCH /cluster request and returns ClusterIdEntity |
| 90 |
|
| 91 |
Abstracts REST call to update cluster attributes |
| 92 |
|
| 93 |
:param cluster_id: UUID of cluster |
| 94 |
:param clusterpatch_listmodel: ClusterPatchCollection |
| 95 |
:returns: response object and ClusterIdEntity object |
| 96 |
""" |
| 97 |
|
| 98 |
resp, body = self.patch( |
| 99 |
self.cluster_uri(cluster_id), |
| 100 |
body=clusterpatch_listmodel.to_json(), **kwargs) |
| 101 |
return self.deserialize(resp, body, cluster_id_model.ClusterIdEntity) |
| 102 |
|
| 103 |
def delete_cluster(self, cluster_id, **kwargs): |
| 104 |
"""Makes DELETE /cluster request and returns response object |
| 105 |
|
| 106 |
Abstracts REST call to delete cluster based on uuid or name |
| 107 |
|
| 108 |
:param cluster_id: UUID or name of cluster |
| 109 |
:returns: response object |
| 110 |
""" |
| 111 |
|
| 112 |
return self.delete(self.cluster_uri(cluster_id), **kwargs) |
| 113 |
|
| 114 |
def wait_for_cluster_to_delete(self, cluster_id): |
| 115 |
utils.wait_for_condition( |
| 116 |
lambda: self.does_cluster_not_exist(cluster_id), 10, 300) |
| 117 |
|
| 118 |
def wait_for_created_cluster(self, cluster_id, delete_on_error=True, |
| 119 |
timeout=3600): |
| 120 |
try: |
| 121 |
utils.wait_for_condition( |
| 122 |
lambda: self.does_cluster_exist(cluster_id), 10, timeout) |
| 123 |
except Exception: |
| 124 |
# In error state. Clean up the cluster id if desired |
| 125 |
self.LOG.error('Cluster %s entered an exception state.', |
| 126 |
cluster_id) |
| 127 |
if delete_on_error: |
| 128 |
self.LOG.warning('We will attempt to delete clusters now.') |
| 129 |
self.delete_cluster(cluster_id) |
| 130 |
self.wait_for_cluster_to_delete(cluster_id) |
| 131 |
raise |
| 132 |
|
| 133 |
def wait_for_final_state(self, cluster_id): |
| 134 |
utils.wait_for_condition( |
| 135 |
lambda: self.is_cluster_in_final_state(cluster_id), 10, 3600) |
| 136 |
|
| 137 |
def is_cluster_in_final_state(self, cluster_id): |
| 138 |
try: |
| 139 |
resp, model = self.get_cluster(cluster_id) |
| 140 |
if model.status in ['CREATED', 'CREATE_COMPLETE', |
| 141 |
'ERROR', 'CREATE_FAILED']: |
| 142 |
self.LOG.info('Cluster %s succeeded.', cluster_id) |
| 143 |
return True |
| 144 |
else: |
| 145 |
return False |
| 146 |
except exceptions.NotFound: |
| 147 |
self.LOG.warning('Cluster %s is not found.', cluster_id) |
| 148 |
return False |
| 149 |
|
| 150 |
def does_cluster_exist(self, cluster_id): |
| 151 |
try: |
| 152 |
resp, model = self.get_cluster(cluster_id) |
| 153 |
if model.status in ['CREATED', 'CREATE_COMPLETE']: |
| 154 |
self.LOG.info('Cluster %s is created.', cluster_id) |
| 155 |
return True |
| 156 |
elif model.status in ['ERROR', 'CREATE_FAILED']: |
| 157 |
self.LOG.error('Cluster %s is in failed state, status_reason: ' |
| 158 |
'%s', cluster_id, model.status_reason) |
| 159 |
raise exceptions.ServerFault( |
| 160 |
"Got into an error condition: %s for %s" % |
| 161 |
(model.status, cluster_id)) |
| 162 |
else: |
| 163 |
return False |
| 164 |
except exceptions.NotFound: |
| 165 |
self.LOG.warning('Cluster %s is not found.', cluster_id) |
| 166 |
return False |
| 167 |
|
| 168 |
def does_cluster_not_exist(self, cluster_id): |
| 169 |
try: |
| 170 |
resp, model = self.get_cluster(cluster_id) |
| 171 |
if model.status in ['ERROR', 'DELETE_FAILED']: |
| 172 |
self.LOG.error('Cluster %s is in failed state, status_reason: ' |
| 173 |
'%s', cluster_id, model.status_reason) |
| 174 |
raise exceptions.ServerFault( |
| 175 |
"Got into an error condition: %s for %s" % |
| 176 |
(model.status, cluster_id)) |
| 177 |
except exceptions.NotFound: |
| 178 |
self.LOG.warning('Cluster %s is not found.', cluster_id) |
| 179 |
return True |
| 180 |
except exceptions.BadRequest: |
| 181 |
self.LOG.warning('Cluster %s returned a bad state. Assuming ' |
| 182 |
'temporary failure and the cluster still exists.', |
| 183 |
cluster_id) |
| 184 |
return False |
| 185 |
return False |