Finish deployment route

This commit is contained in:
Colin McDonald 2016-11-27 01:35:09 -05:00
parent db0adc8b87
commit 021d448c15
2 changed files with 41 additions and 11 deletions

View File

@ -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
}

View File

@ -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<RoutingContext> {
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.<Void>runBlocking(server::insert);
} else {
server.setServerIp(ip + ":" + port);
server.setLastUpdatedAt(Instant.now());
SyncUtils.<Void>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.<Void>runBlocking(server::save);
SyncUtils.<Void>runBlocking(accessToken::save);
ctx.response().end(accessToken.getId());
}