Skip to content
This repository has been archived by the owner on Jan 4, 2022. It is now read-only.

Commit

Permalink
region fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kinarashah authored and Alena Prokharchyk committed Feb 1, 2018
1 parent 49510cb commit 35200ed
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.cattle.platform.servicediscovery.service.impl.RegionServiceImpl;
import io.cattle.platform.servicediscovery.service.impl.RegionUtil;
import io.cattle.platform.servicediscovery.service.impl.RegionUtil.ExternalProject;
import io.cattle.platform.servicediscovery.service.impl.RegionUtil.ExternalProjectResponse;
import io.cattle.platform.servicediscovery.service.impl.RegionUtil.ExternalRegion;
import io.cattle.platform.task.Task;
import io.cattle.platform.object.process.StandardProcess;
Expand Down Expand Up @@ -64,7 +65,7 @@ public void run() {
List<AccountLink> accountLinks = objectManager.find(AccountLink.class, ACCOUNT_LINK.REMOVED, null, ACCOUNT_LINK.LINKED_REGION_ID,
new Condition(ConditionType.NOTNULL));
Map<String, ExternalRegion> externalRegionMap = new HashMap<String, ExternalRegion>();
Map<String, ExternalProject> externalProjectMap = new HashMap<String, ExternalProject>();
Map<String, ExternalProjectResponse> externalProjectMap = new HashMap<String, ExternalProjectResponse>();
for(AccountLink link : accountLinks) {
if (invalidStates.contains(link.getState())) {
continue;
Expand All @@ -78,7 +79,7 @@ public void run() {
try {
// localRegion in targetRegion not present
ExternalRegion externalRegion = null;
String externalRegionKey = String.format("%s$%s", targetRegion, localRegion.getName());
String externalRegionKey = String.format("%s:%s", targetRegion.getName(), localRegion.getName());
if(externalRegionMap.containsKey(externalRegionKey)) {
externalRegion = externalRegionMap.get(externalRegionKey);
} else {
Expand All @@ -90,16 +91,20 @@ public void run() {
continue;
}
// environment not present or its uuid changed
ExternalProject externalProject = null;
String externalProjectKey = String.format("%s$%s", targetRegion, link.getLinkedAccount());
ExternalProjectResponse externalProjectResponse = null;
String externalProjectKey = String.format("%s:%s", targetRegion.getName(), link.getLinkedAccount());
if(externalProjectMap.containsKey(externalProjectKey)) {
externalProject = externalProjectMap.get(externalProjectKey);
externalProjectResponse = externalProjectMap.get(externalProjectKey);
} else {
externalProject = RegionUtil.getTargetProjectByName(targetRegion, link.getLinkedAccount(), jsonMapper);
externalProjectMap.put(externalProjectKey, externalProject);
externalProjectResponse = RegionUtil.getTargetProjectByName(targetRegion, link.getLinkedAccount(), jsonMapper);
externalProjectMap.put(externalProjectKey, externalProjectResponse);
}
String storedUUID = DataAccessor.fieldString(link, "uuid");
if(externalProject == null || (storedUUID!=null && !externalProject.getUuid().equals(storedUUID))) {

String storedUUID = DataAccessor.fieldString(link, "linkedAccountUuid");
ExternalProject externalProject = externalProjectResponse.getExternalProject();
boolean notFound = (externalProject == null && externalProjectResponse.getStatusCode()==200);
boolean uuidsDoNotMatch = (storedUUID!=null && !externalProject.getUuid().equals(storedUUID));
if(notFound || uuidsDoNotMatch) {
objectProcessManager.executeStandardProcess(StandardProcess.REMOVE, link, null);
}
} catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import io.cattle.platform.servicediscovery.service.impl.RegionUtil.ExternalAccountLink;
import io.cattle.platform.servicediscovery.service.impl.RegionUtil.ExternalAgent;
import io.cattle.platform.servicediscovery.service.impl.RegionUtil.ExternalProject;
import io.cattle.platform.servicediscovery.service.impl.RegionUtil.ExternalProjectResponse;
import io.cattle.platform.servicediscovery.service.impl.RegionUtil.ExternalRegion;

import io.github.ibuildthecloud.gdapi.condition.Condition;
Expand Down Expand Up @@ -112,16 +113,33 @@ private void reconcileAccountLinks(long accountId, Map<String, Region> regionsMa
objectProcessManager.scheduleStandardProcess(StandardProcess.REMOVE, item, null);
}
}

Set<String> invalidAccounts = new HashSet<>();

for (String item : toCreate) {
String[] splitted = item.split(":");
String regionName = splitted[0];
String envName = splitted[1];
Region region = regionsMap.get(regionName);
ExternalProject externalProject = null;
if (region != null) {
try {
String externalProjectKey = String.format("%s:%s", regionName, envName);
if(invalidAccounts.contains(externalProjectKey)) {
continue;
}
ExternalProjectResponse externalProjectResponse = RegionUtil.getTargetProjectByName(region, envName, jsonMapper);
externalProject = externalProjectResponse.externalProject;
if(externalProject == null) {
invalidAccounts.add(externalProjectKey);
continue;
}
} catch(Exception ex) {
log.warn("Failed to find account for %s - %s", envName, ex);
}
AccountLink link = objectManager.create(AccountLink.class, ACCOUNT_LINK.ACCOUNT_ID,
accountId, ACCOUNT_LINK.LINKED_ACCOUNT, envName, ACCOUNT_LINK.LINKED_REGION, regionName,
ACCOUNT_LINK.LINKED_REGION_ID, region.getId());
ACCOUNT_LINK.LINKED_REGION_ID, region.getId(), "linkedAccountUuid", externalProject.getUuid());
toUpdate.add(link);
}
}
Expand Down Expand Up @@ -264,7 +282,8 @@ private ExternalAgent createExternalAgent(Agent agent, Account account, Region l
if (externalProjects.containsKey(UUID)) {
targetResourceAccount = externalProjects.get(UUID);
} else {
targetResourceAccount = RegionUtil.getTargetProjectByName(targetRegion, cred.getEnvironmentName(), jsonMapper);
ExternalProjectResponse externalProjectResponse = RegionUtil.getTargetProjectByName(targetRegion, cred.getEnvironmentName(), jsonMapper);
targetResourceAccount = externalProjectResponse.externalProject;
if (targetResourceAccount == null) {
throw new RuntimeException(String.format("Failed to find target environment by name [%s] in region [%s]",
cred.getEnvironmentName(), targetRegion.getName()));
Expand Down Expand Up @@ -311,7 +330,8 @@ public void createExternalAccountLink(AccountLink link) {
throw new RuntimeException(String.format("Failed to find local region [%s] in external region [%s]",
localRegion.getName(), targetRegion.getName()));
}
ExternalProject targetResourceAccount = RegionUtil.getTargetProjectByName(targetRegion, link.getLinkedAccount(), jsonMapper);
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()));
Expand All @@ -330,8 +350,6 @@ public void createExternalAccountLink(AccountLink link) {
data.put("linkedRegion", externalRegion.getName());
data.put("linkedRegionId", externalRegion.getId());
externalLink = RegionUtil.createExternalAccountLink(targetRegion, data, jsonMapper);
DataAccessor.setField(link, "uuid", targetResourceAccount.getUuid());
objectManager.persist(link);
} catch (Exception ex) {
throw new RuntimeException(String.format("Failed to create external account link for accountLink [%d]", link.getId()), ex);
}
Expand All @@ -354,11 +372,18 @@ public void deleteExternalAccountLink(AccountLink link) {
localRegion.getName(), targetRegion.getName()));
return;
}
ExternalProject targetResourceAccount = RegionUtil.getTargetProjectByName(targetRegion, link.getLinkedAccount(), jsonMapper);
ExternalProjectResponse externalProjectResponse = RegionUtil.getTargetProjectByName(targetRegion, link.getLinkedAccount(), jsonMapper);
ExternalProject targetResourceAccount = externalProjectResponse.externalProject;
if (targetResourceAccount == null) {
log.info(String.format("Failed to find target environment by name [%s] in region [%s]",
link.getLinkedAccount(), localRegion.getName()));
return;
String UUID = DataAccessor.fieldString(link, "linkedAccountUuid");
targetResourceAccount = RegionUtil.getTargetProjectByUUID(targetRegion, UUID, jsonMapper);
if (targetResourceAccount == null) {
log.info(String.format("Failed to find target environment by UUID [%s] in region [%s]",
UUID, localRegion.getName()));
return;
}
}
Account localAccount = objectManager.loadResource(Account.class, link.getAccountId());
ExternalAccountLink externalLink = RegionUtil.getExternalAccountLink(targetRegion, targetResourceAccount,
Expand Down Expand Up @@ -420,11 +445,6 @@ protected boolean deactivateAndRemoveExtenralAgent(Agent agent, Region localRegi
try {
log.info(String.format("Removing agent with externalId [%s] in environment [%s] and region [%s]", agent.getUuid(), regionName,
envName));
ExternalProject targetResourceAccount = RegionUtil.getTargetProjectByName(targetRegion, envName, jsonMapper);
if (targetResourceAccount == null) {
log.info(String.format("Failed to find target environment by name [%s] in region [%s]", envName, regionName));
return true;
}
ExternalAgent externalAgent = RegionUtil.getExternalAgent(targetRegion, cred.getAgentUuid(), jsonMapper);
if (externalAgent == null) {
log.info(String.format("Failed to find agent by externalId [%s] in environment [%s] and region [%s]", agent.getUuid(), regionName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.cattle.platform.core.model.Agent;
import io.cattle.platform.core.model.Region;
import io.cattle.platform.json.JsonMapper;
import io.cattle.platform.servicediscovery.service.impl.RegionUtil.ExternalProject;

import java.io.IOException;
import java.util.Arrays;
Expand Down Expand Up @@ -101,12 +102,37 @@ public static String getTargetAgentUri(String localRegionName, String localEnvir
return String.format("%s%s_%s_%s_%s", EXTERNAL_AGENT_URI_PREFIX, localRegionName, localEnvironmentName, agentUuid, targetResourceAccountUuid);
}

public static ExternalProject getTargetProjectByName(Region targetRegion, String accountName, JsonMapper jsonMapper) throws IOException {
public static ExternalProjectResponse getTargetProjectByName(Region targetRegion, String accountName, JsonMapper jsonMapper) throws IOException {
String uri = String.format("%s/v2-beta/projects?name=%s&all=true",
getUrl(targetRegion),
accountName);
Request req = Request.Get(uri);
setHeaders(req, targetRegion);
return req.execute().handleResponse(new ResponseHandler<ExternalProjectResponse>() {
@Override
public ExternalProjectResponse handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
ExternalProjectResponse externalProjectResponse = new ExternalProjectResponse();
externalProjectResponse.statusCode = response.getStatusLine().getStatusCode();
externalProjectResponse.externalProject = null;
if (externalProjectResponse.statusCode != 200) {
return externalProjectResponse;
}

ExternalProjectData data = jsonMapper.readValue(response.getEntity().getContent(), ExternalProjectData.class);
if(data.data.size() > 0) {
externalProjectResponse.externalProject = data.data.get(0);
}
return externalProjectResponse;
}
});
}

public static ExternalProject getTargetProjectByUUID(Region targetRegion, String UUID, JsonMapper jsonMapper) throws IOException {
String uri = String.format("%s/v2-beta/projects?uuid=%s&all=true",
getUrl(targetRegion),
UUID);
Request req = Request.Get(uri);
setHeaders(req, targetRegion);
return req.execute().handleResponse(new ResponseHandler<ExternalProject>() {
@Override
public ExternalProject handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
Expand All @@ -115,9 +141,10 @@ public ExternalProject handleResponse(HttpResponse response) throws ClientProtoc
}

ExternalProjectData data = jsonMapper.readValue(response.getEntity().getContent(), ExternalProjectData.class);
return data.data.size() == 0 ? null : data.data.get(0);
return data.data.size() == 0 ? null : data.data.get(0);
}
});

}

public static ExternalAgent createExternalAgent(Region targetRegion, String targetEnvName, Map<String, Object> params,
Expand Down Expand Up @@ -385,6 +412,19 @@ public void setData(List<ExternalRegion> data) {
this.data = data;
}
}

public static class ExternalProjectResponse {
ExternalProject externalProject;
int statusCode;

public ExternalProject getExternalProject() {
return this.externalProject;
}

public int getStatusCode() {
return this.statusCode;
}
}

public static class ExternalRegion {
String id;
Expand Down
7 changes: 7 additions & 0 deletions resources/content/schema/base/accountLink.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"linkedAccountUuid" : {
"type": "string",
"create": true
}
}

0 comments on commit 35200ed

Please sign in to comment.