Commit 42edb9da authored by Anilople's avatar Anilople Committed by Jason Song

Dynamic environment for apollo-portal (#2867)

parent b7180071
......@@ -56,6 +56,15 @@
<artifactId>javassist</artifactId>
</dependency>
<!-- end of JDK 11+ -->
<!-- test -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<scope>test</scope>
</dependency>
<!-- end of test -->
</dependencies>
<build>
<plugins>
......
......@@ -3,7 +3,7 @@ package com.ctrip.framework.apollo.openapi.v1.controller;
import com.ctrip.framework.apollo.common.dto.ClusterDTO;
import com.ctrip.framework.apollo.common.entity.App;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.openapi.dto.OpenAppDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenEnvClusterDTO;
import com.ctrip.framework.apollo.openapi.util.OpenApiBeanUtils;
......
......@@ -14,7 +14,7 @@ import com.ctrip.framework.apollo.common.dto.ClusterDTO;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.common.utils.InputValidator;
import com.ctrip.framework.apollo.common.utils.RequestPrecondition;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.ctrip.framework.apollo.openapi.dto.OpenClusterDTO;
import com.ctrip.framework.apollo.openapi.util.OpenApiBeanUtils;
......
......@@ -3,7 +3,7 @@ package com.ctrip.framework.apollo.openapi.v1.controller;
import com.ctrip.framework.apollo.common.dto.ItemDTO;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.common.utils.RequestPrecondition;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.ctrip.framework.apollo.openapi.dto.OpenItemDTO;
import com.ctrip.framework.apollo.openapi.util.OpenApiBeanUtils;
......
......@@ -9,7 +9,7 @@ import com.ctrip.framework.apollo.common.dto.NamespaceDTO;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.common.utils.RequestPrecondition;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.ctrip.framework.apollo.openapi.auth.ConsumerPermissionValidator;
import com.ctrip.framework.apollo.openapi.dto.OpenGrayReleaseRuleDTO;
......
......@@ -8,7 +8,7 @@ import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.common.utils.InputValidator;
import com.ctrip.framework.apollo.common.utils.RequestPrecondition;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenNamespaceDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenNamespaceLockDTO;
......
......@@ -4,7 +4,7 @@ import com.ctrip.framework.apollo.common.dto.ReleaseDTO;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.common.utils.RequestPrecondition;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.ctrip.framework.apollo.openapi.auth.ConsumerPermissionValidator;
import com.ctrip.framework.apollo.openapi.dto.NamespaceGrayDelReleaseDTO;
......
package com.ctrip.framework.apollo.portal.api;
import com.ctrip.framework.apollo.common.dto.AccessKeyDTO;
import com.ctrip.framework.apollo.common.dto.AppDTO;
import com.ctrip.framework.apollo.common.dto.AppNamespaceDTO;
import com.ctrip.framework.apollo.common.dto.ClusterDTO;
import com.ctrip.framework.apollo.common.dto.CommitDTO;
import com.ctrip.framework.apollo.common.dto.GrayReleaseRuleDTO;
import com.ctrip.framework.apollo.common.dto.InstanceDTO;
import com.ctrip.framework.apollo.common.dto.ItemChangeSets;
import com.ctrip.framework.apollo.common.dto.ItemDTO;
import com.ctrip.framework.apollo.common.dto.NamespaceDTO;
import com.ctrip.framework.apollo.common.dto.NamespaceLockDTO;
import com.ctrip.framework.apollo.common.dto.PageDTO;
import com.ctrip.framework.apollo.common.dto.ReleaseDTO;
import com.ctrip.framework.apollo.common.dto.ReleaseHistoryDTO;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.common.dto.*;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.google.common.base.Joiner;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.boot.actuate.health.Health;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
......@@ -33,6 +14,8 @@ import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import java.util.*;
@Service
public class AdminServiceAPI {
......
package com.ctrip.framework.apollo.portal.component;
import com.ctrip.framework.apollo.core.MetaDomainConsts;
import com.ctrip.framework.apollo.portal.environment.PortalMetaDomainConsts;
import com.ctrip.framework.apollo.core.dto.ServiceDTO;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.core.utils.ApolloThreadFactory;
import com.ctrip.framework.apollo.tracer.Tracer;
import com.google.common.collect.Lists;
......@@ -106,17 +106,17 @@ public class AdminServiceAddressLocator {
return true;
} catch (Throwable e) {
logger.error(String.format("Get admin server address from meta server failed. env: %s, meta server address:%s",
env, MetaDomainConsts.getDomain(env)), e);
env, PortalMetaDomainConsts.getDomain(env)), e);
Tracer
.logError(String.format("Get admin server address from meta server failed. env: %s, meta server address:%s",
env, MetaDomainConsts.getDomain(env)), e);
env, PortalMetaDomainConsts.getDomain(env)), e);
}
}
return false;
}
private ServiceDTO[] getAdminServerAddress(Env env) {
String domainName = MetaDomainConsts.getDomain(env);
String domainName = PortalMetaDomainConsts.getDomain(env);
String url = domainName + ADMIN_SERVICE_URL_PATH;
return restTemplate.getForObject(url, ServiceDTO[].class);
}
......
package com.ctrip.framework.apollo.portal.component;
import com.ctrip.framework.apollo.core.MetaDomainConsts;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.PortalMetaDomainConsts;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.core.utils.ApolloThreadFactory;
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI;
import com.ctrip.framework.apollo.portal.component.config.PortalConfig;
......@@ -108,14 +108,14 @@ public class PortalSettings {
}
} else {
logger.error("Env health check failed, maybe because of admin server down. env: {}, meta server address: {}", env,
MetaDomainConsts.getDomain(env));
PortalMetaDomainConsts.getDomain(env));
handleEnvDown(env);
}
} catch (Exception e) {
logger.error("Env health check failed, maybe because of meta server down "
+ "or configure wrong meta server address. env: {}, meta server address: {}", env,
MetaDomainConsts.getDomain(env), e);
PortalMetaDomainConsts.getDomain(env), e);
handleEnvDown(env);
}
}
......@@ -133,17 +133,17 @@ public class PortalSettings {
if (!envStatusMark.get(env)) {
logger.error("Env is down. env: {}, failed times: {}, meta server address: {}", env, failedTimes,
MetaDomainConsts.getDomain(env));
PortalMetaDomainConsts.getDomain(env));
} else {
if (failedTimes >= ENV_DOWN_THRESHOLD) {
envStatusMark.put(env, false);
logger.error("Env is down because health check failed for {} times, "
+ "which equals to down threshold. env: {}, meta server address: {}", ENV_DOWN_THRESHOLD, env,
MetaDomainConsts.getDomain(env));
PortalMetaDomainConsts.getDomain(env));
} else {
logger.error(
"Env health check failed for {} times which less than down threshold. down threshold:{}, env: {}, meta server address: {}",
failedTimes, ENV_DOWN_THRESHOLD, env, MetaDomainConsts.getDomain(env));
failedTimes, ENV_DOWN_THRESHOLD, env, PortalMetaDomainConsts.getDomain(env));
}
}
......
package com.ctrip.framework.apollo.portal.component;
import com.ctrip.framework.apollo.common.exception.ServiceException;
import com.ctrip.framework.apollo.core.MetaDomainConsts;
import com.ctrip.framework.apollo.portal.environment.PortalMetaDomainConsts;
import com.ctrip.framework.apollo.core.dto.ServiceDTO;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.constant.TracerEventType;
import com.ctrip.framework.apollo.tracer.Tracer;
import com.ctrip.framework.apollo.tracer.spi.Transaction;
......@@ -116,7 +116,7 @@ public class RetryableRestTemplate {
//all admin server down
ServiceException e =
new ServiceException(String.format("Admin servers are unresponsive. meta server address: %s, admin servers: %s",
MetaDomainConsts.getDomain(env), services));
PortalMetaDomainConsts.getDomain(env), services));
ct.setStatus(e);
ct.complete();
throw e;
......@@ -160,7 +160,7 @@ public class RetryableRestTemplate {
//all admin server down
ServiceException e =
new ServiceException(String.format("Admin servers are unresponsive. meta server address: %s, admin servers: %s",
MetaDomainConsts.getDomain(env), services));
PortalMetaDomainConsts.getDomain(env), services));
ct.setStatus(e);
ct.complete();
throw e;
......@@ -175,7 +175,7 @@ public class RetryableRestTemplate {
ServiceException e = new ServiceException(String.format("No available admin server."
+ " Maybe because of meta server down or all admin server down. "
+ "Meta server address: %s",
MetaDomainConsts.getDomain(env)));
PortalMetaDomainConsts.getDomain(env)));
ct.setStatus(e);
ct.complete();
throw e;
......
......@@ -3,7 +3,7 @@ package com.ctrip.framework.apollo.portal.component.config;
import com.ctrip.framework.apollo.common.config.RefreshableConfig;
import com.ctrip.framework.apollo.common.config.RefreshablePropertySource;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.entity.vo.Organization;
import com.ctrip.framework.apollo.portal.service.PortalDBPropertySource;
import com.ctrip.framework.apollo.portal.service.SystemRoleManagerService;
......@@ -46,7 +46,7 @@ public class PortalConfig extends RefreshableConfig {
List<Env> envs = Lists.newLinkedList();
for (String env : configurations) {
envs.add(Env.fromString(env));
envs.add(Env.addEnvironment(env));
}
return envs;
......
......@@ -8,7 +8,7 @@ import com.ctrip.framework.apollo.common.constants.ReleaseOperationContext;
import com.ctrip.framework.apollo.common.dto.ReleaseDTO;
import com.ctrip.framework.apollo.common.entity.AppNamespace;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.component.config.PortalConfig;
import com.ctrip.framework.apollo.portal.constant.RoleType;
import com.ctrip.framework.apollo.portal.entity.bo.Email;
......
......@@ -5,7 +5,7 @@ import com.google.gson.Gson;
import com.ctrip.framework.apollo.common.constants.GsonType;
import com.ctrip.framework.apollo.common.dto.GrayReleaseRuleItemDTO;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.entity.bo.ReleaseHistoryBO;
import org.springframework.stereotype.Component;
......
package com.ctrip.framework.apollo.portal.component.emailbuilder;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.entity.bo.ReleaseHistoryBO;
import org.springframework.stereotype.Component;
......
package com.ctrip.framework.apollo.portal.component.emailbuilder;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.entity.bo.ReleaseHistoryBO;
import org.springframework.stereotype.Component;
......
package com.ctrip.framework.apollo.portal.component.emailbuilder;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.entity.bo.ReleaseHistoryBO;
import org.springframework.stereotype.Component;
......
package com.ctrip.framework.apollo.portal.controller;
import com.ctrip.framework.apollo.common.dto.AccessKeyDTO;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.service.AccessKeyService;
import com.ctrip.framework.apollo.portal.spi.UserInfoHolder;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.UUID;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
/**
* @author nisiyong
......
......@@ -7,7 +7,7 @@ import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.common.http.MultiResponseEntity;
import com.ctrip.framework.apollo.common.http.RichResponseEntity;
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.component.PortalSettings;
import com.ctrip.framework.apollo.portal.entity.model.AppModel;
import com.ctrip.framework.apollo.portal.entity.po.Role;
......@@ -181,16 +181,16 @@ public class AppController {
}
@GetMapping("/{appId}/miss_envs")
public MultiResponseEntity<Env> findMissEnvs(@PathVariable String appId) {
public MultiResponseEntity<String> findMissEnvs(@PathVariable String appId) {
MultiResponseEntity<Env> response = MultiResponseEntity.ok();
MultiResponseEntity<String> response = MultiResponseEntity.ok();
for (Env env : portalSettings.getActiveEnvs()) {
try {
appService.load(env, appId);
} catch (Exception e) {
if (e instanceof HttpClientErrorException &&
((HttpClientErrorException) e).getStatusCode() == HttpStatus.NOT_FOUND) {
response.addResponseEntity(RichResponseEntity.ok(env));
response.addResponseEntity(RichResponseEntity.ok(env.toString()));
} else {
response.addResponseEntity(RichResponseEntity.error(HttpStatus.INTERNAL_SERVER_ERROR,
String.format("load appId:%s from env %s error.", appId,
......
package com.ctrip.framework.apollo.portal.controller;
import com.ctrip.framework.apollo.common.dto.ClusterDTO;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.service.ClusterService;
import com.ctrip.framework.apollo.portal.spi.UserInfoHolder;
import org.springframework.http.ResponseEntity;
......
......@@ -2,7 +2,7 @@ package com.ctrip.framework.apollo.portal.controller;
import com.ctrip.framework.apollo.common.dto.CommitDTO;
import com.ctrip.framework.apollo.common.utils.RequestPrecondition;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.component.PermissionValidator;
import com.ctrip.framework.apollo.portal.service.CommitService;
import javax.validation.Valid;
......
......@@ -5,7 +5,7 @@ import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.common.exception.ServiceException;
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO;
import com.ctrip.framework.apollo.portal.entity.model.NamespaceTextModel;
import com.ctrip.framework.apollo.portal.service.ItemService;
......
......@@ -2,32 +2,20 @@ package com.ctrip.framework.apollo.portal.controller;
import com.ctrip.framework.apollo.common.dto.NamespaceDTO;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.core.enums.EnvUtils;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.ctrip.framework.apollo.openapi.entity.Consumer;
import com.ctrip.framework.apollo.openapi.entity.ConsumerRole;
import com.ctrip.framework.apollo.openapi.entity.ConsumerToken;
import com.ctrip.framework.apollo.openapi.service.ConsumerService;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Objects;
import org.springframework.web.bind.annotation.*;
import java.util.*;
/**
* @author Jason Song(song_s@ctrip.com)
......@@ -98,7 +86,7 @@ public class ConsumerController {
if (Strings.isNullOrEmpty(env)) {
continue;
}
if (Env.UNKNOWN == EnvUtils.transformEnv(env)) {
if (Env.UNKNOWN.equals(Env.transformEnv(env))) {
throw new BadRequestException(String.format("env: %s is illegal", env));
}
envList.add(env);
......
package com.ctrip.framework.apollo.portal.controller;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.component.PortalSettings;
import com.ctrip.framework.apollo.portal.environment.Env;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
......@@ -19,8 +20,12 @@ public class EnvController {
}
@GetMapping
public List<Env> envs() {
return portalSettings.getActiveEnvs();
public List<String> envs() {
List<String> environments = new ArrayList<>();
for(Env env : portalSettings.getActiveEnvs()) {
environments.add(env.toString());
}
return environments;
}
}
......@@ -3,7 +3,7 @@ package com.ctrip.framework.apollo.portal.controller;
import com.ctrip.framework.apollo.common.dto.InstanceDTO;
import com.ctrip.framework.apollo.common.dto.PageDTO;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.entity.vo.Number;
import com.ctrip.framework.apollo.portal.service.InstanceService;
import com.google.common.base.Splitter;
......
......@@ -5,7 +5,7 @@ import com.ctrip.framework.apollo.common.dto.ItemDTO;
import com.ctrip.framework.apollo.common.dto.NamespaceDTO;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.ctrip.framework.apollo.portal.component.PermissionValidator;
import com.ctrip.framework.apollo.portal.entity.model.NamespaceSyncModel;
......
......@@ -4,7 +4,7 @@ import com.ctrip.framework.apollo.common.dto.GrayReleaseRuleDTO;
import com.ctrip.framework.apollo.common.dto.NamespaceDTO;
import com.ctrip.framework.apollo.common.dto.ReleaseDTO;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.component.PermissionValidator;
import com.ctrip.framework.apollo.portal.component.config.PortalConfig;
import com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO;
......
......@@ -9,7 +9,7 @@ import com.ctrip.framework.apollo.common.http.RichResponseEntity;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.common.utils.InputValidator;
import com.ctrip.framework.apollo.common.utils.RequestPrecondition;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI;
import com.ctrip.framework.apollo.portal.component.PermissionValidator;
import com.ctrip.framework.apollo.portal.component.config.PortalConfig;
......
package com.ctrip.framework.apollo.portal.controller;
import com.ctrip.framework.apollo.common.dto.NamespaceLockDTO;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.entity.vo.LockInfo;
import com.ctrip.framework.apollo.portal.service.NamespaceLockService;
import org.springframework.web.bind.annotation.GetMapping;
......
......@@ -2,8 +2,6 @@ package com.ctrip.framework.apollo.portal.controller;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.common.utils.RequestPrecondition;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.core.enums.EnvUtils;
import com.ctrip.framework.apollo.portal.component.PermissionValidator;
import com.ctrip.framework.apollo.portal.constant.PermissionType;
import com.ctrip.framework.apollo.portal.constant.RoleType;
......@@ -12,6 +10,7 @@ import com.ctrip.framework.apollo.portal.entity.vo.AppRolesAssignedUsers;
import com.ctrip.framework.apollo.portal.entity.vo.NamespaceEnvRolesAssignedUsers;
import com.ctrip.framework.apollo.portal.entity.vo.NamespaceRolesAssignedUsers;
import com.ctrip.framework.apollo.portal.entity.vo.PermissionCondition;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.service.RoleInitializationService;
import com.ctrip.framework.apollo.portal.service.RolePermissionService;
import com.ctrip.framework.apollo.portal.service.SystemRoleManagerService;
......@@ -24,15 +23,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;
import org.springframework.web.bind.annotation.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
......@@ -116,7 +111,7 @@ public class PermissionController {
public NamespaceEnvRolesAssignedUsers getNamespaceEnvRoles(@PathVariable String appId, @PathVariable String env, @PathVariable String namespaceName) {
// validate env parameter
if (Env.UNKNOWN == EnvUtils.transformEnv(env)) {
if (Env.UNKNOWN == Env.transformEnv(env)) {
throw new BadRequestException("env is illegal");
}
......@@ -148,7 +143,7 @@ public class PermissionController {
}
// validate env parameter
if (Env.UNKNOWN == EnvUtils.transformEnv(env)) {
if (Env.UNKNOWN == Env.transformEnv(env)) {
throw new BadRequestException("env is illegal");
}
Set<String> assignedUser = rolePermissionService.assignRoleToUsers(RoleUtils.buildNamespaceRoleName(appId, namespaceName, roleType, env),
......@@ -170,7 +165,7 @@ public class PermissionController {
throw new BadRequestException("role type is illegal");
}
// validate env parameter
if (Env.UNKNOWN == EnvUtils.transformEnv(env)) {
if (Env.UNKNOWN == Env.transformEnv(env)) {
throw new BadRequestException("env is illegal");
}
rolePermissionService.removeRoleFromUsers(RoleUtils.buildNamespaceRoleName(appId, namespaceName, roleType, env),
......
......@@ -3,7 +3,7 @@ package com.ctrip.framework.apollo.portal.controller;
import com.ctrip.framework.apollo.common.dto.ReleaseDTO;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.common.exception.NotFoundException;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.component.PermissionValidator;
import com.ctrip.framework.apollo.portal.component.config.PortalConfig;
import com.ctrip.framework.apollo.portal.entity.bo.ReleaseBO;
......
package com.ctrip.framework.apollo.portal.controller;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.component.PermissionValidator;
import com.ctrip.framework.apollo.portal.entity.bo.ReleaseHistoryBO;
import com.ctrip.framework.apollo.portal.service.ReleaseHistoryService;
......
package com.ctrip.framework.apollo.portal.controller;
import com.ctrip.framework.apollo.Apollo;
import com.ctrip.framework.apollo.core.MetaDomainConsts;
import com.ctrip.framework.apollo.portal.environment.PortalMetaDomainConsts;
import com.ctrip.framework.apollo.core.dto.ServiceDTO;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.component.PortalSettings;
import com.ctrip.framework.apollo.portal.component.RestTemplateFactory;
import com.ctrip.framework.apollo.portal.entity.vo.EnvironmentInfo;
......@@ -101,13 +101,13 @@ public class SystemInfoController {
private EnvironmentInfo adaptEnv2EnvironmentInfo(final Env env) {
EnvironmentInfo environmentInfo = new EnvironmentInfo();
String metaServerAddresses = MetaDomainConsts.getMetaServerAddress(env);
String metaServerAddresses = PortalMetaDomainConsts.getMetaServerAddress(env);
environmentInfo.setEnv(env);
environmentInfo.setActive(portalSettings.isEnvActive(env));
environmentInfo.setMetaServerAddress(metaServerAddresses);
String selectedMetaServerAddress = MetaDomainConsts.getDomain(env);
String selectedMetaServerAddress = PortalMetaDomainConsts.getDomain(env);
try {
environmentInfo.setConfigServices(getServerAddress(selectedMetaServerAddress, CONFIG_SERVICE_URL_PATH));
......
package com.ctrip.framework.apollo.portal.entity.model;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.core.utils.StringUtils;
public class NamespaceReleaseModel implements Verifiable {
......
......@@ -2,7 +2,7 @@ package com.ctrip.framework.apollo.portal.entity.model;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.core.utils.StringUtils;
public class NamespaceTextModel implements Verifiable {
......
package com.ctrip.framework.apollo.portal.entity.vo;
import com.ctrip.framework.apollo.common.dto.ClusterDTO;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import java.util.List;
public class EnvClusterInfo {
private Env env;
private String env;
private List<ClusterDTO> clusters;
public EnvClusterInfo(Env env) {
this.env = env;
this.env = env.toString();
}
public Env getEnv() {
return env;
return Env.valueOf(env);
}
public void setEnv(Env env) {
this.env = env;
this.env = env.toString();
}
public List<ClusterDTO> getClusters() {
......
package com.ctrip.framework.apollo.portal.entity.vo;
import com.ctrip.framework.apollo.core.dto.ServiceDTO;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
public class EnvironmentInfo {
private Env env;
private String env;
private boolean active;
private String metaServerAddress;
......@@ -15,11 +15,11 @@ public class EnvironmentInfo {
private String errorMessage;
public Env getEnv() {
return env;
return Env.valueOf(env);
}
public void setEnv(Env env) {
this.env = env;
this.env = env.toString();
}
public boolean isActive() {
......
package com.ctrip.framework.apollo.portal.entity.vo;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
public class NamespaceEnvRolesAssignedUsers extends NamespaceRolesAssignedUsers {
private Env env;
private String env;
public Env getEnv() {
return env;
return Env.valueOf(env);
}
public void setEnv(Env env) {
this.env = env;
this.env = env.toString();
}
}
package com.ctrip.framework.apollo.portal.entity.vo;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.ctrip.framework.apollo.portal.entity.model.Verifiable;
......
package com.ctrip.framework.apollo.portal.environment;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.google.common.base.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author wxq
*/
public class Env {
private static final Logger logger = LoggerFactory.getLogger(Env.class);
// name of environment, cannot be null
private final String name;
// use to cache Env
private static final Map<String, Env> STRING_ENV_MAP = new ConcurrentHashMap<>();;
// default environments
public static final Env LOCAL = addEnvironment("LOCAL");
public static final Env DEV = addEnvironment("DEV");
public static final Env FWS = addEnvironment("FWS");
public static final Env FAT = addEnvironment("FAT");
public static final Env UAT = addEnvironment("UAT");
public static final Env LPT = addEnvironment("LPT");
public static final Env PRO = addEnvironment("PRO");
public static final Env TOOLS = addEnvironment("TOOLS");
public static final Env UNKNOWN = addEnvironment("UNKNOWN");
/**
* Cannot create by other
* @param name
*/
private Env(String name) {
this.name = name;
}
/**
* add some change to environment name
* trim and to upper
* @param environmentName
* @return
*/
private static String getWellFormName(String environmentName) {
return environmentName.trim().toUpperCase();
}
/**
* logic same as
* @see com.ctrip.framework.apollo.core.enums.EnvUtils transformEnv
* @param envName
* @return
*/
public static Env transformEnv(String envName) {
if(Env.exists(envName)) {
return Env.valueOf(envName);
}
if (StringUtils.isBlank(envName)) {
return Env.UNKNOWN;
}
switch (envName.trim().toUpperCase()) {
case "LPT":
return Env.LPT;
case "FAT":
case "FWS":
return Env.FAT;
case "UAT":
return Env.UAT;
case "PRO":
case "PROD": //just in case
return Env.PRO;
case "DEV":
return Env.DEV;
case "LOCAL":
return Env.LOCAL;
case "TOOLS":
return Env.TOOLS;
default:
return Env.UNKNOWN;
}
}
/**
* a environment name exist or not
* @param name
* @return
*/
public static boolean exists(String name) {
name = getWellFormName(name);
return STRING_ENV_MAP.containsKey(name);
}
/**
* add an environment
* @param name
* @return
*/
public static Env addEnvironment(String name) {
if (StringUtils.isBlank(name)) {
throw new RuntimeException("Cannot add a blank environment: " + "[" + name + "]");
}
name = getWellFormName(name);
if(STRING_ENV_MAP.containsKey(name)) {
// has been existed
logger.debug("{} already exists.", name);
} else {
// not existed
STRING_ENV_MAP.put(name, new Env(name));
}
return STRING_ENV_MAP.get(name);
}
/**
* replace valueOf in enum
* But what would happened if environment not exist?
*
* @param name
* @throws IllegalArgumentException if this existed environment has no Env with the specified name
* @return
*/
public static Env valueOf(String name) {
name = getWellFormName(name);
if(exists(name)) {
return STRING_ENV_MAP.get(name);
} else {
throw new IllegalArgumentException(name + " not exist");
}
}
/**
* Please use {@code Env.valueOf} instead this method
* @param env
* @return
*/
@Deprecated
public static Env fromString(String env) {
Env environment = transformEnv(env);
Preconditions.checkArgument(environment != UNKNOWN, String.format("Env %s is invalid", env));
return environment;
}
/**
* Not just name in Env,
* the address of Env must be same,
* or it will throw {@code RuntimeException}
* @param o
* @throws RuntimeException When same name but different address
* @return
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Env env = (Env) o;
if(getName().equals(env.getName())) {
throw new RuntimeException(getName() + " is same environment name, but their Env not same");
} else {
return false;
}
}
@Override
public int hashCode() {
return Objects.hash(getName());
}
/**
* a Env convert to string, ie its name.
* @return
*/
@Override
public String toString() {
return name;
}
/**
* Backward compatibility with enum's name method
* @return
*/
@Deprecated
public String name() {
return name;
}
public String getName() {
return name;
}
}
package com.ctrip.framework.apollo.portal.environment;
import com.ctrip.framework.apollo.core.utils.ApolloThreadFactory;
import com.ctrip.framework.apollo.core.utils.NetUtil;
import com.ctrip.framework.apollo.tracer.Tracer;
import com.ctrip.framework.apollo.tracer.spi.Transaction;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Only use in apollo-portal
* Provider an available meta server url.
* If there is no available meta server url for the given environment,
* the default meta server url will be used(http://apollo.meta).
* @see com.ctrip.framework.apollo.core.MetaDomainConsts
* @author wxq
*/
public class PortalMetaDomainConsts {
public static final String DEFAULT_META_URL = "http://apollo.meta";
// env -> meta server address cache
private static final Map<Env, String> metaServerAddressCache = Maps.newConcurrentMap();
// initialize meta server provider without cache
private static final PortalMetaServerProvider metaServerProvider = new PortalMetaServerProvider();
private static final long REFRESH_INTERVAL_IN_SECOND = 60;// 1 min
private static final Logger logger = LoggerFactory.getLogger(PortalMetaDomainConsts.class);
// comma separated meta server address -> selected single meta server address cache
private static final Map<String, String> selectedMetaServerAddressCache = Maps.newConcurrentMap();
private static final AtomicBoolean periodicRefreshStarted = new AtomicBoolean(false);
/**
* Return one meta server address. If multiple meta server addresses are configured, will select one.
*/
public static String getDomain(Env env) {
String metaServerAddress = getMetaServerAddress(env);
// if there is more than one address, need to select one
if (metaServerAddress.contains(",")) {
return selectMetaServerAddress(metaServerAddress);
}
return metaServerAddress;
}
/**
* Return meta server address. If multiple meta server addresses are configured, will return the comma separated string.
*/
public static String getMetaServerAddress(Env env) {
// in cache?
if (!metaServerAddressCache.containsKey(env)) {
// put it to cache
metaServerAddressCache.put(
env,
getMetaServerAddressCacheValue(metaServerProvider, env)
);
}
// get from cache
return metaServerAddressCache.get(env);
}
/**
* Get the meta server from provider by given environment.
* If there is no available meta server url for the given environment,
* the default meta server url will be used(http://apollo.meta).
* @param provider
* @param env
* @return
*/
private static String getMetaServerAddressCacheValue(PortalMetaServerProvider provider, Env env) {
String metaAddress = provider.getMetaServerAddress(env);
logger.info("Located meta server address [{}] for env [{}]", metaAddress, env);
if (Strings.isNullOrEmpty(metaAddress)) {
// Fallback to default meta address
metaAddress = DEFAULT_META_URL;
logger.warn(
"Meta server address fallback to [{}] for env [{}], because it is not available in MetaServerProvider",
metaAddress, env);
}
return metaAddress.trim();
}
/**
* Select one available meta server from the comma separated meta server addresses, e.g.
* http://1.2.3.4:8080,http://2.3.4.5:8080
*
* <br />
*
* In production environment, we still suggest using one single domain like http://config.xxx.com(backed by software
* load balancers like nginx) instead of multiple ip addresses
*/
private static String selectMetaServerAddress(String metaServerAddresses) {
String metaAddressSelected = selectedMetaServerAddressCache.get(metaServerAddresses);
if (metaAddressSelected == null) {
// initialize
if (periodicRefreshStarted.compareAndSet(false, true)) {
schedulePeriodicRefresh();
}
updateMetaServerAddresses(metaServerAddresses);
metaAddressSelected = selectedMetaServerAddressCache.get(metaServerAddresses);
}
return metaAddressSelected;
}
private static void updateMetaServerAddresses(String metaServerAddresses) {
logger.debug("Selecting meta server address for: {}", metaServerAddresses);
Transaction transaction = Tracer.newTransaction("Apollo.MetaService", "refreshMetaServerAddress");
transaction.addData("Url", metaServerAddresses);
try {
List<String> metaServers = Lists.newArrayList(metaServerAddresses.split(","));
// random load balancing
Collections.shuffle(metaServers);
boolean serverAvailable = false;
for (String address : metaServers) {
address = address.trim();
//check whether /services/config is accessible
if (NetUtil.pingUrl(address + "/services/config")) {
// select the first available meta server
selectedMetaServerAddressCache.put(metaServerAddresses, address);
serverAvailable = true;
logger.debug("Selected meta server address {} for {}", address, metaServerAddresses);
break;
}
}
// we need to make sure the map is not empty, e.g. the first update might be failed
if (!selectedMetaServerAddressCache.containsKey(metaServerAddresses)) {
selectedMetaServerAddressCache.put(metaServerAddresses, metaServers.get(0).trim());
}
if (!serverAvailable) {
logger.warn("Could not find available meta server for configured meta server addresses: {}, fallback to: {}",
metaServerAddresses, selectedMetaServerAddressCache.get(metaServerAddresses));
}
transaction.setStatus(Transaction.SUCCESS);
} catch (Throwable ex) {
transaction.setStatus(ex);
throw ex;
} finally {
transaction.complete();
}
}
private static void schedulePeriodicRefresh() {
ScheduledExecutorService scheduledExecutorService =
Executors.newScheduledThreadPool(1, ApolloThreadFactory.create("MetaServiceLocator", true));
scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
try {
for (String metaServerAddresses : selectedMetaServerAddressCache.keySet()) {
updateMetaServerAddresses(metaServerAddresses);
}
} catch (Throwable ex) {
logger.warn(String.format("Refreshing meta server address failed, will retry in %d seconds",
REFRESH_INTERVAL_IN_SECOND), ex);
}
}
}, REFRESH_INTERVAL_IN_SECOND, REFRESH_INTERVAL_IN_SECOND, TimeUnit.SECONDS);
}
}
package com.ctrip.framework.apollo.portal.environment;
import com.ctrip.framework.apollo.core.utils.ResourceUtils;
import com.ctrip.framework.apollo.portal.util.KeyValueUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
/**
* Only use in apollo-portal
* load all meta server address from
* - System Property [key ends with "_meta" (case insensitive)]
* - OS environment variable [key ends with "_meta" (case insensitive)]
* - user's configuration file [key ends with ".meta" (case insensitive)]
* when apollo-portal start up.
* @see com.ctrip.framework.apollo.core.internals.LegacyMetaServerProvider
* @author wxq
*/
public class PortalMetaServerProvider {
private static final Logger logger = LoggerFactory.getLogger(PortalMetaServerProvider.class);
/**
* environments and their meta server address
* properties file path
*/
private static final String APOLLO_ENV_PROPERTIES_FILE_PATH = "apollo-env.properties";
// thread safe
private static volatile Map<Env, String> domains = initializeDomains();
public PortalMetaServerProvider() {
}
/**
* load all environment's meta address dynamically when this class loaded by JVM
*/
private static Map<Env, String> initializeDomains() {
// find key-value from System Property which key ends with "_meta" (case insensitive)
Map<String, String> metaServerAddressesFromSystemProperty = KeyValueUtils.filterWithKeyIgnoreCaseEndsWith(System.getProperties(), "_meta");
// remove key's suffix "_meta" (case insensitive)
metaServerAddressesFromSystemProperty = KeyValueUtils.removeKeySuffix(metaServerAddressesFromSystemProperty, "_meta".length());
// find key-value from OS environment variable which key ends with "_meta" (case insensitive)
Map<String, String> metaServerAddressesFromOSEnvironment = KeyValueUtils.filterWithKeyIgnoreCaseEndsWith(System.getenv(), "_meta");
// remove key's suffix "_meta" (case insensitive)
metaServerAddressesFromOSEnvironment = KeyValueUtils.removeKeySuffix(metaServerAddressesFromOSEnvironment, "_meta".length());
// find key-value from properties file which key ends with ".meta" (case insensitive)
Properties properties = new Properties();
properties = ResourceUtils.readConfigFile(APOLLO_ENV_PROPERTIES_FILE_PATH, properties);
Map<String, String> metaServerAddressesFromPropertiesFile = KeyValueUtils.filterWithKeyIgnoreCaseEndsWith(properties, ".meta");
// remove key's suffix ".meta" (case insensitive)
metaServerAddressesFromPropertiesFile = KeyValueUtils.removeKeySuffix(metaServerAddressesFromPropertiesFile, ".meta".length());
// begin to add key-value, key is environment, value is meta server address matched
Map<String, String> metaServerAddresses = new HashMap<>();
// lower priority add first
metaServerAddresses.putAll(metaServerAddressesFromPropertiesFile);
metaServerAddresses.putAll(metaServerAddressesFromOSEnvironment);
metaServerAddresses.putAll(metaServerAddressesFromSystemProperty);
// add to domain
Map<Env, String> map = new ConcurrentHashMap<>();
for(Map.Entry<String, String> entry : metaServerAddresses.entrySet()) {
// add new environment
Env env = Env.addEnvironment(entry.getKey());
// get meta server address value
String value = entry.getValue();
// put pair (Env, meta server address)
map.put(env, value);
}
// log all
logger.info("All environment's meta server address: {}", map);
return map;
}
/**
* reload all
* environments and meta server addresses
*/
public static void reloadAll() {
domains = initializeDomains();
}
public static String getMetaServerAddress(Env targetEnv) {
String metaServerAddress = domains.get(targetEnv);
return metaServerAddress == null ? null : metaServerAddress.trim();
}
/**
* add a environment's meta server address
* for the feature: add self-define environment in the web ui
* @param env
* @param metaServerAddress
*/
public static void addMetaServerAddress(Env env, String metaServerAddress) {
domains.put(env, metaServerAddress);
}
/**
* delete the meta server address of the environment
* @param env
*/
public static void deleteMetaServerAddress(Env env) {
domains.remove(env);
}
/**
* update the meta server address of the environment
* @param env
* @param metaServerAddress
*/
public static void updateMetaServerAddress(Env env, String metaServerAddress) {
domains.put(env, metaServerAddress);
}
/**
* clear all environments and meta server addresses saved
*/
public static void clear() {
domains.clear();
}
}
......@@ -2,7 +2,7 @@ package com.ctrip.framework.apollo.portal.listener;
import com.ctrip.framework.apollo.common.dto.AppDTO;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI;
import com.ctrip.framework.apollo.portal.component.PortalSettings;
import com.ctrip.framework.apollo.tracer.Tracer;
......
package com.ctrip.framework.apollo.portal.listener;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import org.springframework.context.ApplicationEvent;
......@@ -75,7 +75,7 @@ public class ConfigPublishEvent extends ApplicationEvent {
public static class ConfigPublishInfo {
private Env env;
private String env;
private String appId;
private String clusterName;
private String namespaceName;
......@@ -87,11 +87,11 @@ public class ConfigPublishEvent extends ApplicationEvent {
private boolean isGrayPublishEvent;
public Env getEnv() {
return env;
return Env.valueOf(env);
}
public void setEnv(Env env) {
this.env = env;
this.env = env.toString();
}
public String getAppId() {
......
package com.ctrip.framework.apollo.portal.listener;
import com.ctrip.framework.apollo.common.constants.ReleaseOperation;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.core.utils.ApolloThreadFactory;
import com.ctrip.framework.apollo.portal.component.config.PortalConfig;
import com.ctrip.framework.apollo.portal.component.emailbuilder.GrayPublishEmailBuilder;
......
......@@ -3,7 +3,7 @@ package com.ctrip.framework.apollo.portal.listener;
import com.ctrip.framework.apollo.common.dto.AppDTO;
import com.ctrip.framework.apollo.common.dto.AppNamespaceDTO;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI;
import com.ctrip.framework.apollo.portal.component.PortalSettings;
import com.ctrip.framework.apollo.tracer.Tracer;
......
......@@ -3,7 +3,7 @@ package com.ctrip.framework.apollo.portal.listener;
import com.ctrip.framework.apollo.common.dto.AppDTO;
import com.ctrip.framework.apollo.common.dto.AppNamespaceDTO;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI;
import com.ctrip.framework.apollo.portal.component.PortalSettings;
import com.ctrip.framework.apollo.tracer.Tracer;
......
package com.ctrip.framework.apollo.portal.service;
import com.ctrip.framework.apollo.common.dto.AccessKeyDTO;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI;
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI.AccessKeyAPI;
import com.ctrip.framework.apollo.portal.constant.TracerEventType;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.tracer.Tracer;
import java.util.List;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AccessKeyService {
......
......@@ -5,7 +5,7 @@ import com.ctrip.framework.apollo.common.dto.PageDTO;
import com.ctrip.framework.apollo.common.entity.App;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI;
import com.ctrip.framework.apollo.portal.constant.TracerEventType;
import com.ctrip.framework.apollo.portal.entity.bo.UserInfo;
......
......@@ -2,7 +2,7 @@ package com.ctrip.framework.apollo.portal.service;
import com.ctrip.framework.apollo.common.dto.ClusterDTO;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI;
import com.ctrip.framework.apollo.portal.constant.TracerEventType;
import com.ctrip.framework.apollo.portal.spi.UserInfoHolder;
......
package com.ctrip.framework.apollo.portal.service;
import com.ctrip.framework.apollo.common.dto.CommitDTO;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI;
import org.springframework.stereotype.Service;
......
......@@ -2,7 +2,7 @@ package com.ctrip.framework.apollo.portal.service;
import com.ctrip.framework.apollo.common.dto.InstanceDTO;
import com.ctrip.framework.apollo.common.dto.PageDTO;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI;
import org.springframework.stereotype.Service;
......
......@@ -7,7 +7,7 @@ import com.ctrip.framework.apollo.common.dto.NamespaceDTO;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI;
import com.ctrip.framework.apollo.portal.component.txtresolver.ConfigTextResolver;
......
......@@ -6,7 +6,7 @@ import com.ctrip.framework.apollo.common.dto.ItemDTO;
import com.ctrip.framework.apollo.common.dto.NamespaceDTO;
import com.ctrip.framework.apollo.common.dto.ReleaseDTO;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI;
import com.ctrip.framework.apollo.portal.component.ItemsComparator;
import com.ctrip.framework.apollo.portal.constant.TracerEventType;
......
package com.ctrip.framework.apollo.portal.service;
import com.ctrip.framework.apollo.common.dto.NamespaceLockDTO;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI;
import com.ctrip.framework.apollo.portal.component.config.PortalConfig;
import com.ctrip.framework.apollo.portal.entity.vo.LockInfo;
......
......@@ -8,7 +8,6 @@ import com.ctrip.framework.apollo.common.entity.AppNamespace;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI;
import com.ctrip.framework.apollo.portal.component.PortalSettings;
......@@ -17,23 +16,21 @@ import com.ctrip.framework.apollo.portal.constant.RoleType;
import com.ctrip.framework.apollo.portal.constant.TracerEventType;
import com.ctrip.framework.apollo.portal.entity.bo.ItemBO;
import com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.spi.UserInfoHolder;
import com.ctrip.framework.apollo.portal.util.RoleUtils;
import com.ctrip.framework.apollo.tracer.Tracer;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
@Service
public class NamespaceService {
......
......@@ -6,7 +6,7 @@ import com.ctrip.framework.apollo.common.dto.ReleaseDTO;
import com.ctrip.framework.apollo.common.dto.ReleaseHistoryDTO;
import com.ctrip.framework.apollo.common.entity.EntityPair;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI;
import com.ctrip.framework.apollo.portal.entity.bo.ReleaseHistoryBO;
import com.ctrip.framework.apollo.portal.util.RelativeDateFormat;
......
......@@ -3,7 +3,7 @@ package com.ctrip.framework.apollo.portal.service;
import com.ctrip.framework.apollo.common.constants.GsonType;
import com.ctrip.framework.apollo.common.dto.ItemChangeSets;
import com.ctrip.framework.apollo.common.dto.ReleaseDTO;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI;
import com.ctrip.framework.apollo.portal.constant.TracerEventType;
......
package com.ctrip.framework.apollo.portal.spi;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.entity.bo.ReleaseHistoryBO;
public interface MQService {
......
......@@ -3,7 +3,7 @@ package com.ctrip.framework.apollo.portal.spi.ctrip;
import com.google.gson.Gson;
import com.ctrip.framework.apollo.common.entity.App;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.component.config.PortalConfig;
import com.ctrip.framework.apollo.portal.entity.bo.ReleaseHistoryBO;
import com.ctrip.framework.apollo.portal.service.AppService;
......
package com.ctrip.framework.apollo.portal.spi.defaultimpl;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.entity.bo.ReleaseHistoryBO;
import com.ctrip.framework.apollo.portal.spi.MQService;
......
......@@ -3,7 +3,7 @@ package com.ctrip.framework.apollo.portal.spi.defaultimpl;
import com.ctrip.framework.apollo.common.entity.App;
import com.ctrip.framework.apollo.common.entity.BaseEntity;
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.component.config.PortalConfig;
import com.ctrip.framework.apollo.portal.constant.PermissionType;
import com.ctrip.framework.apollo.portal.constant.RoleType;
......
package com.ctrip.framework.apollo.portal.util;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* some tools for manipulate key in map and properties
* @author wxq
*/
public class KeyValueUtils {
/**
* make a filter on properties.
* and convert properties to a map
* the suffix match is case insensitive
* @param properties
* @param suffix suffix in a key
* @return a map which key is ends with suffix
*/
public static Map<String, String> filterWithKeyIgnoreCaseEndsWith(Properties properties, String suffix) {
// use O(n log(n)) algorithm
Map<String, String> keyValues = new HashMap<>();
for(String propertyName : properties.stringPropertyNames()) {
keyValues.put(propertyName, properties.getProperty(propertyName));
}
return filterWithKeyIgnoreCaseEndsWith(keyValues, suffix);
}
/**
* make a filter on map's key,
* keep the k-v which key ends with suffix given
* the suffix match is case insensitive
* @param keyValues
* @param suffix suffix in a key
* @return a map which key is ends with suffix
*/
public static Map<String, String> filterWithKeyIgnoreCaseEndsWith(Map<String, String> keyValues, String suffix) {
// use O(n) algorithm
Map<String, String> map = new HashMap<>();
for(Map.Entry<String, String> entry : keyValues.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
// let key and suffix both to upper,
// so the suffix match doesn't care about the character is upper or lower
if(key.toUpperCase().endsWith(suffix.toUpperCase())) {
map.put(key, value);
}
}
return map;
}
/**
* remove key's suffix in a map
* suppose that all keys's length not smaller than suffixLength,
* if not satisfied, a terrible runtime exception will occur
* @param keyValues
* @param suffixLength suffix string's length
* @return
*/
public static Map<String, String> removeKeySuffix(Map<String, String> keyValues, int suffixLength) {
// use O(n) algorithm
Map<String, String> map = new HashMap<>();
for(Map.Entry<String, String> entry : keyValues.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
String newKey = key.substring(0, key.length() - suffixLength);
map.put(newKey, value);
}
return map;
}
}
package com.ctrip.framework.apollo.openapi.service;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.openapi.entity.Consumer;
import com.ctrip.framework.apollo.openapi.entity.ConsumerRole;
import com.ctrip.framework.apollo.openapi.entity.ConsumerToken;
......@@ -11,12 +10,11 @@ import com.ctrip.framework.apollo.portal.AbstractUnitTest;
import com.ctrip.framework.apollo.portal.component.config.PortalConfig;
import com.ctrip.framework.apollo.portal.entity.bo.UserInfo;
import com.ctrip.framework.apollo.portal.entity.po.Role;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.service.RolePermissionService;
import com.ctrip.framework.apollo.portal.spi.UserInfoHolder;
import com.ctrip.framework.apollo.portal.spi.UserService;
import com.ctrip.framework.apollo.portal.util.RoleUtils;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
......@@ -26,18 +24,12 @@ import org.mockito.Spy;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Optional;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
public class ConsumerServiceTest extends AbstractUnitTest {
......
......@@ -2,7 +2,7 @@ package com.ctrip.framework.apollo.portal;
import com.ctrip.framework.apollo.common.exception.ServiceException;
import com.ctrip.framework.apollo.core.dto.ServiceDTO;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.component.AdminServiceAddressLocator;
import com.ctrip.framework.apollo.portal.component.RetryableRestTemplate;
......
package com.ctrip.framework.apollo.portal.environment;
import com.ctrip.framework.apollo.tracer.spi.MessageProducer;
import com.ctrip.framework.apollo.tracer.spi.Transaction;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.junit.After;
import org.junit.Before;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.ServerSocket;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public abstract class BaseIntegrationTest {
protected static final int PORT = findFreePort();
private Server server;
/**
* init and start a jetty server, remember to call server.stop when the task is finished
*/
protected Server startServerWithHandlers(ContextHandler... handlers) throws Exception {
server = new Server(PORT);
ContextHandlerCollection contexts = new ContextHandlerCollection();
contexts.setHandlers(handlers);
server.setHandler(contexts);
server.start();
return server;
}
@Before
public void setUp() throws Exception {
MessageProducer someProducer = mock(MessageProducer.class);
Transaction someTransaction = mock(Transaction.class);
when(someProducer.newTransaction(anyString(), anyString())).thenReturn(someTransaction);
}
@After
public void tearDown() throws Exception {
if (server != null && server.isStarted()) {
server.stop();
}
}
protected ContextHandler mockServerHandler(final int statusCode, final String response) {
ContextHandler context = new ContextHandler("/");
context.setHandler(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/plain;charset=UTF-8");
response.setStatus(statusCode);
response.getWriter().println(response);
baseRequest.setHandled(true);
}
});
return context;
}
/**
* Returns a free port number on localhost.
*
* Heavily inspired from org.eclipse.jdt.launching.SocketUtil (to avoid a dependency to JDT just because of this).
* Slightly improved with close() missing in JDT. And throws exception instead of returning -1.
*
* @return a free port number on localhost
* @throws IllegalStateException if unable to find a free port
*/
protected static int findFreePort() {
ServerSocket socket = null;
try {
socket = new ServerSocket(0);
socket.setReuseAddress(true);
int port = socket.getLocalPort();
try {
socket.close();
} catch (IOException e) {
// Ignore IOException on close()
}
return port;
} catch (IOException e) {
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
}
}
}
throw new IllegalStateException("Could not find a free TCP/IP port to start embedded Jetty HTTP Server on");
}
}
package com.ctrip.framework.apollo.portal.environment;
import org.junit.Test;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import static org.junit.Assert.*;
public class EnvTest {
@Test
public void exist() {
assertFalse(Env.exists("xxxyyy234"));
assertTrue(Env.exists("local"));
assertTrue(Env.exists("dev"));
}
@Test
public void addEnv() {
String name = "someEEEE";
assertFalse(Env.exists(name));
Env.addEnvironment(name);
assertTrue(Env.exists(name));
}
@Test(expected = IllegalArgumentException.class)
public void valueOf() {
String name = "notexist";
assertFalse(Env.exists(name));
assertEquals(Env.valueOf(name), Env.UNKNOWN);
assertEquals(Env.valueOf("dev"), Env.DEV);
assertEquals(Env.valueOf("UAT"), Env.UAT);
}
@Test
public void testEquals() {
assertEquals(Env.DEV, Env.valueOf("dEv"));
String name = "someEEEE";
Env.addEnvironment(name);
assertFalse(Env.valueOf(name).equals(Env.DEV));
}
@Test(expected = RuntimeException.class)
public void testEqualsWithRuntimeException()
throws NoSuchMethodException,
IllegalAccessException,
InvocationTargetException,
InstantiationException {
// get private constructor
Constructor<Env> envConstructor = Env.class.getDeclaredConstructor(String.class);
// make private constructor accessible
envConstructor.setAccessible(true);
// make a fake Env
Env fakeDevEnv = envConstructor.newInstance(Env.DEV.toString());
// compare, then a RuntimeException will invoke
fakeDevEnv.equals(Env.DEV);
}
@Test
public void testEqualWithoutException() {
assertTrue(Env.DEV.equals(Env.DEV));
assertTrue(Env.DEV.equals(Env.valueOf("dEV")));
assertFalse(Env.PRO.equals(Env.DEV));
assertFalse(Env.DEV.equals(Env.valueOf("uaT")));
}
@Test
public void testToString() {
assertEquals("DEV", Env.DEV.toString());
}
@Test
public void name() {
assertEquals("DEV", Env.DEV.name());
}
@Test
public void getName() {
String name = "getName";
Env.addEnvironment(name);
assertEquals(name.trim().toUpperCase(), Env.valueOf(name).toString());
}
}
\ No newline at end of file
package com.ctrip.framework.apollo.portal.environment;
import org.junit.After;
import org.junit.Test;
import javax.servlet.http.HttpServletResponse;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class PortalMetaDomainConstsTest extends BaseIntegrationTest {
@Override
@After
public void tearDown() throws Exception {
super.tearDown();
MockMetaServerProvider.clear();
}
@Test
public void testGetMetaDomain() {
// local
String localMetaServerAddress = "http://localhost:8080";
PortalMetaServerProvider.updateMetaServerAddress(Env.LOCAL, localMetaServerAddress);
assertEquals(localMetaServerAddress, PortalMetaDomainConsts.getDomain(Env.LOCAL));
// add this environment without meta server address
String randomEnvironment = "randomEnvironment";
Env.addEnvironment(randomEnvironment);
assertEquals(PortalMetaDomainConsts.DEFAULT_META_URL, PortalMetaDomainConsts.getDomain(Env.valueOf(randomEnvironment)));
}
@Test
public void testGetValidAddress() throws Exception {
String someResponse = "some response";
startServerWithHandlers(mockServerHandler(HttpServletResponse.SC_OK, someResponse));
String validServer = " http://localhost:" + PORT + " ";
String invalidServer = "http://localhost:" + findFreePort();
MockMetaServerProvider.mock(Env.FAT, validServer + "," + invalidServer);
MockMetaServerProvider.mock(Env.UAT, invalidServer + "," + validServer);
assertEquals(validServer.trim(), PortalMetaDomainConsts.getDomain(Env.FAT));
assertEquals(validServer.trim(), PortalMetaDomainConsts.getDomain(Env.UAT));
}
@Test
public void testInvalidAddress() {
String invalidServer = "http://localhost:" + findFreePort() + " ";
String anotherInvalidServer = "http://localhost:" + findFreePort() + " ";
MockMetaServerProvider.mock(Env.LPT, invalidServer + "," + anotherInvalidServer);
String metaServer = PortalMetaDomainConsts.getDomain(Env.LPT);
assertTrue(metaServer.equals(invalidServer.trim()) || metaServer.equals(anotherInvalidServer.trim()));
}
public static class MockMetaServerProvider {
private static void mock(Env env, String metaServerAddress) {
PortalMetaServerProvider.addMetaServerAddress(env, metaServerAddress);
}
private static void clear() {
PortalMetaServerProvider.clear();
}
}
}
\ No newline at end of file
package com.ctrip.framework.apollo.portal.environment;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class PortalMetaServerProviderTest {
/**
* may be the environments and meta server addresses
* have been clear, so we need to reload when start the every unit test
*/
@Before
public void reload() {
PortalMetaServerProvider.reloadAll();
}
@After
public void tearDown() throws Exception {
System.clearProperty("dev_meta");
}
@Test
public void testFromPropertyFile() {
assertEquals("http://localhost:8080", PortalMetaServerProvider.getMetaServerAddress(Env.LOCAL));
assertEquals("${dev_meta}", PortalMetaServerProvider.getMetaServerAddress(Env.DEV));
assertEquals("${pro_meta}", PortalMetaServerProvider.getMetaServerAddress(Env.PRO));
}
/**
* testing the environment dynamic added from system property
*/
@Test
public void testDynamicEnvironmentFromSystemProperty() {
String someDevMetaAddress = "someMetaAddress";
String someFatMetaAddress = "someFatMetaAddress";
System.setProperty("dev_meta", someDevMetaAddress);
System.setProperty("fat_meta", someFatMetaAddress);
// reload above added
PortalMetaServerProvider.reloadAll();
assertEquals(someDevMetaAddress, PortalMetaServerProvider.getMetaServerAddress(Env.DEV));
assertEquals(someFatMetaAddress, PortalMetaServerProvider.getMetaServerAddress(Env.FAT));
String randomAddress = "randomAddress";
String randomEnvironment = "randomEnvironment";
System.setProperty(randomEnvironment + "_meta", randomAddress);
// reload above added
PortalMetaServerProvider.reloadAll();
assertEquals(
randomAddress,
PortalMetaServerProvider.getMetaServerAddress(
Env.valueOf(randomEnvironment)
)
);
// clear the property
System.clearProperty(randomEnvironment + "_meta");
}
}
\ No newline at end of file
......@@ -5,7 +5,7 @@ import com.ctrip.framework.apollo.common.dto.ItemDTO;
import com.ctrip.framework.apollo.common.dto.NamespaceDTO;
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.AbstractUnitTest;
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI;
import com.ctrip.framework.apollo.portal.spi.UserInfoHolder;
......
......@@ -6,7 +6,7 @@ import com.ctrip.framework.apollo.common.dto.ReleaseDTO;
import com.ctrip.framework.apollo.common.entity.AppNamespace;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.AbstractUnitTest;
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI;
import com.ctrip.framework.apollo.portal.component.txtresolver.PropertyResolver;
......
package com.ctrip.framework.apollo.portal.spi.defaultImpl;
import com.ctrip.framework.apollo.common.entity.App;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.AbstractUnitTest;
import com.ctrip.framework.apollo.portal.component.config.PortalConfig;
import com.ctrip.framework.apollo.portal.constant.PermissionType;
......
package com.ctrip.framework.apollo.portal.util;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class KeyValueUtilsTest {
@Test
public void testFilterWithKeyEndswith() {
// map
Map<String, String> map = new HashMap<>();
map.put("abc.met", "none");
map.put("abc_meta", "none");
map.put("2bc.meta", "none");
map.put("abc?met", "none");
Map<String, String> afterFilter = KeyValueUtils.filterWithKeyIgnoreCaseEndsWith(map, "_meta");
for(Map.Entry<String, String> entry : afterFilter.entrySet()) {
String key = entry.getKey();
assertTrue(key.endsWith("_meta"));
}
}
@Test
public void testRemoveKeySuffix() {
Map<String, String> map = new HashMap<>();
map.put("abc_meta", "none");
map.put("234_meta", "none");
map.put("888_meta", "none");
Map<String, String> afterFilter = KeyValueUtils.removeKeySuffix(map, "_meta".length());
for(Map.Entry<String, String> entry : afterFilter.entrySet()) {
String key = entry.getKey();
assertFalse(key.endsWith("_meta"));
assertFalse(key.contains("_meta"));
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment