@Override public ExternalAgent handleResponse(HttpResponse response) throws ClientProtocolException, IOException { if (response.getStatusLine().getStatusCode() >= 300) { throw new IOException( String.format("Failed to create external agent with uri [%s] in environment [%s] in region [%s]: response error code %s", params.get(AgentConstants.FIELD_URI), targetEnvName, targetRegion.getName(), response.getStatusLine().getReasonPhrase())); } return jsonMapper.readValue(response.getEntity().getContent(), ExternalAgent.class); } });
public MetaHelperInfo(Account account, Map<Long, Account> accounts, Set<Long> otherAccountsServicesIds, Set<Long> otherAccountsStackIds, MetaDataInfoDao dao, Region regionObj) { super(); this.accounts = accounts; this.otherAccountsServicesIds = otherAccountsServicesIds; this.otherAccountsStackIds = otherAccountsStackIds; this.account = account; // may be fix in the future - get health checker hosts ids for instances of other accounts // otherwise this info is irrelevant this.instanceIdToHealthCheckers = dao.getInstanceIdToHealthCheckers(account); this.hostIdToHostMetadata = dao.getHostIdToHostMetadata(account, accounts, otherAccountsServicesIds); if (regionObj != null) { regionName = regionObj.getName(); } }
@Override public boolean deactivateAndRemoveExternalAgent(Agent agent) { List<? extends ExternalCredential> creds = DataAccessor.fieldObjectList(agent, AccountConstants.FIELD_EXTERNAL_CREDENTIALS, ExternalCredential.class, jsonMapper); if (creds.isEmpty()) { return true; } Map<String, Region> regions = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); for (Region region : objectManager.find(Region.class, REGION.REMOVED, new Condition(ConditionType.NULL))) { regions.put(region.getName(), region); } for (ExternalCredential cred : creds) { if (!deactivateAndRemoveExternalAgent(agent, cred)) { return false; } } return true; } }
@Override public void doWithLockNoResult() { List<Region> regions = objectManager.find(Region.class, REGION.REMOVED, new Condition(ConditionType.NULL)); if (regions.size() == 0) { return; } Account localAccount = objectManager.loadResource(Account.class, accountId); if (INVALID_STATES.contains(localAccount.getState())) { return; } Map<String, Region> regionsMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); Region localRegion = null; for (Region region : regions) { regionsMap.put(region.getName(), region); if (region.getLocal()) { localRegion = region; } } List<AccountLink> toRemove = new ArrayList<>(); List<AccountLink> toUpdate = new ArrayList<>(); Set<String> toCreate = new HashSet<>(); fetchAccountLinks(accountId, regionsMap, localRegion, toRemove, toUpdate, toCreate); reconcileAccountLinks(accountId, regionsMap, toRemove, toUpdate, toCreate); } });
@Override public boolean isRegionsEmpty(Agent agent, Account account, Map<String, Long> externalLinks, Map<String, ExternalProject> projects, Map<Long, Region> regionsIds, Map<String, Region> regionNameToRegion) { List<Region> regions = objectManager.find(Region.class, REGION.REMOVED, (Object) null); for(Region region : regions) { regionsIds.put(region.getId(), region); regionNameToRegion.put(region.getName(), region); } getLinkedEnvironments(account.getId(), externalLinks, regionsIds, projects); boolean noExternalLinks = externalLinks.isEmpty(); List<? extends ExternalCredential> existing = DataAccessor.fieldObjectList(agent, AccountConstants.FIELD_EXTERNAL_CREDENTIALS, ExternalCredential.class, jsonMapper); boolean noExternalCreds = existing.isEmpty(); if (noExternalLinks && noExternalCreds) { return true; } return false; }
String UUID = getUUID(targetRegion.getName(), cred.getEnvironmentName()); ExternalProject targetResourceAccount = null; if (externalProjects.containsKey(UUID)) { if (targetResourceAccount == null) { throw new RuntimeException(String.format("Failed to find target environment by name [%s] in region [%s]", cred.getEnvironmentName(), targetRegion.getName())); String targetAgentUri = RegionUtil.getTargetAgentUri(localRegion.getName(), account.getName(), agent.getUuid(), targetResourceAccount.getUuid()); log.info(String.format("Creating external agent with uri [%s] in environment [%s] in region [%s]", targetAgentUri,
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()); } } }
return; ExternalRegion externalRegion = RegionUtil.getExternalRegion(targetRegion, localRegion.getName(), jsonMapper); if (externalRegion == null) { throw new RuntimeException(String.format("Failed to find local region [%s] in external region [%s]", localRegion.getName(), targetRegion.getName())); if (targetResourceAccount == null) { throw new RuntimeException(String.format("Failed to find target environment by name [%s] in region [%s]", link.getLinkedAccount(), localRegion.getName()));
String externalRegionKey = String.format("%s:%s", targetRegion.getName(), localRegion.getName()); if(externalRegionMap.containsKey(externalRegionKey)) { externalRegion = externalRegionMap.get(externalRegionKey); } else { externalRegion = RegionUtil.getExternalRegion(targetRegion, localRegion.getName(), jsonMapper); externalRegionMap.put(externalRegionKey, externalRegion); String externalProjectKey = String.format("%s:%s", targetRegion.getName(), link.getLinkedAccount()); if(externalProjectMap.containsKey(externalProjectKey)) { externalProjectResponse = externalProjectMap.get(externalProjectKey);
if (targetResourceAccount == null) { log.info(String.format("Failed to find target environment by UUID [%s] in region [%s]", UUID, targetRegion.getName())); return true;
/** * {@inheritDoc} */ @Override public void from(io.cattle.platform.core.model.Region 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()); setUrl(from.getUrl()); setPublicValue(from.getPublicValue()); setSecretValue(from.getSecretValue()); setLocal(from.getLocal()); }