Commit 85f2ff01 authored by Jason Song's avatar Jason Song

Merge pull request #59 from yiming187/entity

Entity and Biz Refactor
parents 60f5869a 98b4c848
package com.ctrip.apollo.adminservice.controller; package com.ctrip.apollo.adminservice.controller;
import com.ctrip.apollo.biz.service.AdminConfigService; import java.util.List;
import com.ctrip.apollo.biz.service.AdminReleaseService;
import com.ctrip.apollo.core.dto.ConfigItemDTO;
import com.ctrip.apollo.core.dto.ReleaseSnapshotDTO;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
...@@ -11,27 +8,26 @@ import org.springframework.web.bind.annotation.RequestMapping; ...@@ -11,27 +8,26 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.List; import com.ctrip.apollo.biz.entity.App;
import com.ctrip.apollo.biz.service.AppService;
import com.ctrip.apollo.biz.utils.BeanUtils;
import com.ctrip.apollo.core.dto.AppDTO;
@RestController @RestController
@RequestMapping("/configs") public class AppController {
public class ConfigController {
@Autowired @Autowired
private AdminConfigService adminConfigService; private AppService appService;
@Autowired
private AdminReleaseService adminReleaseService; @RequestMapping("/apps/{appId}")
public AppDTO findByAppId(@PathVariable("appId") String appId) {
@RequestMapping("/release/{releaseId}") App app = appService.findByAppId(appId);
public List<ReleaseSnapshotDTO> getRelaseSnapshot(@PathVariable long releaseId) { return BeanUtils.transfrom(AppDTO.class, app);
return adminReleaseService.findReleaseSnapshotByReleaseId(releaseId);
} }
@RequestMapping("/latest") @RequestMapping("/apps")
public List<ConfigItemDTO> findConfigItemsByClusters( public List<AppDTO> findByName(@RequestParam("name") String name) {
@RequestParam(value = "clusterIds") List<Long> clusterIds) { List<App> app = appService.findByName(name);
return adminConfigService.findConfigItemsByClusters(clusterIds); return BeanUtils.batchTransform(AppDTO.class, app);
} }
} }
package com.ctrip.apollo.adminservice.controller; package com.ctrip.apollo.adminservice.controller;
import com.ctrip.apollo.biz.service.AdminConfigService; import java.util.List;
import com.ctrip.apollo.core.dto.ClusterDTO;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.List; import com.ctrip.apollo.biz.entity.Cluster;
import com.ctrip.apollo.biz.service.ViewService;
import com.ctrip.apollo.biz.utils.BeanUtils;
import com.ctrip.apollo.core.dto.ClusterDTO;
@RestController @RestController
@RequestMapping("/cluster")
public class ClusterController { public class ClusterController {
@Autowired @Autowired
private AdminConfigService adminConfigService; private ViewService viewService;
@RequestMapping("/app/{appId}") @RequestMapping("/apps/{appId}/clusters")
public List<ClusterDTO> findClustersByApp(@PathVariable String appId) { public List<ClusterDTO> findClusters(@PathVariable("appId") String appId) {
return adminConfigService.findClustersByApp(appId); List<Cluster> clusters = viewService.findClusters(appId);
return BeanUtils.batchTransform(ClusterDTO.class, clusters);
} }
} }
package com.ctrip.apollo.adminservice.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ctrip.apollo.biz.entity.Group;
import com.ctrip.apollo.biz.service.GroupService;
import com.ctrip.apollo.biz.service.ViewService;
import com.ctrip.apollo.biz.utils.BeanUtils;
import com.ctrip.apollo.core.dto.GroupDTO;
@RestController
public class GroupController {
@Autowired
private ViewService viewService;
@Autowired
private GroupService groupService;
@RequestMapping("/apps/{appId}/clusters/{clusterName}/groups")
public List<GroupDTO> findGroups(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName) {
List<Group> groups = viewService.findGroups(appId, clusterName);
return BeanUtils.batchTransform(GroupDTO.class, groups);
}
@RequestMapping("/groups/{groupId}")
public GroupDTO findOne(@PathVariable("groupId") Long groupId) {
Group group = groupService.findOne(groupId);
return BeanUtils.transfrom(GroupDTO.class, group);
}
}
package com.ctrip.apollo.adminservice.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ctrip.apollo.biz.entity.Item;
import com.ctrip.apollo.biz.service.ItemService;
import com.ctrip.apollo.biz.service.ViewService;
import com.ctrip.apollo.biz.utils.BeanUtils;
import com.ctrip.apollo.core.dto.ItemDTO;
@RestController
public class ItemController {
@Autowired
private ViewService viewService;
@Autowired
private ItemService itemService;
@RequestMapping("/apps/{appId}/clusters/{clusterName}/groups/{groupName}/items")
public List<ItemDTO> findItems(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName,
@PathVariable("groupName") String groupName) {
List<Item> items = viewService.findItems(appId, clusterName,groupName);
return BeanUtils.batchTransform(ItemDTO.class, items);
}
@RequestMapping("/items/{itemId}")
public ItemDTO findOne(@PathVariable("itemId") long itemId) {
Item item = itemService.findOne(itemId);
return BeanUtils.transfrom(ItemDTO.class, item);
}
}
package com.ctrip.apollo.adminservice.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ctrip.apollo.biz.entity.Release;
import com.ctrip.apollo.biz.service.ReleaseService;
import com.ctrip.apollo.biz.service.ViewService;
import com.ctrip.apollo.biz.utils.BeanUtils;
import com.ctrip.apollo.core.dto.ReleaseDTO;
@RestController
public class ReleaseController {
@Autowired
private ViewService viewSerivce;
@Autowired
private ReleaseService releaseService;
@RequestMapping("/release/{releaseId}")
public ReleaseDTO findOne(@PathVariable("releaseId") long releaseId) {
Release release = releaseService.findOne(releaseId);
return BeanUtils.transfrom(ReleaseDTO.class, release);
}
@RequestMapping("/apps/{appId}/clusters/{clusterId}/groups/{groupId}/releases")
public List<ReleaseDTO> findReleases(@PathVariable("groupId") Long groupId){
List<Release> releases = viewSerivce.findReleases(groupId);
return BeanUtils.batchTransform(ReleaseDTO.class, releases);
}
}
package com.ctrip.apollo.adminservice.controller;
import com.ctrip.apollo.biz.service.AdminReleaseService;
import com.ctrip.apollo.core.dto.VersionDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/version")
public class VersionController {
@Autowired
private AdminReleaseService adminReleaseService;
@RequestMapping("/app/{appId}")
public List<VersionDTO> versions(@PathVariable String appId) {
return adminReleaseService.findVersionsByApp(appId);
}
@RequestMapping("/{versionId}")
public VersionDTO version(@PathVariable long versionId) {
return adminReleaseService.loadVersionById(versionId);
}
}
package com.ctrip.apollo.biz.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import org.hibernate.annotations.SQLDelete;
@Entity
@SQLDelete(sql = "Update App set isDeleted = 1 where id = ?")
public class App extends BaseEntity {
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String appId;
@Column(nullable = false)
private String ownerName;
@Column(nullable = false)
private String ownerEmail;
public String getAppId() {
return appId;
}
public String getName() {
return name;
}
public String getOwnerEmail() {
return ownerEmail;
}
public String getOwnerName() {
return ownerName;
}
public void setAppId(String appId) {
this.appId = appId;
}
public void setName(String name) {
this.name = name;
}
public void setOwnerEmail(String ownerEmail) {
this.ownerEmail = ownerEmail;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
}
package com.ctrip.apollo.biz.entity; package com.ctrip.apollo.biz.entity;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import java.util.Date; import java.util.Date;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
@Entity import org.hibernate.annotations.Where;
@Where(clause = "isDeleted = 0") @Where(clause = "isDeleted = 0")
@SQLDelete(sql = "Update ConfigItem set isDeleted = 1 where id = ?") public abstract class BaseEntity {
public class ConfigItem {
@Id @Id
@GeneratedValue @GeneratedValue
private long id; private long id;
@Column(nullable = false) private boolean isDeleted;
private long clusterId;
@Column(nullable = false)
private String clusterName;
@Column(nullable = false)
private String appId;
@Column(nullable = false)
private String key;
@Column
private String value;
@Column
private String comment;
@Column(name = "DataChange_CreatedBy") @Column(name = "DataChange_CreatedBy")
private String dataChangeCreatedBy; private String dataChangeCreatedBy;
...@@ -49,103 +29,51 @@ public class ConfigItem { ...@@ -49,103 +29,51 @@ public class ConfigItem {
@Column(name = "DataChange_LastTime") @Column(name = "DataChange_LastTime")
private Date dataChangeLastModifiedTime; private Date dataChangeLastModifiedTime;
@Column public String getDataChangeCreatedBy() {
private boolean IsDeleted; return dataChangeCreatedBy;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getClusterId() {
return clusterId;
}
public void setClusterId(long clusterId) {
this.clusterId = clusterId;
}
public String getClusterName() {
return clusterName;
}
public void setClusterName(String clusterName) {
this.clusterName = clusterName;
}
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
} }
public String getValue() { public Date getDataChangeCreatedTime() {
return value; return dataChangeCreatedTime;
} }
public void setValue(String value) { public String getDataChangeLastModifiedBy() {
this.value = value; return dataChangeLastModifiedBy;
} }
public String getComment() { public Date getDataChangeLastModifiedTime() {
return comment; return dataChangeLastModifiedTime;
} }
public void setComment(String comment) { public long getId() {
this.comment = comment; return id;
} }
public String getDataChangeCreatedBy() { public boolean isDeleted() {
return dataChangeCreatedBy; return isDeleted;
} }
public void setDataChangeCreatedBy(String dataChangeCreatedBy) { public void setDataChangeCreatedBy(String dataChangeCreatedBy) {
this.dataChangeCreatedBy = dataChangeCreatedBy; this.dataChangeCreatedBy = dataChangeCreatedBy;
} }
public Date getDataChangeCreatedTime() {
return dataChangeCreatedTime;
}
public void setDataChangeCreatedTime(Date dataChangeCreatedTime) { public void setDataChangeCreatedTime(Date dataChangeCreatedTime) {
this.dataChangeCreatedTime = dataChangeCreatedTime; this.dataChangeCreatedTime = dataChangeCreatedTime;
} }
public String getDataChangeLastModifiedBy() {
return dataChangeLastModifiedBy;
}
public void setDataChangeLastModifiedBy(String dataChangeLastModifiedBy) { public void setDataChangeLastModifiedBy(String dataChangeLastModifiedBy) {
this.dataChangeLastModifiedBy = dataChangeLastModifiedBy; this.dataChangeLastModifiedBy = dataChangeLastModifiedBy;
} }
public boolean isDeleted() { public void setDataChangeLastModifiedTime(Date dataChangeLastModifiedTime) {
return IsDeleted; this.dataChangeLastModifiedTime = dataChangeLastModifiedTime;
}
public void setDeleted(boolean isDeleted) {
IsDeleted = isDeleted;
} }
public Date getDataChangeLastModifiedTime() { public void setDeleted(boolean deleted) {
return dataChangeLastModifiedTime; isDeleted = deleted;
} }
public void setDataChangeLastModifiedTime(Date dataChangeLastModifiedTime) { public void setId(long id) {
this.dataChangeLastModifiedTime = dataChangeLastModifiedTime; this.id = id;
} }
} }
package com.ctrip.apollo.biz.entity; package com.ctrip.apollo.biz.entity;
import com.ctrip.apollo.core.dto.ClusterDTO;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id; import org.hibernate.annotations.SQLDelete;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
@Entity @Entity
@Where(clause = "isDeleted = 0")
@SQLDelete(sql = "Update Cluster set isDeleted = 1 where id = ?") @SQLDelete(sql = "Update Cluster set isDeleted = 1 where id = ?")
public class Cluster { public class Cluster extends BaseEntity {
@Id
@GeneratedValue
private long id;
@Column(nullable = false) @Column(nullable = false)
private String name; private String name;
...@@ -27,48 +18,20 @@ public class Cluster { ...@@ -27,48 +18,20 @@ public class Cluster {
@Column(nullable = false) @Column(nullable = false)
private String appId; private String appId;
private boolean isDeleted; public String getAppId() {
return appId;
public Cluster() {
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
} }
public String getName() { public String getName() {
return name; return name;
} }
public void setName(String name) {
this.name = name;
}
public String getAppId() {
return appId;
}
public void setAppId(String appId) { public void setAppId(String appId) {
this.appId = appId; this.appId = appId;
} }
public boolean isDeleted() { public void setName(String name) {
return isDeleted; this.name = name;
}
public void setDeleted(boolean deleted) {
isDeleted = deleted;
} }
public ClusterDTO toDTO() {
ClusterDTO dto = new ClusterDTO();
dto.setAppId(appId);
dto.setId(id);
dto.setName(name);
return dto;
}
} }
package com.ctrip.apollo.biz.entity; package com.ctrip.apollo.biz.entity;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/** import org.hibernate.annotations.SQLDelete;
* @author Jason Song(song_s@ctrip.com)
*/
@Entity @Entity
@Where(clause = "isDeleted = 0") @SQLDelete(sql = "Update Group set isDeleted = 1 where id = ?")
@SQLDelete(sql = "Update Version set isDeleted = 1 where id = ?") public class Group extends BaseEntity {
public class Version {
@Id
@GeneratedValue
private long id;
@Column(nullable = false) @Column(nullable = false)
private String name; private String name;
...@@ -26,60 +16,52 @@ public class Version { ...@@ -26,60 +16,52 @@ public class Version {
private String appId; private String appId;
@Column(nullable = false) @Column(nullable = false)
private long releaseId; private long clusterId;
//parent version could be null
private Long parentVersion; @Column(nullable = false)
private boolean isDeleted; private String clusterName;
public Version() { @Column(nullable = false)
private long namespaceId;
public String getAppId() {
return appId;
} }
public long getId() { public long getClusterId() {
return id; return clusterId;
} }
public void setId(long id) { public String getClusterName() {
this.id = id; return clusterName;
} }
public String getName() { public String getName() {
return name; return name;
} }
public void setName(String name) { public long getNamespaceId() {
this.name = name; return namespaceId;
}
public String getAppId() {
return appId;
} }
public void setAppId(String appId) { public void setAppId(String appId) {
this.appId = appId; this.appId = appId;
} }
public long getReleaseId() { public void setClusterId(long clusterId) {
return releaseId; this.clusterId = clusterId;
} }
public void setReleaseId(long releaseId) { public void setClusterName(String clusterName) {
this.releaseId = releaseId; this.clusterName = clusterName;
} }
public boolean isDeleted() { public void setName(String name) {
return isDeleted; this.name = name;
}
public void setDeleted(boolean deleted) {
isDeleted = deleted;
}
public Long getParentVersion() {
return parentVersion;
} }
public void setParentVersion(Long parentVersion) { public void setNamespaceId(long namespaceId) {
this.parentVersion = parentVersion; this.namespaceId = namespaceId;
} }
} }
package com.ctrip.apollo.biz.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import org.hibernate.annotations.SQLDelete;
@Entity
@SQLDelete(sql = "Update Item set isDeleted = 1 where id = ?")
public class Item extends BaseEntity {
@Column(nullable = false)
private long groupId;
@Column(nullable = false)
private String key;
@Column
private String value;
@Column
private String comment;
public long getGroupId() {
return groupId;
}
public void setGroupId(long groupId) {
this.groupId = groupId;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
}
package com.ctrip.apollo.biz.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import org.hibernate.annotations.SQLDelete;
@Entity
@SQLDelete(sql = "Update Namespace set isDeleted = 1 where id = ?")
public class Namespace extends BaseEntity{
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String appId;
@Column
private String comment;
public String getAppId() {
return appId;
}
public String getComment() {
return comment;
}
public String getName() {
return name;
}
public void setAppId(String appId) {
this.appId = appId;
}
public void setComment(String comment) {
this.comment = comment;
}
public void setName(String name) {
this.name = name;
}
}
package com.ctrip.apollo.biz.entity; package com.ctrip.apollo.biz.entity;
import org.hibernate.annotations.SQLDelete; import javax.persistence.Column;
import org.hibernate.annotations.Where;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.Lob;
import javax.persistence.Id;
import org.hibernate.annotations.SQLDelete;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
@Entity @Entity
@Where(clause = "isDeleted = 0")
@SQLDelete(sql = "Update Release set isDeleted = 1 where id = ?") @SQLDelete(sql = "Update Release set isDeleted = 1 where id = ?")
public class Release { public class Release extends BaseEntity {
@Id
@GeneratedValue
private long id;
@Column(nullable = false)
private String name; private String name;
@Column(nullable = false)
private String appId; private String appId;
@Column(nullable = false)
private String clusterName;
@Column
private String groupName;
@Column(nullable = false)
@Lob
private String configurations;
@Column(nullable = false)
private String comment; private String comment;
private boolean isDeleted;
public Release() { public String getAppId() {
return appId;
} }
public long getId() { public String getClusterName() {
return id; return clusterName;
} }
public void setId(long id) { public String getComment() {
this.id = id; return comment;
} }
public String getName() { public String getConfigurations() {
return name; return configurations;
} }
public void setName(String name) { public String getGroupName() {
this.name = name; return groupName;
} }
public String getAppId() { public String getName() {
return appId; return name;
} }
public void setAppId(String appId) { public void setAppId(String appId) {
this.appId = appId; this.appId = appId;
} }
public String getComment() { public void setClusterName(String clusterName) {
return comment; this.clusterName = clusterName;
} }
public void setComment(String comment) { public void setComment(String comment) {
this.comment = comment; this.comment = comment;
} }
public boolean isDeleted() { public void setConfigurations(String configurations) {
return isDeleted; this.configurations = configurations;
} }
public void setDeleted(boolean deleted) { public void setGroupName(String groupName) {
isDeleted = deleted; this.groupName = groupName;
} }
public void setName(String name) {
this.name = name;
}
} }
package com.ctrip.apollo.biz.entity;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@Entity
@Where(clause = "isDeleted = 0")
@SQLDelete(sql = "Update ReleaseSnapShot set isDeleted = 1 where id = ?")
public class ReleaseSnapshot {
@Id
@GeneratedValue
private long id;
@Column(nullable = false)
private long releaseId;
@Column(nullable = false)
private String clusterName;
@Column(nullable = false)
private String configurations;
private boolean isDeleted;
public ReleaseSnapshot() {
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getReleaseId() {
return releaseId;
}
public void setReleaseId(long releaseId) {
this.releaseId = releaseId;
}
public String getClusterName() {
return clusterName;
}
public void setClusterName(String clusterName) {
this.clusterName = clusterName;
}
public String getConfigurations() {
return configurations;
}
public void setConfigurations(String configurations) {
this.configurations = configurations;
}
public boolean isDeleted() {
return isDeleted;
}
public void setDeleted(boolean deleted) {
isDeleted = deleted;
}
}
package com.ctrip.apollo.biz.repository;
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import com.ctrip.apollo.biz.entity.App;
public interface AppRepository extends PagingAndSortingRepository<App, Long> {
@Query("SELECT a from App a WHERE a.name LIKE %':name'%")
List<App> findByName(@Param("name") String name);
App findByAppId(String appId);
}
...@@ -11,4 +11,5 @@ public interface ClusterRepository extends PagingAndSortingRepository<Cluster, L ...@@ -11,4 +11,5 @@ public interface ClusterRepository extends PagingAndSortingRepository<Cluster, L
List<Cluster> findByAppId(String appId); List<Cluster> findByAppId(String appId);
Cluster findByAppIdAndName(String appId, String name);
} }
package com.ctrip.apollo.biz.repository; package com.ctrip.apollo.biz.repository;
import com.ctrip.apollo.biz.entity.Version; import java.util.List;
import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.PagingAndSortingRepository;
import java.util.List; import com.ctrip.apollo.biz.entity.Group;
/** public interface GroupRepository extends PagingAndSortingRepository<Group, Long> {
* @author Jason Song(song_s@ctrip.com)
*/
public interface VersionRepository extends PagingAndSortingRepository<Version, Long> {
Version findByAppIdAndName(String appId, String name);
Version findById(long id); List<Group> findByAppIdAndClusterName(String appId, String clusterName);
List<Version> findByAppId(String appId); Group findByAppIdAndClusterNameAndGroupName(String appId, String clusterName, String groupName);
} }
package com.ctrip.apollo.biz.repository; package com.ctrip.apollo.biz.repository;
import com.ctrip.apollo.biz.entity.ConfigItem; import com.ctrip.apollo.biz.entity.Item;
import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.PagingAndSortingRepository;
import java.util.List; import java.util.List;
public interface ConfigItemRepository extends PagingAndSortingRepository<ConfigItem, Long> { public interface ItemRepository extends PagingAndSortingRepository<Item, Long> {
List<ConfigItem> findByClusterIdIsIn(List<Long> clusterIds); List<Item> findByGroupIdIsIn(List<Long> groupIds);
List<Item> findByGroupId(Long groupId);
} }
package com.ctrip.apollo.biz.repository;
import org.springframework.data.repository.PagingAndSortingRepository;
import com.ctrip.apollo.biz.entity.Namespace;
public interface NamespaceRepository extends PagingAndSortingRepository<Namespace, Long>{
}
package com.ctrip.apollo.biz.repository;
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import com.ctrip.apollo.biz.entity.Release;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public interface ReleaseRepository extends PagingAndSortingRepository<Release, Long> {
@Query("SELECT r FROM Release r WHERE r.appId = :appId AND r.clusterName = :clusterName AND r.groupName = :groupName order by id desc litmit 1")
Release findLatest(@Param("appId") String appId, @Param("clusterName") String clusterName,
@Param("groupName") String groupName);
List<Release> findByGroupId(Long groupId);
}
package com.ctrip.apollo.biz.repository;
import com.ctrip.apollo.biz.entity.ReleaseSnapshot;
import org.springframework.data.repository.PagingAndSortingRepository;
import java.util.List;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public interface ReleaseSnapShotRepository
extends PagingAndSortingRepository<ReleaseSnapshot, Long> {
ReleaseSnapshot findByReleaseIdAndClusterName(long releaseId, String clusterName);
List<ReleaseSnapshot> findByReleaseId(long releaseId);
}
package com.ctrip.apollo.biz.service;
import com.google.common.base.Strings;
import com.ctrip.apollo.biz.entity.ReleaseSnapshot;
import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.repository.ReleaseSnapShotRepository;
import com.ctrip.apollo.biz.repository.VersionRepository;
import com.ctrip.apollo.biz.utils.ApolloBeanUtils;
import com.ctrip.apollo.core.dto.ReleaseSnapshotDTO;
import com.ctrip.apollo.core.dto.VersionDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.List;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@Service("adminReleaseService")
public class AdminReleaseService {
@Autowired
private ReleaseSnapShotRepository releaseSnapShotRepository;
@Autowired
private VersionRepository versionRepository;
public List<ReleaseSnapshotDTO> findReleaseSnapshotByReleaseId(long releaseId) {
if (releaseId <= 0) {
return Collections.EMPTY_LIST;
}
List<ReleaseSnapshot> releaseSnapShots = releaseSnapShotRepository.findByReleaseId(releaseId);
if (releaseSnapShots == null || releaseSnapShots.size() == 0) {
return Collections.EMPTY_LIST;
}
return ApolloBeanUtils.batchTransform(ReleaseSnapshotDTO.class, releaseSnapShots);
}
public List<VersionDTO> findVersionsByApp(String appId) {
if (Strings.isNullOrEmpty(appId)) {
return Collections.EMPTY_LIST;
}
List<Version> versions = versionRepository.findByAppId(appId);
if (versions == null || versions.size() == 0) {
return Collections.EMPTY_LIST;
}
return ApolloBeanUtils.batchTransform(VersionDTO.class, versions);
}
public VersionDTO loadVersionById(long versionId) {
if (versionId <= 0) {
return null;
}
Version version = versionRepository.findById(versionId);
if (version == null) {
return null;
}
VersionDTO dto = ApolloBeanUtils.transfrom(VersionDTO.class, version);
return dto;
}
}
package com.ctrip.apollo.biz.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import com.ctrip.apollo.biz.entity.App;
import com.ctrip.apollo.biz.repository.AppRepository;
@Service
public class AppService {
@Autowired
private AppRepository appRepository;
public App save(App entity){
return appRepository.save(entity);
}
public List<App> findAll(Pageable pageable){
Page<App> page = appRepository.findAll(pageable);
return page.getContent();
}
public List<App> findByName(String name){
return appRepository.findByName(name);
}
public App findByAppId(String appId){
return appRepository.findByAppId(appId);
}
}
package com.ctrip.apollo.biz.service; package com.ctrip.apollo.biz.service;
import com.google.common.collect.Maps; import java.io.IOException;
import java.util.Map;
import com.ctrip.apollo.biz.entity.ReleaseSnapshot;
import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.repository.ReleaseSnapShotRepository;
import com.ctrip.apollo.biz.repository.VersionRepository;
import com.ctrip.apollo.core.dto.ApolloConfig;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.IOException; import com.ctrip.apollo.biz.entity.Release;
import java.util.Map; import com.ctrip.apollo.biz.repository.ReleaseRepository;
import com.ctrip.apollo.core.dto.ApolloConfig;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
/** /**
* Config Service * Config Service
* *
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
@Service("configService") @Service
public class ConfigService { public class ConfigService {
@Autowired @Autowired
private VersionRepository versionRepository; private ReleaseRepository releaseRepository;
@Autowired
private ReleaseSnapShotRepository releaseSnapShotRepository;
@Autowired @Autowired
private ObjectMapper objectMapper; private ObjectMapper objectMapper;
private TypeReference<Map<String, Object>> configurationTypeReference =
new TypeReference<Map<String, Object>>() {
};
/**
* Load configuration from database
*/
public ApolloConfig loadConfig(String appId, String clusterName, String versionName) {
Version version = loadVersionByAppIdAndVersionName(appId, versionName);
if (version == null) {
return null;
}
return loadConfigByVersionAndClusterName(version, clusterName); private TypeReference<Map<String, Object>> configurationTypeReference =
} new TypeReference<Map<String, Object>>() {};
/** public Release findRelease(String appId, String clusterName, String groupName) {
* Load Version by appId and versionName from database Release release = releaseRepository.findLatest(appId, clusterName, groupName);
*/ return release;
public Version loadVersionByAppIdAndVersionName(String appId, String versionName) {
return versionRepository.findByAppIdAndName(appId, versionName);
} }
/** /**
* Load Config by version and clusterName from database * Load configuration from database
*/ */
public ApolloConfig loadConfigByVersionAndClusterName(Version version, String clusterName) { public ApolloConfig loadConfig(Release release, String groupName, String versionName) {
ReleaseSnapshot releaseSnapShot = if (release == null) {
releaseSnapShotRepository
.findByReleaseIdAndClusterName(version.getReleaseId(), clusterName);
if (releaseSnapShot == null) {
return null; return null;
} }
ApolloConfig config = new ApolloConfig(release.getAppId(), release.getClusterName(), groupName,
return assembleConfig(version, releaseSnapShot); versionName, release.getId());
} config.setConfigurations(transformConfigurationToMap(release.getConfigurations()));
private ApolloConfig assembleConfig(Version version, ReleaseSnapshot releaseSnapShot) {
ApolloConfig config =
new ApolloConfig(version.getAppId(), releaseSnapShot.getClusterName(), version.getName(),
version.getReleaseId());
config.setConfigurations(transformConfigurationToMap(releaseSnapShot.getConfigurations()));
return config; return config;
} }
......
package com.ctrip.apollo.biz.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ctrip.apollo.biz.entity.Group;
import com.ctrip.apollo.biz.repository.GroupRepository;
@Service
public class GroupService {
@Autowired
private GroupRepository groupRepository;
public Group findOne(Long groupId){
return groupRepository.findOne(groupId);
}
}
package com.ctrip.apollo.biz.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ctrip.apollo.biz.entity.Item;
import com.ctrip.apollo.biz.repository.ItemRepository;
@Service
public class ItemService {
@Autowired
private ItemRepository itemRepository;
public Item findOne(long itemId) {
Item item = itemRepository.findOne(itemId);
return item;
}
}
package com.ctrip.apollo.biz.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ctrip.apollo.biz.entity.Release;
import com.ctrip.apollo.biz.repository.ReleaseRepository;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@Service
public class ReleaseService {
@Autowired
private ReleaseRepository releaseRepository;
public Release findOne(long releaseId) {
Release release = releaseRepository.findOne(releaseId);
return release;
}
}
package com.ctrip.apollo.biz.service; package com.ctrip.apollo.biz.service;
import com.google.common.base.Strings; import java.util.Collections;
import java.util.List;
import com.ctrip.apollo.biz.entity.Cluster;
import com.ctrip.apollo.biz.entity.ConfigItem;
import com.ctrip.apollo.biz.repository.ClusterRepository;
import com.ctrip.apollo.biz.repository.ConfigItemRepository;
import com.ctrip.apollo.biz.utils.ApolloBeanUtils;
import com.ctrip.apollo.core.dto.ClusterDTO;
import com.ctrip.apollo.core.dto.ConfigItemDTO;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Collections; import com.ctrip.apollo.biz.entity.Cluster;
import java.util.List; import com.ctrip.apollo.biz.entity.Group;
import com.ctrip.apollo.biz.entity.Item;
import com.ctrip.apollo.biz.entity.Release;
import com.ctrip.apollo.biz.repository.ClusterRepository;
import com.ctrip.apollo.biz.repository.GroupRepository;
import com.ctrip.apollo.biz.repository.ItemRepository;
import com.ctrip.apollo.biz.repository.ReleaseRepository;
import com.google.common.base.Strings;
/** /**
* config service for admin * config service for admin
*/ */
@Service("adminConfigService") @Service
public class AdminConfigService { public class ViewService {
@Autowired @Autowired
private ClusterRepository clusterRepository; private ClusterRepository clusterRepository;
@Autowired @Autowired
private ConfigItemRepository configItemRepository; private GroupRepository groupRepository;
@Autowired
private ItemRepository itemRepository;
@Autowired
private ReleaseRepository releaseRepository;
public List<ClusterDTO> findClustersByApp(String appId) { public List<Cluster> findClusters(String appId) {
if (Strings.isNullOrEmpty(appId)) { if (Strings.isNullOrEmpty(appId)) {
return Collections.EMPTY_LIST; return Collections.EMPTY_LIST;
} }
List<Cluster> clusters = clusterRepository.findByAppId(appId); List<Cluster> clusters = clusterRepository.findByAppId(appId);
if (clusters == null || clusters.size() == 0) { if (clusters == null) {
return Collections.EMPTY_LIST; return Collections.EMPTY_LIST;
} }
return clusters;
}
return ApolloBeanUtils.batchTransform(ClusterDTO.class, clusters); public List<Group> findGroups(String appId, String clusterName) {
List<Group> groups = groupRepository.findByAppIdAndClusterName(appId, clusterName);
if (groups == null) {
return Collections.EMPTY_LIST;
}
return groups;
} }
public List<ConfigItemDTO> findConfigItemsByClusters(List<Long> clusterIds) { public List<Item> findItems(String appId, String clusterName, String groupName) {
if (clusterIds == null || clusterIds.size() == 0) { Group group =
groupRepository.findByAppIdAndClusterNameAndGroupName(appId, clusterName, groupName);
if (group != null) {
return findItems(group.getId());
} else {
return Collections.EMPTY_LIST; return Collections.EMPTY_LIST;
} }
List<ConfigItem> configItems = configItemRepository.findByClusterIdIsIn(clusterIds); }
if (configItems == null || configItems.size() == 0) {
public List<Item> findItems(Long groupId) {
List<Item> items = itemRepository.findByGroupId(groupId);
if (items == null) {
return Collections.EMPTY_LIST; return Collections.EMPTY_LIST;
} }
return items;
}
return ApolloBeanUtils.batchTransform(ConfigItemDTO.class, configItems); public List<Release> findReleases(Long groupId) {
List<Release> releases = releaseRepository.findByGroupId(groupId);
if (releases == null) {
return Collections.EMPTY_LIST;
}
return releases;
} }
} }
...@@ -14,7 +14,7 @@ import java.util.Map; ...@@ -14,7 +14,7 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
public class ApolloBeanUtils { public class BeanUtils {
/** /**
* <pre> * <pre>
......
package com.ctrip.apollo.biz.service; package com.ctrip.apollo.biz.service;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.ctrip.apollo.biz.entity.Release;
import com.ctrip.apollo.biz.entity.ReleaseSnapshot; import com.ctrip.apollo.biz.repository.ReleaseRepository;
import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.repository.ReleaseSnapShotRepository;
import com.ctrip.apollo.biz.repository.VersionRepository;
import com.ctrip.apollo.biz.service.ConfigService; import com.ctrip.apollo.biz.service.ConfigService;
import com.ctrip.apollo.core.dto.ApolloConfig;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
...@@ -22,7 +18,6 @@ import java.io.IOException; ...@@ -22,7 +18,6 @@ import java.io.IOException;
import java.util.Map; import java.util.Map;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.anyObject; import static org.mockito.Mockito.anyObject;
import static org.mockito.Mockito.eq; import static org.mockito.Mockito.eq;
...@@ -36,9 +31,7 @@ import static org.mockito.Mockito.when; ...@@ -36,9 +31,7 @@ import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class ConfigServiceTest { public class ConfigServiceTest {
@Mock @Mock
private VersionRepository versionRepository; private ReleaseRepository releaseRepository;
@Mock
private ReleaseSnapShotRepository releaseSnapShotRepository;
@Mock @Mock
private ObjectMapper objectMapper; private ObjectMapper objectMapper;
private ConfigService configService; private ConfigService configService;
...@@ -46,90 +39,91 @@ public class ConfigServiceTest { ...@@ -46,90 +39,91 @@ public class ConfigServiceTest {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
configService = new ConfigService(); configService = new ConfigService();
ReflectionTestUtils.setField(configService, "versionRepository", versionRepository);
ReflectionTestUtils ReflectionTestUtils
.setField(configService, "releaseSnapShotRepository", releaseSnapShotRepository); .setField(configService, "releaseRepository", releaseRepository);
ReflectionTestUtils.setField(configService, "objectMapper", objectMapper); ReflectionTestUtils.setField(configService, "objectMapper", objectMapper);
} }
@Test // @Test
public void testLoadConfig() throws Exception { // public void testLoadConfig() throws Exception {
String someAppId = "1"; // String someAppId = "1";
String someClusterName = "someClusterName"; // String someClusterName = "someClusterName";
String someVersionName = "someVersionName"; // String someGroupName = "someGroupName";
long someReleaseId = 1; // String someVersionName = "someVersionName";
String someValidConfiguration = "{\"apollo.bar\": \"foo\"}"; // long someReleaseId = 1;
// String someValidConfiguration = "{\"apollo.bar\": \"foo\"}";
Version someVersion = assembleVersion(someAppId, someVersionName, someReleaseId); //
ReleaseSnapshot // Version someVersion = assembleVersion(someAppId, someVersionName, someReleaseId);
someReleaseSnapShot = // Release
assembleReleaseSnapShot(someReleaseId, someClusterName, someValidConfiguration); // someRelease =
Map<String, Object> someMap = Maps.newHashMap(); // assembleRelease(someReleaseId, someClusterName, someGroupName, someValidConfiguration);
// Map<String, Object> someMap = Maps.newHashMap();
when(versionRepository.findByAppIdAndName(someAppId, someVersionName)).thenReturn(someVersion); //
when(releaseSnapShotRepository.findByReleaseIdAndClusterName(someReleaseId, someClusterName)) // when(versionRepository.findByAppIdAndName(someAppId, someVersionName)).thenReturn(someVersion);
.thenReturn(someReleaseSnapShot); // when(releaseRepository.findByReleaseIdAndClusterName(someReleaseId, someClusterName))
when(objectMapper.readValue(eq(someValidConfiguration), (TypeReference) anyObject())) // .thenReturn(someReleaseSnapShot);
.thenReturn(someMap); // when(objectMapper.readValue(eq(someValidConfiguration), (TypeReference) anyObject()))
// .thenReturn(someMap);
ApolloConfig result = configService.loadConfig(someAppId, someClusterName, someVersionName); //
// ApolloConfig result = configService.loadConfig(someAppId, someClusterName, someVersionName);
assertEquals(someAppId, result.getAppId()); //
assertEquals(someClusterName, result.getCluster()); // assertEquals(someAppId, result.getAppId());
assertEquals(someVersionName, result.getVersion()); // assertEquals(someClusterName, result.getCluster());
assertEquals(someReleaseId, result.getReleaseId()); // assertEquals(someVersionName, result.getVersion());
assertEquals(someMap, result.getConfigurations()); // assertEquals(someReleaseId, result.getReleaseId());
} // assertEquals(someMap, result.getConfigurations());
// }
@Test //
public void testLoadConfigWithVersionNotFound() throws Exception { // @Test
String someAppId = "1"; // public void testLoadConfigWithVersionNotFound() throws Exception {
String someClusterName = "someClusterName"; // String someAppId = "1";
String someVersionName = "someVersionName"; // String someClusterName = "someClusterName";
// String someVersionName = "someVersionName";
when(versionRepository.findByAppIdAndName(someAppId, someVersionName)).thenReturn(null); //
// when(versionRepository.findByAppIdAndName(someAppId, someVersionName)).thenReturn(null);
ApolloConfig result = configService.loadConfig(someAppId, someClusterName, someVersionName); //
// ApolloConfig result = configService.loadConfig(someAppId, someClusterName, someVersionName);
assertNull(result); //
verify(versionRepository, times(1)).findByAppIdAndName(someAppId, someVersionName); // assertNull(result);
} // verify(versionRepository, times(1)).findByAppIdAndName(someAppId, someVersionName);
// }
@Test //
public void testLoadConfigWithConfigNotFound() throws Exception { // @Test
String someAppId = "1"; // public void testLoadConfigWithConfigNotFound() throws Exception {
String someClusterName = "someClusterName"; // String someAppId = "1";
String someVersionName = "someVersionName"; // String someClusterName = "someClusterName";
long someReleaseId = 1; // String someVersionName = "someVersionName";
Version someVersion = assembleVersion(someAppId, someVersionName, someReleaseId); // long someReleaseId = 1;
// Version someVersion = assembleVersion(someAppId, someVersionName, someReleaseId);
when(versionRepository.findByAppIdAndName(someAppId, someVersionName)).thenReturn(someVersion); //
when(releaseSnapShotRepository.findByReleaseIdAndClusterName(someReleaseId, someClusterName)) // when(versionRepository.findByAppIdAndName(someAppId, someVersionName)).thenReturn(someVersion);
.thenReturn(null); // when(releaseRepository.findByReleaseIdAndClusterName(someReleaseId, someClusterName))
// .thenReturn(null);
ApolloConfig result = configService.loadConfig(someAppId, someClusterName, someVersionName); //
// ApolloConfig result = configService.loadConfig(someAppId, someClusterName, someVersionName);
assertNull(result); //
verify(versionRepository, times(1)).findByAppIdAndName(someAppId, someVersionName); // assertNull(result);
verify(releaseSnapShotRepository, times(1)) // verify(versionRepository, times(1)).findByAppIdAndName(someAppId, someVersionName);
.findByReleaseIdAndClusterName(someReleaseId, someClusterName); // verify(releaseRepository, times(1))
} // .findByReleaseIdAndClusterName(someReleaseId, someClusterName);
// }
private Version assembleVersion(String appId, String versionName, long releaseId) { //
Version version = new Version(); // private Version assembleVersion(String appId, String versionName, long releaseId) {
version.setAppId(appId); // Version version = new Version();
version.setName(versionName); // version.setAppId(appId);
version.setReleaseId(releaseId); // version.setName(versionName);
return version; // version.setReleaseId(releaseId);
} // return version;
// }
private ReleaseSnapshot assembleReleaseSnapShot(long releaseId, String clusterName,
private Release assembleRelease(long releaseId, String clusterName, String groupName,
String configurations) { String configurations) {
ReleaseSnapshot releaseSnapShot = new ReleaseSnapshot(); Release release = new Release();
releaseSnapShot.setReleaseId(releaseId); release.setId(releaseId);
releaseSnapShot.setClusterName(clusterName); release.setClusterName(clusterName);
releaseSnapShot.setConfigurations(configurations); release.setGroupName(groupName);
return releaseSnapShot; release.setConfigurations(configurations);
return release;
} }
......
...@@ -197,10 +197,11 @@ public class ConfigLoaderManagerTest { ...@@ -197,10 +197,11 @@ public class ConfigLoaderManagerTest {
ApolloConfig assembleApolloConfig(String appId, Map<String, Object> configurations) { ApolloConfig assembleApolloConfig(String appId, Map<String, Object> configurations) {
String someCluster = "someCluster"; String someCluster = "someCluster";
String someGroup = "someGroup";
String someVersion = "someVersion"; String someVersion = "someVersion";
long someReleaseId = 1; long someReleaseId = 1;
ApolloConfig config = new ApolloConfig(appId, someCluster, someVersion, someReleaseId); ApolloConfig config = new ApolloConfig(appId, someCluster, someGroup, someVersion, someReleaseId);
config.setConfigurations(configurations); config.setConfigurations(configurations);
......
package com.ctrip.apollo.configservice.controller; package com.ctrip.apollo.configservice.controller;
import com.ctrip.apollo.biz.entity.Version; import java.io.IOException;
import com.ctrip.apollo.biz.service.ConfigService;
import com.ctrip.apollo.core.dto.ApolloConfig; import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
...@@ -11,9 +11,9 @@ import org.springframework.web.bind.annotation.RequestMethod; ...@@ -11,9 +11,9 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.io.IOException; import com.ctrip.apollo.biz.entity.Release;
import com.ctrip.apollo.biz.service.ConfigService;
import javax.servlet.http.HttpServletResponse; import com.ctrip.apollo.core.dto.ApolloConfig;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
...@@ -24,32 +24,31 @@ public class ConfigController { ...@@ -24,32 +24,31 @@ public class ConfigController {
@Autowired @Autowired
private ConfigService configService; private ConfigService configService;
@RequestMapping(value = "/{appId}/{clusterName}/{versionName:.*}", method = RequestMethod.GET) @RequestMapping(value = "/{appId}/{clusterName}/{groupName}/{versionName:.*}", method = RequestMethod.GET)
public ApolloConfig queryConfig(@PathVariable String appId, public ApolloConfig queryConfig(@PathVariable String appId, @PathVariable String clusterName,
@PathVariable String clusterName, @PathVariable String groupName, @PathVariable String versionName,
@PathVariable String versionName, @RequestParam(value = "releaseId", defaultValue = "-1") long clientSideReleaseId,
@RequestParam(value = "releaseId", defaultValue = "-1") long clientSideReleaseId, HttpServletResponse response) throws IOException {
HttpServletResponse response) throws IOException { Release release = configService.findRelease(appId, clusterName, groupName);
Version version = configService.loadVersionByAppIdAndVersionName(appId, versionName); if (release == null) {
if (version == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND, response.sendError(HttpServletResponse.SC_NOT_FOUND,
String.format("Could not load version with appId: %s, versionName: %s", appId, String.format(
versionName)); "Could not load version with appId: %s, clusterName: %s, groupName: %s, versionName: %s",
appId, clusterName, groupName, versionName));
return null; return null;
} }
if (version.getReleaseId() == clientSideReleaseId) { if (release.getId() == clientSideReleaseId) {
//Client side configuration is the same with server side, return 304 // Client side configuration is the same with server side, return 304
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
return null; return null;
} }
ApolloConfig apolloConfig = ApolloConfig apolloConfig = configService.loadConfig(release, groupName, versionName);
configService.loadConfigByVersionAndClusterName(version, clusterName);
if (apolloConfig == null) { if (apolloConfig == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND, response.sendError(HttpServletResponse.SC_NOT_FOUND,
String.format("Could not load config with releaseId: %d, clusterName: %s", String.format("Could not load config with releaseId: %d, clusterName: %s",
version.getReleaseId(), clusterName)); release.getId(), clusterName));
return null; return null;
} }
......
package com.ctrip.apollo.configservice.controller; package com.ctrip.apollo.configservice.controller;
import com.ctrip.apollo.biz.entity.Version; import com.ctrip.apollo.biz.entity.Release;
import com.ctrip.apollo.biz.service.ConfigService; import com.ctrip.apollo.biz.service.ConfigService;
import com.ctrip.apollo.core.dto.ApolloConfig; import com.ctrip.apollo.core.dto.ApolloConfig;
...@@ -39,104 +39,94 @@ public class ConfigControllerTest { ...@@ -39,104 +39,94 @@ public class ConfigControllerTest {
ReflectionTestUtils.setField(configController, "configService", configService); ReflectionTestUtils.setField(configController, "configService", configService);
} }
@Test // @Test
public void testQueryConfig() throws Exception { // public void testQueryConfig() throws Exception {
ApolloConfig someApolloConfig = mock(ApolloConfig.class); // ApolloConfig someApolloConfig = mock(ApolloConfig.class);
String someAppId = "1"; // String someAppId = "1";
String someClusterName = "someClusterName"; // String someClusterName = "someClusterName";
String someVersionName = "someVersion"; // String someGroupName = "someGroupName";
long someClientSideReleaseId = 1; // long someClientSideReleaseId = 1;
long someServerSideNewReleaseId = 2; // long someServerSideNewReleaseId = 2;
HttpServletResponse someResponse = mock(HttpServletResponse.class); // HttpServletResponse someResponse = mock(HttpServletResponse.class);
Version someVersion = mock(Version.class); // Release someRelease = mock(Release.class);
//
when(configService.loadVersionByAppIdAndVersionName(someAppId, someVersionName)) // when(configService.findRelease(someAppId, someClusterName, someGroupName))
.thenReturn(someVersion); // .thenReturn(someRelease);
when(someVersion.getReleaseId()).thenReturn(someServerSideNewReleaseId); // when(someRelease.getId()).thenReturn(someServerSideNewReleaseId);
when(configService.loadConfigByVersionAndClusterName(someVersion, someClusterName)) // when(configService.loadConfig(someRelease, someGroupName))
.thenReturn(someApolloConfig); // .thenReturn(someApolloConfig);
//
ApolloConfig // ApolloConfig result = configController.queryConfig(someAppId, someClusterName, someGroupName,
result = // someClientSideReleaseId, someResponse);
configController //
.queryConfig(someAppId, someClusterName, someVersionName, someClientSideReleaseId, // assertEquals(someApolloConfig, result);
someResponse); // verify(configService, times(1)).findRelease(someAppId, someClusterName, someGroupName
// someVersinName);
assertEquals(someApolloConfig, result); // verify(configService, times(1)).loadConfig(someRelease, someGroupName,);
verify(configService, times(1)).loadVersionByAppIdAndVersionName(someAppId, someVersionName); // }
verify(configService, times(1)).loadConfigByVersionAndClusterName(someVersion, someClusterName); //
} // @Test
// public void testQueryConfigWithVersionNotFound() throws Exception {
@Test // String someAppId = "1";
public void testQueryConfigWithVersionNotFound() throws Exception { // String someClusterName = "someClusterName";
String someAppId = "1"; // String someGroupName = "someGroupName";
String someClusterName = "someClusterName"; // String someVersionName = "someVersion";
String someVersionName = "someVersion"; // long someClientSideReleaseId = 1;
long someClientSideReleaseId = 1; // HttpServletResponse someResponse = mock(HttpServletResponse.class);
HttpServletResponse someResponse = mock(HttpServletResponse.class); //
// when(configService.findRelease(someAppId, someClusterName, someGroupName, someVersionName))
when(configService.loadVersionByAppIdAndVersionName(someAppId, someVersionName)) // .thenReturn(null);
.thenReturn(null); //
// ApolloConfig result = configController.queryConfig(someAppId, someClusterName, someGroupName,
ApolloConfig // someVersionName, someClientSideReleaseId, someResponse);
result = //
configController // assertNull(result);
.queryConfig(someAppId, someClusterName, someVersionName, someClientSideReleaseId, // verify(someResponse, times(1)).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
someResponse); // }
//
assertNull(result); // @Test
verify(someResponse, times(1)).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString()); // public void testQueryConfigWithApolloConfigNotFound() throws Exception {
} // String someAppId = "1";
// String someClusterName = "someClusterName";
@Test // String someGroupName = "someGroupName";
public void testQueryConfigWithApolloConfigNotFound() throws Exception { // String someVersionName = "someVersion";
String someAppId = "1"; // long someClientSideReleaseId = 1;
String someClusterName = "someClusterName"; // long someServerSideNewReleaseId = 2;
String someVersionName = "someVersion"; // HttpServletResponse someResponse = mock(HttpServletResponse.class);
long someClientSideReleaseId = 1; // Release someRelease = mock(Release.class);
long someServerSideNewReleaseId = 2; //
HttpServletResponse someResponse = mock(HttpServletResponse.class); // when(configService.findRelease(someAppId, someClusterName, someGroupName, someVersionName))
Version someVersion = mock(Version.class); // .thenReturn(someRelease);
// when(someRelease.getId()).thenReturn(someServerSideNewReleaseId);
when(configService.loadVersionByAppIdAndVersionName(someAppId, someVersionName)) // when(configService.loadConfig(someRelease, someGroupName, someVersionName)).thenReturn(null);
.thenReturn(someVersion); //
when(someVersion.getReleaseId()).thenReturn(someServerSideNewReleaseId); // ApolloConfig result = configController.queryConfig(someAppId, someClusterName, someGroupName,
when(configService.loadConfigByVersionAndClusterName(someVersion, someClusterName)) // someVersionName, someClientSideReleaseId, someResponse);
.thenReturn(null); //
// assertNull(result);
ApolloConfig // verify(someResponse, times(1)).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
result = // }
configController //
.queryConfig(someAppId, someClusterName, someVersionName, someClientSideReleaseId, // @Test
someResponse); // public void testQueryConfigWithApolloConfigNotModified() throws Exception {
// String someAppId = "1";
assertNull(result); // String someClusterName = "someClusterName";
verify(someResponse, times(1)).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString()); // String someGroupName = "someGroupName";
} // String someVersionName = "someVersion";
// long someClientSideReleaseId = 1;
@Test // long someServerSideReleaseId = someClientSideReleaseId;
public void testQueryConfigWithApolloConfigNotModified() throws Exception { // HttpServletResponse someResponse = mock(HttpServletResponse.class);
String someAppId = "1"; // Release someRelease = mock(Release.class);
String someClusterName = "someClusterName"; //
String someVersionName = "someVersion"; // when(configService.findRelease(someAppId, someClusterName, someGroupName, someVersionName))
long someClientSideReleaseId = 1; // .thenReturn(someRelease);
long someServerSideReleaseId = someClientSideReleaseId; // when(someRelease.getId()).thenReturn(someServerSideReleaseId);
HttpServletResponse someResponse = mock(HttpServletResponse.class); //
Version someVersion = mock(Version.class); // ApolloConfig result = configController.queryConfig(someAppId, someClusterName, someGroupName,
// someVersionName, someClientSideReleaseId, someResponse);
when(configService.loadVersionByAppIdAndVersionName(someAppId, someVersionName)) //
.thenReturn(someVersion); // assertNull(result);
when(someVersion.getReleaseId()).thenReturn(someServerSideReleaseId); // verify(someResponse, times(1)).setStatus(HttpServletResponse.SC_NOT_MODIFIED);
// verify(configService, never()).loadConfig(any(Release.class), anyString(), anyString());
ApolloConfig // }
result =
configController
.queryConfig(someAppId, someClusterName, someVersionName, someClientSideReleaseId,
someResponse);
assertNull(result);
verify(someResponse, times(1)).setStatus(HttpServletResponse.SC_NOT_MODIFIED);
verify(configService, never())
.loadConfigByVersionAndClusterName(any(Version.class), anyString());
}
} }
...@@ -16,6 +16,8 @@ public class ApolloConfig implements Comparable<ApolloConfig> { ...@@ -16,6 +16,8 @@ public class ApolloConfig implements Comparable<ApolloConfig> {
private String cluster; private String cluster;
private String group;
private String version; private String version;
private Map<String, Object> configurations; private Map<String, Object> configurations;
...@@ -27,21 +29,26 @@ public class ApolloConfig implements Comparable<ApolloConfig> { ...@@ -27,21 +29,26 @@ public class ApolloConfig implements Comparable<ApolloConfig> {
@JsonCreator @JsonCreator
public ApolloConfig(@JsonProperty("appId") String appId, public ApolloConfig(@JsonProperty("appId") String appId,
@JsonProperty("cluster") String cluster, @JsonProperty("cluster") String cluster,
@JsonProperty("group") String group,
@JsonProperty("version") String version, @JsonProperty("version") String version,
@JsonProperty("releaseId") long releaseId) { @JsonProperty("releaseId") long releaseId) {
super(); super();
this.appId = appId; this.appId = appId;
this.cluster = cluster; this.cluster = cluster;
this.group = group;
this.version = version; this.version = version;
this.releaseId = releaseId; this.releaseId = releaseId;
} }
public Map<String, Object> getConfigurations() { @Override
return configurations; public int compareTo(ApolloConfig toCompare) {
} if (toCompare == null || this.getOrder() > toCompare.getOrder()) {
return 1;
public void setConfigurations(Map<String, Object> configurations) { }
this.configurations = configurations; if (toCompare.getOrder() > this.getOrder()) {
return -1;
}
return 0;
} }
public String getAppId() { public String getAppId() {
...@@ -52,18 +59,34 @@ public class ApolloConfig implements Comparable<ApolloConfig> { ...@@ -52,18 +59,34 @@ public class ApolloConfig implements Comparable<ApolloConfig> {
return cluster; return cluster;
} }
public String getVersion() { public Map<String, Object> getConfigurations() {
return version; return configurations;
} }
public long getReleaseId() { public String getGroup() {
return releaseId; return group;
} }
public int getOrder() { public int getOrder() {
return order; return order;
} }
public long getReleaseId() {
return releaseId;
}
public String getVersion() {
return version;
}
public void setConfigurations(Map<String, Object> configurations) {
this.configurations = configurations;
}
public void setGroup(String group) {
this.group = group;
}
public void setOrder(int order) { public void setOrder(int order) {
this.order = order; this.order = order;
} }
...@@ -74,20 +97,10 @@ public class ApolloConfig implements Comparable<ApolloConfig> { ...@@ -74,20 +97,10 @@ public class ApolloConfig implements Comparable<ApolloConfig> {
.omitNullValues() .omitNullValues()
.add("appId", appId) .add("appId", appId)
.add("cluster", cluster) .add("cluster", cluster)
.add("group", group)
.add("version", version) .add("version", version)
.add("releaseId", releaseId) .add("releaseId", releaseId)
.add("configurations", configurations) .add("configurations", configurations)
.toString(); .toString();
} }
@Override
public int compareTo(ApolloConfig toCompare) {
if (toCompare == null || this.getOrder() > toCompare.getOrder()) {
return 1;
}
if (toCompare.getOrder() > this.getOrder()) {
return -1;
}
return 0;
}
} }
package com.ctrip.apollo.core.dto;
public class AppDTO {
private String name;
private String appId;
private String ownerName;
private String ownerEmail;
public String getAppId() {
return appId;
}
public String getName() {
return name;
}
public String getOwnerEmail() {
return ownerEmail;
}
public String getOwnerName() {
return ownerName;
}
public void setAppId(String appId) {
this.appId = appId;
}
public void setName(String name) {
this.name = name;
}
public void setOwnerEmail(String ownerEmail) {
this.ownerEmail = ownerEmail;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
}
package com.ctrip.apollo.core.dto;
public class GroupDTO {
private long id;
private long clusterId;
private long namespaceId;
private String name;
public long getClusterId() {
return clusterId;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public long getNamespaceId() {
return namespaceId;
}
public void setClusterId(long clusterId) {
this.clusterId = clusterId;
}
public void setId(long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setNamespaceId(long namespaceId) {
this.namespaceId = namespaceId;
}
}
...@@ -2,7 +2,7 @@ package com.ctrip.apollo.core.dto; ...@@ -2,7 +2,7 @@ package com.ctrip.apollo.core.dto;
import java.util.Date; import java.util.Date;
public class ConfigItemDTO { public class ItemDTO {
private long id; private long id;
...@@ -18,19 +18,11 @@ public class ConfigItemDTO { ...@@ -18,19 +18,11 @@ public class ConfigItemDTO {
private String comment; private String comment;
private String dataChangeCreatedBy; public ItemDTO() {
private Date dataChangeCreatedTime;
private String dataChangeLastModifiedBy;
private Date dataChangeLastModifiedTime;
public ConfigItemDTO() {
} }
public ConfigItemDTO(String key, String value) { public ItemDTO(String key, String value) {
this.key = key; this.key = key;
this.value = value; this.value = value;
} }
...@@ -83,38 +75,6 @@ public class ConfigItemDTO { ...@@ -83,38 +75,6 @@ public class ConfigItemDTO {
this.value = value; this.value = value;
} }
public String getDataChangeCreatedBy() {
return dataChangeCreatedBy;
}
public void setDataChangeCreatedBy(String dataChangeCreatedBy) {
this.dataChangeCreatedBy = dataChangeCreatedBy;
}
public Date getDataChangeCreatedTime() {
return dataChangeCreatedTime;
}
public void setDataChangeCreatedTime(Date dataChangeCreatedTime) {
this.dataChangeCreatedTime = dataChangeCreatedTime;
}
public String getDataChangeLastModifiedBy() {
return dataChangeLastModifiedBy;
}
public void setDataChangeLastModifiedBy(String dataChangeLastModifiedBy) {
this.dataChangeLastModifiedBy = dataChangeLastModifiedBy;
}
public Date getDataChangeLastModifiedTime() {
return dataChangeLastModifiedTime;
}
public void setDataChangeLastModifiedTime(Date dataChangeLastModifiedTime) {
this.dataChangeLastModifiedTime = dataChangeLastModifiedTime;
}
public String getComment() { public String getComment() {
return comment; return comment;
} }
......
package com.ctrip.apollo.core.dto; package com.ctrip.apollo.core.dto;
public class ReleaseSnapshotDTO { public class ReleaseDTO {
private long id; private long id;
...@@ -10,7 +10,7 @@ public class ReleaseSnapshotDTO { ...@@ -10,7 +10,7 @@ public class ReleaseSnapshotDTO {
private String configurations; private String configurations;
public ReleaseSnapshotDTO() { public ReleaseDTO() {
} }
public long getId() { public long getId() {
......
...@@ -4,8 +4,8 @@ import com.google.common.base.Strings; ...@@ -4,8 +4,8 @@ import com.google.common.base.Strings;
import com.ctrip.apollo.Apollo; import com.ctrip.apollo.Apollo;
import com.ctrip.apollo.core.dto.ClusterDTO; import com.ctrip.apollo.core.dto.ClusterDTO;
import com.ctrip.apollo.core.dto.ConfigItemDTO; import com.ctrip.apollo.core.dto.ItemDTO;
import com.ctrip.apollo.core.dto.ReleaseSnapshotDTO; import com.ctrip.apollo.core.dto.ReleaseDTO;
import com.ctrip.apollo.core.dto.VersionDTO; import com.ctrip.apollo.core.dto.VersionDTO;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -19,16 +19,16 @@ public class AdminServiceAPI { ...@@ -19,16 +19,16 @@ public class AdminServiceAPI {
public static class ConfigAPI extends API { public static class ConfigAPI extends API {
public static String CONFIG_RELEASE_API = "/configs/release/"; public static String CONFIG_RELEASE_API = "/configs/release/";
public ReleaseSnapshotDTO[] getConfigByReleaseId(Apollo.Env env, long releaseId) { public ReleaseDTO[] getConfigByReleaseId(Apollo.Env env, long releaseId) {
if (releaseId <= 0) { if (releaseId <= 0) {
return null; return null;
} }
return restTemplate.getForObject(getAdminServiceHost(env) + CONFIG_RELEASE_API + releaseId, return restTemplate.getForObject(getAdminServiceHost(env) + CONFIG_RELEASE_API + releaseId,
ReleaseSnapshotDTO[].class); ReleaseDTO[].class);
} }
public ConfigItemDTO[] getLatestConfigItemsByClusters(Apollo.Env env, List<Long> clusterIds) { public ItemDTO[] getLatestConfigItemsByClusters(Apollo.Env env, List<Long> clusterIds) {
if (clusterIds == null || clusterIds.size() == 0) { if (clusterIds == null || clusterIds.size() == 0) {
return null; return null;
} }
...@@ -38,7 +38,7 @@ public class AdminServiceAPI { ...@@ -38,7 +38,7 @@ public class AdminServiceAPI {
} }
return restTemplate.getForObject(getAdminServiceHost(env) + "/configs/latest?clusterIds=" + sb return restTemplate.getForObject(getAdminServiceHost(env) + "/configs/latest?clusterIds=" + sb
.substring(0, sb.length() - 1), ConfigItemDTO[].class); .substring(0, sb.length() - 1), ItemDTO[].class);
} }
} }
......
...@@ -2,7 +2,7 @@ package com.ctrip.apollo.portal.entity; ...@@ -2,7 +2,7 @@ package com.ctrip.apollo.portal.entity;
import com.ctrip.apollo.Apollo.Env; import com.ctrip.apollo.Apollo.Env;
import com.ctrip.apollo.core.dto.ConfigItemDTO; import com.ctrip.apollo.core.dto.ItemDTO;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
...@@ -21,7 +21,7 @@ public class AppConfigVO { ...@@ -21,7 +21,7 @@ public class AppConfigVO {
/** /**
* default cluster and app self’s configs * default cluster and app self’s configs
*/ */
private List<ConfigItemDTO> defaultClusterConfigs; private List<ItemDTO> defaultClusterConfigs;
/** /**
* default cluster and override other app configs * default cluster and override other app configs
...@@ -63,7 +63,7 @@ public class AppConfigVO { ...@@ -63,7 +63,7 @@ public class AppConfigVO {
public static class OverrideAppConfig { public static class OverrideAppConfig {
private String appId; private String appId;
private List<ConfigItemDTO> configs; private List<ItemDTO> configs;
public OverrideAppConfig() { public OverrideAppConfig() {
...@@ -77,15 +77,15 @@ public class AppConfigVO { ...@@ -77,15 +77,15 @@ public class AppConfigVO {
this.appId = appId; this.appId = appId;
} }
public List<ConfigItemDTO> getConfigs() { public List<ItemDTO> getConfigs() {
return configs; return configs;
} }
public void setConfigs(List<ConfigItemDTO> configs) { public void setConfigs(List<ItemDTO> configs) {
this.configs = configs; this.configs = configs;
} }
public void addConfig(ConfigItemDTO config) { public void addConfig(ItemDTO config) {
if (configs == null) { if (configs == null) {
configs = new LinkedList<>(); configs = new LinkedList<>();
} }
...@@ -97,7 +97,7 @@ public class AppConfigVO { ...@@ -97,7 +97,7 @@ public class AppConfigVO {
public static class OverrideClusterConfig { public static class OverrideClusterConfig {
private String clusterName; private String clusterName;
private List<ConfigItemDTO> configs; private List<ItemDTO> configs;
public OverrideClusterConfig() { public OverrideClusterConfig() {
} }
...@@ -110,11 +110,11 @@ public class AppConfigVO { ...@@ -110,11 +110,11 @@ public class AppConfigVO {
this.clusterName = clusterName; this.clusterName = clusterName;
} }
public List<ConfigItemDTO> getConfigs() { public List<ItemDTO> getConfigs() {
return configs; return configs;
} }
public void setConfigs(List<ConfigItemDTO> configs) { public void setConfigs(List<ItemDTO> configs) {
this.configs = configs; this.configs = configs;
} }
} }
...@@ -144,11 +144,11 @@ public class AppConfigVO { ...@@ -144,11 +144,11 @@ public class AppConfigVO {
this.versionId = versionId; this.versionId = versionId;
} }
public List<ConfigItemDTO> getDefaultClusterConfigs() { public List<ItemDTO> getDefaultClusterConfigs() {
return defaultClusterConfigs; return defaultClusterConfigs;
} }
public void setDefaultClusterConfigs(List<ConfigItemDTO> defaultClusterConfigs) { public void setDefaultClusterConfigs(List<ItemDTO> defaultClusterConfigs) {
this.defaultClusterConfigs = defaultClusterConfigs; this.defaultClusterConfigs = defaultClusterConfigs;
} }
......
...@@ -16,8 +16,8 @@ import org.springframework.stereotype.Service; ...@@ -16,8 +16,8 @@ import org.springframework.stereotype.Service;
import com.ctrip.apollo.Apollo.Env; import com.ctrip.apollo.Apollo.Env;
import com.ctrip.apollo.core.ConfigConsts; import com.ctrip.apollo.core.ConfigConsts;
import com.ctrip.apollo.core.dto.ClusterDTO; import com.ctrip.apollo.core.dto.ClusterDTO;
import com.ctrip.apollo.core.dto.ConfigItemDTO; import com.ctrip.apollo.core.dto.ItemDTO;
import com.ctrip.apollo.core.dto.ReleaseSnapshotDTO; import com.ctrip.apollo.core.dto.ReleaseDTO;
import com.ctrip.apollo.core.dto.VersionDTO; import com.ctrip.apollo.core.dto.VersionDTO;
import com.ctrip.apollo.portal.api.AdminServiceAPI; import com.ctrip.apollo.portal.api.AdminServiceAPI;
import com.ctrip.apollo.portal.constants.PortalConstants; import com.ctrip.apollo.portal.constants.PortalConstants;
...@@ -53,14 +53,14 @@ public class ConfigService { ...@@ -53,14 +53,14 @@ public class ConfigService {
return null; return null;
} }
ReleaseSnapshotDTO[] releaseSnapShots = configAPI.getConfigByReleaseId(env, releaseId); ReleaseDTO[] releaseSnapShots = configAPI.getConfigByReleaseId(env, releaseId);
if (releaseSnapShots == null || releaseSnapShots.length == 0) { if (releaseSnapShots == null || releaseSnapShots.length == 0) {
return null; return null;
} }
AppConfigVO appConfigVO = AppConfigVO.newInstance(appId, versionId); AppConfigVO appConfigVO = AppConfigVO.newInstance(appId, versionId);
for (ReleaseSnapshotDTO snapShot : releaseSnapShots) { for (ReleaseDTO snapShot : releaseSnapShots) {
// default cluster // default cluster
if (ConfigConsts.DEFAULT_CLUSTER_NAME.equals(snapShot.getClusterName())) { if (ConfigConsts.DEFAULT_CLUSTER_NAME.equals(snapShot.getClusterName())) {
...@@ -81,17 +81,17 @@ public class ConfigService { ...@@ -81,17 +81,17 @@ public class ConfigService {
return version.getReleaseId(); return version.getReleaseId();
} }
private void collectDefaultClusterConfigs(String appId, ReleaseSnapshotDTO snapShot, private void collectDefaultClusterConfigs(String appId, ReleaseDTO snapShot,
AppConfigVO appConfigVO) { AppConfigVO appConfigVO) {
Map<String, List<ConfigItemDTO>> groupedConfigs = Map<String, List<ItemDTO>> groupedConfigs =
groupConfigsByApp(appId, snapShot.getConfigurations()); groupConfigsByApp(appId, snapShot.getConfigurations());
List<AppConfigVO.OverrideAppConfig> overrideAppConfigs = appConfigVO.getOverrideAppConfigs(); List<AppConfigVO.OverrideAppConfig> overrideAppConfigs = appConfigVO.getOverrideAppConfigs();
for (Map.Entry<String, List<ConfigItemDTO>> entry : groupedConfigs.entrySet()) { for (Map.Entry<String, List<ItemDTO>> entry : groupedConfigs.entrySet()) {
String configAppId = entry.getKey(); String configAppId = entry.getKey();
List<ConfigItemDTO> kvs = entry.getValue(); List<ItemDTO> kvs = entry.getValue();
if (configAppId.equals(appId)) { if (configAppId.equals(appId)) {
appConfigVO.setDefaultClusterConfigs(kvs); appConfigVO.setDefaultClusterConfigs(kvs);
...@@ -109,12 +109,12 @@ public class ConfigService { ...@@ -109,12 +109,12 @@ public class ConfigService {
/** /**
* appId -> List<KV> * appId -> List<KV>
*/ */
private Map<String, List<ConfigItemDTO>> groupConfigsByApp(String selfAppId, String configJson) { private Map<String, List<ItemDTO>> groupConfigsByApp(String selfAppId, String configJson) {
if (configJson == null || "".equals(configJson)) { if (configJson == null || "".equals(configJson)) {
return Maps.newHashMap(); return Maps.newHashMap();
} }
Map<String, List<ConfigItemDTO>> appIdMapKVs = new HashMap<>(); Map<String, List<ItemDTO>> appIdMapKVs = new HashMap<>();
String key; String key;
Object value; Object value;
...@@ -131,12 +131,12 @@ public class ConfigService { ...@@ -131,12 +131,12 @@ public class ConfigService {
value = entry.getValue(); value = entry.getValue();
String appId = getAppIdFromKey(key); String appId = getAppIdFromKey(key);
List<ConfigItemDTO> kvs = appIdMapKVs.get(appId); List<ItemDTO> kvs = appIdMapKVs.get(appId);
if (kvs == null) { if (kvs == null) {
kvs = new LinkedList<>(); kvs = new LinkedList<>();
appIdMapKVs.put(appId, kvs); appIdMapKVs.put(appId, kvs);
} }
kvs.add(new ConfigItemDTO(key, value.toString())); kvs.add(new ItemDTO(key, value.toString()));
} }
return appIdMapKVs; return appIdMapKVs;
...@@ -147,7 +147,7 @@ public class ConfigService { ...@@ -147,7 +147,7 @@ public class ConfigService {
return key.substring(0, key.indexOf(".")); return key.substring(0, key.indexOf("."));
} }
private void collectSpecialClusterConfigs(String appId, ReleaseSnapshotDTO snapShot, private void collectSpecialClusterConfigs(String appId, ReleaseDTO snapShot,
AppConfigVO appConfigVO) { AppConfigVO appConfigVO) {
List<AppConfigVO.OverrideClusterConfig> overrideClusterConfigs = List<AppConfigVO.OverrideClusterConfig> overrideClusterConfigs =
appConfigVO.getOverrideClusterConfigs(); appConfigVO.getOverrideClusterConfigs();
...@@ -174,17 +174,17 @@ public class ConfigService { ...@@ -174,17 +174,17 @@ public class ConfigService {
clusterIds.add(cluster.getId()); clusterIds.add(cluster.getId());
} }
ConfigItemDTO[] configItems = configAPI.getLatestConfigItemsByClusters(env, clusterIds); ItemDTO[] configItems = configAPI.getLatestConfigItemsByClusters(env, clusterIds);
return buildAPPConfigVO(appId, Arrays.asList(configItems)); return buildAPPConfigVO(appId, Arrays.asList(configItems));
} }
private AppConfigVO buildAPPConfigVO(String appId, List<ConfigItemDTO> configItems) { private AppConfigVO buildAPPConfigVO(String appId, List<ItemDTO> configItems) {
if (configItems == null || configItems.size() == 0) { if (configItems == null || configItems.size() == 0) {
return null; return null;
} }
Map<String, List<ConfigItemDTO>> groupedClusterConfigs = groupConfigByCluster(configItems); Map<String, List<ItemDTO>> groupedClusterConfigs = groupConfigByCluster(configItems);
AppConfigVO appConfigVO = AppConfigVO.newInstance(appId, PortalConstants.LASTEST_VERSION_ID); AppConfigVO appConfigVO = AppConfigVO.newInstance(appId, PortalConstants.LASTEST_VERSION_ID);
...@@ -194,13 +194,13 @@ public class ConfigService { ...@@ -194,13 +194,13 @@ public class ConfigService {
} }
private Map<String, List<ConfigItemDTO>> groupConfigByCluster(List<ConfigItemDTO> configItems) { private Map<String, List<ItemDTO>> groupConfigByCluster(List<ItemDTO> configItems) {
Map<String, List<ConfigItemDTO>> groupedClusterConfigs = new HashMap<>(); Map<String, List<ItemDTO>> groupedClusterConfigs = new HashMap<>();
String clusterName; String clusterName;
for (ConfigItemDTO configItem : configItems) { for (ItemDTO configItem : configItems) {
clusterName = configItem.getClusterName(); clusterName = configItem.getClusterName();
List<ConfigItemDTO> clusterConfigs = groupedClusterConfigs.get(clusterName); List<ItemDTO> clusterConfigs = groupedClusterConfigs.get(clusterName);
if (clusterConfigs == null) { if (clusterConfigs == null) {
clusterConfigs = new LinkedList<>(); clusterConfigs = new LinkedList<>();
groupedClusterConfigs.put(clusterName, clusterConfigs); groupedClusterConfigs.put(clusterName, clusterConfigs);
...@@ -210,11 +210,11 @@ public class ConfigService { ...@@ -210,11 +210,11 @@ public class ConfigService {
return groupedClusterConfigs; return groupedClusterConfigs;
} }
private void groupConfigByAppAndEnrichDTO(Map<String, List<ConfigItemDTO>> groupedClusterConfigs, private void groupConfigByAppAndEnrichDTO(Map<String, List<ItemDTO>> groupedClusterConfigs,
AppConfigVO appConfigVO) { AppConfigVO appConfigVO) {
String appId = appConfigVO.getAppId(); String appId = appConfigVO.getAppId();
List<ConfigItemDTO> defaultClusterConfigs = appConfigVO.getDefaultClusterConfigs(); List<ItemDTO> defaultClusterConfigs = appConfigVO.getDefaultClusterConfigs();
List<AppConfigVO.OverrideAppConfig> overrideAppConfigs = appConfigVO.getOverrideAppConfigs(); List<AppConfigVO.OverrideAppConfig> overrideAppConfigs = appConfigVO.getOverrideAppConfigs();
...@@ -222,8 +222,8 @@ public class ConfigService { ...@@ -222,8 +222,8 @@ public class ConfigService {
appConfigVO.getOverrideClusterConfigs(); appConfigVO.getOverrideClusterConfigs();
String clusterName; String clusterName;
List<ConfigItemDTO> clusterConfigs; List<ItemDTO> clusterConfigs;
for (Map.Entry<String, List<ConfigItemDTO>> entry : groupedClusterConfigs.entrySet()) { for (Map.Entry<String, List<ItemDTO>> entry : groupedClusterConfigs.entrySet()) {
clusterName = entry.getKey(); clusterName = entry.getKey();
clusterConfigs = entry.getValue(); clusterConfigs = entry.getValue();
...@@ -238,13 +238,13 @@ public class ConfigService { ...@@ -238,13 +238,13 @@ public class ConfigService {
} }
} }
private void collectDefaultClusterConfigs(String appId, List<ConfigItemDTO> clusterConfigs, private void collectDefaultClusterConfigs(String appId, List<ItemDTO> clusterConfigs,
List<ConfigItemDTO> defaultClusterConfigs, List<ItemDTO> defaultClusterConfigs,
List<AppConfigVO.OverrideAppConfig> overrideAppConfigs) { List<AppConfigVO.OverrideAppConfig> overrideAppConfigs) {
Map<String, AppConfigVO.OverrideAppConfig> appIdMapOverrideAppConfig = null; Map<String, AppConfigVO.OverrideAppConfig> appIdMapOverrideAppConfig = null;
for (ConfigItemDTO config : clusterConfigs) { for (ItemDTO config : clusterConfigs) {
String targetAppId = config.getAppId(); String targetAppId = config.getAppId();
if (appId.equals(targetAppId)) {// app self's configs if (appId.equals(targetAppId)) {// app self's configs
defaultClusterConfigs.add(config); defaultClusterConfigs.add(config);
...@@ -268,7 +268,7 @@ public class ConfigService { ...@@ -268,7 +268,7 @@ public class ConfigService {
} }
} }
private void collectSpecialClusterConfigs(String clusterName, List<ConfigItemDTO> clusterConfigs, private void collectSpecialClusterConfigs(String clusterName, List<ItemDTO> clusterConfigs,
List<AppConfigVO.OverrideClusterConfig> overrideClusterConfigs) { List<AppConfigVO.OverrideClusterConfig> overrideClusterConfigs) {
AppConfigVO.OverrideClusterConfig overrideClusterConfig = AppConfigVO.OverrideClusterConfig overrideClusterConfig =
new AppConfigVO.OverrideClusterConfig(); new AppConfigVO.OverrideClusterConfig();
......
...@@ -17,8 +17,8 @@ import org.springframework.web.client.RestTemplate; ...@@ -17,8 +17,8 @@ import org.springframework.web.client.RestTemplate;
import com.ctrip.apollo.Apollo.Env; import com.ctrip.apollo.Apollo.Env;
import com.ctrip.apollo.core.ConfigConsts; import com.ctrip.apollo.core.ConfigConsts;
import com.ctrip.apollo.core.dto.ClusterDTO; import com.ctrip.apollo.core.dto.ClusterDTO;
import com.ctrip.apollo.core.dto.ConfigItemDTO; import com.ctrip.apollo.core.dto.ItemDTO;
import com.ctrip.apollo.core.dto.ReleaseSnapshotDTO; import com.ctrip.apollo.core.dto.ReleaseDTO;
import com.ctrip.apollo.core.dto.ServiceDTO; import com.ctrip.apollo.core.dto.ServiceDTO;
import com.ctrip.apollo.core.dto.VersionDTO; import com.ctrip.apollo.core.dto.VersionDTO;
import com.ctrip.apollo.core.exception.ServiceException; import com.ctrip.apollo.core.exception.ServiceException;
...@@ -71,7 +71,7 @@ public class ConfigServiceTest { ...@@ -71,7 +71,7 @@ public class ConfigServiceTest {
long releaseId = 11111; long releaseId = 11111;
VersionDTO someVersion = assembleVersion(appId, "1.0", releaseId); VersionDTO someVersion = assembleVersion(appId, "1.0", releaseId);
ReleaseSnapshotDTO[] someReleaseSnapShots = assembleReleaseSnapShots(); ReleaseDTO[] someReleaseSnapShots = assembleReleaseSnapShots();
when(versionAPI.getVersionById(Env.DEV, versionId)).thenReturn(someVersion); when(versionAPI.getVersionById(Env.DEV, versionId)).thenReturn(someVersion);
when(configAPI.getConfigByReleaseId(Env.DEV, releaseId)).thenReturn(someReleaseSnapShots); when(configAPI.getConfigByReleaseId(Env.DEV, releaseId)).thenReturn(someReleaseSnapShots);
...@@ -92,7 +92,7 @@ public class ConfigServiceTest { ...@@ -92,7 +92,7 @@ public class ConfigServiceTest {
long releaseId = 11111; long releaseId = 11111;
VersionDTO someVersion = assembleVersion(appId, "1.0", releaseId); VersionDTO someVersion = assembleVersion(appId, "1.0", releaseId);
ReleaseSnapshotDTO[] someReleaseSnapShots = new ReleaseSnapshotDTO[1]; ReleaseDTO[] someReleaseSnapShots = new ReleaseDTO[1];
someReleaseSnapShots[0] = assembleReleaseSnapShot(11111, ConfigConsts.DEFAULT_CLUSTER_NAME, someReleaseSnapShots[0] = assembleReleaseSnapShot(11111, ConfigConsts.DEFAULT_CLUSTER_NAME,
"{\"6666.foo\":\"demo1\", \"6666.bar\":\"demo2\"}"); "{\"6666.foo\":\"demo1\", \"6666.bar\":\"demo2\"}");
...@@ -114,7 +114,7 @@ public class ConfigServiceTest { ...@@ -114,7 +114,7 @@ public class ConfigServiceTest {
long versionId = 100; long versionId = 100;
long releaseId = 11111; long releaseId = 11111;
VersionDTO someVersion = assembleVersion(appId, "1.0", releaseId); VersionDTO someVersion = assembleVersion(appId, "1.0", releaseId);
ReleaseSnapshotDTO[] someReleaseSnapShots = new ReleaseSnapshotDTO[1]; ReleaseDTO[] someReleaseSnapShots = new ReleaseDTO[1];
someReleaseSnapShots[0] = assembleReleaseSnapShot(11111, ConfigConsts.DEFAULT_CLUSTER_NAME, someReleaseSnapShots[0] = assembleReleaseSnapShot(11111, ConfigConsts.DEFAULT_CLUSTER_NAME,
"{\"6666.foo\":\"demo1\", \"6666.bar\":\"demo2\", \"5555.bar\":\"demo2\", \"22.bar\":\"demo2\"}"); "{\"6666.foo\":\"demo1\", \"6666.bar\":\"demo2\", \"5555.bar\":\"demo2\", \"22.bar\":\"demo2\"}");
...@@ -136,7 +136,7 @@ public class ConfigServiceTest { ...@@ -136,7 +136,7 @@ public class ConfigServiceTest {
long versionId = 100; long versionId = 100;
long releaseId = 11111; long releaseId = 11111;
VersionDTO someVersion = assembleVersion(appId, "1.0", releaseId); VersionDTO someVersion = assembleVersion(appId, "1.0", releaseId);
ReleaseSnapshotDTO[] someReleaseSnapShots = new ReleaseSnapshotDTO[2]; ReleaseDTO[] someReleaseSnapShots = new ReleaseDTO[2];
someReleaseSnapShots[0] = assembleReleaseSnapShot(11111, ConfigConsts.DEFAULT_CLUSTER_NAME, someReleaseSnapShots[0] = assembleReleaseSnapShot(11111, ConfigConsts.DEFAULT_CLUSTER_NAME,
"{\"6666.foo\":\"demo1\", \"6666.bar\":\"demo2\"}"); "{\"6666.foo\":\"demo1\", \"6666.bar\":\"demo2\"}");
someReleaseSnapShots[1] = assembleReleaseSnapShot(11112, "cluster1", someReleaseSnapShots[1] = assembleReleaseSnapShot(11112, "cluster1",
...@@ -158,7 +158,7 @@ public class ConfigServiceTest { ...@@ -158,7 +158,7 @@ public class ConfigServiceTest {
public void testLoadLastestConfig() { public void testLoadLastestConfig() {
String appId = "6666"; String appId = "6666";
ClusterDTO[] someClusters = assembleClusters(); ClusterDTO[] someClusters = assembleClusters();
ConfigItemDTO[] someConfigItem = assembleConfigItems(); ItemDTO[] someConfigItem = assembleConfigItems();
when(clusterAPI.getClustersByApp(Env.DEV, appId)).thenReturn(someClusters); when(clusterAPI.getClustersByApp(Env.DEV, appId)).thenReturn(someClusters);
when(configAPI.getLatestConfigItemsByClusters(Env.DEV, Arrays when(configAPI.getLatestConfigItemsByClusters(Env.DEV, Arrays
...@@ -181,8 +181,8 @@ public class ConfigServiceTest { ...@@ -181,8 +181,8 @@ public class ConfigServiceTest {
return version; return version;
} }
private ReleaseSnapshotDTO[] assembleReleaseSnapShots() { private ReleaseDTO[] assembleReleaseSnapShots() {
ReleaseSnapshotDTO[] releaseSnapShots = new ReleaseSnapshotDTO[3]; ReleaseDTO[] releaseSnapShots = new ReleaseDTO[3];
releaseSnapShots[0] = assembleReleaseSnapShot(11111, ConfigConsts.DEFAULT_CLUSTER_NAME, releaseSnapShots[0] = assembleReleaseSnapShot(11111, ConfigConsts.DEFAULT_CLUSTER_NAME,
"{\"6666.foo\":\"demo1\", \"6666.bar\":\"demo2\",\"3333.foo\":\"1008\",\"4444.bar\":\"99901\"}"); "{\"6666.foo\":\"demo1\", \"6666.bar\":\"demo2\",\"3333.foo\":\"1008\",\"4444.bar\":\"99901\"}");
releaseSnapShots[1] = assembleReleaseSnapShot(11111, "cluster1", "{\"6666.foo\":\"demo1\"}"); releaseSnapShots[1] = assembleReleaseSnapShot(11111, "cluster1", "{\"6666.foo\":\"demo1\"}");
...@@ -190,9 +190,9 @@ public class ConfigServiceTest { ...@@ -190,9 +190,9 @@ public class ConfigServiceTest {
return releaseSnapShots; return releaseSnapShots;
} }
private ReleaseSnapshotDTO assembleReleaseSnapShot(long releaseId, String clusterName, private ReleaseDTO assembleReleaseSnapShot(long releaseId, String clusterName,
String configurations) { String configurations) {
ReleaseSnapshotDTO releaseSnapShot = new ReleaseSnapshotDTO(); ReleaseDTO releaseSnapShot = new ReleaseDTO();
releaseSnapShot.setReleaseId(releaseId); releaseSnapShot.setReleaseId(releaseId);
releaseSnapShot.setClusterName(clusterName); releaseSnapShot.setClusterName(clusterName);
releaseSnapShot.setConfigurations(configurations); releaseSnapShot.setConfigurations(configurations);
...@@ -214,8 +214,8 @@ public class ConfigServiceTest { ...@@ -214,8 +214,8 @@ public class ConfigServiceTest {
return cluster; return cluster;
} }
private ConfigItemDTO[] assembleConfigItems() { private ItemDTO[] assembleConfigItems() {
ConfigItemDTO[] configItems = new ConfigItemDTO[5]; ItemDTO[] configItems = new ItemDTO[5];
configItems[0] = assembleConfigItem(100, ConfigConsts.DEFAULT_CLUSTER_NAME, "6666", "6666.k1", "6666.v1"); configItems[0] = assembleConfigItem(100, ConfigConsts.DEFAULT_CLUSTER_NAME, "6666", "6666.k1", "6666.v1");
configItems[1] = assembleConfigItem(100, ConfigConsts.DEFAULT_CLUSTER_NAME, "6666", "6666.k2", "6666.v2"); configItems[1] = assembleConfigItem(100, ConfigConsts.DEFAULT_CLUSTER_NAME, "6666", "6666.k2", "6666.v2");
configItems[2] = assembleConfigItem(100, ConfigConsts.DEFAULT_CLUSTER_NAME, "6666", "6666.k3", "6666.v3"); configItems[2] = assembleConfigItem(100, ConfigConsts.DEFAULT_CLUSTER_NAME, "6666", "6666.k3", "6666.v3");
...@@ -224,9 +224,9 @@ public class ConfigServiceTest { ...@@ -224,9 +224,9 @@ public class ConfigServiceTest {
return configItems; return configItems;
} }
private ConfigItemDTO assembleConfigItem(long clusterId, String clusterName, String appId, private ItemDTO assembleConfigItem(long clusterId, String clusterName, String appId,
String key, String value) { String key, String value) {
ConfigItemDTO configItem = new ConfigItemDTO(); ItemDTO configItem = new ItemDTO();
configItem.setClusterName(clusterName); configItem.setClusterName(clusterName);
configItem.setClusterId(clusterId); configItem.setClusterId(clusterId);
configItem.setAppId(appId); configItem.setAppId(appId);
......
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