void createService(ExternalEvent event, Map<String, Object> serviceData) {
    Service svc = serviceDao.getServiceByExternalId(event.getAccountId(), event.getExternalId());
    if (svc != null) {
      return;
    }

    Stack stack = getStack(event);
    if (stack == null) {
      log.info("Can't process service event. Could not get or create stack. Event: [{}]", event);
      return;
    }

    Map<String, Object> service = new HashMap<String, Object>();
    if (serviceData != null) {
      service.putAll(serviceData);
    }
    service.put(ObjectMetaDataManager.ACCOUNT_FIELD, event.getAccountId());
    service.put(FIELD_STACK_ID, stack.getId());

    try {
      String create =
          objectProcessManager.getStandardProcessName(StandardProcess.CREATE, Service.class);
      String activate =
          objectProcessManager.getStandardProcessName(StandardProcess.ACTIVATE, Service.class);
      ProcessUtils.chainInData(service, create, activate);
      resourceDao.createAndSchedule(Service.class, service);
    } catch (ProcessCancelException e) {
      log.info(
          "Create and activate process cancelled for service with account id {}and external id {}",
          event.getAccountId(),
          event.getExternalId());
    }
  }
  void updateService(ExternalEvent event, Map<String, Object> serviceData) {
    Service svc = serviceDao.getServiceByExternalId(event.getAccountId(), event.getExternalId());
    if (svc == null) {
      log.info(
          "Unable to find service while attempting to update. Returning. Service external id: [{}], account id: [{}]",
          event.getExternalId(),
          event.getAccountId());
      return;
    }

    Map<String, Object> fields = DataUtils.getFields(svc);
    Map<String, Object> updates = new HashMap<String, Object>();
    for (Map.Entry<String, Object> resourceField : serviceData.entrySet()) {
      String fieldName = resourceField.getKey();
      Object newFieldValue = resourceField.getValue();
      Object currentFieldValue = fields.get(fieldName);
      if (ObjectUtils.hasWritableProperty(svc, fieldName)) {
        Object property = ObjectUtils.getProperty(svc, fieldName);
        if (newFieldValue != null && !newFieldValue.equals(property) || property == null) {
          updates.put(fieldName, newFieldValue);
        }
      } else if ((newFieldValue != null && !newFieldValue.equals(currentFieldValue))
          || currentFieldValue != null) {
        updates.put(fieldName, newFieldValue);
      }
    }

    if (!updates.isEmpty()) {
      objectManager.setFields(svc, updates);
      resourceDao.updateAndSchedule(svc);
    }
  }
Esempio n. 3
0
  @Override
  public Object create(String type, ApiRequest request, ResourceManager next) {
    ServiceEvent event = request.proxyRequestObject(ServiceEvent.class);

    /* Will never return null, MissingRequired will be thrown if missing */
    Agent agent = getAgent();
    if (agent == null) {
      throw new ClientVisibleException(ResponseCodes.FORBIDDEN, VERIFY_AGENT);
    }

    HealthcheckInstanceHostMap healthcheckInstanceHostMap = null;
    String[] splitted = event.getHealthcheckUuid().split("_");
    if (splitted.length > 2) {
      healthcheckInstanceHostMap = serviceDao.getHealthCheckInstanceUUID(splitted[0], splitted[1]);
    } else {
      healthcheckInstanceHostMap =
          objectManager.findOne(
              HealthcheckInstanceHostMap.class, ObjectMetaDataManager.UUID_FIELD, splitted[0]);
    }

    if (healthcheckInstanceHostMap == null) {
      throw new ClientVisibleException(ResponseCodes.FORBIDDEN, VERIFY_AGENT);
    }

    HealthcheckInstance healthcheckInstance =
        objectManager.loadResource(
            HealthcheckInstance.class, healthcheckInstanceHostMap.getHealthcheckInstanceId());

    if (healthcheckInstance == null) {
      return null;
    }
    Long resourceAccId =
        DataAccessor.fromDataFieldOf(agent)
            .withKey(AgentConstants.DATA_AGENT_RESOURCES_ACCOUNT_ID)
            .as(Long.class);

    if (!healthcheckInstanceHostMap.getAccountId().equals(resourceAccId)) {
      throw new ClientVisibleException(ResponseCodes.FORBIDDEN, VERIFY_AGENT);
    }

    event.setInstanceId(healthcheckInstance.getInstanceId());
    event.setHealthcheckInstanceId(healthcheckInstance.getId());
    event.setHostId(healthcheckInstanceHostMap.getHostId());

    return super.create(type, request, next);
  }
 void deleteService(ExternalEvent event, Map<String, Object> serviceData) {
   Service svc = serviceDao.getServiceByExternalId(event.getAccountId(), event.getExternalId());
   if (svc != null) {
     objectProcessManager.scheduleStandardProcess(StandardProcess.REMOVE, svc, null);
   }
 }