| 358 | |
| 359 | |
| 360 | Future<QuotaStatus> Master::QuotaHandler::_status( |
| 361 | const Option<Principal>& principal) const |
| 362 | { |
| 363 | // Quotas can be updated during preparation of the response. |
| 364 | // Copy current view of the collection to avoid conflicts. |
| 365 | hashmap<std::string, Quota> quotas(master->quotas); |
| 366 | |
| 367 | // Create a list of authorization actions for each role we may return. |
| 368 | // |
| 369 | // TODO(alexr): Use an authorization filter here once they are available. |
| 370 | vector<Future<bool>> authorizedRoles; |
| 371 | authorizedRoles.reserve(quotas.size()); |
| 372 | foreachkey (const string& role, quotas) { |
| 373 | authorizedRoles.push_back(authorizeGetQuota(principal, role)); |
| 374 | } |
| 375 | |
| 376 | return process::collect(authorizedRoles) |
| 377 | .then(defer( |
| 378 | master->self(), |
| 379 | [=](const vector<bool>& authorizedRolesCollected) |
| 380 | -> Future<QuotaStatus> { |
| 381 | CHECK(quotas.size() == authorizedRolesCollected.size()); |
| 382 | |
| 383 | QuotaStatus status; |
| 384 | status.mutable_infos()->Reserve(static_cast<int>(quotas.size())); |
| 385 | |
| 386 | // Create an entry (including role and resources) for each quota, |
| 387 | // except those filtered out based on the authorizer's response. |
| 388 | // |
| 389 | // NOTE: This error-prone code will be removed with |
| 390 | // the introduction of authorization filters. |
| 391 | auto authorizedIt = authorizedRolesCollected.cbegin(); |
| 392 | auto quotaIt = quotas.cbegin(); |
| 393 | for (; authorizedIt != authorizedRolesCollected.cend(); |
| 394 | ++authorizedIt, ++quotaIt) { |
| 395 | if (*authorizedIt) { |
| 396 | // Fill in legacy `QuotaInfo`. |
| 397 | *status.add_infos() = ["aIt]() { |
| 398 | QuotaInfo info; |
| 399 | info.set_role(quotaIt->first); |
| 400 | foreach (auto& quantity, quotaIt->second.guarantees) { |
| 401 | Resource resource; |
| 402 | resource.set_type(Value::SCALAR); |
| 403 | *resource.mutable_name() = quantity.first; |
| 404 | *resource.mutable_scalar() = quantity.second; |
| 405 | |
| 406 | *info.add_guarantee() = std::move(resource); |
| 407 | } |
| 408 | return info; |
| 409 | }(); |
| 410 | |
| 411 | *status.add_configs() = ["aIt]() { |
| 412 | QuotaConfig config; |
| 413 | config.set_role(quotaIt->first); |
| 414 | |
| 415 | foreach (auto& quantity, quotaIt->second.guarantees) { |
| 416 | (*config.mutable_guarantees())[quantity.first] = quantity.second; |
| 417 | } |