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
|
# 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 openstack.cloud import _utils
from openstack.cloud import openstackcloud
from openstack import exceptions
class CoeCloudMixin(openstackcloud._OpenStackCloudMixin):
def list_coe_clusters(self):
"""List COE (Container Orchestration Engine) cluster.
:returns: A list of container infrastructure management ``Cluster``
objects.
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call.
"""
return list(self.container_infrastructure_management.clusters())
def search_coe_clusters(self, name_or_id=None, filters=None):
"""Search COE cluster.
:param name_or_id: cluster name or ID.
:param filters: a dict containing additional filters to use.
:param detail: a boolean to control if we need summarized or
detailed output.
:returns: A list of container infrastructure management ``Cluster``
objects.
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call.
"""
coe_clusters = self.list_coe_clusters()
return _utils._filter_list(coe_clusters, name_or_id, filters)
def get_coe_cluster(self, name_or_id, filters=None):
"""Get a COE cluster by name or ID.
:param name_or_id: Name or ID of the cluster.
:param filters:
A dictionary of meta data to use for further filtering. Elements
of this dictionary may, themselves, be dictionaries. Example::
{'last_name': 'Smith', 'other': {'gender': 'Female'}}
OR
A string containing a jmespath expression for further filtering.
Example:: "[?last_name==`Smith`] | [?other.gender]==`Female`]"
:returns: A container infrastructure management ``Cluster`` object if
found, else None.
"""
return _utils._get_entity(self, 'coe_cluster', name_or_id, filters)
def create_coe_cluster(
self,
name,
cluster_template_id,
**kwargs,
):
"""Create a COE cluster based on given cluster template.
:param string name: Name of the cluster.
:param string cluster_template_id: ID of the cluster template to use.
:param dict kwargs: Any other arguments to pass in.
:returns: The created container infrastructure management ``Cluster``
object.
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call
"""
cluster = self.container_infrastructure_management.create_cluster(
name=name,
cluster_template_id=cluster_template_id,
**kwargs,
)
return cluster
def delete_coe_cluster(self, name_or_id):
"""Delete a COE cluster.
:param name_or_id: Name or unique ID of the cluster.
:returns: True if the delete succeeded, False if the
cluster was not found.
:raises: :class:`~openstack.exceptions.SDKException` on operation
error.
"""
cluster = self.get_coe_cluster(name_or_id)
if not cluster:
self.log.debug(
"COE Cluster %(name_or_id)s does not exist",
{'name_or_id': name_or_id},
exc_info=True,
)
return False
self.container_infrastructure_management.delete_cluster(cluster)
return True
def update_coe_cluster(self, name_or_id, **kwargs):
"""Update a COE cluster.
:param name_or_id: Name or ID of the COE cluster being updated.
:param kwargs: Cluster attributes to be updated.
:returns: The updated cluster ``Cluster`` object.
:raises: :class:`~openstack.exceptions.SDKException` on operation
error.
"""
cluster = self.get_coe_cluster(name_or_id)
if not cluster:
raise exceptions.SDKException(
f"COE cluster {name_or_id} not found."
)
cluster = self.container_infrastructure_management.update_cluster(
cluster, **kwargs
)
return cluster
def get_coe_cluster_certificate(self, cluster_id):
"""Get details about the CA certificate for a cluster by name or ID.
:param cluster_id: ID of the cluster.
:returns: Details about the CA certificate for the given cluster.
"""
return (
self.container_infrastructure_management.get_cluster_certificate(
cluster_id
)
)
def sign_coe_cluster_certificate(self, cluster_id, csr):
"""Sign client key and generate the CA certificate for a cluster
:param cluster_id: UUID of the cluster.
:param csr: Certificate Signing Request (CSR) for authenticating
client key.The CSR will be used by Magnum to generate a signed
certificate that client will use to communicate with the cluster.
:returns: a dict representing the signed certs.
:raises: :class:`~openstack.exceptions.SDKException` on operation
error.
"""
return self.container_infrastructure_management.create_cluster_certificate( # noqa: E501
cluster_uuid=cluster_id, csr=csr
)
def list_cluster_templates(self, detail=False):
"""List cluster templates.
:param bool detail. Ignored. Included for backwards compat.
ClusterTemplates are always returned with full details.
:returns: a list of dicts containing the cluster template details.
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call.
"""
return list(
self.container_infrastructure_management.cluster_templates()
)
def search_cluster_templates(
self, name_or_id=None, filters=None, detail=False
):
"""Search cluster templates.
:param name_or_id: cluster template name or ID.
:param filters: a dict containing additional filters to use.
:param detail: a boolean to control if we need summarized or
detailed output.
:returns: a list of dict containing the cluster templates
:raises: :class:`~openstack.exceptions.SDKException`: if something goes
wrong during the OpenStack API call.
"""
cluster_templates = self.list_cluster_templates(detail=detail)
return _utils._filter_list(cluster_templates, name_or_id, filters)
def get_cluster_template(self, name_or_id, filters=None, detail=False):
"""Get a cluster template by name or ID.
:param name_or_id: Name or ID of the cluster template.
:param filters:
A dictionary of meta data to use for further filtering. Elements
of this dictionary may, themselves, be dictionaries. Example::
{'last_name': 'Smith', 'other': {'gender': 'Female'}}
OR
A string containing a jmespath expression for further filtering.
Example:: "[?last_name==`Smith`] | [?other.gender]==`Female`]"
:returns: A cluster template dict or None if no matching
cluster template is found.
"""
return _utils._get_entity(
self,
'cluster_template',
name_or_id,
filters=filters,
detail=detail,
)
def create_cluster_template(
self, name, image_id=None, keypair_id=None, coe=None, **kwargs
):
"""Create a cluster template.
:param string name: Name of the cluster template.
:param string image_id: Name or ID of the image to use.
:param string keypair_id: Name or ID of the keypair to use.
:param string coe: Name of the coe for the cluster template.
Other arguments will be passed in kwargs.
:returns: a dict containing the cluster template description
:raises: :class:`~openstack.exceptions.SDKException` if something goes
wrong during the OpenStack API call
"""
cluster_template = (
self.container_infrastructure_management.create_cluster_template(
name=name,
image_id=image_id,
keypair_id=keypair_id,
coe=coe,
**kwargs,
)
)
return cluster_template
def delete_cluster_template(self, name_or_id):
"""Delete a cluster template.
:param name_or_id: Name or unique ID of the cluster template.
:returns: True if the delete succeeded, False if the
cluster template was not found.
:raises: :class:`~openstack.exceptions.SDKException` on operation
error.
"""
cluster_template = self.get_cluster_template(name_or_id)
if not cluster_template:
self.log.debug(
"Cluster template %(name_or_id)s does not exist",
{'name_or_id': name_or_id},
exc_info=True,
)
return False
self.container_infrastructure_management.delete_cluster_template(
cluster_template
)
return True
def update_cluster_template(self, name_or_id, **kwargs):
"""Update a cluster template.
:param name_or_id: Name or ID of the cluster template being updated.
:returns: an update cluster template.
:raises: :class:`~openstack.exceptions.SDKException` on operation
error.
"""
cluster_template = self.get_cluster_template(name_or_id)
if not cluster_template:
raise exceptions.SDKException(
f"Cluster template {name_or_id} not found."
)
cluster_template = (
self.container_infrastructure_management.update_cluster_template(
cluster_template, **kwargs
)
)
return cluster_template
def list_magnum_services(self):
"""List all Magnum services.
:returns: a list of dicts containing the service details.
:raises: :class:`~openstack.exceptions.SDKException` on operation
error.
"""
return list(self.container_infrastructure_management.services())
|