Rename Bay to Cluster in api

This is the first of several patches to add new Cluster commands
that will replace the Bay terminalogy in Magnum. This patch adds
the new Cluster and ClusterTemplate commands in addition to the
Bay and Baymodel commands.  Additional patches will be created
for client, docs, and additional functional tests.

Change-Id: Ie686281a6f98a1a9931158d2a79eee6ac21ed9a1
Implements: blueprint rename-bay-to-cluster
This commit is contained in:
Jaycen Grant 2016-08-10 13:29:31 -07:00
parent 570173d999
commit eaddb942fd
34 changed files with 4352 additions and 155 deletions

View File

@ -20,6 +20,21 @@
"baymodel:update": "rule:default",
"baymodel:publish": "rule:admin_or_owner",
"cluster:create": "rule:default",
"cluster:delete": "rule:default",
"cluster:detail": "rule:default",
"cluster:get": "rule:default",
"cluster:get_all": "rule:default",
"cluster:update": "rule:default",
"clustertemplate:create": "rule:default",
"clustertemplate:delete": "rule:default",
"clustertemplate:detail": "rule:default",
"clustertemplate:get": "rule:default",
"clustertemplate:get_all": "rule:default",
"clustertemplate:update": "rule:default",
"clustertemplate:publish": "rule:admin_or_owner",
"rc:create": "rule:default",
"rc:delete": "rule:default",
"rc:detail": "rule:default",

View File

@ -69,7 +69,7 @@ class Root(base.APIBase):
root = Root()
root.name = "OpenStack Magnum API"
root.description = ("Magnum is an OpenStack project which aims to "
"provide container management.")
"provide container cluster management.")
root.versions = [Version.convert('v1', "CURRENT",
versions.CURRENT_MAX_VER,
versions.BASE_VER)]

View File

