| 17 | REGISTER_URLHANDLER("/v1/objects", CreateObjectHandler); |
| 18 | |
| 19 | bool CreateObjectHandler::HandleRequest( |
| 20 | const WaitGroup::Ptr& waitGroup, |
| 21 | const HttpApiRequest& request, |
| 22 | HttpApiResponse& response, |
| 23 | boost::asio::yield_context& |
| 24 | ) |
| 25 | { |
| 26 | namespace http = boost::beast::http; |
| 27 | |
| 28 | auto url = request.Url(); |
| 29 | auto user = request.User(); |
| 30 | auto params = request.Params(); |
| 31 | |
| 32 | if (url->GetPath().size() != 4) |
| 33 | return false; |
| 34 | |
| 35 | if (request.method() != http::verb::put) |
| 36 | return false; |
| 37 | |
| 38 | Type::Ptr type = FilterUtility::TypeFromPluralName(url->GetPath()[2]); |
| 39 | |
| 40 | if (!type) { |
| 41 | HttpUtility::SendJsonError(response, params, 400, "Invalid type specified."); |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | FilterUtility::CheckPermission(user, "objects/create/" + type->GetName()); |
| 46 | |
| 47 | String name = url->GetPath()[3]; |
| 48 | Array::Ptr templates = params->Get("templates"); |
| 49 | Dictionary::Ptr attrs = params->Get("attrs"); |
| 50 | |
| 51 | /* Put created objects into the local zone if not explicitly defined. |
| 52 | * This allows additional zone members to sync the |
| 53 | * configuration at some later point. |
| 54 | */ |
| 55 | Zone::Ptr localZone = Zone::GetLocalZone(); |
| 56 | String localZoneName; |
| 57 | |
| 58 | if (localZone) { |
| 59 | localZoneName = localZone->GetName(); |
| 60 | |
| 61 | if (!attrs) { |
| 62 | attrs = new Dictionary({ |
| 63 | { "zone", localZoneName } |
| 64 | }); |
| 65 | } else if (!attrs->Contains("zone")) { |
| 66 | attrs->Set("zone", localZoneName); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /* Sanity checks for unique groups array. */ |
| 71 | if (attrs->Contains("groups")) { |
| 72 | Array::Ptr groups = attrs->Get("groups"); |
| 73 | |
| 74 | if (groups) |
| 75 | attrs->Set("groups", groups->Unique()); |
| 76 | } |