check if code already exists during the creation of the node via rest

This commit is contained in:
root 2015-11-23 19:34:14 +01:00
parent 2245ebddcb
commit e86df238a4
3 changed files with 18 additions and 5 deletions

View File

@ -216,15 +216,16 @@ class NodesController(rest.RestController):
raise exception.MissingParameterValue(
_("Location is not specified."))
"""
if Node.name:
if not api_utils.allow_node_logical_names():
raise exception.NotAcceptable()
if not api_utils.is_valid_node_name(Node.name):
msg = _("Cannot create node with invalid name %(name)s")
raise wsme.exc.ClientSideError(msg % {'name': Node.name},
status_code=400)
"""
try:
objects.Node.get_by_name(pecan.request.context, Node.name)
except:
raise exception.DuplicateCode(code=Node.code)
Node.status = 'DISCONNECTED'
Node.uuid = uuidutils.generate_uuid()

View File

@ -159,10 +159,11 @@ class InstanceAssociated(Conflict):
message = _("Instance %(instance_uuid)s is already associated with a node,"
" it cannot be associated with this other node %(node)s")
class DuplicateName(Conflict):
message = _("A node with name %(name)s already exists.")
class DuplicateCode(Conflict):
message = _("A node with code %(code)s already exists.")
class InvalidUUID(Invalid):
message = _("Expected a uuid but received %(uuid)s.")

View File

@ -97,6 +97,17 @@ class Node(base.IotronicObject):
node = Node._from_db_object(cls(context), db_node)
return node
@base.remotable_classmethod
def get_by_name(cls, context, name):
"""Find a node based on name and return a Node object.
:param name: the logical name of a node.
:returns: a :class:`Node` object.
"""
db_node = cls.dbapi.get_node_by_name(name)
node = Node._from_db_object(cls(context), db_node)
return node
@base.remotable_classmethod
def get_by_instance_uuid(cls, context, instance_uuid):
"""Find a node based on the instance uuid and return a Node object.