Commit 06e51680 authored by Yiming Liu's avatar Yiming Liu

Refine biz and entity

parent 05ed035b
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/{clusterId}/groups")
public List<GroupDTO> findGroups(@PathVariable("clusterId") Long clusterId) {
List<Group> groups = viewService.findGroups(clusterId);
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.ViewService;
import com.ctrip.apollo.biz.utils.BeanUtils;
import com.ctrip.apollo.core.dto.ItemDTO;
@RestController
public class ItemController {
@Autowired
private ViewService viewService;
@RequestMapping("/apps/{appId}/clusters/{clusterId}/groups/{groupId}/items")
public List<ItemDTO> findItems(
@PathVariable("groupId") Long groupId) {
List<Item> items = viewService.findItems(groupId);
return BeanUtils.batchTransform(ItemDTO.class, items);
}
}
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; package com.ctrip.apollo.adminservice.controller;
import com.ctrip.apollo.biz.service.AdminReleaseService; import java.util.List;
import com.ctrip.apollo.core.dto.VersionDTO;
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.Version;
import com.ctrip.apollo.biz.service.VersionService;
import com.ctrip.apollo.biz.service.ViewService;
import com.ctrip.apollo.biz.utils.BeanUtils;
import com.ctrip.apollo.core.dto.VersionDTO;
@RestController @RestController
@RequestMapping("/version")
public class VersionController { public class VersionController {
@Autowired @Autowired
private AdminReleaseService adminReleaseService; private ViewService viewService;
@RequestMapping("/app/{appId}") @Autowired
public List<VersionDTO> versions(@PathVariable String appId) { private VersionService versionService;
return adminReleaseService.findVersionsByApp(appId);
@RequestMapping("/app/{appId}/clusters/{clusterId}/versions")
public List<VersionDTO> findVersions(@PathVariable("appId") String appId, @PathVariable("clusterId") Long clusterId) {
List<Version> versions = viewService.findVersions(clusterId);
return BeanUtils.batchTransform(VersionDTO.class, versions);
} }
@RequestMapping("/{versionId}") @RequestMapping("/versions/{versionId}")
public VersionDTO version(@PathVariable long versionId) { public VersionDTO findOne(@PathVariable("versionId") long versionId) {
return adminReleaseService.loadVersionById(versionId); Version version = versionService.findOne(versionId);
return BeanUtils.transfrom(VersionDTO.class, version);
} }
} }
...@@ -2,15 +2,9 @@ package com.ctrip.apollo.biz.entity; ...@@ -2,15 +2,9 @@ package com.ctrip.apollo.biz.entity;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity @Entity
public class App extends BaseEntity{ public class App extends BaseEntity {
@Id
@GeneratedValue
private long id;
@Column(nullable = false) @Column(nullable = false)
private String name; private String name;
...@@ -19,37 +13,40 @@ public class App extends BaseEntity{ ...@@ -19,37 +13,40 @@ public class App extends BaseEntity{
private String appId; private String appId;
@Column(nullable = false) @Column(nullable = false)
private String owner; private String ownerName;
@Column(nullable = false)
private String ownerEmail;
public String getAppId() { public String getAppId() {
return appId; return appId;
} }
public long getId() {
return id;
}
public String getName() { public String getName() {
return name; return name;
} }
public String getOwner() { public String getOwnerEmail() {
return owner; return ownerEmail;
} }
public void setAppId(String appId) { public String getOwnerName() {
this.appId = appId; return ownerName;
} }
public void setId(long id) { public void setAppId(String appId) {
this.id = id; this.appId = appId;
} }
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public void setOwner(String owner) { public void setOwnerEmail(String ownerEmail) {
this.owner = owner; this.ownerEmail = ownerEmail;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
} }
} }
...@@ -3,6 +3,8 @@ package com.ctrip.apollo.biz.entity; ...@@ -3,6 +3,8 @@ package com.ctrip.apollo.biz.entity;
import java.util.Date; import java.util.Date;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.annotations.SQLDelete; import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where; import org.hibernate.annotations.Where;
...@@ -11,8 +13,12 @@ import org.hibernate.annotations.Where; ...@@ -11,8 +13,12 @@ import org.hibernate.annotations.Where;
@SQLDelete(sql = "Update Cluster set isDeleted = 1 where id = ?") @SQLDelete(sql = "Update Cluster set isDeleted = 1 where id = ?")
public abstract class BaseEntity { public abstract class BaseEntity {
@Id
@GeneratedValue
private long id;
private boolean isDeleted; private boolean isDeleted;
@Column(name = "DataChange_CreatedBy") @Column(name = "DataChange_CreatedBy")
private String dataChangeCreatedBy; private String dataChangeCreatedBy;
...@@ -24,7 +30,7 @@ public abstract class BaseEntity { ...@@ -24,7 +30,7 @@ public abstract class BaseEntity {
@Column(name = "DataChange_LastTime") @Column(name = "DataChange_LastTime")
private Date dataChangeLastModifiedTime; private Date dataChangeLastModifiedTime;
public String getDataChangeCreatedBy() { public String getDataChangeCreatedBy() {
return dataChangeCreatedBy; return dataChangeCreatedBy;
} }
...@@ -41,6 +47,10 @@ public abstract class BaseEntity { ...@@ -41,6 +47,10 @@ public abstract class BaseEntity {
return dataChangeLastModifiedTime; return dataChangeLastModifiedTime;
} }
public long getId() {
return id;
}
public boolean isDeleted() { public boolean isDeleted() {
return isDeleted; return isDeleted;
} }
...@@ -64,4 +74,8 @@ public abstract class BaseEntity { ...@@ -64,4 +74,8 @@ public abstract class BaseEntity {
public void setDeleted(boolean deleted) { public void setDeleted(boolean deleted) {
isDeleted = deleted; isDeleted = deleted;
} }
public void setId(long id) {
this.id = id;
}
} }
...@@ -2,18 +2,12 @@ package com.ctrip.apollo.biz.entity; ...@@ -2,18 +2,12 @@ package com.ctrip.apollo.biz.entity;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
@Entity @Entity
public class Cluster extends BaseEntity{ public class Cluster extends BaseEntity {
@Id
@GeneratedValue
private long id;
@Column(nullable = false) @Column(nullable = false)
private String name; private String name;
...@@ -21,42 +15,20 @@ public class Cluster extends BaseEntity{ ...@@ -21,42 +15,20 @@ public class Cluster extends BaseEntity{
@Column(nullable = false) @Column(nullable = false)
private String appId; private String appId;
@Column public String getAppId() {
private long clusterVersionId; 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 long getClusterVersionId() { public void setName(String name) {
return clusterVersionId; this.name = name;
}
public void setClusterVersionId(long clusterVersionId) {
this.clusterVersionId = clusterVersionId;
} }
} }
...@@ -2,15 +2,9 @@ package com.ctrip.apollo.biz.entity; ...@@ -2,15 +2,9 @@ package com.ctrip.apollo.biz.entity;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity @Entity
public class Group extends BaseEntity{ public class Group extends BaseEntity {
@Id
@GeneratedValue
private long id;
@Column(nullable = false) @Column(nullable = false)
private long clusterId; private long clusterId;
...@@ -18,12 +12,15 @@ public class Group extends BaseEntity{ ...@@ -18,12 +12,15 @@ public class Group extends BaseEntity{
@Column(nullable = false) @Column(nullable = false)
private long namespaceId; private long namespaceId;
@Column(nullable = false)
private String name;
public long getClusterId() { public long getClusterId() {
return clusterId; return clusterId;
} }
public long getId() { public String getName() {
return id; return name;
} }
public long getNamespaceId() { public long getNamespaceId() {
...@@ -34,8 +31,8 @@ public class Group extends BaseEntity{ ...@@ -34,8 +31,8 @@ public class Group extends BaseEntity{
this.clusterId = clusterId; this.clusterId = clusterId;
} }
public void setId(long id) { public void setName(String name) {
this.id = id; this.name = name;
} }
public void setNamespaceId(long namespaceId) { public void setNamespaceId(long namespaceId) {
......
...@@ -2,15 +2,9 @@ package com.ctrip.apollo.biz.entity; ...@@ -2,15 +2,9 @@ package com.ctrip.apollo.biz.entity;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity @Entity
public class ConfigItem extends BaseEntity { public class Item extends BaseEntity {
@Id
@GeneratedValue
private long id;
@Column(nullable = false) @Column(nullable = false)
private long groupId; private long groupId;
...@@ -24,14 +18,6 @@ public class ConfigItem extends BaseEntity { ...@@ -24,14 +18,6 @@ public class ConfigItem extends BaseEntity {
@Column @Column
private String comment; private String comment;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getGroupId() { public long getGroupId() {
return groupId; return groupId;
} }
......
...@@ -2,16 +2,10 @@ package com.ctrip.apollo.biz.entity; ...@@ -2,16 +2,10 @@ package com.ctrip.apollo.biz.entity;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity @Entity
public class Namespace extends BaseEntity{ public class Namespace extends BaseEntity{
@Id
@GeneratedValue
private long id;
@Column(nullable = false) @Column(nullable = false)
private String name; private String name;
...@@ -29,10 +23,6 @@ public class Namespace extends BaseEntity{ ...@@ -29,10 +23,6 @@ public class Namespace extends BaseEntity{
return comment; return comment;
} }
public long getId() {
return id;
}
public String getName() { public String getName() {
return name; return name;
} }
...@@ -45,10 +35,6 @@ public class Namespace extends BaseEntity{ ...@@ -45,10 +35,6 @@ public class Namespace extends BaseEntity{
this.comment = comment; this.comment = comment;
} }
public void setId(long id) {
this.id = id;
}
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
......
package com.ctrip.apollo.biz.entity; package com.ctrip.apollo.biz.entity;
import java.util.List;
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 javax.persistence.Lob; import javax.persistence.Lob;
import javax.persistence.ManyToMany;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
@Entity @Entity
public class Release extends BaseEntity{ public class Release extends BaseEntity{
@Id
@GeneratedValue
private long id;
@Column(nullable = false) @Column(nullable = false)
private String name; private String name;
...@@ -21,6 +19,9 @@ public class Release extends BaseEntity{ ...@@ -21,6 +19,9 @@ public class Release extends BaseEntity{
@Column(nullable = false) @Column(nullable = false)
private long groupId; private long groupId;
@Column
private String groupName;
@Column(nullable = false) @Column(nullable = false)
@Lob @Lob
private String configurations; private String configurations;
...@@ -28,11 +29,21 @@ public class Release extends BaseEntity{ ...@@ -28,11 +29,21 @@ public class Release extends BaseEntity{
@Column(nullable = false) @Column(nullable = false)
private String comment; private String comment;
public Release() { @ManyToMany
private List<Version> versions;
@Column(nullable=false)
private String appId;
@Column(nullable=false)
private String clusterName;
public String getAppId() {
return appId;
} }
public long getGroupId() { public String getClusterName() {
return groupId; return clusterName;
} }
public String getComment() { public String getComment() {
...@@ -43,16 +54,28 @@ public class Release extends BaseEntity{ ...@@ -43,16 +54,28 @@ public class Release extends BaseEntity{
return configurations; return configurations;
} }
public long getId() { public long getGroupId() {
return id; return groupId;
}
public String getGroupName() {
return groupName;
} }
public String getName() { public String getName() {
return name; return name;
} }
public void setGroupId(long groupId) { public List<Version> getVersions() {
this.groupId = groupId; return versions;
}
public void setAppId(String appId) {
this.appId = appId;
}
public void setClusterName(String clusterName) {
this.clusterName = clusterName;
} }
public void setComment(String comment) { public void setComment(String comment) {
...@@ -63,11 +86,19 @@ public class Release extends BaseEntity{ ...@@ -63,11 +86,19 @@ public class Release extends BaseEntity{
this.configurations = configurations; this.configurations = configurations;
} }
public void setId(long id) { public void setGroupId(long groupId) {
this.id = id; this.groupId = groupId;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
} }
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public void setVersions(List<Version> versions) {
this.versions = versions;
}
} }
...@@ -4,40 +4,31 @@ import java.util.List; ...@@ -4,40 +4,31 @@ import java.util.List;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.ManyToMany;
import javax.persistence.Id;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
@Entity @Entity
public class Version extends BaseEntity { public class Version extends BaseEntity {
@Id
@GeneratedValue
private long id;
@Column(nullable = false) @Column(nullable = false)
private String name; private String name;
@Column(nullable = false) @Column(nullable = false)
private String clusterId; private long clusterId;
// parent version could be null // parent version could be null
@Column @Column
private Long parentVersion; private Long parentVersion;
private List<Release> releases; @ManyToMany
private List<Release> releases;
public Version() {}
public long getClusterId() {
public String getClusterId() {
return clusterId; return clusterId;
} }
public long getId() {
return id;
}
public String getName() { public String getName() {
return name; return name;
} }
...@@ -50,12 +41,9 @@ public class Version extends BaseEntity { ...@@ -50,12 +41,9 @@ public class Version extends BaseEntity {
return releases; return releases;
} }
public void setClusterId(String clusterId) {
this.clusterId = clusterId;
}
public void setId(long id) { public void setClusterId(long clusterId) {
this.id = id; this.clusterId = clusterId;
} }
public void setName(String name) { public void setName(String name) {
......
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.ConfigItem; 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 ConfigItemRepository extends PagingAndSortingRepository<ConfigItem, Long> { public interface GroupRepository extends PagingAndSortingRepository<Group, Long> {
List<ConfigItem> findByClusterIdIsIn(List<Long> clusterIds); List<Group> findByClusterId(Long clusterId);
} }
package com.ctrip.apollo.biz.repository;
import com.ctrip.apollo.biz.entity.Item;
import org.springframework.data.repository.PagingAndSortingRepository;
import java.util.List;
public interface ItemRepository extends PagingAndSortingRepository<Item, Long> {
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 VersionReleaseMapping m INNER JOIN Release r on m.releaseId = r.Id INNER JOIN Version v on m.versionId = v.id WHERE r.id = :versionId AND r.groupName = :groupName")
Release findByGroupNameAndVersionId(@Param("groupName") String groupName, @Param("versionId") long versionId);
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.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.Version;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
public interface VersionRepository extends PagingAndSortingRepository<Version, Long> { public interface VersionRepository extends PagingAndSortingRepository<Version, Long> {
Version findByAppIdAndName(String appId, String name);
Version findById(long id); Version findByClusterIdAndName(Long id, String name);
List<Version> findByAppId(String appId); List<Version> findByClusterId(Long clusterId);
} }
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ctrip.apollo.biz.entity.Cluster;
import com.ctrip.apollo.biz.entity.Release;
import com.ctrip.apollo.biz.entity.Version; import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.repository.ReleaseSnapShotRepository; import com.ctrip.apollo.biz.repository.ClusterRepository;
import com.ctrip.apollo.biz.repository.ReleaseRepository;
import com.ctrip.apollo.biz.repository.VersionRepository; import com.ctrip.apollo.biz.repository.VersionRepository;
import com.ctrip.apollo.core.dto.ApolloConfig; 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;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.Map;
/** /**
* 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
private ClusterRepository clusterRepository;
@Autowired @Autowired
private VersionRepository versionRepository; private VersionRepository versionRepository;
@Autowired @Autowired
private ReleaseSnapShotRepository releaseSnapShotRepository; private ReleaseRepository releaseRepository;
@Autowired @Autowired
private ObjectMapper objectMapper; private ObjectMapper objectMapper;
private TypeReference<Map<String, Object>> configurationTypeReference = private TypeReference<Map<String, Object>> configurationTypeReference =
new TypeReference<Map<String, Object>>() { new TypeReference<Map<String, Object>>() {};
};
/** public Release findRelease(String appId, String clusterName, String groupName, String versionName) {
* Load configuration from database Cluster cluster = clusterRepository.findByAppIdAndName(appId, clusterName);
*/ if (cluster == null) {
public ApolloConfig loadConfig(String appId, String clusterName, String versionName) { return null;
Version version = loadVersionByAppIdAndVersionName(appId, versionName); }
Version version = versionRepository.findByClusterIdAndName(cluster.getId(), versionName);
if (version == null) { if (version == null) {
return null; return null;
} }
Release release = releaseRepository.findByGroupNameAndVersionId(groupName, version.getId());
return loadConfigByVersionAndClusterName(version, clusterName); return release;
} }
/** /**
* Load Version by appId and versionName from database * Load configuration from database
*/
public Version loadVersionByAppIdAndVersionName(String appId, String versionName) {
return versionRepository.findByAppIdAndName(appId, versionName);
}
/**
* Load Config by version and clusterName 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;
} }
return assembleConfig(version, releaseSnapShot);
}
private ApolloConfig assembleConfig(Version version, ReleaseSnapshot releaseSnapShot) {
ApolloConfig config = ApolloConfig config =
new ApolloConfig(version.getAppId(), releaseSnapShot.getClusterName(), version.getName(), new ApolloConfig(release.getAppId(), release.getClusterName(), groupName, versionName, release.getId());
version.getReleaseId()); config.setConfigurations(transformConfigurationToMap(release.getConfigurations()));
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.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;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.repository.VersionRepository;
@Service
public class VersionService {
@Autowired
private VersionRepository versionRepository;
public Version findOne(Long versionId){
return versionRepository.findOne(versionId);
}
}
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.entity.Version;
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.ctrip.apollo.biz.repository.VersionRepository;
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
private GroupRepository groupRepository;
@Autowired @Autowired
private ConfigItemRepository configItemRepository; private VersionRepository versionRepository;
@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(Long clusterId) {
List<Group> groups = groupRepository.findByClusterId(clusterId);
if (groups == null) {
return Collections.EMPTY_LIST;
}
return groups;
} }
public List<ConfigItemDTO> findConfigItemsByClusters(List<Long> clusterIds) { public List<Version> findVersions(Long clusterId) {
if (clusterIds == null || clusterIds.size() == 0) { List<Version> versions = versionRepository.findByClusterId(clusterId);
if (versions == null) {
return Collections.EMPTY_LIST; return Collections.EMPTY_LIST;
} }
List<ConfigItem> configItems = configItemRepository.findByClusterIdIsIn(clusterIds); return versions;
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.entity.Version; import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.repository.ReleaseSnapShotRepository; import com.ctrip.apollo.biz.repository.ReleaseRepository;
import com.ctrip.apollo.biz.repository.VersionRepository; 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.ctrip.apollo.core.dto.ApolloConfig;
...@@ -38,7 +37,7 @@ public class ConfigServiceTest { ...@@ -38,7 +37,7 @@ public class ConfigServiceTest {
@Mock @Mock
private VersionRepository versionRepository; private VersionRepository versionRepository;
@Mock @Mock
private ReleaseSnapShotRepository releaseSnapShotRepository; private ReleaseRepository releaseRepository;
@Mock @Mock
private ObjectMapper objectMapper; private ObjectMapper objectMapper;
private ConfigService configService; private ConfigService configService;
...@@ -48,88 +47,90 @@ public class ConfigServiceTest { ...@@ -48,88 +47,90 @@ public class ConfigServiceTest {
configService = new ConfigService(); configService = new ConfigService();
ReflectionTestUtils.setField(configService, "versionRepository", versionRepository); 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, versionName);
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;
...@@ -44,45 +44,42 @@ public class ConfigControllerTest { ...@@ -44,45 +44,42 @@ public class ConfigControllerTest {
ApolloConfig someApolloConfig = mock(ApolloConfig.class); ApolloConfig someApolloConfig = mock(ApolloConfig.class);
String someAppId = "1"; String someAppId = "1";
String someClusterName = "someClusterName"; String someClusterName = "someClusterName";
String someGroupName = "someGroupName";
String someVersionName = "someVersion"; String someVersionName = "someVersion";
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, someVersionName))
.thenReturn(someVersion); .thenReturn(someRelease);
when(someVersion.getReleaseId()).thenReturn(someServerSideNewReleaseId); when(someRelease.getId()).thenReturn(someServerSideNewReleaseId);
when(configService.loadConfigByVersionAndClusterName(someVersion, someClusterName)) when(configService.loadConfig(someRelease, someGroupName, someVersionName))
.thenReturn(someApolloConfig); .thenReturn(someApolloConfig);
ApolloConfig ApolloConfig result = configController.queryConfig(someAppId, someClusterName, someGroupName,
result = someVersionName, someClientSideReleaseId, someResponse);
configController
.queryConfig(someAppId, someClusterName, someVersionName, someClientSideReleaseId,
someResponse);
assertEquals(someApolloConfig, result); assertEquals(someApolloConfig, result);
verify(configService, times(1)).loadVersionByAppIdAndVersionName(someAppId, someVersionName); verify(configService, times(1)).findRelease(someAppId, someClusterName, someGroupName,
verify(configService, times(1)).loadConfigByVersionAndClusterName(someVersion, someClusterName); someVersionName);
verify(configService, times(1)).loadConfig(someRelease, someGroupName, someVersionName);
} }
@Test @Test
public void testQueryConfigWithVersionNotFound() throws Exception { public void testQueryConfigWithVersionNotFound() throws Exception {
String someAppId = "1"; String someAppId = "1";
String someClusterName = "someClusterName"; String someClusterName = "someClusterName";
String someGroupName = "someGroupName";
String someVersionName = "someVersion"; String someVersionName = "someVersion";
long someClientSideReleaseId = 1; long someClientSideReleaseId = 1;
HttpServletResponse someResponse = mock(HttpServletResponse.class); HttpServletResponse someResponse = mock(HttpServletResponse.class);
when(configService.loadVersionByAppIdAndVersionName(someAppId, someVersionName)) when(configService.findRelease(someAppId, someClusterName, someGroupName, someVersionName))
.thenReturn(null); .thenReturn(null);
ApolloConfig ApolloConfig result = configController.queryConfig(someAppId, someClusterName, someGroupName,
result = someVersionName, someClientSideReleaseId, someResponse);
configController
.queryConfig(someAppId, someClusterName, someVersionName, someClientSideReleaseId,
someResponse);
assertNull(result); assertNull(result);
verify(someResponse, times(1)).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString()); verify(someResponse, times(1)).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
...@@ -92,23 +89,20 @@ public class ConfigControllerTest { ...@@ -92,23 +89,20 @@ public class ConfigControllerTest {
public void testQueryConfigWithApolloConfigNotFound() throws Exception { public void testQueryConfigWithApolloConfigNotFound() throws Exception {
String someAppId = "1"; String someAppId = "1";
String someClusterName = "someClusterName"; String someClusterName = "someClusterName";
String someGroupName = "someGroupName";
String someVersionName = "someVersion"; String someVersionName = "someVersion";
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, someVersionName))
.thenReturn(someVersion); .thenReturn(someRelease);
when(someVersion.getReleaseId()).thenReturn(someServerSideNewReleaseId); when(someRelease.getId()).thenReturn(someServerSideNewReleaseId);
when(configService.loadConfigByVersionAndClusterName(someVersion, someClusterName)) when(configService.loadConfig(someRelease, someGroupName, someVersionName)).thenReturn(null);
.thenReturn(null);
ApolloConfig ApolloConfig result = configController.queryConfig(someAppId, someClusterName, someGroupName,
result = someVersionName, someClientSideReleaseId, someResponse);
configController
.queryConfig(someAppId, someClusterName, someVersionName, someClientSideReleaseId,
someResponse);
assertNull(result); assertNull(result);
verify(someResponse, times(1)).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString()); verify(someResponse, times(1)).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
...@@ -118,25 +112,22 @@ public class ConfigControllerTest { ...@@ -118,25 +112,22 @@ public class ConfigControllerTest {
public void testQueryConfigWithApolloConfigNotModified() throws Exception { public void testQueryConfigWithApolloConfigNotModified() throws Exception {
String someAppId = "1"; String someAppId = "1";
String someClusterName = "someClusterName"; String someClusterName = "someClusterName";
String someGroupName = "someGroupName";
String someVersionName = "someVersion"; String someVersionName = "someVersion";
long someClientSideReleaseId = 1; long someClientSideReleaseId = 1;
long someServerSideReleaseId = someClientSideReleaseId; long someServerSideReleaseId = someClientSideReleaseId;
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, someVersionName))
.thenReturn(someVersion); .thenReturn(someRelease);
when(someVersion.getReleaseId()).thenReturn(someServerSideReleaseId); when(someRelease.getId()).thenReturn(someServerSideReleaseId);
ApolloConfig ApolloConfig result = configController.queryConfig(someAppId, someClusterName, someGroupName,
result = someVersionName, someClientSideReleaseId, someResponse);
configController
.queryConfig(someAppId, someClusterName, someVersionName, someClientSideReleaseId,
someResponse);
assertNull(result); assertNull(result);
verify(someResponse, times(1)).setStatus(HttpServletResponse.SC_NOT_MODIFIED); verify(someResponse, times(1)).setStatus(HttpServletResponse.SC_NOT_MODIFIED);
verify(configService, never()) verify(configService, never()).loadConfig(any(Release.class), anyString(), anyString());
.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