All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hubspot.singularity.resources.AbstractMachineResource Maven / Gradle / Ivy

There is a newer version: 1.5.0
Show newest version
package com.hubspot.singularity.resources;

import static com.hubspot.singularity.WebExceptions.checkNotFound;

import com.google.common.base.Optional;
import com.hubspot.singularity.MachineState;
import com.hubspot.singularity.SingularityDeleteResult;
import com.hubspot.singularity.SingularityMachineAbstraction;
import com.hubspot.singularity.SingularityUser;
import com.hubspot.singularity.api.SingularityMachineChangeRequest;
import com.hubspot.singularity.auth.SingularityAuthorizationHelper;
import com.hubspot.singularity.data.AbstractMachineManager;
import com.hubspot.singularity.data.AbstractMachineManager.StateChangeResult;
import com.sun.jersey.api.ConflictException;
import com.sun.jersey.api.NotFoundException;

public abstract class AbstractMachineResource> {

  protected final AbstractMachineManager manager;
  protected final Optional user;

  protected final SingularityAuthorizationHelper authorizationHelper;

  public AbstractMachineResource(AbstractMachineManager manager, SingularityAuthorizationHelper authorizationHelper, Optional user) {
    this.manager = manager;
    this.authorizationHelper = authorizationHelper;
    this.user = user;
  }

  protected void remove(String objectId) {
    authorizationHelper.checkAdminAuthorization(user);
    checkNotFound(manager.deleteObject(objectId) == SingularityDeleteResult.DELETED, "Couldn't find dead %s with id %s", getObjectTypeString(), objectId);
  }

  protected abstract String getObjectTypeString();

  private void changeState(String objectId, MachineState newState, Optional changeRequest, Optional user) {
    Optional message = Optional.absent();

    if (changeRequest.isPresent()) {
      message = changeRequest.get().getMessage();
    }

    StateChangeResult result = manager.changeState(objectId, newState, message, user);

    switch (result) {
      case FAILURE_NOT_FOUND:
        throw new NotFoundException(String.format("Couldn't find an active %s with id %s (result: %s)", getObjectTypeString(), objectId, result.name()));
      case FAILURE_ALREADY_AT_STATE:
      case FAILURE_ILLEGAL_TRANSITION:
        throw new ConflictException(String.format("%s - %s %s is in %s state", result.name(), getObjectTypeString(), objectId, newState));
      default:
        break;
    }

  }

  protected void decommission(String objectId, Optional decomissionRequest, Optional queryUser) {
    authorizationHelper.checkAdminAuthorization(user);
    changeState(objectId, MachineState.STARTING_DECOMMISSION, decomissionRequest, queryUser);
  }

  protected void freeze(String objectId, Optional freezeRequest, Optional queryUser) {
    authorizationHelper.checkAdminAuthorization(user);
    changeState(objectId, MachineState.FROZEN, freezeRequest, queryUser);
  }

  protected void activate(String objectId, Optional activateRequest, Optional queryUser) {
    authorizationHelper.checkAdminAuthorization(user);
    changeState(objectId, MachineState.ACTIVE, activateRequest, queryUser);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy