def user_add_project(user, project): """Add a user to a project. If the project or user does not exist, a NotFoundError will be raised. """ get_auth_backend().require_admin() user = api._must_find(User, user) project = api._must_find(model.Project, project) if project in user.projects: raise errors.DuplicateError('User %s is already in project %s' % (user.label, project.label)) user.projects.append(project) db.session.commit()
def port_connect_nic(switch, port, node, nic): """Connect a port on a switch to a nic on a node. If any of the three arguments does not exist, a NotFoundError will be raised. If the port or the nic is already connected to something, a DuplicateError will be raised. """ get_auth_backend().require_admin() switch = get_or_404(model.Switch, switch) port = get_child_or_404(switch, model.Port, port) node = get_or_404(model.Node, node) nic = get_child_or_404(node, model.Nic, nic) if nic.port is not None: raise errors.DuplicateError(nic.label) if port.nic is not None: raise errors.DuplicateError(port.label) nic.port = port db.session.commit()
def absent_or_conflict(cls, name): """Raises a DuplicateError if the given object is already in the database. This is useful for most of the *_create functions. Arguments: cls - the class of the object to query. name - the name of the object in question. Must be called within a request context. """ obj = db.session.query(cls).filter_by(label=name).first() if obj: raise errors.DuplicateError("%s %s already exists." % (cls.__name__, name))
def network_grant_project_access(project, network): """Add access to <network> to <project>. If the project or network does not exist, a NotFoundError will be raised. If the project already has access to the network a DuplicateError will be raised. """ network = get_or_404(model.Network, network) project = get_or_404(model.Project, project) # Must be admin or the owner of the network to add projects get_auth_backend().require_project_access(network.owner) if project in network.access: raise errors.DuplicateError('Network %s is already in project %s' % (network.label, project.label)) network.access.append(project) db.session.commit()
def absent_child_or_conflict(obj_outer, cls_inner, name_inner): """Raises DuplicateError if a "namespaced" object, such as a node's nic, exists. Otherwise returns successfully. Arguments: obj_outer - the "owner" object cls_inner - the "owned" class name_inner - the name of the "owned" object Must be called within a request context. """ obj_inner = _namespaced_query(obj_outer, cls_inner, name_inner) if obj_inner is not None: raise errors.DuplicateError( "%s %s on %s %s already exists" % (cls_inner.__name__, name_inner, obj_outer.__class__.__name__, obj_outer.label))