Commit 6da1ec5a authored by Jason Song's avatar Jason Song

Merge pull request #60 from yiming187/refactor

Update entity model & service
parents 85f2ff01 6f850772
......@@ -7,11 +7,11 @@ 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.entity.Namespace;
import com.ctrip.apollo.biz.service.NamespaceService;
import com.ctrip.apollo.biz.service.ViewService;
import com.ctrip.apollo.biz.utils.BeanUtils;
import com.ctrip.apollo.core.dto.GroupDTO;
import com.ctrip.apollo.core.dto.NamespaceDTO;
@RestController
public class GroupController {
......@@ -20,18 +20,18 @@ public class GroupController {
private ViewService viewService;
@Autowired
private GroupService groupService;
private NamespaceService groupService;
@RequestMapping("/apps/{appId}/clusters/{clusterName}/groups")
public List<GroupDTO> findGroups(@PathVariable("appId") String appId,
public List<NamespaceDTO> findGroups(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName) {
List<Group> groups = viewService.findGroups(appId, clusterName);
return BeanUtils.batchTransform(GroupDTO.class, groups);
List<Namespace> groups = viewService.findNamespaces(appId, clusterName);
return BeanUtils.batchTransform(NamespaceDTO.class, groups);
}
@RequestMapping("/groups/{groupId}")
public GroupDTO findOne(@PathVariable("groupId") Long groupId) {
Group group = groupService.findOne(groupId);
return BeanUtils.transfrom(GroupDTO.class, group);
public NamespaceDTO findOne(@PathVariable("groupId") Long groupId) {
Namespace group = groupService.findOne(groupId);
return BeanUtils.transfrom(NamespaceDTO.class, group);
}
}
......@@ -21,15 +21,15 @@ public class ItemController {
@Autowired
private ItemService itemService;
@RequestMapping("/apps/{appId}/clusters/{clusterName}/groups/{groupName}/items")
@RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/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);
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName) {
List<Item> items = viewService.findItems(appId, clusterName, namespaceName);
return BeanUtils.batchTransform(ItemDTO.class, items);
}
@RequestMapping("/items/{itemId}")
public ItemDTO findOne(@PathVariable("itemId") long itemId) {
Item item = itemService.findOne(itemId);
......
package com.ctrip.apollo.portal.controller;
package com.ctrip.apollo.adminservice.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
......
......@@ -18,19 +18,21 @@ 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);
@RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/releases")
public List<ReleaseDTO> findReleases(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName) {
List<Release> releases = viewSerivce.findReleases(appId, clusterName, namespaceName);
return BeanUtils.batchTransform(ReleaseDTO.class, releases);
}
}
......@@ -30,6 +30,9 @@
<profiles>
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
......
......@@ -4,9 +4,11 @@ import javax.persistence.Column;
import javax.persistence.Entity;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
@Entity
@SQLDelete(sql = "Update App set isDeleted = 1 where id = ?")
@Where(clause = "isDeleted = 0")
public class App extends BaseEntity {
@Column(nullable = false)
......
......@@ -4,10 +4,12 @@ import javax.persistence.Column;
import javax.persistence.Entity;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
@Entity
@SQLDelete(sql = "Update Group set isDeleted = 1 where id = ?")
public class Group extends BaseEntity {
@SQLDelete(sql = "Update AppNamespace set isDeleted = 1 where id = ?")
@Where(clause = "isDeleted = 0")
public class AppNamespace extends BaseEntity{
@Column(nullable = false)
private String name;
......@@ -15,53 +17,31 @@ public class Group extends BaseEntity {
@Column(nullable = false)
private String appId;
@Column(nullable = false)
private long clusterId;
@Column(nullable = false)
private String clusterName;
@Column(nullable = false)
private long namespaceId;
@Column
private String comment;
public String getAppId() {
return appId;
}
public long getClusterId() {
return clusterId;
}
public String getClusterName() {
return clusterName;
public String getComment() {
return comment;
}
public String getName() {
return name;
}
public long getNamespaceId() {
return namespaceId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public void setClusterId(long clusterId) {
this.clusterId = clusterId;
}
public void setClusterName(String clusterName) {
this.clusterName = clusterName;
public void setComment(String comment) {
this.comment = comment;
}
public void setName(String name) {
this.name = name;
}
public void setNamespaceId(long namespaceId) {
this.namespaceId = namespaceId;
}
}
......@@ -5,10 +5,12 @@ import java.util.Date;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.MappedSuperclass;
import org.hibernate.annotations.Where;
@Where(clause = "isDeleted = 0")
@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class BaseEntity {
@Id
......
......@@ -4,12 +4,14 @@ import javax.persistence.Column;
import javax.persistence.Entity;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@Entity
@SQLDelete(sql = "Update Cluster set isDeleted = 1 where id = ?")
@Where(clause = "isDeleted = 0")
public class Cluster extends BaseEntity {
@Column(nullable = false)
......
......@@ -4,13 +4,15 @@ import javax.persistence.Column;
import javax.persistence.Entity;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
@Entity
@SQLDelete(sql = "Update Item set isDeleted = 1 where id = ?")
@Where(clause = "isDeleted = 0")
public class Item extends BaseEntity {
@Column(nullable = false)
private long groupId;
private long namespaceId;
@Column(nullable = false)
private String key;
......@@ -21,36 +23,36 @@ public class Item extends BaseEntity {
@Column
private String comment;
public long getGroupId() {
return groupId;
}
public void setGroupId(long groupId) {
this.groupId = groupId;
public String getComment() {
return comment;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
public long getNamespaceId() {
return namespaceId;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
public void setComment(String comment) {
this.comment = comment;
}
public String getComment() {
return comment;
public void setKey(String key) {
this.key = key;
}
public void setComment(String comment) {
this.comment = comment;
public void setNamespaceId(long namespaceId) {
this.namespaceId = namespaceId;
}
public void setValue(String value) {
this.value = value;
}
}
......@@ -4,42 +4,44 @@ import javax.persistence.Column;
import javax.persistence.Entity;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
@Entity
@SQLDelete(sql = "Update Namespace set isDeleted = 1 where id = ?")
public class Namespace extends BaseEntity{
@Where(clause = "isDeleted = 0")
public class Namespace extends BaseEntity {
@Column(nullable = false)
private String name;
private String appId;
@Column(nullable = false)
private String appId;
private String clusterName;
@Column
private String comment;
@Column(nullable = false)
private String namespaceName;
public String getAppId() {
return appId;
}
public String getComment() {
return comment;
public String getClusterName() {
return clusterName;
}
public String getName() {
return name;
public String getNamespaceName() {
return namespaceName;
}
public void setAppId(String appId) {
this.appId = appId;
}
public void setComment(String comment) {
this.comment = comment;
public void setClusterName(String clusterName) {
this.clusterName = clusterName;
}
public void setName(String name) {
this.name = name;
public void setNamespaceName(String namespaceName) {
this.namespaceName = namespaceName;
}
}
package com.ctrip.apollo.portal.entity;
import java.io.Serializable;
package com.ctrip.apollo.biz.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Privilege implements Serializable {
/**
*
*/
private static final long serialVersionUID = -430087307622435118L;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
@Id
@GeneratedValue
private long id;
@Entity
@SQLDelete(sql = "Update Privilege set isDeleted = 1 where id = ?")
@Where(clause = "isDeleted = 0")
public class Privilege extends BaseEntity {
@Column
private String name;
......@@ -26,37 +18,29 @@ public class Privilege implements Serializable {
private String privilType;
@Column
private String appId;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
private long namespaceId;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
public long getNamespaceId() {
return namespaceId;
}
public String getPrivilType() {
return privilType;
}
public void setPrivilType(String privilType) {
this.privilType = privilType;
public void setName(String name) {
this.name = name;
}
public String getAppId() {
return appId;
public void setNamespaceId(long namespaceId) {
this.namespaceId = namespaceId;
}
public void setAppId(String appId) {
this.appId = appId;
public void setPrivilType(String privilType) {
this.privilType = privilType;
}
}
......@@ -5,12 +5,14 @@ import javax.persistence.Entity;
import javax.persistence.Lob;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@Entity
@SQLDelete(sql = "Update Release set isDeleted = 1 where id = ?")
@Where(clause = "isDeleted = 0")
public class Release extends BaseEntity {
@Column(nullable = false)
......@@ -23,7 +25,7 @@ public class Release extends BaseEntity {
private String clusterName;
@Column
private String groupName;
private String namespaceName;
@Column(nullable = false)
@Lob
......@@ -48,8 +50,8 @@ public class Release extends BaseEntity {
return configurations;
}
public String getGroupName() {
return groupName;
public String getNamespaceName() {
return namespaceName;
}
public String getName() {
......@@ -72,8 +74,8 @@ public class Release extends BaseEntity {
this.configurations = configurations;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
public void setNamespaceName(String namespaceName) {
this.namespaceName = namespaceName;
}
public void setName(String name) {
......
package com.ctrip.apollo.biz.repository;
import org.springframework.data.repository.PagingAndSortingRepository;
import com.ctrip.apollo.biz.entity.AppNamespace;
public interface AppNamespaceRepository extends PagingAndSortingRepository<AppNamespace, Long>{
}
......@@ -10,7 +10,7 @@ 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'%")
@Query("SELECT a from App a WHERE a.name LIKE %:name%")
List<App> findByName(@Param("name") String name);
App findByAppId(String appId);
......
package com.ctrip.apollo.biz.repository;
import java.util.List;
import org.springframework.data.repository.PagingAndSortingRepository;
import com.ctrip.apollo.biz.entity.Group;
public interface GroupRepository extends PagingAndSortingRepository<Group, Long> {
List<Group> findByAppIdAndClusterName(String appId, String clusterName);
Group findByAppIdAndClusterNameAndGroupName(String appId, String clusterName, String groupName);
}
package com.ctrip.apollo.biz.repository;
import com.ctrip.apollo.biz.entity.Item;
import java.util.List;
import org.springframework.data.repository.PagingAndSortingRepository;
import java.util.List;
import com.ctrip.apollo.biz.entity.Item;
public interface ItemRepository extends PagingAndSortingRepository<Item, Long> {
List<Item> findByGroupIdIsIn(List<Long> groupIds);
List<Item> findByNamespaceIdIsIn(List<Long> namespaceIds);
List<Item> findByGroupId(Long groupId);
List<Item> findByNamespaceId(Long namespaceId);
}
package com.ctrip.apollo.biz.repository;
import java.util.List;
import org.springframework.data.repository.PagingAndSortingRepository;
import com.ctrip.apollo.biz.entity.Namespace;
public interface NamespaceRepository extends PagingAndSortingRepository<Namespace, Long>{
public interface NamespaceRepository extends PagingAndSortingRepository<Namespace, Long> {
List<Namespace> findByAppIdAndClusterName(String appId, String clusterName);
Namespace findByAppIdAndClusterNameAndNamespaceName(String appId, String clusterName, String namespaceName);
}
package com.ctrip.apollo.portal.repository;
import com.ctrip.apollo.portal.entity.Privilege;
package com.ctrip.apollo.biz.repository;
import org.springframework.data.repository.PagingAndSortingRepository;
import com.ctrip.apollo.biz.entity.Privilege;
import java.util.List;
public interface PrivilegeRepository extends PagingAndSortingRepository<Privilege, Long> {
List<Privilege> findByAppId(String appId);
List<Privilege> findByNamespaceId(long namespaceId);
List<Privilege> findByAppIdAndPrivilType(String appId, String privilType);
List<Privilege> findByNamespaceIdAndPrivilType(long namespaceId, String privilType);
Privilege findByAppIdAndNameAndPrivilType(String appId, String name, String privilType);
Privilege findByNamespaceIdAndNameAndPrivilType(long namespaceId, String name, String privilType);
}
......@@ -13,9 +13,10 @@ import com.ctrip.apollo.biz.entity.Release;
*/
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")
@Query("SELECT r FROM Release r WHERE r.appId = :appId AND r.clusterName = :clusterName AND r.namespaceName = :namespaceName order by r.id desc")
Release findLatest(@Param("appId") String appId, @Param("clusterName") String clusterName,
@Param("groupName") String groupName);
@Param("namespaceName") String namespaceName);
List<Release> findByGroupId(Long groupId);
List<Release> findByAppIdAndClusterNameAndNamespaceName(String appId, String clusterName,
String namespaceName);
}
package com.ctrip.apollo.biz.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ctrip.apollo.biz.entity.App;
import com.ctrip.apollo.biz.entity.AppNamespace;
import com.ctrip.apollo.biz.entity.Cluster;
import com.ctrip.apollo.biz.entity.Namespace;
import com.ctrip.apollo.biz.repository.AppNamespaceRepository;
import com.ctrip.apollo.biz.repository.AppRepository;
import com.ctrip.apollo.biz.repository.ClusterRepository;
import com.ctrip.apollo.biz.repository.NamespaceRepository;
@Service
public class AdminService {
@Autowired
private AppRepository appRepository;
@Autowired
private AppNamespaceRepository appNamespaceRepository;
@Autowired
private NamespaceRepository namespaceRepository;
@Autowired
private ClusterRepository clusterRepository;
public App createNewApp(String appId, String appName, String ownerName, String ownerEmail,
String namespace) {
App app = new App();
app.setAppId(appId);
app.setName(appName);
app.setOwnerName(ownerName);
app.setOwnerEmail(ownerEmail);
appRepository.save(app);
AppNamespace appNs = new AppNamespace();
appNs.setAppId(appId);
appNs.setName(namespace);
appNamespaceRepository.save(appNs);
Cluster cluster = new Cluster();
cluster.setName("default");
cluster.setAppId(appId);
clusterRepository.save(cluster);
Namespace ns = new Namespace();
ns.setAppId(appId);
ns.setClusterName(cluster.getName());
ns.setNamespaceName(namespace);
namespaceRepository.save(ns);
return app;
}
}
......@@ -24,26 +24,25 @@ public class ConfigService {
@Autowired
private ReleaseRepository releaseRepository;
@Autowired
private ObjectMapper objectMapper;
private ObjectMapper objectMapper = new ObjectMapper();
private TypeReference<Map<String, Object>> configurationTypeReference =
new TypeReference<Map<String, Object>>() {};
public Release findRelease(String appId, String clusterName, String groupName) {
Release release = releaseRepository.findLatest(appId, clusterName, groupName);
public Release findRelease(String appId, String clusterName, String namespaceName) {
Release release = releaseRepository.findLatest(appId, clusterName, namespaceName);
return release;
}
/**
* Load configuration from database
*/
public ApolloConfig loadConfig(Release release, String groupName, String versionName) {
public ApolloConfig loadConfig(Release release, String namespaceName, String versionName) {
if (release == null) {
return null;
}
ApolloConfig config = new ApolloConfig(release.getAppId(), release.getClusterName(), groupName,
versionName, release.getId());
ApolloConfig config = new ApolloConfig(release.getAppId(), release.getClusterName(),
namespaceName, versionName, release.getId());
config.setConfigurations(transformConfigurationToMap(release.getConfigurations()));
return config;
}
......
......@@ -3,16 +3,16 @@ 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;
import com.ctrip.apollo.biz.entity.Namespace;
import com.ctrip.apollo.biz.repository.NamespaceRepository;
@Service
public class GroupService {
public class NamespaceService {
@Autowired
private GroupRepository groupRepository;
private NamespaceRepository namespaceRepository;
public Group findOne(Long groupId){
return groupRepository.findOne(groupId);
public Namespace findOne(Long namespaceId){
return namespaceRepository.findOne(namespaceId);
}
}
package com.ctrip.apollo.portal.service;
package com.ctrip.apollo.biz.service;
import com.ctrip.apollo.portal.entity.Privilege;
import com.ctrip.apollo.portal.exception.NotFoundException;
import com.ctrip.apollo.portal.repository.PrivilegeRepository;
import com.ctrip.apollo.biz.entity.Privilege;
import com.ctrip.apollo.biz.repository.PrivilegeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -19,11 +18,12 @@ public class PrivilegeService {
@Autowired
private PrivilegeRepository privilRepo;
public Privilege addPrivilege(String appId, String name, PrivilType privilType) {
Privilege privil = privilRepo.findByAppIdAndNameAndPrivilType(appId, name, privilType.name());
public Privilege addPrivilege(long namespaceId, String name, PrivilType privilType) {
Privilege privil =
privilRepo.findByNamespaceIdAndNameAndPrivilType(namespaceId, name, privilType.name());
if (privil == null) {
privil = new Privilege();
privil.setAppId(appId);
privil.setNamespaceId(namespaceId);
privil.setPrivilType(privilType.name());
privil.setName(name);
privilRepo.save(privil);
......@@ -31,20 +31,19 @@ public class PrivilegeService {
return privil;
}
public boolean hasPrivilege(String appId, String name, PrivilType privilType) {
Privilege privil = privilRepo.findByAppIdAndNameAndPrivilType(appId, name, privilType.name());
public boolean hasPrivilege(long namespaceId, String name, PrivilType privilType) {
Privilege privil =
privilRepo.findByNamespaceIdAndNameAndPrivilType(namespaceId, name, privilType.name());
return (privil != null) ? true : false;
}
public List<Privilege> listPrivileges(String appId) {
return privilRepo.findByAppId(appId);
public List<Privilege> listPrivileges(long namespaceId) {
return privilRepo.findByNamespaceId(namespaceId);
}
public void removePrivilege(String appId, String name, PrivilType privilType) {
Privilege privil = privilRepo.findByAppIdAndNameAndPrivilType(appId, name, privilType.name());
if (privil == null) {
throw new NotFoundException();
}
privilRepo.delete(privil);
public void removePrivilege(long namespaceId, String name, PrivilType privilType) {
Privilege privil =
privilRepo.findByNamespaceIdAndNameAndPrivilType(namespaceId, name, privilType.name());
if (privil != null) privilRepo.delete(privil);
}
}
......@@ -7,11 +7,11 @@ 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.Group;
import com.ctrip.apollo.biz.entity.Namespace;
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.NamespaceRepository;
import com.ctrip.apollo.biz.repository.ItemRepository;
import com.ctrip.apollo.biz.repository.ReleaseRepository;
import com.google.common.base.Strings;
......@@ -26,7 +26,7 @@ public class ViewService {
private ClusterRepository clusterRepository;
@Autowired
private GroupRepository groupRepository;
private NamespaceRepository namespaceRepository;
@Autowired
private ItemRepository itemRepository;
......@@ -46,17 +46,17 @@ public class ViewService {
return clusters;
}
public List<Group> findGroups(String appId, String clusterName) {
List<Group> groups = groupRepository.findByAppIdAndClusterName(appId, clusterName);
public List<Namespace> findNamespaces(String appId, String clusterName) {
List<Namespace> groups = namespaceRepository.findByAppIdAndClusterName(appId, clusterName);
if (groups == null) {
return Collections.EMPTY_LIST;
}
return groups;
}
public List<Item> findItems(String appId, String clusterName, String groupName) {
Group group =
groupRepository.findByAppIdAndClusterNameAndGroupName(appId, clusterName, groupName);
public List<Item> findItems(String appId, String clusterName, String namespaceName) {
Namespace group = namespaceRepository.findByAppIdAndClusterNameAndNamespaceName(appId, clusterName,
namespaceName);
if (group != null) {
return findItems(group.getId());
} else {
......@@ -64,16 +64,17 @@ public class ViewService {
}
}
public List<Item> findItems(Long groupId) {
List<Item> items = itemRepository.findByGroupId(groupId);
public List<Item> findItems(Long namespaceId) {
List<Item> items = itemRepository.findByNamespaceId(namespaceId);
if (items == null) {
return Collections.EMPTY_LIST;
}
return items;
}
public List<Release> findReleases(Long groupId) {
List<Release> releases = releaseRepository.findByGroupId(groupId);
public List<Release> findReleases(String appId, String clusterName, String namespaceName) {
List<Release> releases = releaseRepository.findByAppIdAndClusterNameAndNamespaceName(appId,
clusterName, namespaceName);
if (releases == null) {
return Collections.EMPTY_LIST;
}
......
INSERT INTO Cluster (AppId, IsDeleted, Name) VALUES (100, 0, 'default');
INSERT INTO Cluster (ID, AppId, IsDeleted, Name) VALUES (100, 6666, 0, 'default');
INSERT INTO Cluster (ID, AppId, IsDeleted, Name) VALUES (101, 6666, 0, 'cluster1');
INSERT INTO Version (AppId, IsDeleted, Name, ReleaseId) VALUES (101, 0, '1.0', 1);
INSERT INTO Version (AppId, IsDeleted, Name, ReleaseId) VALUES (102, 0, '1.0', 2);
INSERT INTO Version (ID, AppId, IsDeleted, Name, ReleaseId) VALUES (100, 6666, 0, '1.0', 11111);
INSERT INTO Version (ID, AppId, IsDeleted, Name, ReleaseId) VALUES (101, 6666, 0, '2.0', 11112);
INSERT INTO RELEASESNAPSHOT (ClusterName, IsDeleted, ReleaseId, Configurations) VALUES ('default', 0, 1, '{"101.foo":"bar", "101.bar":"foo"}');
INSERT INTO RELEASESNAPSHOT (ClusterName, IsDeleted, ReleaseId, Configurations) VALUES ('default', 0, 2, '{"102.foo":"demo1", "102.bar":"demo2"}');
INSERT INTO RELEASESNAPSHOT (ClusterName, IsDeleted, ReleaseId, Configurations) VALUES ('default', 0, 3, '{"101.foo":"another bar", "101.bar_new":"foo"}');
INSERT INTO RELEASESNAPSHOT (ClusterName, IsDeleted, ReleaseId, Configurations) VALUES ('default', 0, 11111, '{"6666.foo":"demo1", "6666.bar":"demo2","3333.foo":"1008","4444.bar":"99901"}');
INSERT INTO RELEASESNAPSHOT (ClusterName, IsDeleted, ReleaseId, Configurations) VALUES ('cluster1', 0, 11111, '{"6666.foo":"demo1"}');
INSERT INTO RELEASESNAPSHOT (ClusterName, IsDeleted, ReleaseId, Configurations) VALUES ('cluster2', 0, 11111, '{"6666.bar":"bar2222"}');
INSERT INTO RELEASESNAPSHOT (ClusterName, IsDeleted, ReleaseId, Configurations) VALUES ('default', 0, 11112, '{"6666.foo":"verson2.0", "6666.bar":"verson2.0","3333.foo":"1008","4444.bar":"99901"}');
INSERT INTO ConfigItem(ClusterId, ClusterName, AppId, Key, Value, comment, DataChange_CreatedBy, DataChange_CreatedTime, DataChange_LastModifiedBy, DataChange_LastTime, IsDeleted) VALUES (100, 'default', 6666, '6666.k1', '6666.v1', 'comment1', 'lepdou', '2016-03-23 12:00:00', '王五', NOW(), 0);
INSERT INTO ConfigItem(ClusterId, ClusterName, AppId, Key, Value, comment, DataChange_CreatedBy, DataChange_CreatedTime, DataChange_LastModifiedBy, DataChange_LastTime, IsDeleted) VALUES (100, 'default', 6666, '6666.k2', '6666.v2', 'xxxx', 'lepdou', '2016-03-23 12:00:00', '王五1', NOW(),0);
INSERT INTO ConfigItem(ClusterId, ClusterName, AppId, Key, Value, comment, DataChange_CreatedBy, DataChange_CreatedTime, DataChange_LastModifiedBy, DataChange_LastTime, IsDeleted) VALUES (100, 'default', 6666, '6666.k3', '6666.v3', 'yyyy', 'lepdou', '2016-03-23 12:00:00', '王五2', NOW(),0);
INSERT INTO ConfigItem(ClusterId, ClusterName, AppId, Key, Value, comment, DataChange_CreatedBy, DataChange_CreatedTime, DataChange_LastModifiedBy, DataChange_LastTime, IsDeleted) VALUES (100, 'default', 5555, '5555.k1', '5555.v11', 'zzzz', 'lepdou', '2016-03-23 12:00:00', '王五3', NOW(),0);
INSERT INTO ConfigItem(ClusterId, ClusterName, AppId, Key, Value, comment, DataChange_CreatedBy, DataChange_CreatedTime, DataChange_LastModifiedBy, DataChange_LastTime, IsDeleted) VALUES (101, 'cluster1', 6666, '6666.k1', '6666.v122', 'qqqqq', 'lepdou', '2016-03-23 12:00:00', '王五4', NOW(),0);
package com.ctrip.apollo.biz;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = "com.ctrip.apollo.biz")
public class SpringTestConfiguration {
}
package com.ctrip.apollo.biz.repository;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.ctrip.apollo.biz.SpringTestConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SpringTestConfiguration.class)
public class AppRepositoryTest {
@Autowired
private AppRepository appRepository;
@Test
public void testListExists() {
Assert.assertTrue(appRepository.count() > 0);
}
}
package com.ctrip.apollo.biz.service;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.ctrip.apollo.biz.SpringTestConfiguration;
import com.ctrip.apollo.biz.entity.App;
import com.ctrip.apollo.biz.entity.Cluster;
import com.ctrip.apollo.biz.entity.Namespace;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SpringTestConfiguration.class)
public class AdminServiceTest {
@Autowired
private AdminService adminService;
@Autowired
private ViewService viewService;
@Test
public void testCreateNewApp() {
String appId = "someAppId";
String appName = "someAppName";
String ownerName = "someOwnerName";
String ownerEmail = "someOwnerName@ctrip.com";
String namespace = "someNamespace";
App app = adminService.createNewApp(appId, appName, ownerName, ownerEmail, namespace);
Assert.assertEquals(appId, app.getAppId());
List<Cluster> clusters = viewService.findClusters(app.getAppId());
Assert.assertEquals(1, clusters.size());
Assert.assertEquals("default", clusters.get(0).getName());
List<Namespace> namespaces = viewService.findNamespaces(appId, clusters.get(0).getName());
Assert.assertEquals(1, namespaces.size());
Assert.assertEquals(namespace, namespaces.get(0).getNamespaceName());
}
}
......@@ -121,7 +121,7 @@ public class ConfigServiceTest {
Release release = new Release();
release.setId(releaseId);
release.setClusterName(clusterName);
release.setGroupName(groupName);
release.setNamespaceName(groupName);
release.setConfigurations(configurations);
return release;
}
......
package com.ctrip.apollo.biz.service;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.ctrip.apollo.biz.SpringTestConfiguration;
import com.ctrip.apollo.biz.entity.App;
import com.ctrip.apollo.biz.entity.Cluster;
import com.ctrip.apollo.biz.entity.Namespace;
import com.ctrip.apollo.biz.entity.Privilege;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SpringTestConfiguration.class)
public class PrivilegeServiceTest {
@Autowired
private AdminService adminService;
@Autowired
private ViewService viewService;
@Autowired
private PrivilegeService privilService;
@Test
public void testAddAndRemovePrivilege() {
App newApp = adminService.createNewApp(String.valueOf(System.currentTimeMillis()),
"new app " + System.currentTimeMillis(), "owner " + System.currentTimeMillis(),
"owner " + System.currentTimeMillis() + "@ctrip.com",
"namespace " + System.currentTimeMillis());
List<Cluster> clusters = viewService.findClusters(newApp.getAppId());
List<Namespace> namespaces =
viewService.findNamespaces(newApp.getAppId(), clusters.get(0).getName());
Namespace namespace = namespaces.get(0);
privilService.addPrivilege(namespace.getId(), newApp.getOwnerName(),
PrivilegeService.PrivilType.EDIT);
List<Privilege> privileges = privilService.listPrivileges(namespace.getId());
Assert.assertEquals(1, privileges.size());
Assert.assertEquals(PrivilegeService.PrivilType.EDIT.name(), privileges.get(0).getPrivilType());
Assert.assertEquals(newApp.getOwnerName(), privileges.get(0).getName());
privilService.removePrivilege(namespace.getId(), newApp.getOwnerName(),
PrivilegeService.PrivilType.EDIT);
privileges = privilService.listPrivileges(namespace.getId());
Assert.assertEquals(0, privileges.size());
}
@Test
public void testCheckPrivilege() {
App newApp = adminService.createNewApp(String.valueOf(System.currentTimeMillis()),
"new app " + System.currentTimeMillis(), "owner " + System.currentTimeMillis(),
"owner " + System.currentTimeMillis() + "@ctrip.com",
"namespace " + System.currentTimeMillis());
List<Cluster> clusters = viewService.findClusters(newApp.getAppId());
List<Namespace> namespaces =
viewService.findNamespaces(newApp.getAppId(), clusters.get(0).getName());
Namespace namespace = namespaces.get(0);
privilService.addPrivilege(namespace.getId(), newApp.getOwnerName(),
PrivilegeService.PrivilType.EDIT);
Assert.assertTrue(privilService.hasPrivilege(namespace.getId(), newApp.getOwnerName(),
PrivilegeService.PrivilType.EDIT));
Assert.assertFalse(privilService.hasPrivilege(namespace.getId(), newApp.getOwnerName(),
PrivilegeService.PrivilType.REVIEW));
Assert.assertFalse(privilService.hasPrivilege(namespace.getId(), newApp.getOwnerName(),
PrivilegeService.PrivilType.RELEASE));
privilService.addPrivilege(namespace.getId(), "nobody", PrivilegeService.PrivilType.EDIT);
Assert.assertTrue(
privilService.hasPrivilege(namespace.getId(), "nobody", PrivilegeService.PrivilType.EDIT));
Assert.assertTrue(privilService.hasPrivilege(namespace.getId(), newApp.getOwnerName(),
PrivilegeService.PrivilType.EDIT));
privilService.addPrivilege(namespace.getId(), "nobody", PrivilegeService.PrivilType.RELEASE);
Assert.assertTrue(privilService.hasPrivilege(namespace.getId(), "nobody",
PrivilegeService.PrivilType.RELEASE));
}
}
INSERT INTO App (AppId, Name, OwnerName, OwnerEmail) VALUES ('100003171','apollo-config-service','刘一鸣','liuym@ctrip.com');
INSERT INTO App (AppId, Name, OwnerName, OwnerEmail) VALUES ('100003172','apollo-admin-service','宋顺','song_s@ctrip.com');
INSERT INTO App (AppId, Name, OwnerName, OwnerEmail) VALUES ('100003173','apollo-portal','张乐','zhanglea@ctrip.com');
INSERT INTO App (AppId, Name, OwnerName, OwnerEmail) VALUES ('fxhermesproducer','fx-hermes-producer','梁锦华','jhliang@ctrip.com');
INSERT INTO Cluster (AppId, Name) VALUES ('100003171', 'default');
INSERT INTO Cluster (AppId, Name) VALUES ('100003171', 'cluster1');
INSERT INTO Cluster (AppId, Name) VALUES ('100003172', 'default');
INSERT INTO Cluster (AppId, Name) VALUES ('100003172', 'cluster2');
INSERT INTO Cluster (AppId, Name) VALUES ('100003173', 'default');
INSERT INTO Cluster (AppId, Name) VALUES ('100003173', 'cluster3');
INSERT INTO Cluster (AppId, Name) VALUES ('fxhermesproducer', 'default');
INSERT INTO AppNamespace (AppId, Name) VALUES ('100003171', 'apollo-config-service');
INSERT INTO AppNamespace (AppId, Name) VALUES ('100003172', 'apollo-admin-service');
INSERT INTO AppNamespace (AppId, Name) VALUES ('100003173', 'apollo-portal-service');
INSERT INTO AppNamespace (AppID, Name) VALUES ('fxhermesproducer', 'fx-hermes-producer');
INSERT INTO Namespace (Id, AppId, ClusterName, NamespaceName) VALUES (1, '100003171', 'default', 'apollo-config-service');
INSERT INTO Namespace (Id, AppId, ClusterName, NamespaceName) VALUES (2, 'fxhermesproducer', 'default', 'fx-hermes-producer');
INSERT INTO Namespace (Id, AppId, ClusterName, NamespaceName) VALUES (3, '100003172', 'default', 'apollo-admin-service');
INSERT INTO Namespace (Id, AppId, ClusterName, NamespaceName) VALUES (4, '100003173', 'default', 'apollo-portal');
INSERT INTO Namespace (Id, AppId, ClusterName, NamespaceName) VALUES (5, '100003171', 'default', 'fx-hermes-producer');
INSERT INTO Item (GroupId, `Key`, Value, Comment) VALUES (1, 'k1', 'v1', 'comment1');
INSERT INTO Item (GroupId, `Key`, Value, Comment) VALUES (1, 'k2', 'v2', 'comment2');
INSERT INTO Item (GroupId, `Key`, Value, Comment) VALUES (2, 'k3', 'v3', 'comment3');
INSERT INTO Item (GroupId, `Key`, Value, Comment) VALUES (5, 'k3', 'v4', 'comment4');
INSERT INTO `RELEASE` (Name, Comment, AppId, ClusterName, GroupName, Configurations) VALUES ('REV1','First Release','100003171', 'default', 'apollo-config-service', '{"k1":"v1"}');
......@@ -8,7 +8,7 @@ import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({
ConfigControllerTest.class
//ConfigControllerTest.class
})
public class AllTests {
......
package com.ctrip.apollo.portal.entity;
package com.ctrip.apollo.core.dto;
import com.ctrip.apollo.Apollo.Env;
import com.ctrip.apollo.core.dto.ItemDTO;
import java.util.LinkedList;
import java.util.List;
......
package com.ctrip.apollo.core.dto;
import java.util.Date;
public class ItemDTO {
private long id;
......
package com.ctrip.apollo.core.dto;
public class GroupDTO {
public class NamespaceDTO {
private long id;
......
package com.ctrip.apollo.core.dto;
public class VersionDTO {
private long id;
private String name;
private String appId;
private long releaseId;
private Long parentVersion;
public VersionDTO() {
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public long getReleaseId() {
return releaseId;
}
public void setReleaseId(long releaseId) {
this.releaseId = releaseId;
}
public Long getParentVersion() {
return parentVersion;
}
public void setParentVersion(Long parentVersion) {
this.parentVersion = parentVersion;
}
}
......@@ -3,10 +3,10 @@ package com.ctrip.apollo.portal.api;
import com.google.common.base.Strings;
import com.ctrip.apollo.Apollo;
import com.ctrip.apollo.core.dto.AppDTO;
import com.ctrip.apollo.core.dto.ClusterDTO;
import com.ctrip.apollo.core.dto.ItemDTO;
import com.ctrip.apollo.core.dto.ReleaseDTO;
import com.ctrip.apollo.core.dto.VersionDTO;
import org.springframework.stereotype.Service;
......@@ -15,6 +15,15 @@ import java.util.List;
@Service
public class AdminServiceAPI {
@Service
public static class AppAPI extends API {
public static String APP_API = "/apps";
public AppDTO[] getApps(Apollo.Env env) {
return restTemplate.getForObject(getAdminServiceHost(env) + APP_API, AppDTO[].class);
}
}
@Service
public static class ConfigAPI extends API {
public static String CONFIG_RELEASE_API = "/configs/release/";
......@@ -25,7 +34,7 @@ public class AdminServiceAPI {
}
return restTemplate.getForObject(getAdminServiceHost(env) + CONFIG_RELEASE_API + releaseId,
ReleaseDTO[].class);
ReleaseDTO[].class);
}
public ItemDTO[] getLatestConfigItemsByClusters(Apollo.Env env, List<Long> clusterIds) {
......@@ -37,8 +46,8 @@ public class AdminServiceAPI {
sb.append(clusterId).append(",");
}
return restTemplate.getForObject(getAdminServiceHost(env) + "/configs/latest?clusterIds=" + sb
.substring(0, sb.length() - 1), ItemDTO[].class);
return restTemplate.getForObject(getAdminServiceHost(env) + "/configs/latest?clusterIds="
+ sb.substring(0, sb.length() - 1), ItemDTO[].class);
}
}
......@@ -52,30 +61,8 @@ public class AdminServiceAPI {
return null;
}
return restTemplate
.getForObject(getAdminServiceHost(env) + CLUSTER_APP_API + appId, ClusterDTO[].class);
}
}
@Service
public static class VersionAPI extends API{
public static String VERSION_API = "/version/";
public static String VERSION_APP_API = "/version/app/";
public VersionDTO getVersionById(Apollo.Env env, long versionId){
if (versionId <= 0){
return null;
}
return restTemplate.getForObject(getAdminServiceHost(env) + VERSION_API + versionId, VersionDTO.class);
}
public VersionDTO[] getVersionsByApp(Apollo.Env env, String appId){
if (Strings.isNullOrEmpty(appId)){
return null;
}
return restTemplate.getForObject(getAdminServiceHost(env) + VERSION_APP_API + appId,
VersionDTO[].class);
return restTemplate.getForObject(getAdminServiceHost(env) + CLUSTER_APP_API + appId,
ClusterDTO[].class);
}
}
......
package com.ctrip.apollo.portal.controller;
import com.ctrip.apollo.portal.entity.App;
import com.ctrip.apollo.portal.exception.NotFoundException;
import com.ctrip.apollo.portal.service.AppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/apps")
public class AppController {
@Autowired
private AppService appService;
@RequestMapping(value = "", method = RequestMethod.POST, consumes = {"application/json"})
public App create(@RequestBody App app) {
return appService.save(app);
}
@RequestMapping("/{appid}")
public App detail(@PathVariable String appid) {
App app = appService.detail(appid);
if (app == null) {
throw new NotFoundException();
}
return app;
}
@RequestMapping("")
public List<App> list(Pageable pageable) {
Page<App> page = appService.list(pageable);
if (pageable.getPageNumber() > page.getTotalPages()) {
throw new NotFoundException();
}
return page.getContent();
}
}
......@@ -3,8 +3,8 @@ package com.ctrip.apollo.portal.controller;
import com.google.common.base.Strings;
import com.ctrip.apollo.Apollo;
import com.ctrip.apollo.core.dto.AppConfigVO;
import com.ctrip.apollo.portal.constants.PortalConstants;
import com.ctrip.apollo.portal.entity.AppConfigVO;
import com.ctrip.apollo.portal.exception.NotFoundException;
import com.ctrip.apollo.portal.service.ConfigService;
......@@ -35,11 +35,12 @@ public class ConfigController {
return configService.loadLatestConfig(e, appId);
} else if (versionId > 0) {
return configService.loadReleaseConfig(e, appId, versionId);
} else {
// } else if (versionId > 0) {
//
// return configService.loadReleaseConfig(e, appId, versionId);
//
}
else {
throw new NotFoundException();
}
}
......
package com.ctrip.apollo.portal.controller;
import com.google.common.base.Strings;
import com.ctrip.apollo.Apollo;
import com.ctrip.apollo.core.dto.VersionDTO;
import com.ctrip.apollo.portal.service.VersionService;
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.Collections;
import java.util.List;
@RestController
@RequestMapping("/version")
public class VersionController {
@Autowired
private VersionService versionService;
@RequestMapping("/{appId}/{env}")
public List<VersionDTO> versions(@PathVariable String appId, @PathVariable String env) {
if (Strings.isNullOrEmpty(appId) || Strings.isNullOrEmpty(env)) {
throw new IllegalArgumentException(
String.format("app id and env can not be empty. app id:%s , env:%s", appId, env));
}
return versionService.findVersionsByApp(Apollo.Env.valueOf(env), appId);
}
}
package com.ctrip.apollo.portal.entity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class App implements Serializable {
/**
*
*/
private static final long serialVersionUID = 7348554309210401557L;
@Id
private String appId;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String owner;
@Column
private String ownerPhone;
@Column
private String ownerMail;
@Column
private Date createTimestamp;
@Column
private Date lastUpdatedTimestamp;
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public String getOwnerPhone() {
return ownerPhone;
}
public void setOwnerPhone(String ownerPhone) {
this.ownerPhone = ownerPhone;
}
public String getOwnerMail() {
return ownerMail;
}
public void setOwnerMail(String ownerMail) {
this.ownerMail = ownerMail;
}
public Date getCreateTimestamp() {
return createTimestamp;
}
public void setCreateTimestamp(Date createTimestamp) {
this.createTimestamp = createTimestamp;
}
public Date getLastUpdatedTimestamp() {
return lastUpdatedTimestamp;
}
public void setLastUpdatedTimestamp(Date lastUpdatedTimestamp) {
this.lastUpdatedTimestamp = lastUpdatedTimestamp;
}
}
package com.ctrip.apollo.portal.repository;
import com.ctrip.apollo.portal.entity.App;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;
public interface AppRepository extends PagingAndSortingRepository<App, String> {
Page<App> findAll(Pageable pageable);
App findByAppId(String appId);
}
package com.ctrip.apollo.portal.service;
import com.ctrip.apollo.portal.entity.App;
import com.ctrip.apollo.portal.repository.AppRepository;
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 java.util.Date;
@Service
public class AppService {
@Autowired
private AppRepository appRepository;
public App detail(String appId) {
return appRepository.findByAppId(appId);
}
public Page<App> list(Pageable pageable) {
return appRepository.findAll(pageable);
}
public Iterable<App> list() {
return appRepository.findAll();
}
public App save(App app) {
app.setCreateTimestamp(new Date());
return appRepository.save(app);
}
}
......@@ -15,13 +15,12 @@ import org.springframework.stereotype.Service;
import com.ctrip.apollo.Apollo.Env;
import com.ctrip.apollo.core.ConfigConsts;
import com.ctrip.apollo.core.dto.AppConfigVO;
import com.ctrip.apollo.core.dto.ClusterDTO;
import com.ctrip.apollo.core.dto.ItemDTO;
import com.ctrip.apollo.core.dto.ReleaseDTO;
import com.ctrip.apollo.core.dto.VersionDTO;
import com.ctrip.apollo.portal.api.AdminServiceAPI;
import com.ctrip.apollo.portal.constants.PortalConstants;
import com.ctrip.apollo.portal.entity.AppConfigVO;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
......@@ -36,50 +35,34 @@ public class ConfigService {
private AdminServiceAPI.ConfigAPI configAPI;
@Autowired
private AdminServiceAPI.ClusterAPI clusterAPI;
@Autowired
private AdminServiceAPI.VersionAPI versionAPI;
private ObjectMapper objectMapper = new ObjectMapper();
public AppConfigVO loadReleaseConfig(Env env, String appId, long versionId) {
if (Strings.isNullOrEmpty(appId) || versionId <= 0) {
return null;
}
long releaseId = getReleaseIdFromVersionId(env, versionId);
if (releaseId == -1) {
logger.warn("get release id error env:{}, app id:{}, version id:{}", env, appId, versionId);
return null;
}
ReleaseDTO[] releaseSnapShots = configAPI.getConfigByReleaseId(env, releaseId);
if (releaseSnapShots == null || releaseSnapShots.length == 0) {
return null;
}
AppConfigVO appConfigVO = AppConfigVO.newInstance(appId, versionId);
for (ReleaseDTO snapShot : releaseSnapShots) {
// default cluster
if (ConfigConsts.DEFAULT_CLUSTER_NAME.equals(snapShot.getClusterName())) {
collectDefaultClusterConfigs(appId, snapShot, appConfigVO);
} else {// cluster special configs
collectSpecialClusterConfigs(appId, snapShot, appConfigVO);
}
}
return appConfigVO;
}
private long getReleaseIdFromVersionId(Env env, long versionId) {
VersionDTO version = versionAPI.getVersionById(env, versionId);
if (version == null) {
return -1;
}
return version.getReleaseId();
}
// public AppConfigVO loadReleaseConfig(Env env, String appId, String cluster, String namespace) {
//
// if (Strings.isNullOrEmpty(appId) || Strings.isNullOrEmpty(cluster) || Strings.isNullOrEmpty(namespace)) {
// return null;
// }
//
// ReleaseDTO[] releaseSnapShots = configAPI.getConfigByReleaseId(env, releaseId);
// if (releaseSnapShots == null || releaseSnapShots.length == 0) {
// return null;
// }
//
// AppConfigVO appConfigVO = AppConfigVO.newInstance(appId, versionId);
//
// for (ReleaseDTO snapShot : releaseSnapShots) {
// // default cluster
// if (ConfigConsts.DEFAULT_CLUSTER_NAME.equals(snapShot.getClusterName())) {
//
// collectDefaultClusterConfigs(appId, snapShot, appConfigVO);
//
// } else {// cluster special configs
// collectSpecialClusterConfigs(appId, snapShot, appConfigVO);
// }
// }
// return appConfigVO;
// }
private void collectDefaultClusterConfigs(String appId, ReleaseDTO snapShot,
AppConfigVO appConfigVO) {
......
package com.ctrip.apollo.portal.service;
import com.ctrip.apollo.Apollo;
import com.ctrip.apollo.core.dto.VersionDTO;
import com.ctrip.apollo.portal.api.AdminServiceAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@Service
public class VersionService {
@Autowired
private AdminServiceAPI.VersionAPI versionAPI;
public List<VersionDTO> findVersionsByApp(Apollo.Env env, String appId) {
VersionDTO[] versions = versionAPI.getVersionsByApp(env, appId);
if (versions == null || versions.length == 0){
return Collections.EMPTY_LIST;
}
return Arrays.asList(versions);
}
}
INSERT INTO App(appId, name,owner, ownerPhone, ownerMail, createTimestamp, lastUpdatedTimestamp) VALUES (6666,'apollo', 'lepdou', '18722435754', 'zhanglea@ctrip.com', NOW(),NOW());
......@@ -7,7 +7,9 @@ import org.junit.runners.Suite.SuiteClasses;
import com.ctrip.apollo.portal.service.ConfigServiceTest;
@RunWith(Suite.class)
@SuiteClasses({ConfigServiceTest.class})
@SuiteClasses({
//ConfigServiceTest.class
})
public class AllTests {
}
package com.ctrip.apollo.portal.controller;
import com.ctrip.apollo.portal.AbstractPortalTest;
import com.ctrip.apollo.portal.entity.App;
import com.ctrip.apollo.portal.repository.AppRepository;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.net.URI;
import java.net.URISyntaxException;
@WebIntegrationTest
public class AppControllerTest extends AbstractPortalTest {
RestTemplate restTemplate = new TestRestTemplate();
@Autowired
AppRepository appRepository;
@Value("${local.server.port}")
String serverPort;
@Test
public void testCreate() throws URISyntaxException {
App newApp = new App();
newApp.setAppId(String.valueOf(System.currentTimeMillis()));
newApp.setName("new app " + System.currentTimeMillis());
newApp.setOwner("owner " + System.currentTimeMillis());
URI uri = new URI("http://localhost:" + serverPort + "/apps");
App createdApp = restTemplate.postForObject(uri, newApp, App.class);
Assert.assertEquals(newApp.getAppId(), createdApp.getAppId());
Assert.assertNull(newApp.getCreateTimestamp());
Assert.assertNotNull(createdApp.getCreateTimestamp());
App foundApp = appRepository.findByAppId(newApp.getAppId());
Assert.assertEquals(newApp.getAppId(), foundApp.getAppId());
}
@Test
public void testList() throws URISyntaxException {
App newApp = new App();
newApp.setAppId(String.valueOf(System.currentTimeMillis()));
newApp.setName("new app " + System.currentTimeMillis());
newApp.setOwner("owner " + System.currentTimeMillis());
appRepository.save(newApp);
URI uri = new URI("http://localhost:" + serverPort + "/apps");
App[] apps = restTemplate.getForObject(uri, App[].class);
Assert.assertEquals(1, apps.length);
Assert.assertEquals(newApp.getAppId(), apps[0].getAppId());
}
@Test
public void testListOutOfRange() throws URISyntaxException {
App newApp = new App();
newApp.setAppId(String.valueOf(System.currentTimeMillis()));
newApp.setName("new app " + System.currentTimeMillis());
newApp.setOwner("owner " + System.currentTimeMillis());
appRepository.save(newApp);
URI uri = new URI("http://localhost:" + serverPort + "/apps?page=2");
ResponseEntity<App[]> entity = restTemplate.getForEntity(uri, App[].class);
Assert.assertEquals(HttpStatus.NOT_FOUND, entity.getStatusCode());
Assert.assertNull(entity.getBody());
}
}
package com.ctrip.apollo.portal.repository;
import com.ctrip.apollo.portal.AbstractPortalTest;
import com.ctrip.apollo.portal.entity.App;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
public class AppRepositoryTest extends AbstractPortalTest {
@Autowired
AppRepository repository;
@Test
public void testCreate() {
Assert.assertEquals(0, repository.count());
App ramdomApp = new App();
ramdomApp.setAppId(String.valueOf(System.currentTimeMillis()));
ramdomApp.setName("new app " + System.currentTimeMillis());
ramdomApp.setOwner("owner " + System.currentTimeMillis());
repository.save(ramdomApp);
Assert.assertEquals(1, repository.count());
}
}
package com.ctrip.apollo.portal.service;
import com.ctrip.apollo.portal.AbstractPortalTest;
import com.ctrip.apollo.portal.entity.App;
import com.ctrip.apollo.portal.entity.Privilege;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
public class PrivilegeServiceTest extends AbstractPortalTest {
@Autowired
AppService appService;
@Autowired
PrivilegeService privilService;
@Test
public void testAddAndRemovePrivilege() {
App newApp = new App();
newApp.setAppId(String.valueOf(System.currentTimeMillis()));
newApp.setName("new app " + System.currentTimeMillis());
newApp.setOwner("owner " + System.currentTimeMillis());
appService.save(newApp);
privilService.addPrivilege(newApp.getAppId(), newApp.getOwner(),
PrivilegeService.PrivilType.EDIT);
List<Privilege> privileges = privilService.listPrivileges(newApp.getAppId());
Assert.assertEquals(1, privileges.size());
Assert.assertEquals(PrivilegeService.PrivilType.EDIT.name(), privileges.get(0).getPrivilType());
Assert.assertEquals(newApp.getOwner(), privileges.get(0).getName());
privilService.removePrivilege(newApp.getAppId(), newApp.getOwner(),
PrivilegeService.PrivilType.EDIT);
privileges = privilService.listPrivileges(newApp.getAppId());
Assert.assertEquals(0, privileges.size());
}
@Test
public void testCheckPrivilege() {
App newApp = new App();
newApp.setAppId(String.valueOf(System.currentTimeMillis()));
newApp.setName("new app " + System.currentTimeMillis());
newApp.setOwner("owner " + System.currentTimeMillis());
appService.save(newApp);
privilService.addPrivilege(newApp.getAppId(), newApp.getOwner(),
PrivilegeService.PrivilType.EDIT);
Assert.assertTrue(privilService.hasPrivilege(newApp.getAppId(), newApp.getOwner(),
PrivilegeService.PrivilType.EDIT));
Assert.assertFalse(privilService.hasPrivilege(newApp.getAppId(), newApp.getOwner(),
PrivilegeService.PrivilType.REVIEW));
Assert.assertFalse(privilService.hasPrivilege(newApp.getAppId(), newApp.getOwner(),
PrivilegeService.PrivilType.RELEASE));
privilService.addPrivilege(newApp.getAppId(), "nobody", PrivilegeService.PrivilType.EDIT);
Assert.assertTrue(
privilService.hasPrivilege(newApp.getAppId(), "nobody", PrivilegeService.PrivilType.EDIT));
Assert.assertTrue(privilService.hasPrivilege(newApp.getAppId(), newApp.getOwner(),
PrivilegeService.PrivilType.EDIT));
privilService.addPrivilege(newApp.getAppId(), "nobody", PrivilegeService.PrivilType.RELEASE);
Assert.assertTrue(privilService.hasPrivilege(newApp.getAppId(), "nobody",
PrivilegeService.PrivilType.RELEASE));
}
}
......@@ -274,12 +274,6 @@
</plugins>
</build>
</profile>
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>ctrip</id>
<dependencies>
......
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