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); } }
Stack getStack(final ExternalEvent event) { final Map<String, Object> env = CollectionUtils.castMap(DataUtils.getFields(event).get(FIELD_ENVIRIONMENT)); Object eId = CollectionUtils.getNestedValue(env, FIELD_EXTERNAL_ID); if (eId == null) { return null; } final String envExtId = eId.toString(); Stack stack = stackDao.getStackByExternalId(event.getAccountId(), envExtId); // If stack has not been created yet if (stack == null) { final Stack newEnv = objectManager.newRecord(Stack.class); Object possibleName = CollectionUtils.getNestedValue(env, "name"); newEnv.setExternalId(envExtId); newEnv.setAccountId(event.getAccountId()); String name = possibleName != null ? possibleName.toString() : envExtId; newEnv.setName(name); stack = DeferredUtils.nest( new Callable<Stack>() { @Override public Stack call() { return resourceDao.createAndSchedule(newEnv); } }); stack = resourceMonitor.waitFor( stack, new ResourcePredicate<Stack>() { @Override public boolean evaluate(Stack obj) { return obj != null && CommonStatesConstants.ACTIVE.equals(obj.getState()); } @Override public String getMessage() { return "active state"; } }); } return stack; }
@Override public HandlerResult handle(ProcessState state, ProcessInstance process) { final ExternalEvent event = (ExternalEvent) state.getResource(); if (!ExternalEventConstants.KIND_SERVICE_EVENT.equals(event.getKind())) { return null; } lockManager.lock( new ExternalEventLock(SERVICE_LOCK_NAME, event.getAccountId(), event.getExternalId()), new LockCallbackNoReturn() { @Override public void doWithLockNoResult() { Map<String, Object> serviceData = CollectionUtils.toMap(DataUtils.getFields(event).get(FIELD_SERVICE)); if (serviceData.isEmpty()) { log.warn("Empty service for externalServiceEvent: {}.", event); return; } String kind = serviceData.get(ObjectMetaDataManager.KIND_FIELD) != null ? serviceData.get(ObjectMetaDataManager.KIND_FIELD).toString() : null; if (StringUtils.isEmpty(kind) || schemaFactory.getSchema(kind) == null) { log.warn("Couldn't find schema for service type [{}]. Returning.", kind); return; } if (StringUtils.equals(event.getEventType(), TYPE_SERVICE_CREATE)) { createService(event, serviceData); } else if (StringUtils.equals(event.getEventType(), TYPE_SERVICE_UPDATE)) { updateService(event, serviceData); } else if (StringUtils.equals(event.getEventType(), TYPE_SERVICE_DELETE)) { deleteService(event, serviceData); } else if (StringUtils.equals(event.getEventType(), TYPE_STACK_DELETE)) { deleteStack(event, serviceData); } } }); return null; }
void deleteStack(ExternalEvent event, Map<String, Object> stackData) { Stack env = stackDao.getStackByExternalId(event.getAccountId(), event.getExternalId()); if (env != null) { objectProcessManager.scheduleStandardProcess(StandardProcess.REMOVE, env, null); } }
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); } }