@ -27,6 +27,8 @@ from magnum.api.controllers import link
from magnum.api.controllers.v1 import bay
from magnum.api.controllers.v1 import baymodel
from magnum.api.controllers.v1 import certificate
from magnum.api.controllers.v1 import cluster
from magnum.api.controllers.v1 import cluster_template
from magnum.api.controllers.v1 import magnum_services
from magnum.api.controllers import versions as ver
from magnum.api import expose
@ -77,6 +79,12 @@ class V1(controllers_base.APIBase):
bays = [link.Link]
"""Links to the bays resource"""
clustertemplates = [link.Link]
"""Links to the clustertemplates resource"""
clusters = [link.Link]
"""Links to the clusters resource"""
certificates = [link.Link]
"""Links to the certificates resource"""
@ -108,6 +116,19 @@ class V1(controllers_base.APIBase):
pecan.request.host_url,
'bays', '',
bookmark=True)]
v1.clustertemplates = [link.Link.make_link('self',
pecan.request.host_url,
'clustertemplates', ''),
link.Link.make_link('bookmark',
pecan.request.host_url,
'clustertemplates', '',
bookmark=True)]
v1.clusters = [link.Link.make_link('self', pecan.request.host_url,
'clusters', ''),
link.Link.make_link('bookmark',
pecan.request.host_url,
'clusters', '',
bookmark=True)]
v1.certificates = [link.Link.make_link('self', pecan.request.host_url,
'certificates', ''),
link.Link.make_link('bookmark',
@ -128,6 +149,8 @@ class Controller(controllers_base.Controller):
bays = bay.BaysController()
baymodels = baymodel.BayModelsController()
clusters = cluster.ClustersController()
clustertemplates = cluster_template.ClusterTemplatesController()
certificates = certificate.CertificateController()
mservices = magnum_services.MagnumServiceController()

View File

@ -80,7 +80,7 @@ class Bay(base.APIBase):
try:
baymodel = api_utils.get_resource('BayModel', value)
self._baymodel_id = baymodel.uuid
except exception.BayModelNotFound as e:
except exception.ClusterTemplateNotFound as e:
# Change error code because 404 (NotFound) is inappropriate
# response for a POST request to create a Bay
e.code = 400 # BadRequest

View File

@ -343,7 +343,7 @@ class BayModelsController(base.Controller):
if baymodel_dict['public']:
if not policy.enforce(context, "baymodel:publish", None,
do_raise=False):
raise exception.BaymodelPublishDenied()
raise exception.ClusterTemplatePublishDenied()
# NOTE(yuywz): We will generate a random human-readable name for
# baymodel if the name is not spcified by user.
@ -386,7 +386,7 @@ class BayModelsController(base.Controller):
if baymodel.public != new_baymodel.public:
if not policy.enforce(context, "baymodel:publish", None,
do_raise=False):
raise exception.BaymodelPublishDenied()
raise exception.ClusterTemplatePublishDenied()
# Update only the fields that have changed
for field in objects.BayModel.fields:

View File

@ -48,7 +48,7 @@ class Certificate(base.APIBase):
try:
self._bay = api_utils.get_resource('Bay', value)
self._bay_uuid = self._bay.uuid
except exception.BayNotFound as e:
except exception.ClusterNotFound as e:
# Change error code because 404 (NotFound) is inappropriate
# response for a POST request to create a Bay
e.code = 400 # BadRequest

View File

@ -0,0 +1,468 @@
# Copyright 2013 UnitedStack 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.
import uuid
from oslo_log import log as logging
from oslo_utils import timeutils
import pecan
import wsme
from wsme import types as wtypes
from magnum.api import attr_validator
from magnum.api.controllers import base
from magnum.api.controllers import link
from magnum.api.controllers.v1 import collection
from magnum.api.controllers.v1 import types
from magnum.api import expose
from magnum.api import utils as api_utils
from magnum.api.validation import validate_bay_properties
from magnum.common import clients
from magnum.common import exception
from magnum.common import name_generator
from magnum.common import policy
from magnum.i18n import _LW
from magnum import objects
from magnum.objects import fields
LOG = logging.getLogger(__name__)
class ClusterPatchType(types.JsonPatchType):
@staticmethod
def mandatory_attrs():
return ['/cluster_template_id']
@staticmethod
def internal_attrs():
internal_attrs = ['/api_address', '/node_addresses',
'/master_addresses', '/stack_id',
'/ca_cert_ref', '/magnum_cert_ref',
'/trust_id', '/trustee_user_name',
'/trustee_password', '/trustee_user_id']
return types.JsonPatchType.internal_attrs() + internal_attrs
class ClusterID(wtypes.Base):
"""API representation of a cluster ID
This class enforces type checking and value constraints, and converts
between the internal object model and the API representation of a cluster
ID.
"""
uuid = types.uuid
"""Unique UUID for this cluster"""
def __init__(self, uuid):
self.uuid = uuid
class Cluster(base.APIBase):
"""API representation of a cluster.
This class enforces type checking and value constraints, and converts
between the internal object model and the API representation of a bay.
"""
_cluster_template_id = None
def _get_cluster_template_id(self):
return self._cluster_template_id
def _set_cluster_template_id(self, value):
if value and self._cluster_template_id != value:
try:
cluster_template = api_utils.get_resource('BayModel', value)
self._cluster_template_id = cluster_template.uuid
except exception.ClusterTemplateNotFound as e:
# Change error code because 404 (NotFound) is inappropriate
# response for a POST request to create a Bay
e.code = 400 # BadRequest
raise
elif value == wtypes.Unset:
self._cluster_template_id = wtypes.Unset
uuid = types.uuid
"""Unique UUID for this cluster"""
name = wtypes.StringType(min_length=1, max_length=242,
pattern='^[a-zA-Z][a-zA-Z0-9_.-]*$')
"""Name of this cluster, max length is limited to 242 because of heat
stack requires max length limit to 255, and Magnum amend a uuid length"""
cluster_template_id = wsme.wsproperty(wtypes.text,
_get_cluster_template_id,
_set_cluster_template_id,
mandatory=True)
"""The cluster_template UUID"""
node_count = wsme.wsattr(wtypes.IntegerType(minimum=1), default=1)
"""The node count for this cluster. Default to 1 if not set"""
master_count = wsme.wsattr(wtypes.IntegerType(minimum=1), default=1)
"""The number of master nodes for this cluster. Default to 1 if not set"""
create_timeout = wsme.wsattr(wtypes.IntegerType(minimum=0), default=60)
"""Timeout for creating the cluster in minutes. Default to 60 if not set"""
links = wsme.wsattr([link.Link], readonly=True)
"""A list containing a self link and associated cluster links"""
stack_id = wsme.wsattr(wtypes.text, readonly=True)
"""Stack id of the heat stack"""
status = wtypes.Enum(str, *fields.BayStatus.ALL)
"""Status of the cluster from the heat stack"""
status_reason = wtypes.text
"""Status reason of the cluster from the heat stack"""
discovery_url = wtypes.text
"""Url used for cluster node discovery"""
api_address = wsme.wsattr(wtypes.text, readonly=True)
"""Api address of cluster master node"""
node_addresses = wsme.wsattr([wtypes.text], readonly=True)
"""IP addresses of cluster agent nodes"""
master_addresses = wsme.wsattr([wtypes.text], readonly=True)
"""IP addresses of cluster master nodes"""
faults = wsme.wsattr(wtypes.DictType(str, wtypes.text))
"""Fault info collected from the heat resources of this cluster"""
def __init__(self, **kwargs):
super(Cluster, self).__init__()
self.fields = []
for field in objects.Bay.fields:
# Skip fields we do not expose.
if not hasattr(self, field):
continue
self.fields.append(field)
setattr(self, field, kwargs.get(field, wtypes.Unset))
# Set the renamed attributes for clusters
self.fields.append('cluster_template_id')
if 'cluster_template_id' in kwargs.keys():
setattr(self, 'cluster_template_id',
kwargs.get('cluster_template_id', wtypes.Unset))
else:
setattr(self, 'cluster_template_id', kwargs.get('baymodel_id',
wtypes.Unset))
self.fields.append('create_timeout')
if 'create_timeout' in kwargs.keys():
setattr(self, 'create_timeout', kwargs.get('create_timeout',
wtypes.Unset))
else:
setattr(self, 'create_timeout', kwargs.get('bay_create_timeout',
wtypes.Unset))
self.fields.append('faults')
if 'faults' in kwargs.keys():
setattr(self, 'faults', kwargs.get('faults', wtypes.Unset))
else:
setattr(self, 'faults', kwargs.get('bay_faults', wtypes.Unset))
@staticmethod
def _convert_with_links(cluster, url, expand=True):
if not expand:
cluster.unset_fields_except(['uuid', 'name', 'cluster_template_id',
'node_count', 'status',
'create_timeout', 'master_count',
'stack_id'])
cluster.links = [link.Link.make_link('self', url,
'bays', cluster.uuid),
link.Link.make_link('bookmark', url,
'bays', cluster.uuid,
bookmark=True)]
return cluster
@classmethod
def convert_with_links(cls, rpc_bay, expand=True):
cluster = Cluster(**rpc_bay.as_dict())
return cls._convert_with_links(cluster, pecan.request.host_url, expand)
@classmethod
def sample(cls, expand=True):
temp_id = '4a96ac4b-2447-43f1-8ca6-9fd6f36d146d'
sample = cls(uuid='27e3153e-d5bf-4b7e-b517-fb518e17f34c',
name='example',
cluster_template_id=temp_id,
node_count=2,
master_count=1,
create_timeout=15,
stack_id='49dc23f5-ffc9-40c3-9d34-7be7f9e34d63',
status=fields.BayStatus.CREATE_COMPLETE,
status_reason="CREATE completed successfully",
api_address='172.24.4.3',
node_addresses=['172.24.4.4', '172.24.4.5'],
created_at=timeutils.utcnow(),
updated_at=timeutils.utcnow())
return cls._convert_with_links(sample, 'http://localhost:9511', expand)
def as_dict(self):
"""Render this object as a dict of its fields."""
# Override this for updated cluster values
d = super(Cluster, self).as_dict()
if 'cluster_template_id' in d.keys():
d['baymodel_id'] = d['cluster_template_id']
del d['cluster_template_id']
if 'create_timeout' in d.keys():
d['bay_create_timeout'] = d['create_timeout']
del d['create_timeout']
if 'faults' in d.keys():
d['bay_faults'] = d['faults']
del d['faults']
return d
class ClusterCollection(collection.Collection):
"""API representation of a collection of clusters."""
clusters = [Cluster]
"""A list containing cluster objects"""
def __init__(self, **kwargs):
self._type = 'clusters'
@staticmethod
def convert_with_links(rpc_bays, limit, url=None, expand=False, **kwargs):
collection = ClusterCollection()
collection.clusters = [Cluster.convert_with_links(p, expand)
for p in rpc_bays]
collection.next = collection.get_next(limit, url=url, **kwargs)
return collection
@classmethod
def sample(cls):
sample = cls()
sample.clusters = [Cluster.sample(expand=False)]
return sample
class ClustersController(base.Controller):
"""REST controller for Clusters."""
def __init__(self):
super(ClustersController, self).__init__()
_custom_actions = {
'detail': ['GET'],
}
def _generate_name_for_cluster(self, context):
"""Generate a random name like: zeta-22-bay."""
name_gen = name_generator.NameGenerator()
name = name_gen.generate()
return name + '-cluster'
def _get_clusters_collection(self, marker, limit,
sort_key, sort_dir, expand=False,
resource_url=None):
limit = api_utils.validate_limit(limit)
sort_dir = api_utils.validate_sort_dir(sort_dir)
marker_obj = None
if marker:
marker_obj = objects.Bay.get_by_uuid(pecan.request.context,
marker)
clusters = objects.Bay.list(pecan.request.context, limit,
marker_obj, sort_key=sort_key,
sort_dir=sort_dir)
return ClusterCollection.convert_with_links(clusters, limit,
url=resource_url,
expand=expand,
sort_key=sort_key,
sort_dir=sort_dir)
@expose.expose(ClusterCollection, types.uuid, int, wtypes.text,
wtypes.text)
def get_all(self, marker=None, limit=None, sort_key='id',
sort_dir='asc'):
"""Retrieve a list of clusters.
:param marker: pagination marker for large data sets.
:param limit: maximum number of resources to return in a single result.
:param sort_key: column to sort results by. Default: id.
:param sort_dir: direction to sort. "asc" or "desc". Default: asc.
"""
context = pecan.request.context
policy.enforce(context, 'cluster:get_all',
action='cluster:get_all')
return self._get_clusters_collection(marker, limit, sort_key,
sort_dir)
@expose.expose(ClusterCollection, types.uuid, int, wtypes.text,
wtypes.text)
def detail(self, marker=None, limit=None, sort_key='id',
sort_dir='asc'):
"""Retrieve a list of clusters with detail.
:param marker: pagination marker for large data sets.
:param limit: maximum number of resources to return in a single result.
:param sort_key: column to sort results by. Default: id.
:param sort_dir: direction to sort. "asc" or "desc". Default: asc.
"""
context = pecan.request.context
policy.enforce(context, 'cluster:detail',
action='cluster:detail')
# NOTE(lucasagomes): /detail should only work against collections
parent = pecan.request.path.split('/')[:-1][-1]
if parent != "clusters":
raise exception.HTTPNotFound
expand = True
resource_url = '/'.join(['clusters', 'detail'])
return self._get_clusters_collection(marker, limit,
sort_key, sort_dir, expand,
resource_url)
def _collect_fault_info(self, context, cluster):
"""Collect fault info from heat resources of given cluster
and store them into cluster.faults.
"""
osc = clients.OpenStackClients(context)
filters = {'status': 'FAILED'}
try:
failed_resources = osc.heat().resources.list(
cluster.stack_id, nested_depth=2, filters=filters)
except Exception as e:
failed_resources = []
LOG.warning(_LW("Failed to retrieve failed resources for "
"cluster %(cluster)s from Heat stack "
"%(stack)s due to error: %(e)s"),
{'cluster': cluster.uuid,
'stack': cluster.stack_id, 'e': e},
exc_info=True)
return {res.resource_name: res.resource_status_reason
for res in failed_resources}
@expose.expose(Cluster, types.uuid_or_name)
def get_one(self, bay_ident):
"""Retrieve information about the given bay.
:param bay_ident: UUID of a bay or logical name of the bay.
"""
context = pecan.request.context
cluster = api_utils.get_resource('Bay', bay_ident)
policy.enforce(context, 'cluster:get', cluster,
action='cluster:get')
cluster = Cluster.convert_with_links(cluster)
if cluster.status in fields.BayStatus.STATUS_FAILED:
cluster.faults = self._collect_fault_info(context, cluster)
return cluster
@expose.expose(ClusterID, body=Cluster, status_code=202)
def post(self, cluster):
"""Create a new cluster.
:param cluster: a cluster within the request body.
"""
context = pecan.request.context
policy.enforce(context, 'cluster:create',
action='cluster:create')
temp_id = cluster.cluster_template_id
cluster_template = objects.BayModel.get_by_uuid(context, temp_id)
cluster_dict = cluster.as_dict()
attr_validator.validate_os_resources(context,
cluster_template.as_dict())
attr_validator.validate_master_count(cluster_dict,
cluster_template.as_dict())
cluster_dict['project_id'] = context.project_id
cluster_dict['user_id'] = context.user_id
# NOTE(yuywz): We will generate a random human-readable name for
# cluster if the name is not specified by user.
name = cluster_dict.get('name') or \
self._generate_name_for_cluster(context)
cluster_dict['name'] = name
new_cluster = objects.Bay(context, **cluster_dict)
new_cluster.uuid = uuid.uuid4()
pecan.request.rpcapi.bay_create_async(new_cluster,
cluster.create_timeout)
return ClusterID(new_cluster.uuid)
@wsme.validate(types.uuid, [ClusterPatchType])
@expose.expose(ClusterID, types.uuid_or_name, body=[ClusterPatchType],
status_code=202)
def patch(self, cluster_ident, patch):
"""Update an existing bay.
:param cluster_ident: UUID or logical name of a bay.
:param patch: a json PATCH document to apply to this bay.
"""
context = pecan.request.context
cluster = api_utils.get_resource('Bay', cluster_ident)
policy.enforce(context, 'cluster:update', cluster,
action='cluster:update')
try:
cluster_dict = cluster.as_dict()
new_cluster = Cluster(**api_utils.apply_jsonpatch(cluster_dict,
patch))
except api_utils.JSONPATCH_EXCEPTIONS as e:
raise exception.PatchError(patch=patch, reason=e)
# Update only the fields that have changed
for field in objects.Bay.fields:
try:
patch_val = getattr(new_cluster, field)
except AttributeError:
# Ignore fields that aren't exposed in the API
continue
if patch_val == wtypes.Unset:
patch_val = None
if cluster[field] != patch_val:
cluster[field] = patch_val
delta = cluster.obj_what_changed()
validate_bay_properties(delta)
pecan.request.rpcapi.bay_update_async(cluster)
return ClusterID(cluster.uuid)
@expose.expose(None, types.uuid_or_name, status_code=204)
def delete(self, cluster_ident):
"""Delete a cluster.
:param cluster_ident: UUID of cluster or logical name of the cluster.
"""
context = pecan.request.context
cluster = api_utils.get_resource('Bay', cluster_ident)
policy.enforce(context, 'cluster:delete', cluster,
action='cluster:delete')
pecan.request.rpcapi.bay_delete_async(cluster.uuid)

View File

@ -0,0 +1,434 @@
# 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 oslo_utils import timeutils
import pecan
import wsme
from wsme import types as wtypes
from magnum.api import attr_validator
from magnum.api.controllers import base
from magnum.api.controllers import link
from magnum.api.controllers.v1 import collection
from magnum.api.controllers.v1 import types
from magnum.api import expose
from magnum.api import utils as api_utils
from magnum.api import validation
from magnum.common import clients
from magnum.common import exception
from magnum.common import name_generator
from magnum.common import policy
from magnum import objects
from magnum.objects import fields
class ClusterTemplatePatchType(types.JsonPatchType):
@staticmethod
def mandatory_attrs():
return ['/image_id', '/keypair_id', '/external_network_id', '/coe',
'/tls_disabled', '/public', '/registry_enabled',
'/server_type', '/cluster_distro', '/network_driver']
class ClusterTemplate(base.APIBase):
"""API representation of a clustertemplate.
This class enforces type checking and value constraints, and converts
between the internal object model and the API representation of
a clustertemplate.
"""
uuid = types.uuid
"""Unique UUID for this clustertemplate"""
name = wtypes.StringType(min_length=1, max_length=255)
"""The name of the clustertemplate"""
coe = wtypes.Enum(str, *fields.BayType.ALL, mandatory=True)
"""The Container Orchestration Engine for this clustertemplate"""
image_id = wsme.wsattr(wtypes.StringType(min_length=1, max_length=255),
mandatory=True)
"""The image name or UUID to use as an image for this clustertemplate"""
flavor_id = wtypes.StringType(min_length=1, max_length=255)
"""The flavor of this clustertemplate"""
master_flavor_id = wtypes.StringType(min_length=1, max_length=255)
"""The flavor of the master node for this clustertemplate"""
dns_nameserver = wtypes.IPv4AddressType()
"""The DNS nameserver address"""
keypair_id = wsme.wsattr(wtypes.StringType(min_length=1, max_length=255),
mandatory=True)
"""The name or id of the nova ssh keypair"""
external_network_id = wtypes.StringType(min_length=1, max_length=255)
"""The external network to attach the cluster"""
fixed_network = wtypes.StringType(min_length=1, max_length=255)
"""The fixed network name to attach the cluster"""
fixed_subnet = wtypes.StringType(min_length=1, max_length=255)
"""The fixed subnet name to attach the cluster"""
network_driver = wtypes.StringType(min_length=1, max_length=255)
"""The name of the driver used for instantiating container networks"""
apiserver_port = wtypes.IntegerType(minimum=1024, maximum=65535)
"""The API server port for k8s"""
docker_volume_size = wtypes.IntegerType(minimum=1)
"""The size in GB of the docker volume"""
cluster_distro = wtypes.StringType(min_length=1, max_length=255)
"""The Cluster distro for the cluster, ex - coreos, fedora-atomic."""
links = wsme.wsattr([link.Link], readonly=True)
"""A list containing a self link and associated clustertemplate links"""
http_proxy = wtypes.StringType(min_length=1, max_length=255)
"""Address of a proxy that will receive all HTTP requests and relay them.
The format is a URL including a port number.
"""
https_proxy = wtypes.StringType(min_length=1, max_length=255)
"""Address of a proxy that will receive all HTTPS requests and relay them.
The format is a URL including a port number.
"""
no_proxy = wtypes.StringType(min_length=1, max_length=255)
"""A comma separated list of ips for which proxies should not
used in the cluster
"""
volume_driver = wtypes.StringType(min_length=1, max_length=255)
"""The name of the driver used for instantiating container volume driver"""
registry_enabled = wsme.wsattr(types.boolean, default=False)
"""Indicates whether the docker registry is enabled"""
labels = wtypes.DictType(str, str)
"""One or more key/value pairs"""
tls_disabled = wsme.wsattr(types.boolean, default=False)
"""Indicates whether the TLS should be disabled"""
public = wsme.wsattr(types.boolean, default=False)
"""Indicates whether the clustertemplate is public or not."""
server_type = wsme.wsattr(wtypes.StringType(min_length=1,
max_length=255),
default='vm')
"""Server type for this clustertemplate """
insecure_registry = wtypes.StringType(min_length=1, max_length=255)
"""insecure registry url when create clustertemplate """
docker_storage_driver = wtypes.Enum(str, *fields.DockerStorageDriver.ALL)
"""Docker storage driver"""
master_lb_enabled = wsme.wsattr(types.boolean, default=False)
"""Indicates whether created bays should have a load balancer for master
nodes or not.
"""
floating_ip_enabled = wsme.wsattr(types.boolean, default=True)
"""Indicates whether created bays should have a floating ip or not."""
def __init__(self, **kwargs):
self.fields = []
for field in objects.BayModel.fields:
# Skip fields we do not expose.
if not hasattr(self, field):
continue
self.fields.append(field)
setattr(self, field, kwargs.get(field, wtypes.Unset))
@staticmethod
def _convert_with_links(cluster_template, url):
cluster_template.links = [link.Link.make_link('self', url,
'clustertemplates',
cluster_template.uuid),
link.Link.make_link('bookmark', url,
'clustertemplates',
cluster_template.uuid,
bookmark=True)]
return cluster_template
@classmethod
def convert_with_links(cls, rpc_baymodel):
cluster_template = ClusterTemplate(**rpc_baymodel.as_dict())
return cls._convert_with_links(cluster_template,
pecan.request.host_url)
@classmethod
def sample(cls):
sample = cls(
uuid='27e3153e-d5bf-4b7e-b517-fb518e17f34c',
name='example',
image_id='Fedora-k8s',
flavor_id='m1.small',
master_flavor_id='m1.small',
dns_nameserver='8.8.1.1',
keypair_id='keypair1',
external_network_id='ffc44e4a-2319-4062-bce0-9ae1c38b05ba',
fixed_network='private',
fixed_subnet='private-subnet',
network_driver='libnetwork',
volume_driver='cinder',
apiserver_port=8080,
docker_volume_size=25,
docker_storage_driver='devicemapper',
cluster_distro='fedora-atomic',
coe=fields.BayType.KUBERNETES,
http_proxy='http://proxy.com:123',
https_proxy='https://proxy.com:123',
no_proxy='192.168.0.1,192.168.0.2,192.168.0.3',
labels={'key1': 'val1', 'key2': 'val2'},
server_type='vm',
insecure_registry='10.238.100.100:5000',
created_at=timeutils.utcnow(),
updated_at=timeutils.utcnow(),
public=False,
master_lb_enabled=False,
floating_ip_enabled=True)
return cls._convert_with_links(sample, 'http://localhost:9511')
class ClusterTemplateCollection(collection.Collection):
"""API representation of a collection of clustertemplates."""
clustertemplates = [ClusterTemplate]
"""A list containing clustertemplates objects"""
def __init__(self, **kwargs):
self._type = 'clustertemplates'
@staticmethod
def convert_with_links(rpc_baymodels, limit, url=None, **kwargs):
collection = ClusterTemplateCollection()
collection.clustertemplates = [ClusterTemplate.convert_with_links(p)
for p in rpc_baymodels]
collection.next = collection.get_next(limit, url=url, **kwargs)
return collection
@classmethod
def sample(cls):
sample = cls()
sample.clustertemplates = [ClusterTemplate.sample()]
return sample
class ClusterTemplatesController(base.Controller):
"""REST controller for ClusterTemplates."""
_custom_actions = {
'detail': ['GET'],
}
def _generate_name_for_cluster_template(self, context):
"""Generate a random name like: zeta-22-model."""
name_gen = name_generator.NameGenerator()
name = name_gen.generate()
return name + '-template'
def _get_cluster_templates_collection(self, marker, limit,
sort_key, sort_dir,
resource_url=None):
limit = api_utils.validate_limit(limit)
sort_dir = api_utils.validate_sort_dir(sort_dir)
marker_obj = None
if marker:
marker_obj = objects.BayModel.get_by_uuid(pecan.request.context,
marker)
cluster_templates = objects.BayModel.list(pecan.request.context, limit,
marker_obj,
sort_key=sort_key,
sort_dir=sort_dir)
return ClusterTemplateCollection.convert_with_links(cluster_templates,
limit,
url=resource_url,
sort_key=sort_key,
sort_dir=sort_dir)
@expose.expose(ClusterTemplateCollection, types.uuid, int, wtypes.text,
wtypes.text)
def get_all(self, marker=None, limit=None, sort_key='id',
sort_dir='asc'):
"""Retrieve a list of baymodels.
:param marker: pagination marker for large data sets.
:param limit: maximum number of resources to return in a single result.
:param sort_key: column to sort results by. Default: id.
:param sort_dir: direction to sort. "asc" or "desc". Default: asc.
"""
context = pecan.request.context
policy.enforce(context, 'clustertemplate:get_all',
action='clustertemplate:get_all')
return self._get_cluster_templates_collection(marker, limit, sort_key,
sort_dir)
@expose.expose(ClusterTemplateCollection, types.uuid, int, wtypes.text,
wtypes.text)
def detail(self, marker=None, limit=None, sort_key='id',
sort_dir='asc'):
"""Retrieve a list of clustertemplates with detail.
:param marker: pagination marker for large data sets.
:param limit: maximum number of resources to return in a single result.
:param sort_key: column to sort results by. Default: id.
:param sort_dir: direction to sort. "asc" or "desc". Default: asc.
"""
context = pecan.request.context
policy.enforce(context, 'clustertemplate:detail',
action='clustertemplate:detail')
# NOTE(lucasagomes): /detail should only work against collections
parent = pecan.request.path.split('/')[:-1][-1]
if parent != "clustertemplates":
raise exception.HTTPNotFound
resource_url = '/'.join(['clustertemplates', 'detail'])
return self._get_cluster_templates_collection(marker, limit,
sort_key, sort_dir,
resource_url)
@expose.expose(ClusterTemplate, types.uuid_or_name)
def get_one(self, cluster_template_ident):
"""Retrieve information about the given clustertemplate.
:param cluster_template_ident: UUID or logical name of a
clustertemplate.
"""
context = pecan.request.context
cluster_template = api_utils.get_resource('BayModel',
cluster_template_ident)
if not cluster_template.public:
policy.enforce(context, 'clustertemplate:get', cluster_template,
action='clustertemplate:get')
return ClusterTemplate.convert_with_links(cluster_template)
@expose.expose(ClusterTemplate, body=ClusterTemplate, status_code=201)
@validation.enforce_network_driver_types_create()
@validation.enforce_volume_driver_types_create()
@validation.enforce_volume_storage_size_create()
def post(self, cluster_template):
"""Create a new cluster_template.
:param cluster_template: a cluster_template within the request body.
"""
context = pecan.request.context
policy.enforce(context, 'clustertemplate:create',
action='clustertemplate:create')
cluster_template_dict = cluster_template.as_dict()
cli = clients.OpenStackClients(context)
attr_validator.validate_os_resources(context, cluster_template_dict)
image_data = attr_validator.validate_image(cli,
cluster_template_dict[
'image_id'])
cluster_template_dict['cluster_distro'] = image_data['os_distro']
cluster_template_dict['project_id'] = context.project_id
cluster_template_dict['user_id'] = context.user_id
# check permissions for making cluster_template public
if cluster_template_dict['public']:
if not policy.enforce(context, "clustertemplate:publish", None,
do_raise=False):
raise exception.ClusterTemplatePublishDenied()
# NOTE(yuywz): We will generate a random human-readable name for
# cluster_template if the name is not specified by user.
arg_name = cluster_template_dict.get('name')
name = arg_name or self._generate_name_for_cluster_template(context)
cluster_template_dict['name'] = name
new_cluster_template = objects.BayModel(context,
**cluster_template_dict)
new_cluster_template.create()
# Set the HTTP Location Header
pecan.response.location = link.build_url('clustertemplates',
new_cluster_template.uuid)
return ClusterTemplate.convert_with_links(new_cluster_template)
@wsme.validate(types.uuid_or_name, [ClusterTemplatePatchType])
@expose.expose(ClusterTemplate, types.uuid_or_name,
body=[ClusterTemplatePatchType])
@validation.enforce_network_driver_types_update()
@validation.enforce_volume_driver_types_update()
def patch(self, cluster_template_ident, patch):
"""Update an existing cluster_template.
:param cluster_template_ident: UUID or logic name of a
cluster_template.
:param patch: a json PATCH document to apply to this
cluster_template.
"""
context = pecan.request.context
cluster_template = api_utils.get_resource('BayModel',
cluster_template_ident)
policy.enforce(context, 'clustertemplate:update', cluster_template,
action='clustertemplate:update')
try:
cluster_template_dict = cluster_template.as_dict()
new_cluster_template = ClusterTemplate(**api_utils.apply_jsonpatch(
cluster_template_dict,
patch))
except api_utils.JSONPATCH_EXCEPTIONS as e:
raise exception.PatchError(patch=patch, reason=e)
new_cluster_template_dict = new_cluster_template.as_dict()
attr_validator.validate_os_resources(context,
new_cluster_template_dict)
# check permissions when updating baymodel public flag
if cluster_template.public != new_cluster_template.public:
if not policy.enforce(context, "clustertemplate:publish", None,
do_raise=False):
raise exception.ClusterTemplatePublishDenied()
# Update only the fields that have changed
for field in objects.BayModel.fields:
try:
patch_val = getattr(new_cluster_template, field)
except AttributeError:
# Ignore fields that aren't exposed in the API
continue
if patch_val == wtypes.Unset:
patch_val = None
if cluster_template[field] != patch_val:
cluster_template[field] = patch_val
cluster_template.save()
return ClusterTemplate.convert_with_links(cluster_template)
@expose.expose(None, types.uuid_or_name, status_code=204)
def delete(self, cluster_template_ident):
"""Delete a cluster_template.
:param cluster_template_ident: UUID or logical name of a
cluster_template.
"""
context = pecan.request.context
cluster_template = api_utils.get_resource('BayModel',
cluster_template_ident)
policy.enforce(context, 'clustertemplate:delete', cluster_template,
action='clustertemplate:delete')
cluster_template.destroy()

View File

@ -229,28 +229,29 @@ class FileSystemNotSupported(MagnumException):
"File system %(fs)s is not supported.")
class BayModelNotFound(ResourceNotFound):
message = _("Baymodel %(baymodel)s could not be found.")
class ClusterTemplateNotFound(ResourceNotFound):
message = _("ClusterTemplate %(clustertemplate)s could not be found.")
class BayModelAlreadyExists(Conflict):
message = _("A baymodel with UUID %(uuid)s already exists.")
class ClusterTemplateAlreadyExists(Conflict):
message = _("A ClusterTemplate with UUID %(uuid)s already exists.")
class BayModelReferenced(Invalid):
message = _("Baymodel %(baymodel)s is referenced by one or multiple bays.")
class ClusterTemplateReferenced(Invalid):
message = _("ClusterTemplate %(clustertemplate)s is referenced by one or"
" multiple clusters.")
class BaymodelPublishDenied(NotAuthorized):
message = _("Not authorized to set public flag for baymodel.")
class ClusterTemplatePublishDenied(NotAuthorized):
message = _("Not authorized to set public flag for cluster template.")
class BayNotFound(ResourceNotFound):
message = _("Bay %(bay)s could not be found.")
class ClusterNotFound(ResourceNotFound):
message = _("Cluster %(cluster)s could not be found.")
class BayAlreadyExists(Conflict):
message = _("A bay with UUID %(uuid)s already exists.")
class ClusterAlreadyExists(Conflict):
message = _("A cluster with UUID %(uuid)s already exists.")
class ContainerNotFound(ResourceNotFound):

View File

@ -234,7 +234,7 @@ class Handler(object):
trust_manager.delete_trustee_and_trust(osc, context, bay)
cert_manager.delete_certificates_from_bay(bay, context=context)
bay.destroy()
except exception.BayNotFound:
except exception.ClusterNotFound:
LOG.info(_LI('The bay %s has been deleted by others.'), uuid)
conductor_utils.notify_about_bay_operation(
context, taxonomy.ACTION_DELETE, taxonomy.OUTCOME_SUCCESS)
@ -348,7 +348,7 @@ class HeatPoller(object):
cert_manager.delete_certificates_from_bay(self.bay,
context=self.context)
self.bay.destroy()
except exception.BayNotFound:
except exception.ClusterNotFound:
LOG.info(_LI('The bay %s has been deleted by others.')
% self.bay.uuid)

View File

@ -118,7 +118,7 @@ class Connection(object):
:param bay_id: The id or uuid of a bay.
:returns: A bay.
:raises: BayNotFound
:raises: ClusterNotFound
"""
@abc.abstractmethod
@ -201,7 +201,7 @@ class Connection(object):
:param baymodel_id: The id or uuid of a baymodel.
:returns: A baymodel.
:raises: BayModelNotFound
:raises: ClusterTemplateNotFound
"""
@abc.abstractmethod

View File

@ -157,7 +157,7 @@ class Connection(api.Connection):
try:
bay.save()
except db_exc.DBDuplicateEntry:
raise exception.BayAlreadyExists(uuid=values['uuid'])
raise exception.ClusterAlreadyExists(uuid=values['uuid'])
return bay
def get_bay_by_id(self, context, bay_id):
@ -167,7 +167,7 @@ class Connection(api.Connection):
try:
return query.one()
except NoResultFound:
raise exception.BayNotFound(bay=bay_id)
raise exception.ClusterNotFound(cluster=bay_id)
def get_bay_by_name(self, context, bay_name):
query = model_query(models.Bay)
@ -179,7 +179,7 @@ class Connection(api.Connection):
raise exception.Conflict('Multiple bays exist with same name.'
' Please use the bay uuid instead.')
except NoResultFound:
raise exception.BayNotFound(bay=bay_name)
raise exception.ClusterNotFound(cluster=bay_name)
def get_bay_by_uuid(self, context, bay_uuid):
query = model_query(models.Bay)
@ -188,7 +188,7 @@ class Connection(api.Connection):
try:
return query.one()
except NoResultFound:
raise exception.BayNotFound(bay=bay_uuid)
raise exception.ClusterNotFound(cluster=bay_uuid)
def destroy_bay(self, bay_id):
session = get_session()
@ -199,7 +199,7 @@ class Connection(api.Connection):
try:
query.one()
except NoResultFound:
raise exception.BayNotFound(bay=bay_id)
raise exception.ClusterNotFound(cluster=bay_id)
query.delete()
@ -219,7 +219,7 @@ class Connection(api.Connection):
try:
ref = query.with_lockmode('update').one()
except NoResultFound:
raise exception.BayNotFound(bay=bay_id)
raise exception.ClusterNotFound(cluster=bay_id)
if 'provision_state' in values:
values['provision_updated_at'] = timeutils.utcnow()
@ -264,7 +264,7 @@ class Connection(api.Connection):
try:
baymodel.save()
except db_exc.DBDuplicateEntry:
raise exception.BayModelAlreadyExists(uuid=values['uuid'])
raise exception.ClusterTemplateAlreadyExists(uuid=values['uuid'])
return baymodel
def get_baymodel_by_id(self, context, baymodel_id):
@ -276,7 +276,8 @@ class Connection(api.Connection):
try:
return query.one()
except NoResultFound:
raise exception.BayModelNotFound(baymodel=baymodel_id)
raise exception.ClusterTemplateNotFound(
clustertemplate=baymodel_id)
def get_baymodel_by_uuid(self, context, baymodel_uuid):
query = model_query(models.BayModel)
@ -287,7 +288,8 @@ class Connection(api.Connection):
try:
return query.one()
except NoResultFound:
raise exception.BayModelNotFound(baymodel=baymodel_uuid)
raise exception.ClusterTemplateNotFound(
clustertemplate=baymodel_uuid)
def get_baymodel_by_name(self, context, baymodel_name):
query = model_query(models.BayModel)
@ -301,7 +303,8 @@ class Connection(api.Connection):
raise exception.Conflict('Multiple baymodels exist with same name.'
' Please use the baymodel uuid instead.')
except NoResultFound:
raise exception.BayModelNotFound(baymodel=baymodel_name)
raise exception.ClusterTemplateNotFound(
clustertemplate=baymodel_name)
def _is_baymodel_referenced(self, session, baymodel_uuid):
"""Checks whether the baymodel is referenced by bay(s)."""
@ -324,10 +327,12 @@ class Connection(api.Connection):
try:
baymodel_ref = query.one()
except NoResultFound:
raise exception.BayModelNotFound(baymodel=baymodel_id)
raise exception.ClusterTemplateNotFound(
clustertemplate=baymodel_id)
if self._is_baymodel_referenced(session, baymodel_ref['uuid']):
raise exception.BayModelReferenced(baymodel=baymodel_id)
raise exception.ClusterTemplateReferenced(
clustertemplate=baymodel_id)
query.delete()
@ -347,12 +352,14 @@ class Connection(api.Connection):
try:
ref = query.with_lockmode('update').one()
except NoResultFound:
raise exception.BayModelNotFound(baymodel=baymodel_id)
raise exception.ClusterTemplateNotFound(
clustertemplate=baymodel_id)
if self._is_baymodel_referenced(session, ref['uuid']):
# we only allow to update baymodel to be public
if not self._is_publishing_baymodel(values):
raise exception.BayModelReferenced(baymodel=baymodel_id)
raise exception.ClusterTemplateReferenced(
clustertemplate=baymodel_id)
ref.update(values)
return ref

View File

@ -165,7 +165,7 @@ class MagnumPeriodicTasks(periodic_task.PeriodicTasks):
def _sync_deleted_stack(self, bay):
try:
bay.destroy()
except exception.BayNotFound:
except exception.ClusterNotFound:
LOG.info(_LI('The bay %s has been deleted by others.'), bay.uuid)
else:
LOG.info(_LI("Bay with id %(id)s not found in heat "

View File

@ -0,0 +1,176 @@
# 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 oslo_log import log as logging
from tempest.lib import exceptions
from magnum.i18n import _LE
from magnum.i18n import _LI
from magnum.i18n import _LW
from magnum.tests.functional.api.v1.models import cluster_id_model
from magnum.tests.functional.api.v1.models import cluster_model
from magnum.tests.functional.common import client
from magnum.tests.functional.common import utils
class ClusterClient(client.MagnumClient):
"""Encapsulates REST calls and maps JSON to/from models"""
LOG = logging.getLogger(__name__)
@classmethod
def clusters_uri(cls, filters=None):
"""Construct clusters uri with optional filters
:param filters: Optional k:v dict that's converted to url query
:returns: url string
"""
url = "/clusters"
if filters:
url = cls.add_filters(url, filters)
return url
@classmethod
def cluster_uri(cls, cluster_id):
"""Construct cluster uri
:param cluster_id: cluster uuid or name
:returns: url string
"""
return "{0}/{1}".format(cls.clusters_uri(), cluster_id)
def list_clusters(self, filters=None, **kwargs):
"""Makes GET /clusters request and returns ClusterCollection
Abstracts REST call to return all clusters
:param filters: Optional k:v dict that's converted to url query
:returns: response object and ClusterCollection object
"""
resp, body = self.get(self.clusters_uri(filters), **kwargs)
return self.deserialize(resp, body, cluster_model.ClusterCollection)
def get_cluster(self, cluster_id, **kwargs):
"""Makes GET /cluster request and returns ClusterEntity
Abstracts REST call to return a single cluster based on uuid or name
:param cluster_id: cluster uuid or name
:returns: response object and ClusterCollection object
"""
resp, body = self.get(self.cluster_uri(cluster_id))
return self.deserialize(resp, body, cluster_model.ClusterEntity)
def post_cluster(self, model, **kwargs):
"""Makes POST /cluster request and returns ClusterIdEntity
Abstracts REST call to create new cluster
:param model: ClusterEntity
:returns: response object and ClusterIdEntity object
"""
resp, body = self.post(
self.clusters_uri(),
body=model.to_json(), **kwargs)
return self.deserialize(resp, body, cluster_id_model.ClusterIdEntity)
def patch_cluster(self, cluster_id, clusterpatch_listmodel, **kwargs):
"""Makes PATCH /cluster request and returns ClusterIdEntity
Abstracts REST call to update cluster attributes
:param cluster_id: UUID of cluster
:param clusterpatch_listmodel: ClusterPatchCollection
:returns: response object and ClusterIdEntity object
"""
resp, body = self.patch(
self.cluster_uri(cluster_id),
body=clusterpatch_listmodel.to_json(), **kwargs)