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; ...@@ -7,11 +7,11 @@ 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 com.ctrip.apollo.biz.entity.Group; import com.ctrip.apollo.biz.entity.Namespace;
import com.ctrip.apollo.biz.service.GroupService; import com.ctrip.apollo.biz.service.NamespaceService;
import com.ctrip.apollo.biz.service.ViewService; import com.ctrip.apollo.biz.service.ViewService;
import com.ctrip.apollo.biz.utils.BeanUtils; import com.ctrip.apollo.biz.utils.BeanUtils;
import com.ctrip.apollo.core.dto.GroupDTO; import com.ctrip.apollo.core.dto.NamespaceDTO;
@RestController @RestController
public class GroupController { public class GroupController {
...@@ -20,18 +20,18 @@ public class GroupController { ...@@ -20,18 +20,18 @@ public class GroupController {
private ViewService viewService; private ViewService viewService;
@Autowired @Autowired
private GroupService groupService; private NamespaceService groupService;
@RequestMapping("/apps/{appId}/clusters/{clusterName}/groups") @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) { @PathVariable("clusterName") String clusterName) {
List<Group> groups = viewService.findGroups(appId, clusterName); List<Namespace> groups = viewService.findNamespaces(appId, clusterName);
return BeanUtils.batchTransform(GroupDTO.class, groups); return BeanUtils.batchTransform(NamespaceDTO.class, groups);
} }
@RequestMapping("/groups/{groupId}") @RequestMapping("/groups/{groupId}")
public GroupDTO findOne(@PathVariable("groupId") Long groupId) { public NamespaceDTO findOne(@PathVariable("groupId") Long groupId) {
Group group = groupService.findOne(groupId); Namespace group = groupService.findOne(groupId);
return BeanUtils.transfrom(GroupDTO.class, group); return BeanUtils.transfrom(NamespaceDTO.class, group);
} }
} }
...@@ -22,11 +22,11 @@ public class ItemController { ...@@ -22,11 +22,11 @@ public class ItemController {
@Autowired @Autowired
private ItemService itemService; 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, public List<ItemDTO> findItems(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName, @PathVariable("clusterName") String clusterName,
@PathVariable("groupName") String groupName) { @PathVariable("namespaceName") String namespaceName) {
List<Item> items = viewService.findItems(appId, clusterName,groupName); List<Item> items = viewService.findItems(appId, clusterName, namespaceName);
return BeanUtils.batchTransform(ItemDTO.class, items); return BeanUtils.batchTransform(ItemDTO.class, items);
} }
......
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
......
...@@ -28,9 +28,11 @@ public class ReleaseController { ...@@ -28,9 +28,11 @@ public class ReleaseController {
return BeanUtils.transfrom(ReleaseDTO.class, release); return BeanUtils.transfrom(ReleaseDTO.class, release);
} }
@RequestMapping("/apps/{appId}/clusters/{clusterId}/groups/{groupId}/releases") @RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/releases")
public List<ReleaseDTO> findReleases(@PathVariable("groupId") Long groupId){ public List<ReleaseDTO> findReleases(@PathVariable("appId") String appId,
List<Release> releases = viewSerivce.findReleases(groupId); @PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName) {
List<Release> releases = viewSerivce.findReleases(appId, clusterName, namespaceName);
return BeanUtils.batchTransform(ReleaseDTO.class, releases); return BeanUtils.batchTransform(ReleaseDTO.class, releases);
} }
} }
...@@ -30,6 +30,9 @@ ...@@ -30,6 +30,9 @@
<profiles> <profiles>
<profile> <profile>
<id>local</id> <id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.h2database</groupId> <groupId>com.h2database</groupId>
......
...@@ -4,9 +4,11 @@ import javax.persistence.Column; ...@@ -4,9 +4,11 @@ import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import org.hibernate.annotations.SQLDelete; import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
@Entity @Entity
@SQLDelete(sql = "Update App set isDeleted = 1 where id = ?") @SQLDelete(sql = "Update App set isDeleted = 1 where id = ?")
@Where(clause = "isDeleted = 0")
public class App extends BaseEntity { public class App extends BaseEntity {
@Column(nullable = false) @Column(nullable = false)
......
...@@ -4,10 +4,12 @@ import javax.persistence.Column; ...@@ -4,10 +4,12 @@ import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import org.hibernate.annotations.SQLDelete; import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
@Entity @Entity
@SQLDelete(sql = "Update Group set isDeleted = 1 where id = ?") @SQLDelete(sql = "Update AppNamespace set isDeleted = 1 where id = ?")
public class Group extends BaseEntity { @Where(clause = "isDeleted = 0")
public class AppNamespace extends BaseEntity{
@Column(nullable = false) @Column(nullable = false)
private String name; private String name;
...@@ -15,53 +17,31 @@ public class Group extends BaseEntity { ...@@ -15,53 +17,31 @@ public class Group extends BaseEntity {
@Column(nullable = false) @Column(nullable = false)
private String appId; private String appId;
@Column(nullable = false) @Column
private long clusterId; private String comment;
@Column(nullable = false)
private String clusterName;
@Column(nullable = false)
private long namespaceId;
public String getAppId() { public String getAppId() {
return appId; return appId;
} }
public long getClusterId() { public String getComment() {
return clusterId; return comment;
}
public String getClusterName() {
return clusterName;
} }
public String getName() { public String getName() {
return name; return name;
} }
public long getNamespaceId() {
return namespaceId;
}
public void setAppId(String appId) { public void setAppId(String appId) {
this.appId = appId; this.appId = appId;
} }
public void setClusterId(long clusterId) { public void setComment(String comment) {
this.clusterId = clusterId; this.comment = comment;
}
public void setClusterName(String clusterName) {
this.clusterName = clusterName;
} }
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public void setNamespaceId(long namespaceId) {
this.namespaceId = namespaceId;
}
} }
...@@ -5,10 +5,12 @@ import java.util.Date; ...@@ -5,10 +5,12 @@ import java.util.Date;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.MappedSuperclass;
import org.hibernate.annotations.Where; @MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Where(clause = "isDeleted = 0")
public abstract class BaseEntity { public abstract class BaseEntity {
@Id @Id
......
...@@ -4,12 +4,14 @@ import javax.persistence.Column; ...@@ -4,12 +4,14 @@ import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import org.hibernate.annotations.SQLDelete; import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
@Entity @Entity
@SQLDelete(sql = "Update Cluster set isDeleted = 1 where id = ?") @SQLDelete(sql = "Update Cluster set isDeleted = 1 where id = ?")
@Where(clause = "isDeleted = 0")
public class Cluster extends BaseEntity { public class Cluster extends BaseEntity {
@Column(nullable = false) @Column(nullable = false)
......
...@@ -4,13 +4,15 @@ import javax.persistence.Column; ...@@ -4,13 +4,15 @@ import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import org.hibernate.annotations.SQLDelete; import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
@Entity @Entity
@SQLDelete(sql = "Update Item set isDeleted = 1 where id = ?") @SQLDelete(sql = "Update Item set isDeleted = 1 where id = ?")
@Where(clause = "isDeleted = 0")
public class Item extends BaseEntity { public class Item extends BaseEntity {
@Column(nullable = false) @Column(nullable = false)
private long groupId; private long namespaceId;
@Column(nullable = false) @Column(nullable = false)
private String key; private String key;
...@@ -21,36 +23,36 @@ public class Item extends BaseEntity { ...@@ -21,36 +23,36 @@ public class Item extends BaseEntity {
@Column @Column
private String comment; private String comment;
public long getGroupId() { public String getComment() {
return groupId; return comment;
}
public void setGroupId(long groupId) {
this.groupId = groupId;
} }
public String getKey() { public String getKey() {
return key; return key;
} }
public void setKey(String key) { public long getNamespaceId() {
this.key = key; return namespaceId;
} }
public String getValue() { public String getValue() {
return value; return value;
} }
public void setValue(String value) { public void setComment(String comment) {
this.value = value; this.comment = comment;
} }
public String getComment() { public void setKey(String key) {
return comment; this.key = key;
} }
public void setComment(String comment) { public void setNamespaceId(long namespaceId) {
this.comment = comment; this.namespaceId = namespaceId;
}
public void setValue(String value) {
this.value = value;
} }
} }
...@@ -4,42 +4,44 @@ import javax.persistence.Column; ...@@ -4,42 +4,44 @@ import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import org.hibernate.annotations.SQLDelete; import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
@Entity @Entity
@SQLDelete(sql = "Update Namespace set isDeleted = 1 where id = ?") @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) @Column(nullable = false)
private String name; private String appId;
@Column(nullable = false) @Column(nullable = false)
private String appId; private String clusterName;
@Column @Column(nullable = false)
private String comment; private String namespaceName;
public String getAppId() { public String getAppId() {
return appId; return appId;
} }
public String getComment() { public String getClusterName() {
return comment; return clusterName;
} }
public String getName() { public String getNamespaceName() {
return name; return namespaceName;
} }
public void setAppId(String appId) { public void setAppId(String appId) {
this.appId = appId; this.appId = appId;
} }
public void setComment(String comment) { public void setClusterName(String clusterName) {
this.comment = comment; this.clusterName = clusterName;
} }
public void setName(String name) { public void setNamespaceName(String namespaceName) {
this.name = name; this.namespaceName = namespaceName;
} }
} }
package com.ctrip.apollo.portal.entity; package com.ctrip.apollo.biz.entity;
import java.io.Serializable;
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
public class Privilege implements Serializable {
/** import org.hibernate.annotations.SQLDelete;
* import org.hibernate.annotations.Where;
*/
private static final long serialVersionUID = -430087307622435118L;
@Id @Entity
@GeneratedValue @SQLDelete(sql = "Update Privilege set isDeleted = 1 where id = ?")
private long id; @Where(clause = "isDeleted = 0")
public class Privilege extends BaseEntity {
@Column @Column
private String name; private String name;
...@@ -26,37 +18,29 @@ public class Privilege implements Serializable { ...@@ -26,37 +18,29 @@ public class Privilege implements Serializable {
private String privilType; private String privilType;
@Column @Column
private String appId; private long namespaceId;
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) { public long getNamespaceId() {
this.name = name; return namespaceId;
} }
public String getPrivilType() { public String getPrivilType() {
return privilType; return privilType;
} }
public void setPrivilType(String privilType) { public void setName(String name) {
this.privilType = privilType; this.name = name;
} }
public String getAppId() { public void setNamespaceId(long namespaceId) {
return appId; this.namespaceId = namespaceId;
} }
public void setAppId(String appId) { public void setPrivilType(String privilType) {
this.appId = appId; this.privilType = privilType;
} }
} }
...@@ -5,12 +5,14 @@ import javax.persistence.Entity; ...@@ -5,12 +5,14 @@ import javax.persistence.Entity;
import javax.persistence.Lob; import javax.persistence.Lob;
import org.hibernate.annotations.SQLDelete; import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
*/ */
@Entity @Entity
@SQLDelete(sql = "Update Release set isDeleted = 1 where id = ?") @SQLDelete(sql = "Update Release set isDeleted = 1 where id = ?")
@Where(clause = "isDeleted = 0")
public class Release extends BaseEntity { public class Release extends BaseEntity {
@Column(nullable = false) @Column(nullable = false)
...@@ -23,7 +25,7 @@ public class Release extends BaseEntity { ...@@ -23,7 +25,7 @@ public class Release extends BaseEntity {
private String clusterName; private String clusterName;
@Column @Column
private String groupName; private String namespaceName;
@Column(nullable = false) @Column(nullable = false)
@Lob @Lob
...@@ -48,8 +50,8 @@ public class Release extends BaseEntity { ...@@ -48,8 +50,8 @@ public class Release extends BaseEntity {
return configurations; return configurations;
} }
public String getGroupName() { public String getNamespaceName() {
return groupName; return namespaceName;
} }
public String getName() { public String getName() {
...@@ -72,8 +74,8 @@ public class Release extends BaseEntity { ...@@ -72,8 +74,8 @@ public class Release extends BaseEntity {
this.configurations = configurations; this.configurations = configurations;
} }
public void setGroupName(String groupName) { public void setNamespaceName(String namespaceName) {
this.groupName = groupName; this.namespaceName = namespaceName;
} }
public void setName(String name) { 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; ...@@ -10,7 +10,7 @@ import com.ctrip.apollo.biz.entity.App;
public interface AppRepository extends PagingAndSortingRepository<App, Long> { 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); List<App> findByName(@Param("name") String name);
App findByAppId(String appId); 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; package com.ctrip.apollo.biz.repository;
import com.ctrip.apollo.biz.entity.Item; 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.Item;
public interface ItemRepository extends PagingAndSortingRepository<Item, Long> { 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; package com.ctrip.apollo.biz.repository;
import java.util.List;
import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.PagingAndSortingRepository;
import com.ctrip.apollo.biz.entity.Namespace; 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; package com.ctrip.apollo.biz.repository;
import com.ctrip.apollo.portal.entity.Privilege;
import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.PagingAndSortingRepository;
import com.ctrip.apollo.biz.entity.Privilege;
import java.util.List; import java.util.List;
public interface PrivilegeRepository extends PagingAndSortingRepository<Privilege, Long> { 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; ...@@ -13,9 +13,10 @@ import com.ctrip.apollo.biz.entity.Release;
*/ */
public interface ReleaseRepository extends PagingAndSortingRepository<Release, Long> { 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, 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 { ...@@ -24,26 +24,25 @@ public class ConfigService {
@Autowired @Autowired
private ReleaseRepository releaseRepository; private ReleaseRepository releaseRepository;
@Autowired private ObjectMapper objectMapper = new 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) { public Release findRelease(String appId, String clusterName, String namespaceName) {
Release release = releaseRepository.findLatest(appId, clusterName, groupName); Release release = releaseRepository.findLatest(appId, clusterName, namespaceName);
return release; return release;
} }
/** /**
* Load configuration from database * 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) { if (release == null) {
return null; return null;
} }
ApolloConfig config = new ApolloConfig(release.getAppId(), release.getClusterName(), groupName, ApolloConfig config = new ApolloConfig(release.getAppId(), release.getClusterName(),
versionName, release.getId()); namespaceName, versionName, release.getId());
config.setConfigurations(transformConfigurationToMap(release.getConfigurations())); config.setConfigurations(transformConfigurationToMap(release.getConfigurations()));
return config; return config;
} }
......
...@@ -3,16 +3,16 @@ package com.ctrip.apollo.biz.service; ...@@ -3,16 +3,16 @@ package com.ctrip.apollo.biz.service;
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 com.ctrip.apollo.biz.entity.Group; import com.ctrip.apollo.biz.entity.Namespace;
import com.ctrip.apollo.biz.repository.GroupRepository; import com.ctrip.apollo.biz.repository.NamespaceRepository;
@Service @Service
public class GroupService { public class NamespaceService {
@Autowired @Autowired
private GroupRepository groupRepository; private NamespaceRepository namespaceRepository;
public Group findOne(Long groupId){ public Namespace findOne(Long namespaceId){
return groupRepository.findOne(groupId); 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.biz.entity.Privilege;
import com.ctrip.apollo.portal.exception.NotFoundException; import com.ctrip.apollo.biz.repository.PrivilegeRepository;
import com.ctrip.apollo.portal.repository.PrivilegeRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -19,11 +18,12 @@ public class PrivilegeService { ...@@ -19,11 +18,12 @@ public class PrivilegeService {
@Autowired @Autowired
private PrivilegeRepository privilRepo; private PrivilegeRepository privilRepo;
public Privilege addPrivilege(String appId, String name, PrivilType privilType) { public Privilege addPrivilege(long namespaceId, String name, PrivilType privilType) {
Privilege privil = privilRepo.findByAppIdAndNameAndPrivilType(appId, name, privilType.name()); Privilege privil =
privilRepo.findByNamespaceIdAndNameAndPrivilType(namespaceId, name, privilType.name());
if (privil == null) { if (privil == null) {
privil = new Privilege(); privil = new Privilege();
privil.setAppId(appId); privil.setNamespaceId(namespaceId);
privil.setPrivilType(privilType.name()); privil.setPrivilType(privilType.name());
privil.setName(name); privil.setName(name);
privilRepo.save(privil); privilRepo.save(privil);
...@@ -31,20 +31,19 @@ public class PrivilegeService { ...@@ -31,20 +31,19 @@ public class PrivilegeService {
return privil; return privil;
} }
public boolean hasPrivilege(String appId, String name, PrivilType privilType) { public boolean hasPrivilege(long namespaceId, String name, PrivilType privilType) {
Privilege privil = privilRepo.findByAppIdAndNameAndPrivilType(appId, name, privilType.name()); Privilege privil =
privilRepo.findByNamespaceIdAndNameAndPrivilType(namespaceId, name, privilType.name());
return (privil != null) ? true : false; return (privil != null) ? true : false;
} }
public List<Privilege> listPrivileges(String appId) { public List<Privilege> listPrivileges(long namespaceId) {
return privilRepo.findByAppId(appId); return privilRepo.findByNamespaceId(namespaceId);
} }
public void removePrivilege(String appId, String name, PrivilType privilType) { public void removePrivilege(long namespaceId, String name, PrivilType privilType) {
Privilege privil = privilRepo.findByAppIdAndNameAndPrivilType(appId, name, privilType.name()); Privilege privil =
if (privil == null) { privilRepo.findByNamespaceIdAndNameAndPrivilType(namespaceId, name, privilType.name());
throw new NotFoundException(); if (privil != null) privilRepo.delete(privil);
}
privilRepo.delete(privil);
} }
} }
...@@ -7,11 +7,11 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -7,11 +7,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.ctrip.apollo.biz.entity.Cluster; 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.Item;
import com.ctrip.apollo.biz.entity.Release; import com.ctrip.apollo.biz.entity.Release;
import com.ctrip.apollo.biz.repository.ClusterRepository; 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.ItemRepository;
import com.ctrip.apollo.biz.repository.ReleaseRepository; import com.ctrip.apollo.biz.repository.ReleaseRepository;
import com.google.common.base.Strings; import com.google.common.base.Strings;
...@@ -26,7 +26,7 @@ public class ViewService { ...@@ -26,7 +26,7 @@ public class ViewService {
private ClusterRepository clusterRepository; private ClusterRepository clusterRepository;
@Autowired @Autowired
private GroupRepository groupRepository; private NamespaceRepository namespaceRepository;
@Autowired @Autowired
private ItemRepository itemRepository; private ItemRepository itemRepository;
...@@ -46,17 +46,17 @@ public class ViewService { ...@@ -46,17 +46,17 @@ public class ViewService {
return clusters; return clusters;
} }
public List<Group> findGroups(String appId, String clusterName) { public List<Namespace> findNamespaces(String appId, String clusterName) {
List<Group> groups = groupRepository.findByAppIdAndClusterName(appId, clusterName); List<Namespace> groups = namespaceRepository.findByAppIdAndClusterName(appId, clusterName);
if (groups == null) { if (groups == null) {
return Collections.EMPTY_LIST; return Collections.EMPTY_LIST;
} }
return groups; return groups;
} }
public List<Item> findItems(String appId, String clusterName, String groupName) { public List<Item> findItems(String appId, String clusterName, String namespaceName) {
Group group = Namespace group = namespaceRepository.findByAppIdAndClusterNameAndNamespaceName(appId, clusterName,
groupRepository.findByAppIdAndClusterNameAndGroupName(appId, clusterName, groupName); namespaceName);
if (group != null) { if (group != null) {
return findItems(group.getId()); return findItems(group.getId());
} else { } else {
...@@ -64,16 +64,17 @@ public class ViewService { ...@@ -64,16 +64,17 @@ public class ViewService {
} }
} }
public List<Item> findItems(Long groupId) { public List<Item> findItems(Long namespaceId) {
List<Item> items = itemRepository.findByGroupId(groupId); List<Item> items = itemRepository.findByNamespaceId(namespaceId);
if (items == null) { if (items == null) {
return Collections.EMPTY_LIST; return Collections.EMPTY_LIST;
} }
return items; return items;
} }
public List<Release> findReleases(Long groupId) { public List<Release> findReleases(String appId, String clusterName, String namespaceName) {
List<Release> releases = releaseRepository.findByGroupId(groupId); List<Release> releases = releaseRepository.findByAppIdAndClusterNameAndNamespaceName(appId,
clusterName, namespaceName);
if (releases == null) { if (releases == null) {
return Collections.EMPTY_LIST; 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 { ...@@ -121,7 +121,7 @@ public class ConfigServiceTest {
Release release = new Release(); Release release = new Release();
release.setId(releaseId); release.setId(releaseId);
release.setClusterName(clusterName); release.setClusterName(clusterName);
release.setGroupName(groupName); release.setNamespaceName(groupName);
release.setConfigurations(configurations); release.setConfigurations(configurations);
return release; 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; ...@@ -8,7 +8,7 @@ import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class) @RunWith(Suite.class)
@SuiteClasses({ @SuiteClasses({
ConfigControllerTest.class //ConfigControllerTest.class
}) })
public class AllTests { 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.Apollo.Env;
import com.ctrip.apollo.core.dto.ItemDTO;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
......
package com.ctrip.apollo.core.dto; package com.ctrip.apollo.core.dto;
import java.util.Date;
public class ItemDTO { public class ItemDTO {
private long id; private long id;
......
package com.ctrip.apollo.core.dto; package com.ctrip.apollo.core.dto;
public class GroupDTO { public class NamespaceDTO {
private long id; 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; ...@@ -3,10 +3,10 @@ package com.ctrip.apollo.portal.api;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.ctrip.apollo.Apollo; 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.ClusterDTO;
import com.ctrip.apollo.core.dto.ItemDTO; import com.ctrip.apollo.core.dto.ItemDTO;
import com.ctrip.apollo.core.dto.ReleaseDTO; import com.ctrip.apollo.core.dto.ReleaseDTO;
import com.ctrip.apollo.core.dto.VersionDTO;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -15,6 +15,15 @@ import java.util.List; ...@@ -15,6 +15,15 @@ import java.util.List;
@Service @Service
public class AdminServiceAPI { 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 @Service
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/";
...@@ -37,8 +46,8 @@ public class AdminServiceAPI { ...@@ -37,8 +46,8 @@ public class AdminServiceAPI {
sb.append(clusterId).append(","); sb.append(clusterId).append(",");
} }
return restTemplate.getForObject(getAdminServiceHost(env) + "/configs/latest?clusterIds=" + sb return restTemplate.getForObject(getAdminServiceHost(env) + "/configs/latest?clusterIds="
.substring(0, sb.length() - 1), ItemDTO[].class); + sb.substring(0, sb.length() - 1), ItemDTO[].class);
} }
} }
...@@ -52,30 +61,8 @@ public class AdminServiceAPI { ...@@ -52,30 +61,8 @@ public class AdminServiceAPI {
return null; return null;
} }
return restTemplate return restTemplate.getForObject(getAdminServiceHost(env) + CLUSTER_APP_API + appId,
.getForObject(getAdminServiceHost(env) + CLUSTER_APP_API + appId, ClusterDTO[].class); 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);
} }
} }
......
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; ...@@ -3,8 +3,8 @@ package com.ctrip.apollo.portal.controller;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.ctrip.apollo.Apollo; import com.ctrip.apollo.Apollo;
import com.ctrip.apollo.core.dto.AppConfigVO;
import com.ctrip.apollo.portal.constants.PortalConstants; 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.exception.NotFoundException;
import com.ctrip.apollo.portal.service.ConfigService; import com.ctrip.apollo.portal.service.ConfigService;
...@@ -35,11 +35,12 @@ public class ConfigController { ...@@ -35,11 +35,12 @@ public class ConfigController {
return configService.loadLatestConfig(e, appId); return configService.loadLatestConfig(e, appId);
} else if (versionId > 0) { // } else if (versionId > 0) {
//
return configService.loadReleaseConfig(e, appId, versionId); // return configService.loadReleaseConfig(e, appId, versionId);
//
} else { }
else {
throw new NotFoundException(); 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; ...@@ -15,13 +15,12 @@ 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.AppConfigVO;
import com.ctrip.apollo.core.dto.ClusterDTO; import com.ctrip.apollo.core.dto.ClusterDTO;
import com.ctrip.apollo.core.dto.ItemDTO; import com.ctrip.apollo.core.dto.ItemDTO;
import com.ctrip.apollo.core.dto.ReleaseDTO; 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.api.AdminServiceAPI;
import com.ctrip.apollo.portal.constants.PortalConstants; import com.ctrip.apollo.portal.constants.PortalConstants;
import com.ctrip.apollo.portal.entity.AppConfigVO;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings; import com.google.common.base.Strings;
...@@ -36,50 +35,34 @@ public class ConfigService { ...@@ -36,50 +35,34 @@ public class ConfigService {
private AdminServiceAPI.ConfigAPI configAPI; private AdminServiceAPI.ConfigAPI configAPI;
@Autowired @Autowired
private AdminServiceAPI.ClusterAPI clusterAPI; private AdminServiceAPI.ClusterAPI clusterAPI;
@Autowired
private AdminServiceAPI.VersionAPI versionAPI;
private ObjectMapper objectMapper = new ObjectMapper(); private ObjectMapper objectMapper = new ObjectMapper();
public AppConfigVO loadReleaseConfig(Env env, String appId, long versionId) { // public AppConfigVO loadReleaseConfig(Env env, String appId, String cluster, String namespace) {
//
if (Strings.isNullOrEmpty(appId) || versionId <= 0) { // if (Strings.isNullOrEmpty(appId) || Strings.isNullOrEmpty(cluster) || Strings.isNullOrEmpty(namespace)) {
return null; // return null;
} // }
//
long releaseId = getReleaseIdFromVersionId(env, versionId); // ReleaseDTO[] releaseSnapShots = configAPI.getConfigByReleaseId(env, releaseId);
if (releaseId == -1) { // if (releaseSnapShots == null || releaseSnapShots.length == 0) {
logger.warn("get release id error env:{}, app id:{}, version id:{}", env, appId, versionId); // return null;
return null; // }
} //
// AppConfigVO appConfigVO = AppConfigVO.newInstance(appId, versionId);
ReleaseDTO[] releaseSnapShots = configAPI.getConfigByReleaseId(env, releaseId); //
if (releaseSnapShots == null || releaseSnapShots.length == 0) { // for (ReleaseDTO snapShot : releaseSnapShots) {
return null; // // default cluster
} // if (ConfigConsts.DEFAULT_CLUSTER_NAME.equals(snapShot.getClusterName())) {
//
AppConfigVO appConfigVO = AppConfigVO.newInstance(appId, versionId); // collectDefaultClusterConfigs(appId, snapShot, appConfigVO);
//
for (ReleaseDTO snapShot : releaseSnapShots) { // } else {// cluster special configs
// default cluster // collectSpecialClusterConfigs(appId, snapShot, appConfigVO);
if (ConfigConsts.DEFAULT_CLUSTER_NAME.equals(snapShot.getClusterName())) { // }
// }
collectDefaultClusterConfigs(appId, snapShot, appConfigVO); // return 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();
}
private void collectDefaultClusterConfigs(String appId, ReleaseDTO snapShot, private void collectDefaultClusterConfigs(String appId, ReleaseDTO snapShot,
AppConfigVO appConfigVO) { 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; ...@@ -7,7 +7,9 @@ import org.junit.runners.Suite.SuiteClasses;
import com.ctrip.apollo.portal.service.ConfigServiceTest; import com.ctrip.apollo.portal.service.ConfigServiceTest;
@RunWith(Suite.class) @RunWith(Suite.class)
@SuiteClasses({ConfigServiceTest.class}) @SuiteClasses({
//ConfigServiceTest.class
})
public class AllTests { 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 @@ ...@@ -274,12 +274,6 @@
</plugins> </plugins>
</build> </build>
</profile> </profile>
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile> <profile>
<id>ctrip</id> <id>ctrip</id>
<dependencies> <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