diff --git a/src/main/java/net/frozenorb/apiv3/actor/ActorType.java b/src/main/java/net/frozenorb/apiv3/actor/ActorType.java index 32a4aa5..a187e2a 100644 --- a/src/main/java/net/frozenorb/apiv3/actor/ActorType.java +++ b/src/main/java/net/frozenorb/apiv3/actor/ActorType.java @@ -2,6 +2,6 @@ package net.frozenorb.apiv3.actor; public enum ActorType { - WEBSITE, STORE, BUNGEE_CORD, SERVER, UNKNOWN + WEBSITE, ANSIBLE, STORE, BUNGEE_CORD, SERVER, UNKNOWN } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/route/deployment/POSTDeploymentUpdateServer.java b/src/main/java/net/frozenorb/apiv3/route/deployment/POSTDeploymentUpdateServer.java index 69fa19f..4f08e41 100644 --- a/src/main/java/net/frozenorb/apiv3/route/deployment/POSTDeploymentUpdateServer.java +++ b/src/main/java/net/frozenorb/apiv3/route/deployment/POSTDeploymentUpdateServer.java @@ -1,10 +1,13 @@ package net.frozenorb.apiv3.route.deployment; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import net.frozenorb.apiv3.actor.Actor; import net.frozenorb.apiv3.actor.ActorType; import net.frozenorb.apiv3.model.AccessToken; import net.frozenorb.apiv3.model.Server; +import net.frozenorb.apiv3.model.ServerGroup; import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.SyncUtils; @@ -17,39 +20,66 @@ import io.vertx.ext.web.RoutingContext; public final class POSTDeploymentUpdateServer implements Handler { public void handle(RoutingContext ctx) { + Actor actor = ctx.get("actor"); + + if (actor.getType() != ActorType.ANSIBLE) { + ErrorUtils.respondOther(ctx, 403, "This action can only be performed by Ansible.", "ansibleOnly", ImmutableMap.of()); + return; + } + String serverId = ctx.request().getParam("serverId"); JsonObject requestBody = ctx.getBodyAsJson(); String ip = requestBody.getString("ip"); int port = requestBody.getInteger("port", 0); + String typeRaw = requestBody.getString("type"); + String groupRaw = requestBody.getString("group"); + String displayName = requestBody.getString("displayName"); - if (ip == null || port == 0) { - ErrorUtils.respondInvalidInput(ctx, "'ip' and 'port' are required."); + if (ip == null || port == 0 || typeRaw == null || groupRaw == null || displayName == null) { + ErrorUtils.respondInvalidInput(ctx, "'ip', 'port', 'type', 'group', and 'displayName' are required."); + return; + } + + ActorType type; + + try { + type = ActorType.valueOf(typeRaw.toUpperCase()); + } catch (Exception ignored) { + ErrorUtils.respondInvalidInput(ctx, "type must be one of [ WEBSITE, ANSIBLE, STORE, BUNGEE_CORD, SERVER ]"); + return; + } + + ServerGroup group = ServerGroup.findById(groupRaw); + + if (group == null) { + ErrorUtils.respondNotFound(ctx, "server group", groupRaw); + return; } Server server = Server.findById(serverId); - AccessToken accessToken = SyncUtils.runBlocking(v -> AccessToken.findByNameAndType(serverId, ActorType.SERVER, v)); if (server == null) { - ErrorUtils.respondNotFound(ctx, "server", serverId); - return; + server = new Server(serverId, displayName, group, ip + ":" + port); + SyncUtils.runBlocking(server::insert); + } else { + server.setServerIp(ip + ":" + port); + server.setLastUpdatedAt(Instant.now()); + SyncUtils.runBlocking(server::save); } + AccessToken accessToken = SyncUtils.runBlocking(v -> AccessToken.findByNameAndType(serverId, type, v)); + if (accessToken == null) { ErrorUtils.respondNotFound(ctx, "access token", serverId); return; } - server.setServerIp(ip + ":" + port); - server.setLastUpdatedAt(Instant.now()); - accessToken.setLockedIps(ImmutableList.of(ip)); accessToken.setLastUpdatedAt(Instant.now()); - SyncUtils.runBlocking(server::save); SyncUtils.runBlocking(accessToken::save); - ctx.response().end(accessToken.getId()); }