| 369 | |
| 370 | |
| 371 | void convertResourceFormat(Resource* resource, ResourceFormat format) |
| 372 | { |
| 373 | switch (format) { |
| 374 | case PRE_RESERVATION_REFINEMENT: |
| 375 | case ENDPOINT: { |
| 376 | CHECK(!resource->has_role()); |
| 377 | CHECK(!resource->has_reservation()); |
| 378 | switch (resource->reservations_size()) { |
| 379 | // Unreserved resource. |
| 380 | case 0: { |
| 381 | resource->set_role("*"); |
| 382 | break; |
| 383 | } |
| 384 | // Resource with a single reservation. |
| 385 | case 1: { |
| 386 | const Resource::ReservationInfo& source = resource->reservations(0); |
| 387 | |
| 388 | if (source.type() == Resource::ReservationInfo::DYNAMIC) { |
| 389 | Resource::ReservationInfo* target = resource->mutable_reservation(); |
| 390 | if (source.has_principal()) { |
| 391 | target->set_principal(source.principal()); |
| 392 | } |
| 393 | if (source.has_labels()) { |
| 394 | target->mutable_labels()->CopyFrom(source.labels()); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | resource->set_role(source.role()); |
| 399 | |
| 400 | if (format == PRE_RESERVATION_REFINEMENT) { |
| 401 | resource->clear_reservations(); |
| 402 | } |
| 403 | break; |
| 404 | } |
| 405 | // Resource with refined reservations. |
| 406 | default: { |
| 407 | CHECK_NE(PRE_RESERVATION_REFINEMENT, format) |
| 408 | << "Invalid resource format conversion: A 'Resource' object" |
| 409 | " being converted to the PRE_RESERVATION_REFINEMENT format" |
| 410 | " must not have refined reservations"; |
| 411 | } |
| 412 | } |
| 413 | break; |
| 414 | } |
| 415 | case POST_RESERVATION_REFINEMENT: { |
| 416 | if (resource->reservations_size() > 0) { |
| 417 | // In this case, we're either already in |
| 418 | // the "post-reservation-refinement" format, |
| 419 | // or we're in the "endpoint" format. |
| 420 | |
| 421 | // We clear out the "pre-reservation-refinement" fields |
| 422 | // in case the resources are in the "endpoint" format. |
| 423 | resource->clear_role(); |
| 424 | resource->clear_reservation(); |
| 425 | return; |
| 426 | } |
| 427 | |
| 428 | // Unreserved resources. |
no test coverage detected