2014-03-17 20:57:35 +00:00
|
|
|
from endpoints.api import (resource, nickname, ApiResource, log_action, related_user_resource,
|
2014-03-19 16:09:07 +00:00
|
|
|
Unauthorized, require_user_admin, internal_only)
|
2014-03-14 20:02:13 +00:00
|
|
|
from auth.permissions import AdministerOrganizationPermission, OrganizationMemberPermission
|
|
|
|
from auth.auth_context import get_authenticated_user
|
|
|
|
from data import model
|
|
|
|
from util.names import format_robot_username
|
|
|
|
|
|
|
|
|
|
|
|
def robot_view(name, token):
|
|
|
|
return {
|
|
|
|
'name': name,
|
|
|
|
'token': token,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-14 20:09:16 +00:00
|
|
|
@resource('/v1/user/robots')
|
2014-03-19 16:09:07 +00:00
|
|
|
@internal_only
|
2014-03-14 20:02:13 +00:00
|
|
|
class UserRobotList(ApiResource):
|
|
|
|
""" Resource for listing user robots. """
|
2014-03-18 23:21:27 +00:00
|
|
|
@require_user_admin
|
2014-03-14 20:02:13 +00:00
|
|
|
@nickname('getUserRobots')
|
|
|
|
def get(self):
|
|
|
|
""" List the available robots for the user. """
|
|
|
|
user = get_authenticated_user()
|
|
|
|
robots = model.list_entity_robots(user.username)
|
|
|
|
return {
|
|
|
|
'robots': [robot_view(name, password) for name, password in robots]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-14 20:09:16 +00:00
|
|
|
@resource('/v1/user/robots/<robot_shortname>')
|
2014-03-19 16:09:07 +00:00
|
|
|
@internal_only
|
2014-03-14 20:02:13 +00:00
|
|
|
class UserRobot(ApiResource):
|
|
|
|
""" Resource for managing a user's robots. """
|
2014-03-18 23:21:27 +00:00
|
|
|
@require_user_admin
|
2014-03-14 20:02:13 +00:00
|
|
|
@nickname('createUserRobot')
|
|
|
|
def put(self, robot_shortname):
|
|
|
|
""" Create a new user robot with the specified name. """
|
|
|
|
parent = get_authenticated_user()
|
|
|
|
robot, password = model.create_robot(robot_shortname, parent)
|
|
|
|
log_action('create_robot', parent.username, {'robot': robot_shortname})
|
2014-03-17 18:52:52 +00:00
|
|
|
return robot_view(robot.username, password), 201
|
2014-03-14 20:02:13 +00:00
|
|
|
|
2014-03-18 23:21:27 +00:00
|
|
|
@require_user_admin
|
2014-03-14 20:02:13 +00:00
|
|
|
@nickname('deleteUserRobot')
|
|
|
|
def delete(self, robot_shortname):
|
|
|
|
""" Delete an existing robot. """
|
|
|
|
parent = get_authenticated_user()
|
|
|
|
model.delete_robot(format_robot_username(parent.username, robot_shortname))
|
|
|
|
log_action('delete_robot', parent.username, {'robot': robot_shortname})
|
|
|
|
return 'Deleted', 204
|
|
|
|
|
|
|
|
|
2014-03-14 20:09:16 +00:00
|
|
|
@resource('/v1/organization/<orgname>/robots')
|
2014-03-19 16:09:07 +00:00
|
|
|
@internal_only
|
2014-03-14 21:35:52 +00:00
|
|
|
@related_user_resource(UserRobotList)
|
2014-03-14 20:02:13 +00:00
|
|
|
class OrgRobotList(ApiResource):
|
|
|
|
""" Resource for listing an organization's robots. """
|
|
|
|
@nickname('getOrgRobots')
|
|
|
|
def get(self, orgname):
|
|
|
|
""" List the organization's robots. """
|
|
|
|
permission = OrganizationMemberPermission(orgname)
|
|
|
|
if permission.can():
|
|
|
|
robots = model.list_entity_robots(orgname)
|
|
|
|
return {
|
|
|
|
'robots': [robot_view(name, password) for name, password in robots]
|
|
|
|
}
|
|
|
|
|
2014-03-17 20:57:35 +00:00
|
|
|
raise Unauthorized()
|
2014-03-14 20:02:13 +00:00
|
|
|
|
|
|
|
|
2014-03-14 20:09:16 +00:00
|
|
|
@resource('/v1/organization/<orgname>/robots/<robot_shortname>')
|
2014-03-19 16:09:07 +00:00
|
|
|
@internal_only
|
2014-03-14 21:35:52 +00:00
|
|
|
@related_user_resource(UserRobot)
|
2014-03-14 20:02:13 +00:00
|
|
|
class OrgRobot(ApiResource):
|
|
|
|
""" Resource for managing an organization's robots. """
|
|
|
|
@nickname('createOrgRobot')
|
|
|
|
def put(self, orgname, robot_shortname):
|
|
|
|
""" Create a new robot in the organization. """
|
|
|
|
permission = AdministerOrganizationPermission(orgname)
|
|
|
|
if permission.can():
|
|
|
|
parent = model.get_organization(orgname)
|
|
|
|
robot, password = model.create_robot(robot_shortname, parent)
|
|
|
|
log_action('create_robot', orgname, {'robot': robot_shortname})
|
2014-03-17 19:26:16 +00:00
|
|
|
return robot_view(robot.username, password), 201
|
2014-03-14 20:02:13 +00:00
|
|
|
|
2014-03-17 20:57:35 +00:00
|
|
|
raise Unauthorized()
|
2014-03-14 20:02:13 +00:00
|
|
|
|
|
|
|
@nickname('deleteOrgRobot')
|
|
|
|
def delete(self, orgname, robot_shortname):
|
|
|
|
""" Delete an existing organization robot. """
|
|
|
|
permission = AdministerOrganizationPermission(orgname)
|
|
|
|
if permission.can():
|
|
|
|
model.delete_robot(format_robot_username(orgname, robot_shortname))
|
|
|
|
log_action('delete_robot', orgname, {'robot': robot_shortname})
|
|
|
|
return 'Deleted', 204
|
|
|
|
|
2014-03-17 20:57:35 +00:00
|
|
|
raise Unauthorized()
|