Skip to content

Commit

Permalink
Added class definition for the 'IDAcceptor'.
Browse files Browse the repository at this point in the history
This commit contains the class definition for 'IDAcceptor', which is
used to filter IDs in the '/master/frameworks', '/master/slaves',
'/master/tasks', and '/slave/containers' endpoints.

Review: https://reviews.apache.org/r/60820/
  • Loading branch information
suanmiao authored and greggomann committed Jul 19, 2017
1 parent 91b3abc commit aa244ba
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 76 deletions.
41 changes: 0 additions & 41 deletions src/common/http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1165,45 +1165,4 @@ Future<Owned<AuthorizationAcceptor>> AuthorizationAcceptor::create(
}


FrameworkIDAcceptor::FrameworkIDAcceptor(
const Option<std::string>& _frameworkId)
{
if (_frameworkId.isSome()) {
FrameworkID frameworkId_;
frameworkId_.set_value(_frameworkId.get());
frameworkId = frameworkId_;
}
}


TaskIDAcceptor::TaskIDAcceptor(const Option<std::string>& _taskId)
{
if (_taskId.isSome()) {
TaskID taskId_;
taskId_.set_value(_taskId.get());
taskId = taskId_;
}
}


bool FrameworkIDAcceptor::accept(const FrameworkID& _frameworkId)
{
if (frameworkId.isSome()) {
return frameworkId.get() == _frameworkId;
}

return true;
}


bool TaskIDAcceptor::accept(const TaskID& _taskId)
{
if (taskId.isSome()) {
return taskId.get() == _taskId;
}

return true;
}


} // namespace mesos {
42 changes: 21 additions & 21 deletions src/common/http.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,34 +195,34 @@ class AuthorizationAcceptor


/**
* Filtering results based on framework ID. When no framework ID is specified
* it will accept all inputs.
* Used to filter results for API handlers. Provides the 'accept()' method to
* test whether the supplied ID is equal to a stored target ID. If no target
* ID is provided when the acceptor is constructed, it will accept all inputs.
*/
class FrameworkIDAcceptor
template <typename T>
class IDAcceptor
{
public:
FrameworkIDAcceptor(const Option<std::string>& _frameworkId);

bool accept(const FrameworkID& frameworkId);

protected:
Option<FrameworkID> frameworkId;
};

IDAcceptor(const Option<std::string>& id = None())
{
if (id.isSome()) {
T targetId_;
targetId_.set_value(id.get());
targetId = targetId_;
}
}

/**
* Filtering results based on task ID. When no task ID is specified
* it will accept all inputs.
*/
class TaskIDAcceptor
{
public:
TaskIDAcceptor(const Option<std::string>& _taskId);
bool accept(const T& candidateId) const
{
if (targetId.isNone()) {
return true;
}

bool accept(const TaskID& taskId);
return candidateId.value() == targetId->value();
}

protected:
Option<TaskID> taskId;
Option<T> targetId;
};


Expand Down
27 changes: 13 additions & 14 deletions src/master/http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3900,11 +3900,10 @@ Future<Response> Master::Http::tasks(
principal,
master->authorizer,
authorization::VIEW_TASK);
Future<Owned<FrameworkIDAcceptor>> selectFrameworkId =
Owned<FrameworkIDAcceptor>(
new FrameworkIDAcceptor(request.url.query.get("framework_id")));
Future<Owned<TaskIDAcceptor>> selectTaskId =
Owned<TaskIDAcceptor>(new TaskIDAcceptor(request.url.query.get("task_id")));
Future<IDAcceptor<FrameworkID>> selectFrameworkId =
IDAcceptor<FrameworkID>(request.url.query.get("framework_id"));
Future<IDAcceptor<TaskID>> selectTaskId =
IDAcceptor<TaskID>(request.url.query.get("task_id"));

return collect(
authorizeFrameworkInfo,
Expand All @@ -3915,12 +3914,12 @@ Future<Response> Master::Http::tasks(
master->self(),
[=](const tuple<Owned<AuthorizationAcceptor>,
Owned<AuthorizationAcceptor>,
Owned<FrameworkIDAcceptor>,
Owned<TaskIDAcceptor>>& acceptors)-> Future<Response> {
IDAcceptor<FrameworkID>,
IDAcceptor<TaskID>>& acceptors)-> Future<Response> {
Owned<AuthorizationAcceptor> authorizeFrameworkInfo;
Owned<AuthorizationAcceptor> authorizeTask;
Owned<FrameworkIDAcceptor> selectFrameworkId;
Owned<TaskIDAcceptor> selectTaskId;
IDAcceptor<FrameworkID> selectFrameworkId;
IDAcceptor<TaskID> selectTaskId;
tie(authorizeFrameworkInfo,
authorizeTask,
selectFrameworkId,
Expand All @@ -3931,7 +3930,7 @@ Future<Response> Master::Http::tasks(
foreachvalue (Framework* framework, master->frameworks.registered) {
// Skip unauthorized frameworks or frameworks without matching
// framework ID.
if (!selectFrameworkId->accept(framework->id()) ||
if (!selectFrameworkId.accept(framework->id()) ||
!authorizeFrameworkInfo->accept(framework->info)) {
continue;
}
Expand All @@ -3943,7 +3942,7 @@ Future<Response> Master::Http::tasks(
master->frameworks.completed) {
// Skip unauthorized frameworks or frameworks without matching
// framework ID.
if (!selectFrameworkId->accept(framework->id()) ||
if (!selectFrameworkId.accept(framework->id()) ||
!authorizeFrameworkInfo->accept(framework->info)) {
continue;
}
Expand All @@ -3958,7 +3957,7 @@ Future<Response> Master::Http::tasks(
foreachvalue (Task* task, framework->tasks) {
CHECK_NOTNULL(task);
// Skip unauthorized tasks or tasks without matching task ID.
if (!selectTaskId->accept(task->task_id()) ||
if (!selectTaskId.accept(task->task_id()) ||
!authorizeTask->accept(*task, framework->info)) {
continue;
}
Expand All @@ -3970,7 +3969,7 @@ Future<Response> Master::Http::tasks(
const Owned<Task>& task,
framework->unreachableTasks) {
// Skip unauthorized tasks or tasks without matching task ID.
if (!selectTaskId->accept(task.get()->task_id()) ||
if (!selectTaskId.accept(task.get()->task_id()) ||
!authorizeTask->accept(*task.get(), framework->info)) {
continue;
}
Expand All @@ -3980,7 +3979,7 @@ Future<Response> Master::Http::tasks(

foreach (const Owned<Task>& task, framework->completedTasks) {
// Skip unauthorized tasks or tasks without matching task ID.
if (!selectTaskId->accept(task.get()->task_id()) ||
if (!selectTaskId.accept(task.get()->task_id()) ||
!authorizeTask->accept(*task.get(), framework->info)) {
continue;
}
Expand Down

0 comments on commit aa244ba

Please sign in to comment.