/** * {@inheritDoc} */ @Override public void from(io.cattle.platform.core.model.AccountLink from) { setId(from.getId()); setName(from.getName()); setAccountId(from.getAccountId()); setKind(from.getKind()); setUuid(from.getUuid()); setDescription(from.getDescription()); setState(from.getState()); setCreated(from.getCreated()); setRemoved(from.getRemoved()); setRemoveTime(from.getRemoveTime()); setData(from.getData()); setLinkedAccountId(from.getLinkedAccountId()); setLinkedAccount(from.getLinkedAccount()); setLinkedRegionId(from.getLinkedRegionId()); setLinkedRegion(from.getLinkedRegion()); setExternal(from.getExternal()); }
/** * {@inheritDoc} */ @Override public <E extends io.cattle.platform.core.model.AccountLink> E into(E into) { into.from(this); return into; }
private void getLinkedEnvironments(long accountId, Map<String, Long> links, Map<Long, Region> regionsIds, Map<String, ExternalProject> externalProjects) { List<AccountLink> accountLinks = objectManager.find(AccountLink.class, ACCOUNT_LINK.ACCOUNT_ID, accountId, ACCOUNT_LINK.REMOVED, null, ACCOUNT_LINK.LINKED_REGION_ID, new Condition(ConditionType.NOTNULL)); for (AccountLink link : accountLinks) { if (link.getState().equalsIgnoreCase(CommonStatesConstants.REMOVING)) { continue; } if(link.getState().equalsIgnoreCase(CommonStatesConstants.REMOVED)) { continue; } Region targetRegion = regionsIds.get(link.getLinkedRegionId()); if (targetRegion == null) { continue; } String UUID = getUUID(targetRegion.getName(), link.getLinkedAccount()); if (!externalProjects.containsKey(UUID)) { links.put(UUID, targetRegion.getId()); } } }
new Condition(ConditionType.NOTNULL)); for(AccountLink link : accountLinks) { if (invalidStates.contains(link.getState())) { continue; Region targetRegion = regionMap.get(link.getLinkedRegionId()); if(targetRegion == null) { objectProcessManager.executeStandardProcess(StandardProcess.REMOVE, link, null); String externalProjectKey = String.format("%s:%s", targetRegion.getName(), link.getLinkedAccount()); if(externalProjectMap.containsKey(externalProjectKey)) { externalProjectResponse = externalProjectMap.get(externalProjectKey); } else { externalProjectResponse = RegionUtil.getTargetProjectByName(targetRegion, link.getLinkedAccount(), jsonMapper); externalProjectMap.put(externalProjectKey, externalProjectResponse); if(link.getExternal()) { Account localAccount = objectManager.loadResource(Account.class, link.getAccountId()); ExternalAccountLink accLink = RegionUtil.getAccountLinkForExternal(targetRegion, externalProject, localAccount, jsonMapper); if(accLink == null || removedStates.contains(accLink.getState())){ log.warn(String.format("Failed to monitor account link for %s - %s", link.getId(), ex)); existingLinks.add(String.format("%s:%s", link.getLinkedRegion(), link.getLinkedAccount())); continue;
@Override public boolean deleteExternalAccountLink(AccountLink link) { if (link.getExternal()) { return true; Region targetRegion = objectManager.loadResource(Region.class, link.getLinkedRegionId()); if(targetRegion == null) { return true; ExternalProjectResponse externalProjectResponse = RegionUtil.getTargetProjectByName(targetRegion, link.getLinkedAccount(), jsonMapper); ExternalProject targetResourceAccount = externalProjectResponse.externalProject; if (targetResourceAccount == null) { Account localAccount = objectManager.loadResource(Account.class, link.getAccountId()); ExternalAccountLink externalLink = RegionUtil.getExternalAccountLink(targetRegion, targetResourceAccount, localAccount, jsonMapper); return true; } catch (Exception ex) { log.error(String.format("Failed to delete external account link for accountLink [%d]", link.getId()), ex); return false;
@Override public HandlerResult handle(ProcessState state, ProcessInstance process) { AccountLink accountLink = (AccountLink) state.getResource(); if (accountLink.getLinkedAccountId() == null) { regionSvc.deleteExternalAccountLink(accountLink); } else { updateServices(accountLink); Account account = objectManager.loadResource(Account.class, accountLink.getAccountId()); if (account == null) { return null; } regenerateAccountLinks(account); } return null; }
protected void deleteOldAccountLinks(Account account, List<? extends Long> newAccountIds) { List<? extends AccountLink> allLinks = objectManager.find(AccountLink.class, ACCOUNT_LINK.ACCOUNT_ID, account.getId(), ACCOUNT_LINK.REMOVED, null); for (AccountLink link : allLinks) { if (!newAccountIds.contains(link.getLinkedAccountId())) { objectProcessManager.scheduleStandardProcessAsync(StandardProcess.REMOVE, link, null); } } }
Set<String> existingLinksKeys = new HashSet<>(); for (AccountLink existingLink : existingLinks) { existingLinksKeys.add(getUUID(existingLink.getLinkedRegion(), existingLink.getLinkedAccount())); if (!toAdd.contains(getUUID(link.getLinkedRegion(), link.getLinkedAccount()))) { toRemove.add(link); } else {
private void cleanupExternalLinks(Region region) { for (AccountLink link : objectManager.find(AccountLink.class, ACCOUNT_LINK.REMOVED, null, ACCOUNT_LINK.LINKED_REGION_ID, region.getId())) { if (!link.getState().equalsIgnoreCase(CommonStatesConstants.REMOVING)) { objectProcessManager.executeStandardProcess(StandardProcess.REMOVE, link, null); } } } }
private void registerAsTargetService(Service targetService) { List<Long> accountIds = new ArrayList<>(); accountIds.add(targetService.getAccountId()); List<? extends AccountLink> linkedToAccounts = objectManager.find(AccountLink.class, ACCOUNT_LINK.LINKED_ACCOUNT_ID, targetService.getAccountId(), ACCOUNT_LINK.REMOVED, null); // add all accounts that are linked to service's account for (AccountLink linkedToAccount : linkedToAccounts) { accountIds.add(linkedToAccount.getAccountId()); } List<Service> services = new ArrayList<>(); for (Long accountId : accountIds) { services.addAll(objectManager.find(Service.class, SERVICE.ACCOUNT_ID, accountId, SERVICE.REMOVED, null)); } for (Service service : services) { // skip itself if (targetService.getId().equals(service.getId())) { continue; } if (isSelectorLinkMatch(service.getSelectorLink(), targetService)) { addServiceLink(service, targetService); } } }
if (link.getExternal()) { return; Region targetRegion = objectManager.loadResource(Region.class, link.getLinkedRegionId()); Region localRegion = objectManager.findAny(Region.class, REGION.LOCAL, true, REGION.REMOVED, null); if(localRegion == null) { localRegion.getName(), targetRegion.getName())); ExternalProjectResponse externalProjectResponse = RegionUtil.getTargetProjectByName(targetRegion, link.getLinkedAccount(), jsonMapper); ExternalProject targetResourceAccount = externalProjectResponse.externalProject; if (targetResourceAccount == null) { throw new RuntimeException(String.format("Failed to find target environment by name [%s] in region [%s]", link.getLinkedAccount(), localRegion.getName())); Account localAccount = objectManager.loadResource(Account.class, link.getAccountId()); ExternalAccountLink externalLink = RegionUtil.getExternalAccountLink(targetRegion, targetResourceAccount, localAccount, jsonMapper); externalLink = RegionUtil.createExternalAccountLink(targetRegion, data, jsonMapper); } catch (Exception ex) { throw new RuntimeException(String.format("Failed to create external account link for accountLink [%d]", link.getId()), ex);
@Override public HandlerResult handle(ProcessState state, ProcessInstance process) { AccountLink accountLink = (AccountLink) state.getResource(); if (accountLink.getLinkedAccountId() == null) { regionSvc.createExternalAccountLink(accountLink); } else { List<? extends Service> services = objectManager.find(Service.class, SERVICE.ACCOUNT_ID, accountLink.getAccountId(), SERVICE.SELECTOR_LINK, new Condition(ConditionType.NOTNULL), SERVICE.REMOVED, null); for (Service service : services) { sdSvc.registerServiceLinks(service); } } return null; }
@Override public void doWithLockNoResult() { List<? extends AccountLink> allLinks = objectManager.find(AccountLink.class, ACCOUNT_LINK.ACCOUNT_ID, account.getId(), ACCOUNT_LINK.REMOVED, null, ACCOUNT_LINK.STATE, new Condition(ConditionType.NE, CommonStatesConstants.REMOVING)); List<Long> newLinks = new ArrayList<>(); for (AccountLink aLink : allLinks) { newLinks.add(aLink.getLinkedAccountId()); } List<Long> existingLinks = DataAccessor.fieldLongList(account, AccountConstants.FIELD_ACCOUNT_LINKS); if (existingLinks.containsAll(newLinks) && newLinks.containsAll(existingLinks)) { return; } objectManager.setFields(account, AccountConstants.FIELD_ACCOUNT_LINKS, newLinks); ObjectUtils.publishChanged(eventService, objectManager, account); } });
private void reconcileAccountLinks(long accountId, Map<String, Region> regionsMap, List<AccountLink> toRemove, List<AccountLink> toUpdate, Set<String> toCreate) { for (AccountLink item : toRemove) { if (!item.getState().equalsIgnoreCase(CommonStatesConstants.REMOVING)) { objectProcessManager.scheduleStandardProcess(StandardProcess.REMOVE, item, null); if (item.getState().equalsIgnoreCase(CommonStatesConstants.REQUESTED)) { objectProcessManager.scheduleStandardProcessAsync(StandardProcess.CREATE, item, null);
@SuppressWarnings("unchecked") private void updateServices(AccountLink accountLink) { List<? extends ServiceConsumeMap> consumeMaps = objectManager.find(ServiceConsumeMap.class, SERVICE_CONSUME_MAP.ACCOUNT_ID, accountLink.getAccountId(), SERVICE_CONSUME_MAP.REMOVED, null); List<? extends Service> services = objectManager.find(Service.class, SERVICE.ACCOUNT_ID, accountLink.getLinkedAccountId(), SERVICE.REMOVED, null); List<Long> serviceIds = (List<Long>) CollectionUtils.collect(services, TransformerUtils.invokerTransformer("getId")); for (ServiceConsumeMap consumeMap : consumeMaps) { if (serviceIds.contains(consumeMap.getConsumedServiceId())) { objectProcessManager.scheduleStandardProcessAsync(StandardProcess.REMOVE, consumeMap, null); } } }
private void registerTargetServices(Service service) { List<Long> targetAccountIds = new ArrayList<>(); targetAccountIds.add(service.getAccountId()); // add all accounts that are linked to service's account List<? extends AccountLink> linkedAccounts = objectManager.find(AccountLink.class, ACCOUNT_LINK.ACCOUNT_ID, service.getAccountId(), ACCOUNT_LINK.REMOVED, null, ACCOUNT_LINK.LINKED_ACCOUNT_ID, new Condition(ConditionType.NOTNULL)); for (AccountLink linkedAccount : linkedAccounts) { targetAccountIds.add(linkedAccount.getLinkedAccountId()); } List<Service> targetServices = new ArrayList<>(); for (Long accountId : targetAccountIds) { targetServices.addAll(objectManager.find(Service.class, SERVICE.ACCOUNT_ID, accountId, SERVICE.REMOVED, null)); } for (Service targetService : targetServices) { // skip itself if (targetService.getId().equals(service.getId())) { continue; } if (isSelectorLinkMatch(service.getSelectorLink(), targetService)) { addServiceLink(service, targetService); } } }
protected void createNewAccountLinks(Account account, List<? extends Long> newAccountIds) { for (Long accountId : newAccountIds) { AccountLink link = objectManager.findAny(AccountLink.class, ACCOUNT_LINK.ACCOUNT_ID, account.getId(), ACCOUNT_LINK.LINKED_ACCOUNT_ID, accountId, ACCOUNT_LINK.REMOVED, null); if (link == null) { link = objectManager.create(AccountLink.class, ACCOUNT_LINK.ACCOUNT_ID, account.getId(), ACCOUNT_LINK.LINKED_ACCOUNT_ID, accountId); } if (link.getState().equalsIgnoreCase(CommonStatesConstants.REQUESTED)) { objectProcessManager.executeStandardProcess(StandardProcess.CREATE, link, null); } } }
account.getId(), ACCOUNT_LINK.REMOVED, null, ACCOUNT_LINK.LINKED_ACCOUNT_ID, new Condition(ConditionType.NOTNULL)); for (AccountLink accountLink : accountLinks) { Long accountId = accountLink.getLinkedAccountId(); accounts.put(accountId, allAccountsMap.get(accountId)); account.getId(), ACCOUNT_LINK.REMOVED, null); for (AccountLink accountLink : accountLinks) { Long accountId = accountLink.getAccountId(); accounts.put(accountId, allAccountsMap.get(accountId));