diff --git a/src/main/java/com/ctrip/zeus/restful/resource/GroupResource.java b/src/main/java/com/ctrip/zeus/restful/resource/GroupResource.java index 63d5f4d0..ea2c49bc 100644 --- a/src/main/java/com/ctrip/zeus/restful/resource/GroupResource.java +++ b/src/main/java/com/ctrip/zeus/restful/resource/GroupResource.java @@ -131,6 +131,72 @@ public class GroupResource { return input; } }) + .addFilterId(new FilterSet() { + @Override + public Set filter(Set input) throws Exception { + input.removeAll(groupCriteriaQuery.queryAllVGroups()); + return input; + } + }) + .build(); + for (Group group : groupRepository.list(executer.run())) { + groupList.addGroup(getGroupByType(group, type)); + } + groupList.setTotal(groupList.getGroups().size()); + return responseHandler.handle(groupList, hh.getMediaType()); + } + + @GET + @Path("/vgroups") + @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + @Authorize(name = "getAllGroups") + public Response listVGroups(@Context HttpHeaders hh, + @Context HttpServletRequest request, + @QueryParam("slbId") final Long slbId, + @TrimmedQueryParam("domain") final String domain, + @TrimmedQueryParam("type") String type, + @TrimmedQueryParam("tag") final String tag, + @TrimmedQueryParam("pname") final String pname, + @TrimmedQueryParam("pvalue") final String pvalue) throws Exception { + GroupList groupList = new GroupList(); + QueryExecuter executer = new QueryExecuter.Builder() + .addFilterId(new FilterSet() { + @Override + public Set filter(Set input) throws Exception { + return groupCriteriaQuery.queryAllVGroups(); + } + }) + .addFilterId(new FilterSet() { + @Override + public Set filter(Set input) throws Exception { + if (tag != null) { + input.retainAll(tagService.query(tag, "group")); + } + return input; + } + }) + .addFilterId(new FilterSet() { + @Override + public Set filter(Set input) throws Exception { + if (pname != null) { + if (pvalue != null) + input.retainAll(propertyService.query(pname, pvalue, "group")); + else + input.retainAll(propertyService.query(pname, "group")); + } + return input; + } + }) + .addFilterId(new FilterSet() { + @Override + public Set filter(Set input) throws Exception { + if (domain != null) { + Set vsIds = virtualServerCriteriaQuery.queryByDomain(domain); + input.retainAll(groupCriteriaQuery.queryByVsIds(vsIds.toArray(new Long[vsIds.size()]))); + } + return input; + } + }) .build(); for (Group group : groupRepository.list(executer.run())) { groupList.addGroup(getGroupByType(group, type)); @@ -172,6 +238,15 @@ public class GroupResource { return responseHandler.handle(g, hh.getMediaType()); } + @POST + @Path("/vgroup/new") + @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, "*/*"}) + @Authorize(name = "addGroup") + public Response addVGroup(@Context HttpHeaders hh, @Context HttpServletRequest request, String group) throws Exception { + Group g = groupRepository.addVGroup(parseGroup(hh.getMediaType(), group)); + return responseHandler.handle(g, hh.getMediaType()); + } + @POST @Path("/group/update") @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, "*/*"}) @@ -188,6 +263,22 @@ public class GroupResource { return responseHandler.handle(g, hh.getMediaType()); } + @POST + @Path("/vgroup/update") + @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, "*/*"}) + @Authorize(name = "updateGroup") + public Response updateVGroup(@Context HttpHeaders hh, @Context HttpServletRequest request, String group) throws Exception { + Group g = parseGroup(hh.getMediaType(), group); + DistLock lock = dbLockFactory.newLock(g.getName() + "_updateGroup"); + try { + lock.lock(); + g = groupRepository.updateVGroup(g); + } finally { + lock.unlock(); + } + return responseHandler.handle(g, hh.getMediaType()); + } + @GET @Path("/group/delete") @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) @@ -199,6 +290,17 @@ public class GroupResource { return Response.ok().build(); } + @GET + @Path("/vgroup/delete") + @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + @Authorize(name = "deleteGroup") + public Response deleteVGroup(@Context HttpHeaders hh, @Context HttpServletRequest request, @QueryParam("groupId") Long groupId) throws Exception { + if (groupId == null) + throw new Exception("Missing parameter."); + groupRepository.deleteVGroup(groupId); + return Response.ok().build(); + } + @GET @Path("/group/upgradeAll") @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) @@ -234,18 +336,23 @@ public class GroupResource { throw new Exception("Group cannot be parsed."); } } - g.setAppId(g.getAppId().trim()); - g.setName(g.getName().trim()); + g.setAppId(trimIfNotNull(g.getAppId())); + g.setName(trimIfNotNull(g.getName())); if (g.getHealthCheck() != null) - g.getHealthCheck().setUri(g.getHealthCheck().getUri().trim()); + g.getHealthCheck().setUri(trimIfNotNull(g.getHealthCheck().getUri())); for (GroupServer groupServer : g.getGroupServers()) { - groupServer.setIp(groupServer.getIp().trim()); - groupServer.setHostName(groupServer.getHostName().trim()); + groupServer.setIp(trimIfNotNull(groupServer.getIp())); + groupServer.setHostName(trimIfNotNull(groupServer.getHostName())); } - g.getLoadBalancingMethod().setValue(g.getLoadBalancingMethod().getValue()); + if (g.getLoadBalancingMethod() != null) + g.getLoadBalancingMethod().setValue(trimIfNotNull(g.getLoadBalancingMethod().getValue())); return g; } + private String trimIfNotNull(String value) { + return value != null ? value.trim() : value; + } + private Group getGroupByType(Group group, String type) { if ("INFO".equalsIgnoreCase(type)) { return new Group().setId(group.getId()) diff --git a/src/main/java/com/ctrip/zeus/service/build/conf/LocationConf.java b/src/main/java/com/ctrip/zeus/service/build/conf/LocationConf.java index 3f31bb8d..c6a5e435 100644 --- a/src/main/java/com/ctrip/zeus/service/build/conf/LocationConf.java +++ b/src/main/java/com/ctrip/zeus/service/build/conf/LocationConf.java @@ -1,5 +1,6 @@ package com.ctrip.zeus.service.build.conf; +import com.ctrip.zeus.exceptions.ValidationException; import com.ctrip.zeus.model.entity.Group; import com.ctrip.zeus.model.entity.GroupVirtualServer; import com.ctrip.zeus.model.entity.Slb; @@ -28,11 +29,23 @@ public class LocationConf { private static DynamicStringProperty errorPage_500 = DynamicPropertyFactory.getInstance().getStringProperty("errorPage.500.url", null);//"http://slberrorpages.ctripcorp.com/slberrorpages/500.htm"); private static DynamicBooleanProperty errorPageEnable = DynamicPropertyFactory.getInstance().getBooleanProperty("errorPage.enable", false);//"http://slberrorpages.ctripcorp.com/slberrorpages/500.htm"); private static DynamicBooleanProperty errorPageEnableAll = DynamicPropertyFactory.getInstance().getBooleanProperty("errorPage.enable-all", false);//"http://slberrorpages.ctripcorp.com/slberrorpages/500.htm"); + private static DynamicStringProperty upstreamKeepAlive = DynamicPropertyFactory.getInstance().getStringProperty("upstream.keep-alive", null);//"http://slberrorpages.ctripcorp.com/slberrorpages/500.htm"); public static String generate(Slb slb, VirtualServer vs, Group group, String upstreamName)throws Exception { StringBuilder b = new StringBuilder(1024); - + if (group.isVirtual()){ + b.append("location ").append(getPath(slb, vs, group)).append(" {\n"); + if (group.getGroupVirtualServers().size()==1) + { + b.append("rewrite ").append(group.getGroupVirtualServers().get(0).getRedirect()).append(" redirect;\n"); + }else { + throw new ValidationException("Virtual Group has Multiple Group VirtualServers Redirect"); + } + b.append("}\n"); + return b.toString(); + } b.append("location ").append(getPath(slb, vs, group)).append(" {\n"); + if (clientMaxSizeList.get() !=null) { String []sizeList = clientMaxSizeList.get().split(";"); @@ -53,6 +66,7 @@ public class LocationConf { b.append("proxy_set_header Host $host").append(";\n"); b.append("proxy_set_header X-Real-IP $remote_addr;\n"); + addKeepAliveSettings(b,group.getId()); addProxyReadTimeout(group.getId(),b); @@ -101,6 +115,23 @@ public class LocationConf { return b.toString(); } + private static void addKeepAliveSettings(StringBuilder b,Long groupId) { + String tmp = upstreamKeepAlive.get(); + if (tmp==null||tmp.trim().equals("")){ + return; + }else if (tmp.equals("All")){ + b.append("proxy_set_header Connection \"\";\n"); + }else { + String[] pairs = tmp.split(";"); + for (String pair : pairs){ + String[] t = pair.split("="); + if (t.length==2&&t[0].trim().equals(String.valueOf(groupId))){ + b.append("proxy_set_header Connection \"\";\n"); + } + } + } + } + private static void addProxyReadTimeout(Long gid , StringBuilder sb) { String defaultConfig = proxyTimeoutDefault.get(); String config = proxyTimeoutList.get(); diff --git a/src/main/java/com/ctrip/zeus/service/build/conf/NginxConf.java b/src/main/java/com/ctrip/zeus/service/build/conf/NginxConf.java index 505996c0..f392c139 100644 --- a/src/main/java/com/ctrip/zeus/service/build/conf/NginxConf.java +++ b/src/main/java/com/ctrip/zeus/service/build/conf/NginxConf.java @@ -24,7 +24,7 @@ public class NginxConf { "'\"$query_string\" $server_port $remote_user $remote_addr $http_x_forwarded_for '\n" + "'$server_protocol \"$http_user_agent\" \"$cookie_COOKIE\" \"$http_referer\" '\n" + "'$host $status $body_bytes_sent $request_time $upstream_response_time '\n" + - "'$upstream_addr $upstream_status';\n" + "'$upstream_addr $upstream_status $proxy_host';\n" ); diff --git a/src/main/java/com/ctrip/zeus/service/build/conf/UpstreamsConf.java b/src/main/java/com/ctrip/zeus/service/build/conf/UpstreamsConf.java index 6b50c841..7a3e1dc5 100644 --- a/src/main/java/com/ctrip/zeus/service/build/conf/UpstreamsConf.java +++ b/src/main/java/com/ctrip/zeus/service/build/conf/UpstreamsConf.java @@ -6,6 +6,9 @@ import com.ctrip.zeus.model.entity.Slb; import com.ctrip.zeus.model.entity.VirtualServer; import com.ctrip.zeus.util.AssertUtils; import com.ctrip.zeus.util.StringFormat; +import com.netflix.config.DynamicIntProperty; +import com.netflix.config.DynamicPropertyFactory; +import com.netflix.config.DynamicStringProperty; import java.util.ArrayList; import java.util.List; @@ -16,6 +19,9 @@ import java.util.Set; * @date: 3/10/2015. */ public class UpstreamsConf { + private static DynamicStringProperty upstreamKeepAlive = DynamicPropertyFactory.getInstance().getStringProperty("upstream.keep-alive", null);//"http://slberrorpages.ctripcorp.com/slberrorpages/500.htm"); + private static DynamicIntProperty upstreamKeepAliveDefault = DynamicPropertyFactory.getInstance().getIntProperty("upstream.keep-alive.default", 100);//"http://slberrorpages.ctripcorp.com/slberrorpages/500.htm"); + public static String generate(Slb slb, VirtualServer vs, List groups, Set allDownServers, Set allUpGroupServers)throws Exception { StringBuilder b = new StringBuilder(10240); @@ -34,6 +40,9 @@ public class UpstreamsConf { } public static String buildUpstreamConf(Slb slb, VirtualServer vs, Group group, String upstreamName, Set allDownServers, Set allUpGroupServers) throws Exception { + if (group.isVirtual()){ + return ""; + } StringBuilder b = new StringBuilder(1024); String body = buildUpstreamConfBody(slb,vs,group,allDownServers,allUpGroupServers); if (null == body){ @@ -79,11 +88,28 @@ public class UpstreamsConf { .append(isDown?" down":"") .append(";\n"); } - + addKeepAliveSetting(b,group.getId()); //HealthCheck b.append(HealthCheckConf.generate(slb, vs, group)); return b.toString(); } + private static void addKeepAliveSetting(StringBuilder b, Long gid) { + String tmp = upstreamKeepAlive.get(); + if (tmp==null||tmp.trim().equals("")){ + return; + }else if (tmp.equals("All")){ + b.append("keepalive ").append(upstreamKeepAliveDefault.get()).append(";\n"); + }else { + String[] pairs = tmp.split(";"); + for (String pair : pairs){ + String[] t = pair.split("="); + if (t.length==2&&t[0].trim().equals(String.valueOf(gid))){ + b.append("keepalive ").append(t[1]).append(";\n"); + } + } + } + } + } diff --git a/src/main/java/com/ctrip/zeus/service/model/AutoFiller.java b/src/main/java/com/ctrip/zeus/service/model/AutoFiller.java index 3117bf1a..7ad38bc8 100644 --- a/src/main/java/com/ctrip/zeus/service/model/AutoFiller.java +++ b/src/main/java/com/ctrip/zeus/service/model/AutoFiller.java @@ -12,6 +12,7 @@ import javax.annotation.Resource; public class AutoFiller { @Resource private VirtualServerRepository virtualServerRepository; + private static final String RegexRootPath = " /"; public void autofill(Group group) throws Exception { for (GroupVirtualServer gvs : group.getGroupVirtualServers()) { @@ -23,7 +24,10 @@ public class AutoFiller { tvs.getDomains().add(domain); } if (gvs.getPriority() == null) { - gvs.setPriority(gvs.getRewrite() == null ? 1000 : -1000); + if (gvs.getPath().endsWith(RegexRootPath)) + gvs.setPriority(Integer.MIN_VALUE); + else + gvs.setPriority(gvs.getRewrite() == null ? 1000 : -1000); } } HealthCheck hc = group.getHealthCheck(); @@ -44,6 +48,27 @@ public class AutoFiller { } } + public void autofillVGroup(Group group) throws Exception { + for (GroupVirtualServer gvs : group.getGroupVirtualServers()) { + VirtualServer tvs = gvs.getVirtualServer(); + VirtualServer vs = virtualServerRepository.getById(gvs.getVirtualServer().getId()); + tvs.setName(vs.getName()).setSlbId(vs.getSlbId()).setPort(vs.getPort()).setSsl(vs.getSsl()); + tvs.getDomains().clear(); + for (Domain domain : vs.getDomains()) { + tvs.getDomains().add(domain); + } + if (gvs.getPriority() == null) { + if (gvs.getPath().endsWith(RegexRootPath)) + gvs.setPriority(Integer.MIN_VALUE); + else + gvs.setPriority(gvs.getRewrite() == null ? 1000 : -1000); + } + } + group.setHealthCheck(null); + group.setLoadBalancingMethod(null); + group.getGroupServers().clear(); + } + public void autofill(Slb slb) throws Exception { slb.setNginxBin("/opt/app/nginx/sbin").setNginxConf("/opt/app/nginx/conf").setNginxWorkerProcesses(9) .setStatus(slb.getStatus() == null ? "Default" : slb.getStatus()); diff --git a/src/main/java/com/ctrip/zeus/service/model/GroupRepository.java b/src/main/java/com/ctrip/zeus/service/model/GroupRepository.java index 97f8b349..4204d1e8 100644 --- a/src/main/java/com/ctrip/zeus/service/model/GroupRepository.java +++ b/src/main/java/com/ctrip/zeus/service/model/GroupRepository.java @@ -17,10 +17,16 @@ public interface GroupRepository extends Repository { Group add(Group group) throws Exception; + Group addVGroup(Group group) throws Exception; + Group update(Group group) throws Exception; + Group updateVGroup(Group group) throws Exception; + int delete(Long groupId) throws Exception; + int deleteVGroup(Long groupId) throws Exception; + @Deprecated List listGroupsByGroupServer(String groupServerIp) throws Exception; diff --git a/src/main/java/com/ctrip/zeus/service/model/handler/GroupSync.java b/src/main/java/com/ctrip/zeus/service/model/handler/GroupSync.java index 74497a4e..34d31d0b 100644 --- a/src/main/java/com/ctrip/zeus/service/model/handler/GroupSync.java +++ b/src/main/java/com/ctrip/zeus/service/model/handler/GroupSync.java @@ -12,6 +12,8 @@ public interface GroupSync { void add(Group group) throws Exception; + void add(Group group, boolean isVirtual) throws Exception; + void update(Group group) throws Exception; void updateVersion(Long[] groupIds) throws Exception; diff --git a/src/main/java/com/ctrip/zeus/service/model/handler/VGroupValidator.java b/src/main/java/com/ctrip/zeus/service/model/handler/VGroupValidator.java new file mode 100644 index 00000000..a4f3cff1 --- /dev/null +++ b/src/main/java/com/ctrip/zeus/service/model/handler/VGroupValidator.java @@ -0,0 +1,9 @@ +package com.ctrip.zeus.service.model.handler; + +import com.ctrip.zeus.model.entity.Group; + +/** + * Created by zhoumy on 2015/11/23. + */ +public interface VGroupValidator extends ModelValidator { +} diff --git a/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultGroupValidator.java b/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultGroupValidator.java index cfc8bc09..039e3d9c 100644 --- a/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultGroupValidator.java +++ b/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultGroupValidator.java @@ -33,11 +33,14 @@ public class DefaultGroupValidator implements GroupValidator { @Resource private RGroupVsDao rGroupVsDao; @Resource + private RGroupVgDao rGroupVgDao; + @Resource private GroupDao groupDao; @Override public boolean exists(Long targetId) throws Exception { - return groupDao.findById(targetId, GroupEntity.READSET_FULL) != null; + return groupDao.findById(targetId, GroupEntity.READSET_FULL) != null + && rGroupVgDao.findByGroup(targetId, RGroupVgEntity.READSET_FULL) == null; } @Override diff --git a/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultVGroupValidator.java b/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultVGroupValidator.java new file mode 100644 index 00000000..44eb1522 --- /dev/null +++ b/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultVGroupValidator.java @@ -0,0 +1,55 @@ +package com.ctrip.zeus.service.model.handler.impl; + +import com.ctrip.zeus.dal.core.GroupDao; +import com.ctrip.zeus.dal.core.GroupEntity; +import com.ctrip.zeus.dal.core.RGroupVgDao; +import com.ctrip.zeus.dal.core.RGroupVgEntity; +import com.ctrip.zeus.exceptions.ValidationException; +import com.ctrip.zeus.model.entity.Group; +import com.ctrip.zeus.model.entity.GroupVirtualServer; +import com.ctrip.zeus.service.model.handler.GroupValidator; +import com.ctrip.zeus.service.model.handler.VGroupValidator; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; + +/** + * Created by zhoumy on 2015/11/23. + */ +@Component("vGroupValidator") +public class DefaultVGroupValidator implements VGroupValidator { + @Resource + private RGroupVgDao rGroupVgDao; + @Resource + private GroupDao groupDao; + @Resource + private GroupValidator groupModelValidator; + + @Override + public boolean exists(Long targetId) throws Exception { + return groupDao.findById(targetId, GroupEntity.READSET_FULL) != null + && rGroupVgDao.findByGroup(targetId, RGroupVgEntity.READSET_FULL) != null; + } + + @Override + public void validate(Group target) throws Exception { + if (target.getName() == null || target.getName().isEmpty()) { + throw new ValidationException("Group name is required."); + } + groupModelValidator.validateGroupVirtualServers(target.getId(), target.getGroupVirtualServers()); + for (GroupVirtualServer groupVirtualServer : target.getGroupVirtualServers()) { + if (groupVirtualServer.getRedirect() == null) + throw new ValidationException("Redirect value is required."); + } + } + + @Override + public void checkVersion(Group target) throws Exception { + groupModelValidator.checkVersion(target); + } + + @Override + public void removable(Long targetId) throws Exception { + groupModelValidator.removable(targetId); + } +} diff --git a/src/main/java/com/ctrip/zeus/service/model/handler/impl/GroupEntityManager.java b/src/main/java/com/ctrip/zeus/service/model/handler/impl/GroupEntityManager.java index 0c8792d7..3239de1b 100644 --- a/src/main/java/com/ctrip/zeus/service/model/handler/impl/GroupEntityManager.java +++ b/src/main/java/com/ctrip/zeus/service/model/handler/impl/GroupEntityManager.java @@ -29,11 +29,16 @@ public class GroupEntityManager implements GroupSync { private RGroupVsDao rGroupVsDao; @Resource private RGroupGsDao rGroupGsDao; + @Resource + private RGroupVgDao rGroupVgDao; @Override public void add(Group group) throws Exception { group.setVersion(1); GroupDo d = C.toGroupDo(0L, group); + if (d.getAppId() == null) + // if app id is null, it must be virtual group + d.setAppId("VirtualGroup"); groupDao.insert(d); group.setId(d.getId()); archiveGroupDao.insert(new ArchiveGroupDo().setGroupId(group.getId()).setVersion(group.getVersion()).setContent(ContentWriters.writeGroupContent(group))); @@ -41,6 +46,13 @@ public class GroupEntityManager implements GroupSync { relSyncGs(group, true); } + @Override + public void add(Group group, boolean isVirtual) throws Exception { + add(group); + if (isVirtual) + relSyncVg(group); + } + @Override public void update(Group group) throws Exception { GroupDo check = groupDao.findById(group.getId(), GroupEntity.READSET_FULL); @@ -48,7 +60,7 @@ public class GroupEntityManager implements GroupSync { throw new ValidationException("Newer Group version is detected."); group.setVersion(group.getVersion() + 1); - GroupDo d = C.toGroupDo(group.getId(), group); + GroupDo d = C.toGroupDo(group.getId(), group).setAppId("VirtualGroup"); groupDao.updateById(d, GroupEntity.UPDATESET_FULL); archiveGroupDao.insert(new ArchiveGroupDo().setGroupId(group.getId()).setVersion(group.getVersion()).setContent(ContentWriters.writeGroupContent(group))); relSyncVs(group, false); @@ -64,6 +76,7 @@ public class GroupEntityManager implements GroupSync { public int delete(Long groupId) throws Exception { rGroupVsDao.deleteAllByGroup(new RelGroupVsDo().setGroupId(groupId)); rGroupGsDao.deleteAllByGroup(new RelGroupGsDo().setGroupId(groupId)); + rGroupVgDao.deleteByPK(new RelGroupVgDo().setGroupId(groupId)); int count = groupDao.deleteById(new GroupDo().setId(groupId)); archiveGroupDao.deleteByGroup(new ArchiveGroupDo().setGroupId(groupId)); return count; @@ -129,6 +142,10 @@ public class GroupEntityManager implements GroupSync { rGroupGsDao.insert(dos); } + private void relSyncVg(Group group) throws DalException { + rGroupVgDao.insert(new RelGroupVgDo().setGroupId(group.getId())); + } + private void relSyncVs(Group group, boolean isnew) throws DalException { if (isnew) { for (GroupVirtualServer groupVirtualServer : group.getGroupVirtualServers()) { diff --git a/src/main/java/com/ctrip/zeus/service/model/handler/impl/GroupSyncImpl.java b/src/main/java/com/ctrip/zeus/service/model/handler/impl/GroupSyncImpl.java index 524bf562..8bec5ff7 100644 --- a/src/main/java/com/ctrip/zeus/service/model/handler/impl/GroupSyncImpl.java +++ b/src/main/java/com/ctrip/zeus/service/model/handler/impl/GroupSyncImpl.java @@ -38,6 +38,11 @@ public class GroupSyncImpl implements GroupSync { cascadeSync(group); } + @Override + public void add(Group group, boolean isVirtual) throws Exception { + add(group); + } + @Override public void update(Group group) throws Exception { GroupDo check = groupDao.findById(group.getId(), GroupEntity.READSET_FULL); diff --git a/src/main/java/com/ctrip/zeus/service/model/impl/GroupRepositoryImpl.java b/src/main/java/com/ctrip/zeus/service/model/impl/GroupRepositoryImpl.java index 20af1c2c..bb32a0c3 100644 --- a/src/main/java/com/ctrip/zeus/service/model/impl/GroupRepositoryImpl.java +++ b/src/main/java/com/ctrip/zeus/service/model/impl/GroupRepositoryImpl.java @@ -5,6 +5,7 @@ import com.ctrip.zeus.model.entity.*; import com.ctrip.zeus.service.model.*; import com.ctrip.zeus.service.model.handler.GroupSync; import com.ctrip.zeus.service.model.handler.GroupValidator; +import com.ctrip.zeus.service.model.handler.VGroupValidator; import com.ctrip.zeus.service.query.GroupCriteriaQuery; import org.springframework.stereotype.Repository; @@ -29,12 +30,8 @@ public class GroupRepositoryImpl implements GroupRepository { private VirtualServerRepository virtualServerRepository; @Resource private GroupValidator groupModelValidator; - - @Override - public List list(Long slbId) throws Exception { - Set groupIds = groupCriteriaQuery.queryBySlbId(slbId); - return list(groupIds.toArray(new Long[groupIds.size()])); - } + @Resource + private VGroupValidator vGroupValidator; @Override public List list(Long[] ids) throws Exception { @@ -44,52 +41,81 @@ public class GroupRepositoryImpl implements GroupRepository { groupVirtualServer.setVirtualServer( virtualServerRepository.getById(groupVirtualServer.getVirtualServer().getId())); } + autoFiller.autofill(group); + hideVirtualValue(group); } return result; } @Override public Group getById(Long id) throws Exception { - if (groupModelValidator.exists(id)) { + if (groupModelValidator.exists(id) || vGroupValidator.exists(id)) { Group result = archiveService.getLatestGroup(id); for (GroupVirtualServer groupVirtualServer : result.getGroupVirtualServers()) { groupVirtualServer.setVirtualServer( virtualServerRepository.getById(groupVirtualServer.getVirtualServer().getId())); } + autoFiller.autofill(result); + hideVirtualValue(result); return result; } return null; } - @Override - public Group get(String groupName) throws Exception { - return getById(groupCriteriaQuery.queryByName(groupName)); - } - @Override public Group add(Group group) throws Exception { groupModelValidator.validate(group); autoFiller.autofill(group); - groupEntityManager.add(group); + hideVirtualValue(group); + groupEntityManager.add(group, false); + return group; + } + + @Override + public Group addVGroup(Group group) throws Exception { + vGroupValidator.validate(group); + autoFiller.autofillVGroup(group); + group.setVirtual(true); + groupEntityManager.add(group, true); + hideVirtualValue(group); return group; } @Override public Group update(Group group) throws Exception { if (!groupModelValidator.exists(group.getId())) - throw new ValidationException("Group with id " + group.getId() + "does not exist."); + throw new ValidationException("Group with id " + group.getId() + " does not exist."); groupModelValidator.validate(group); autoFiller.autofill(group); + hideVirtualValue(group); groupEntityManager.update(group); return group; } + @Override + public Group updateVGroup(Group group) throws Exception { + if (!vGroupValidator.exists(group.getId())) + throw new ValidationException("Group with id " + group.getId() + " does not exist."); + vGroupValidator.validate(group); + autoFiller.autofillVGroup(group); + group.setVirtual(true); + groupEntityManager.update(group); + hideVirtualValue(group); + return group; + } + @Override public int delete(Long groupId) throws Exception { groupModelValidator.removable(groupId); return groupEntityManager.delete(groupId); } + @Override + public int deleteVGroup(Long groupId) throws Exception { + vGroupValidator.removable(groupId); + return delete(groupId); + } + @Override public List listGroupsByGroupServer(String groupServerIp) throws Exception { Set groupIds = groupCriteriaQuery.queryByGroupServerIp(groupServerIp); @@ -109,4 +135,19 @@ public class GroupRepositoryImpl implements GroupRepository { Group group = getById(groupId); groupEntityManager.port(group); } + + @Override + public Group get(String groupName) throws Exception { + return getById(groupCriteriaQuery.queryByName(groupName)); + } + + @Override + public List list(Long slbId) throws Exception { + Set groupIds = groupCriteriaQuery.queryBySlbId(slbId); + return list(groupIds.toArray(new Long[groupIds.size()])); + } + + private void hideVirtualValue(Group group) { + group.setVirtual(null); + } } diff --git a/src/main/java/com/ctrip/zeus/service/query/GroupCriteriaQuery.java b/src/main/java/com/ctrip/zeus/service/query/GroupCriteriaQuery.java index e5f73ef3..9dd8dac3 100644 --- a/src/main/java/com/ctrip/zeus/service/query/GroupCriteriaQuery.java +++ b/src/main/java/com/ctrip/zeus/service/query/GroupCriteriaQuery.java @@ -20,4 +20,6 @@ public interface GroupCriteriaQuery { Set queryByVsIds(Long[] vsIds) throws Exception; Set queryByGroupServerIp(String ip) throws Exception; + + Set queryAllVGroups() throws Exception; } diff --git a/src/main/java/com/ctrip/zeus/service/query/impl/DefaultGroupCriteriaQuery.java b/src/main/java/com/ctrip/zeus/service/query/impl/DefaultGroupCriteriaQuery.java index 6d0f635a..6237fe39 100644 --- a/src/main/java/com/ctrip/zeus/service/query/impl/DefaultGroupCriteriaQuery.java +++ b/src/main/java/com/ctrip/zeus/service/query/impl/DefaultGroupCriteriaQuery.java @@ -23,6 +23,8 @@ public class DefaultGroupCriteriaQuery implements GroupCriteriaQuery { private RGroupGsDao rGroupGsDao; @Resource private RVsSlbDao rVsSlbDao; + @Resource + private RGroupVgDao rGroupVgDao; @Override public Long queryByName(String name) throws Exception { @@ -83,4 +85,13 @@ public class DefaultGroupCriteriaQuery implements GroupCriteriaQuery { } return groupIds; } + + @Override + public Set queryAllVGroups() throws Exception { + Set groupIds = new HashSet<>(); + for (RelGroupVgDo relGroupVgDo : rGroupVgDao.findAll(RGroupVgEntity.READSET_FULL)) { + groupIds.add(relGroupVgDo.getGroupId()); + } + return groupIds; + } } diff --git a/src/main/resources/META-INF/dal/jdbc/rel-group.xml b/src/main/resources/META-INF/dal/jdbc/rel-group.xml index 49b74f45..4682f9b9 100644 --- a/src/main/resources/META-INF/dal/jdbc/rel-group.xml +++ b/src/main/resources/META-INF/dal/jdbc/rel-group.xml @@ -1,6 +1,28 @@ - + + + + + + FROM + ]]> + + + + + + + FROM
+ WHERE = ${group-id} + ]]> + + + + + diff --git a/src/main/resources/META-INF/dal/model/model-codegen.xml b/src/main/resources/META-INF/dal/model/model-codegen.xml index 005d9ac5..50eb3d09 100644 --- a/src/main/resources/META-INF/dal/model/model-codegen.xml +++ b/src/main/resources/META-INF/dal/model/model-codegen.xml @@ -63,6 +63,7 @@ + @@ -83,6 +84,7 @@ + diff --git a/src/main/resources/META-INF/wizard/jdbc/wizard.xml b/src/main/resources/META-INF/wizard/jdbc/wizard.xml index cde35083..7d9b74c6 100644 --- a/src/main/resources/META-INF/wizard/jdbc/wizard.xml +++ b/src/main/resources/META-INF/wizard/jdbc/wizard.xml @@ -56,6 +56,7 @@
+
diff --git a/src/main/resources/sql/create-tables.sql b/src/main/resources/sql/create-tables.sql index 7eb7de3a..88abf1ed 100644 --- a/src/main/resources/sql/create-tables.sql +++ b/src/main/resources/sql/create-tables.sql @@ -558,6 +558,20 @@ CREATE TABLE IF NOT EXISTS `r_group_gs` ( -- Data exporting was unselected. +-- Dumping structure for table r_group_vg +DROP TABLE IF EXISTS `r_group_vg`; +CREATE TABLE IF NOT EXISTS `r_group_vg` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'primary key', + `group_id` bigint(20) NOT NULL DEFAULT '0' COMMENT 'group id', + `DataChange_LastTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'last time modified', + PRIMARY KEY (`id`), + UNIQUE KEY `group_id` (`group_id`), + KEY `DataChange_LastTime` (`DataChange_LastTime`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +-- Data exporting was unselected. + + -- Dumping structure for table r_group_vs DROP TABLE IF EXISTS `r_group_vs`; CREATE TABLE IF NOT EXISTS `r_group_vs` ( diff --git a/src/test/resources/com/ctrip/zeus/model/model.xml b/src/test/resources/com/ctrip/zeus/model/model.xml index ccd3db6f..c42656a2 100644 --- a/src/test/resources/com/ctrip/zeus/model/model.xml +++ b/src/test/resources/com/ctrip/zeus/model/model.xml @@ -41,6 +41,7 @@ /hotel ^/regex + /fullurl @@ -54,7 +55,7 @@ - +