Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
apollo
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openSource
apollo
Commits
d521b24f
Commit
d521b24f
authored
Apr 21, 2016
by
Yiming Liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add unique check for app/cluster/namespace
parent
cf7feb30
Hide whitespace changes
Inline
Side-by-side
Showing
19 changed files
with
319 additions
and
171 deletions
+319
-171
AppController.java
...m/ctrip/apollo/adminservice/controller/AppController.java
+5
-0
AppNamespaceController.java
...pollo/adminservice/controller/AppNamespaceController.java
+21
-0
ClusterController.java
...rip/apollo/adminservice/controller/ClusterController.java
+6
-5
ItemController.java
.../ctrip/apollo/adminservice/controller/ItemController.java
+1
-5
NamespaceController.java
...p/apollo/adminservice/controller/NamespaceController.java
+1
-5
ReleaseController.java
...rip/apollo/adminservice/controller/ReleaseController.java
+1
-5
AppControllerTest.java
...rip/apollo/adminservice/controller/AppControllerTest.java
+18
-0
AppNamespaceRepository.java
...m/ctrip/apollo/biz/repository/AppNamespaceRepository.java
+2
-0
AdminService.java
.../main/java/com/ctrip/apollo/biz/service/AdminService.java
+8
-57
AppNamespaceService.java
...ava/com/ctrip/apollo/biz/service/AppNamespaceService.java
+45
-0
AppService.java
...rc/main/java/com/ctrip/apollo/biz/service/AppService.java
+10
-0
ClusterService.java
...ain/java/com/ctrip/apollo/biz/service/ClusterService.java
+43
-0
ItemService.java
...c/main/java/com/ctrip/apollo/biz/service/ItemService.java
+21
-0
NamespaceService.java
...n/java/com/ctrip/apollo/biz/service/NamespaceService.java
+43
-1
ReleaseService.java
...ain/java/com/ctrip/apollo/biz/service/ReleaseService.java
+10
-0
ViewService.java
...c/main/java/com/ctrip/apollo/biz/service/ViewService.java
+0
-84
AdminServiceTest.java
...t/java/com/ctrip/apollo/biz/service/AdminServiceTest.java
+30
-4
ClusterServiceTest.java
...java/com/ctrip/apollo/biz/service/ClusterServiceTest.java
+46
-0
PrivilegeServiceTest.java
...va/com/ctrip/apollo/biz/service/PrivilegeServiceTest.java
+8
-5
No files found.
apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/AppController.java
View file @
d521b24f
...
...
@@ -73,4 +73,9 @@ public class AppController {
return
BeanUtils
.
transfrom
(
AppDTO
.
class
,
app
);
}
@RequestMapping
(
"/apps/{appId}/unique"
)
public
boolean
isAppIdUnique
(
@PathVariable
(
"appId"
)
String
appId
)
{
return
appService
.
isAppIdUnique
(
appId
);
}
}
apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/AppNamespaceController.java
0 → 100644
View file @
d521b24f
package
com
.
ctrip
.
apollo
.
adminservice
.
controller
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
com.ctrip.apollo.biz.service.AppNamespaceService
;
@RestController
public
class
AppNamespaceController
{
@Autowired
private
AppNamespaceService
appNamespaceService
;
@RequestMapping
(
"/apps/{appId}/appnamespace/{appnamespace}/unique"
)
public
boolean
isAppNamespaceUnique
(
@PathVariable
(
"appId"
)
String
appId
,
@PathVariable
(
"appnamespace"
)
String
appnamespace
)
{
return
appNamespaceService
.
isAppNamespaceNameUnique
(
appId
,
appnamespace
);
}
}
apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/ClusterController.java
View file @
d521b24f
...
...
@@ -12,7 +12,6 @@ import org.springframework.web.bind.annotation.RestController;
import
com.ctrip.apollo.biz.entity.Cluster
;
import
com.ctrip.apollo.biz.service.ClusterService
;
import
com.ctrip.apollo.biz.service.ViewService
;
import
com.ctrip.apollo.common.auth.ActiveUser
;
import
com.ctrip.apollo.common.utils.BeanUtils
;
import
com.ctrip.apollo.core.dto.ClusterDTO
;
...
...
@@ -22,9 +21,6 @@ import com.ctrip.apollo.core.exception.NotFoundException;
public
class
ClusterController
{
@Autowired
private
ViewService
viewService
;
@Autowired
private
ClusterService
clusterService
;
@RequestMapping
(
path
=
"/apps/{appId}/clusters"
,
method
=
RequestMethod
.
POST
)
...
...
@@ -56,7 +52,7 @@ public class ClusterController {
@RequestMapping
(
"/apps/{appId}/clusters"
)
public
List
<
ClusterDTO
>
find
(
@PathVariable
(
"appId"
)
String
appId
)
{
List
<
Cluster
>
clusters
=
view
Service
.
findClusters
(
appId
);
List
<
Cluster
>
clusters
=
cluster
Service
.
findClusters
(
appId
);
return
BeanUtils
.
batchTransform
(
ClusterDTO
.
class
,
clusters
);
}
...
...
@@ -68,4 +64,9 @@ public class ClusterController {
return
BeanUtils
.
transfrom
(
ClusterDTO
.
class
,
cluster
);
}
@RequestMapping
(
"/apps/{appId}/cluster/{clusterName}/unique"
)
public
boolean
isAppIdUnique
(
@PathVariable
(
"appId"
)
String
appId
,
@PathVariable
(
"clusterName"
)
String
clusterName
)
{
return
clusterService
.
isClusterNameUnique
(
appId
,
clusterName
);
}
}
apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/ItemController.java
View file @
d521b24f
...
...
@@ -13,7 +13,6 @@ import org.springframework.web.bind.annotation.RestController;
import
com.ctrip.apollo.biz.entity.Item
;
import
com.ctrip.apollo.biz.service.ItemService
;
import
com.ctrip.apollo.biz.service.ViewService
;
import
com.ctrip.apollo.common.auth.ActiveUser
;
import
com.ctrip.apollo.common.utils.BeanUtils
;
import
com.ctrip.apollo.core.dto.ItemDTO
;
...
...
@@ -23,9 +22,6 @@ import com.ctrip.apollo.core.exception.NotFoundException;
public
class
ItemController
{
@Autowired
private
ViewService
viewService
;
@Autowired
private
ItemService
itemService
;
@RequestMapping
(
path
=
"/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items"
,
method
=
RequestMethod
.
POST
)
...
...
@@ -59,7 +55,7 @@ public class ItemController {
public
List
<
ItemDTO
>
findItems
(
@PathVariable
(
"appId"
)
String
appId
,
@PathVariable
(
"clusterName"
)
String
clusterName
,
@PathVariable
(
"namespaceName"
)
String
namespaceName
)
{
List
<
Item
>
items
=
view
Service
.
findItems
(
appId
,
clusterName
,
namespaceName
);
List
<
Item
>
items
=
item
Service
.
findItems
(
appId
,
clusterName
,
namespaceName
);
List
<
ItemDTO
>
itemDTOs
=
new
LinkedList
<>();
for
(
Item
item:
items
){
...
...
apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/NamespaceController.java
View file @
d521b24f
...
...
@@ -12,7 +12,6 @@ import org.springframework.web.bind.annotation.RestController;
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.common.auth.ActiveUser
;
import
com.ctrip.apollo.common.utils.BeanUtils
;
import
com.ctrip.apollo.core.dto.NamespaceDTO
;
...
...
@@ -22,9 +21,6 @@ import com.ctrip.apollo.core.exception.NotFoundException;
public
class
NamespaceController
{
@Autowired
private
ViewService
viewService
;
@Autowired
private
NamespaceService
namespaceService
;
@RequestMapping
(
path
=
"/apps/{appId}/clusters/{clusterName}/namespaces"
,
method
=
RequestMethod
.
POST
)
...
...
@@ -59,7 +55,7 @@ public class NamespaceController {
@RequestMapping
(
"/apps/{appId}/clusters/{clusterName}/namespaces"
)
public
List
<
NamespaceDTO
>
find
(
@PathVariable
(
"appId"
)
String
appId
,
@PathVariable
(
"clusterName"
)
String
clusterName
)
{
List
<
Namespace
>
groups
=
view
Service
.
findNamespaces
(
appId
,
clusterName
);
List
<
Namespace
>
groups
=
namespace
Service
.
findNamespaces
(
appId
,
clusterName
);
return
BeanUtils
.
batchTransform
(
NamespaceDTO
.
class
,
groups
);
}
...
...
apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/ReleaseController.java
View file @
d521b24f
...
...
@@ -15,7 +15,6 @@ import com.ctrip.apollo.biz.entity.Release;
import
com.ctrip.apollo.biz.service.ConfigService
;
import
com.ctrip.apollo.biz.service.NamespaceService
;
import
com.ctrip.apollo.biz.service.ReleaseService
;
import
com.ctrip.apollo.biz.service.ViewService
;
import
com.ctrip.apollo.common.auth.ActiveUser
;
import
com.ctrip.apollo.common.utils.BeanUtils
;
import
com.ctrip.apollo.core.dto.ReleaseDTO
;
...
...
@@ -25,9 +24,6 @@ import com.ctrip.apollo.core.exception.NotFoundException;
public
class
ReleaseController
{
@Autowired
private
ViewService
viewSerivce
;
@Autowired
private
ReleaseService
releaseService
;
@Autowired
...
...
@@ -48,7 +44,7 @@ public class ReleaseController {
public
List
<
ReleaseDTO
>
find
(
@PathVariable
(
"appId"
)
String
appId
,
@PathVariable
(
"clusterName"
)
String
clusterName
,
@PathVariable
(
"namespaceName"
)
String
namespaceName
)
{
List
<
Release
>
releases
=
viewSeriv
ce
.
findReleases
(
appId
,
clusterName
,
namespaceName
);
List
<
Release
>
releases
=
releaseServi
ce
.
findReleases
(
appId
,
clusterName
,
namespaceName
);
return
BeanUtils
.
batchTransform
(
ReleaseDTO
.
class
,
releases
);
}
...
...
apollo-adminservice/src/test/java/com/ctrip/apollo/adminservice/controller/AppControllerTest.java
View file @
d521b24f
...
...
@@ -24,6 +24,24 @@ public class AppControllerTest extends AbstractControllerTest {
}
@Test
public
void
testCheckIfAppIdUnique
()
{
AppDTO
dto
=
generateSampleDTOData
();
ResponseEntity
<
AppDTO
>
response
=
restTemplate
.
postForEntity
(
getBaseAppUrl
(),
dto
,
AppDTO
.
class
);
AppDTO
result
=
response
.
getBody
();
Assert
.
assertEquals
(
HttpStatus
.
OK
,
response
.
getStatusCode
());
Assert
.
assertEquals
(
dto
.
getAppId
(),
result
.
getAppId
());
Assert
.
assertTrue
(
result
.
getId
()
>
0
);
Boolean
falseUnique
=
restTemplate
.
getForObject
(
getBaseAppUrl
()
+
dto
.
getAppId
()
+
"/unique"
,
Boolean
.
class
);
Assert
.
assertFalse
(
falseUnique
);
Boolean
trueUnique
=
restTemplate
.
getForObject
(
getBaseAppUrl
()
+
dto
.
getAppId
()
+
"true"
+
"/unique"
,
Boolean
.
class
);
Assert
.
assertTrue
(
trueUnique
);
}
@Test
@Sql
(
scripts
=
"/controller/cleanup.sql"
,
executionPhase
=
ExecutionPhase
.
AFTER_TEST_METHOD
)
public
void
testCreate
()
{
AppDTO
dto
=
generateSampleDTOData
();
...
...
apollo-biz/src/main/java/com/ctrip/apollo/biz/repository/AppNamespaceRepository.java
View file @
d521b24f
...
...
@@ -6,4 +6,6 @@ import com.ctrip.apollo.biz.entity.AppNamespace;
public
interface
AppNamespaceRepository
extends
PagingAndSortingRepository
<
AppNamespace
,
Long
>{
AppNamespace
findByAppIdAndName
(
String
appId
,
String
namespaceName
);
}
apollo-biz/src/main/java/com/ctrip/apollo/biz/service/AdminService.java
View file @
d521b24f
...
...
@@ -5,85 +5,36 @@ import org.springframework.stereotype.Service;
import
org.springframework.transaction.annotation.Transactional
;
import
com.ctrip.apollo.biz.entity.App
;
import
com.ctrip.apollo.biz.entity.AppNamespace
;
import
com.ctrip.apollo.biz.entity.Audit
;
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
;
import
com.ctrip.apollo.core.ConfigConsts
;
@Service
public
class
AdminService
{
@Autowired
private
App
Repository
appRepository
;
private
App
Service
appService
;
@Autowired
private
AppNamespace
Repository
appNamespaceRepository
;
private
AppNamespace
Service
appNamespaceService
;
@Autowired
private
NamespaceRepository
namespaceRepository
;
private
ClusterService
clusterService
;
@Autowired
private
ClusterRepository
clusterRepository
;
@Autowired
private
AuditService
auditService
;
private
NamespaceService
namespaceService
;
@Transactional
public
App
createNewApp
(
App
app
)
{
String
createBy
=
app
.
getDataChangeCreatedBy
();
App
createdApp
=
appRepository
.
save
(
app
);
auditService
.
audit
(
App
.
class
.
getSimpleName
(),
createdApp
.
getId
(),
Audit
.
OP
.
INSERT
,
createBy
);
App
createdApp
=
appService
.
save
(
app
);
String
appId
=
createdApp
.
getAppId
();
createDefaultAppNamespace
(
appId
,
createBy
);
appNamespaceService
.
createDefaultAppNamespace
(
appId
,
createBy
);
createDefaultCluster
(
appId
,
createBy
);
c
lusterService
.
c
reateDefaultCluster
(
appId
,
createBy
);
createDefaultNamespace
(
appId
,
createBy
);
namespaceService
.
createDefaultNamespace
(
appId
,
createBy
);
return
app
;
}
private
void
createDefaultAppNamespace
(
String
appId
,
String
createBy
)
{
AppNamespace
appNs
=
new
AppNamespace
();
appNs
.
setAppId
(
appId
);
appNs
.
setName
(
appId
);
appNs
.
setComment
(
"default app namespace"
);
appNs
.
setDataChangeCreatedBy
(
createBy
);
appNs
.
setDataChangeLastModifiedBy
(
createBy
);
appNamespaceRepository
.
save
(
appNs
);
auditService
.
audit
(
AppNamespace
.
class
.
getSimpleName
(),
appNs
.
getId
(),
Audit
.
OP
.
INSERT
,
createBy
);
}
private
void
createDefaultCluster
(
String
appId
,
String
createBy
)
{
Cluster
cluster
=
new
Cluster
();
cluster
.
setName
(
ConfigConsts
.
CLUSTER_NAME_DEFAULT
);
cluster
.
setAppId
(
appId
);
cluster
.
setDataChangeCreatedBy
(
createBy
);
cluster
.
setDataChangeLastModifiedBy
(
createBy
);
clusterRepository
.
save
(
cluster
);
auditService
.
audit
(
Cluster
.
class
.
getSimpleName
(),
cluster
.
getId
(),
Audit
.
OP
.
INSERT
,
createBy
);
}
private
void
createDefaultNamespace
(
String
appId
,
String
createBy
)
{
Namespace
ns
=
new
Namespace
();
ns
.
setAppId
(
appId
);
ns
.
setClusterName
(
ConfigConsts
.
CLUSTER_NAME_DEFAULT
);
ns
.
setNamespaceName
(
appId
);
ns
.
setDataChangeCreatedBy
(
createBy
);
ns
.
setDataChangeLastModifiedBy
(
createBy
);
namespaceRepository
.
save
(
ns
);
auditService
.
audit
(
Namespace
.
class
.
getSimpleName
(),
ns
.
getId
(),
Audit
.
OP
.
INSERT
,
createBy
);
}
}
apollo-biz/src/main/java/com/ctrip/apollo/biz/service/AppNamespaceService.java
0 → 100644
View file @
d521b24f
package
com
.
ctrip
.
apollo
.
biz
.
service
;
import
java.util.Objects
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Transactional
;
import
com.ctrip.apollo.biz.entity.AppNamespace
;
import
com.ctrip.apollo.biz.entity.Audit
;
import
com.ctrip.apollo.biz.repository.AppNamespaceRepository
;
import
com.ctrip.apollo.core.exception.ServiceException
;
@Service
public
class
AppNamespaceService
{
@Autowired
private
AppNamespaceRepository
appNamespaceRepository
;
@Autowired
private
AuditService
auditService
;
public
boolean
isAppNamespaceNameUnique
(
String
appId
,
String
namespaceName
)
{
Objects
.
requireNonNull
(
appId
,
"AppId must not be null"
);
Objects
.
requireNonNull
(
namespaceName
,
"Namespace must not be null"
);
return
Objects
.
isNull
(
appNamespaceRepository
.
findByAppIdAndName
(
appId
,
namespaceName
));
}
@Transactional
public
void
createDefaultAppNamespace
(
String
appId
,
String
createBy
)
{
if
(!
isAppNamespaceNameUnique
(
appId
,
appId
))
{
throw
new
ServiceException
(
"appnamespace not unique"
);
}
AppNamespace
appNs
=
new
AppNamespace
();
appNs
.
setAppId
(
appId
);
appNs
.
setName
(
appId
);
appNs
.
setComment
(
"default app namespace"
);
appNs
.
setDataChangeCreatedBy
(
createBy
);
appNs
.
setDataChangeLastModifiedBy
(
createBy
);
appNamespaceRepository
.
save
(
appNs
);
auditService
.
audit
(
AppNamespace
.
class
.
getSimpleName
(),
appNs
.
getId
(),
Audit
.
OP
.
INSERT
,
createBy
);
}
}
apollo-biz/src/main/java/com/ctrip/apollo/biz/service/AppService.java
View file @
d521b24f
package
com
.
ctrip
.
apollo
.
biz
.
service
;
import
java.util.List
;
import
java.util.Objects
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.data.domain.Page
;
...
...
@@ -12,6 +13,7 @@ import com.ctrip.apollo.biz.entity.App;
import
com.ctrip.apollo.biz.entity.Audit
;
import
com.ctrip.apollo.biz.repository.AppRepository
;
import
com.ctrip.apollo.common.utils.BeanUtils
;
import
com.ctrip.apollo.core.exception.ServiceException
;
@Service
public
class
AppService
{
...
...
@@ -22,6 +24,11 @@ public class AppService {
@Autowired
private
AuditService
auditService
;
public
boolean
isAppIdUnique
(
String
appId
)
{
Objects
.
requireNonNull
(
appId
,
"AppId must not be null"
);
return
Objects
.
isNull
(
appRepository
.
findByAppId
(
appId
));
}
@Transactional
public
void
delete
(
long
id
,
String
owner
)
{
appRepository
.
delete
(
id
);
...
...
@@ -44,6 +51,9 @@ public class AppService {
@Transactional
public
App
save
(
App
entity
)
{
if
(!
isAppIdUnique
(
entity
.
getAppId
()))
{
throw
new
ServiceException
(
"appId not unique"
);
}
App
app
=
appRepository
.
save
(
entity
);
auditService
.
audit
(
App
.
class
.
getSimpleName
(),
app
.
getId
(),
Audit
.
OP
.
INSERT
,
...
...
apollo-biz/src/main/java/com/ctrip/apollo/biz/service/ClusterService.java
View file @
d521b24f
package
com
.
ctrip
.
apollo
.
biz
.
service
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.Objects
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Transactional
;
...
...
@@ -8,6 +12,9 @@ import com.ctrip.apollo.biz.entity.Audit;
import
com.ctrip.apollo.biz.entity.Cluster
;
import
com.ctrip.apollo.biz.repository.ClusterRepository
;
import
com.ctrip.apollo.common.utils.BeanUtils
;
import
com.ctrip.apollo.core.ConfigConsts
;
import
com.ctrip.apollo.core.exception.ServiceException
;
import
com.google.common.base.Strings
;
@Service
public
class
ClusterService
{
...
...
@@ -18,12 +25,33 @@ public class ClusterService {
@Autowired
private
AuditService
auditService
;
public
boolean
isClusterNameUnique
(
String
appId
,
String
clusterName
)
{
Objects
.
requireNonNull
(
appId
,
"AppId must not be null"
);
Objects
.
requireNonNull
(
clusterName
,
"ClusterName must not be null"
);
return
Objects
.
isNull
(
clusterRepository
.
findByAppIdAndName
(
appId
,
clusterName
));
}
public
Cluster
findOne
(
String
appId
,
String
name
)
{
return
clusterRepository
.
findByAppIdAndName
(
appId
,
name
);
}
public
List
<
Cluster
>
findClusters
(
String
appId
)
{
if
(
Strings
.
isNullOrEmpty
(
appId
))
{
return
Collections
.
emptyList
();
}
List
<
Cluster
>
clusters
=
clusterRepository
.
findByAppId
(
appId
);
if
(
clusters
==
null
)
{
return
Collections
.
emptyList
();
}
return
clusters
;
}
@Transactional
public
Cluster
save
(
Cluster
entity
)
{
if
(!
isClusterNameUnique
(
entity
.
getAppId
(),
entity
.
getName
()))
{
throw
new
ServiceException
(
"cluster not unique"
);
}
Cluster
cluster
=
clusterRepository
.
save
(
entity
);
auditService
.
audit
(
Cluster
.
class
.
getSimpleName
(),
cluster
.
getId
(),
Audit
.
OP
.
INSERT
,
...
...
@@ -51,4 +79,19 @@ public class ClusterService {
return
managedCluster
;
}
@Transactional
public
void
createDefaultCluster
(
String
appId
,
String
createBy
)
{
if
(!
isClusterNameUnique
(
appId
,
ConfigConsts
.
CLUSTER_NAME_DEFAULT
))
{
throw
new
ServiceException
(
"cluster not unique"
);
}
Cluster
cluster
=
new
Cluster
();
cluster
.
setName
(
ConfigConsts
.
CLUSTER_NAME_DEFAULT
);
cluster
.
setAppId
(
appId
);
cluster
.
setDataChangeCreatedBy
(
createBy
);
cluster
.
setDataChangeLastModifiedBy
(
createBy
);
clusterRepository
.
save
(
cluster
);
auditService
.
audit
(
Cluster
.
class
.
getSimpleName
(),
cluster
.
getId
(),
Audit
.
OP
.
INSERT
,
createBy
);
}
}
apollo-biz/src/main/java/com/ctrip/apollo/biz/service/ItemService.java
View file @
d521b24f
package
com
.
ctrip
.
apollo
.
biz
.
service
;
import
java.util.Collections
;
import
java.util.List
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Transactional
;
...
...
@@ -47,6 +50,24 @@ public class ItemService {
return
item
;
}
public
List
<
Item
>
findItems
(
Long
namespaceId
)
{
List
<
Item
>
items
=
itemRepository
.
findByNamespaceIdOrderByLineNumAsc
(
namespaceId
);
if
(
items
==
null
)
{
return
Collections
.
emptyList
();
}
return
items
;
}
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
{
return
Collections
.
emptyList
();
}
}
@Transactional
public
Item
save
(
Item
entity
)
{
Item
item
=
itemRepository
.
save
(
entity
);
...
...
apollo-biz/src/main/java/com/ctrip/apollo/biz/service/NamespaceService.java
View file @
d521b24f
package
com
.
ctrip
.
apollo
.
biz
.
service
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.Objects
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Transactional
;
...
...
@@ -8,6 +12,8 @@ import com.ctrip.apollo.biz.entity.Audit;
import
com.ctrip.apollo.biz.entity.Namespace
;
import
com.ctrip.apollo.biz.repository.NamespaceRepository
;
import
com.ctrip.apollo.common.utils.BeanUtils
;
import
com.ctrip.apollo.core.ConfigConsts
;
import
com.ctrip.apollo.core.exception.ServiceException
;
@Service
public
class
NamespaceService
{
...
...
@@ -18,6 +24,14 @@ public class NamespaceService {
@Autowired
private
AuditService
auditService
;
public
boolean
isNamespaceUnique
(
String
appId
,
String
cluster
,
String
namespace
)
{
Objects
.
requireNonNull
(
appId
,
"AppId must not be null"
);
Objects
.
requireNonNull
(
cluster
,
"Cluster must not be null"
);
Objects
.
requireNonNull
(
namespace
,
"Namespace must not be null"
);
return
Objects
.
isNull
(
namespaceRepository
.
findByAppIdAndClusterNameAndNamespaceName
(
appId
,
cluster
,
namespace
));
}
@Transactional
public
void
delete
(
long
id
,
String
owner
)
{
namespaceRepository
.
delete
(
id
);
...
...
@@ -34,8 +48,19 @@ public class NamespaceService {
namespaceName
);
}
public
List
<
Namespace
>
findNamespaces
(
String
appId
,
String
clusterName
)
{
List
<
Namespace
>
groups
=
namespaceRepository
.
findByAppIdAndClusterName
(
appId
,
clusterName
);
if
(
groups
==
null
)
{
return
Collections
.
emptyList
();
}
return
groups
;
}
@Transactional
public
Namespace
save
(
Namespace
entity
)
{
if
(!
isNamespaceUnique
(
entity
.
getAppId
(),
entity
.
getClusterName
(),
entity
.
getNamespaceName
()))
{
throw
new
ServiceException
(
"namespace not unique"
);
}
Namespace
namespace
=
namespaceRepository
.
save
(
entity
);
auditService
.
audit
(
Namespace
.
class
.
getSimpleName
(),
namespace
.
getId
(),
Audit
.
OP
.
INSERT
,
...
...
@@ -53,7 +78,24 @@ public class NamespaceService {
auditService
.
audit
(
Namespace
.
class
.
getSimpleName
(),
managedNamespace
.
getId
(),
Audit
.
OP
.
UPDATE
,
managedNamespace
.
getDataChangeLastModifiedBy
());
return
managedNamespace
;
}
@Transactional
public
void
createDefaultNamespace
(
String
appId
,
String
createBy
)
{
if
(!
isNamespaceUnique
(
appId
,
ConfigConsts
.
CLUSTER_NAME_DEFAULT
,
appId
))
{
throw
new
ServiceException
(
"namespace not unique"
);
}
Namespace
ns
=
new
Namespace
();
ns
.
setAppId
(
appId
);
ns
.
setClusterName
(
ConfigConsts
.
CLUSTER_NAME_DEFAULT
);
ns
.
setNamespaceName
(
appId
);
ns
.
setDataChangeCreatedBy
(
createBy
);
ns
.
setDataChangeLastModifiedBy
(
createBy
);
namespaceRepository
.
save
(
ns
);
auditService
.
audit
(
Namespace
.
class
.
getSimpleName
(),
ns
.
getId
(),
Audit
.
OP
.
INSERT
,
createBy
);
}
}
apollo-biz/src/main/java/com/ctrip/apollo/biz/service/ReleaseService.java
View file @
d521b24f
package
com
.
ctrip
.
apollo
.
biz
.
service
;
import
java.util.Collections
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.List
;
...
...
@@ -40,6 +41,15 @@ public class ReleaseService {
return
release
;
}
public
List
<
Release
>
findReleases
(
String
appId
,
String
clusterName
,
String
namespaceName
)
{
List
<
Release
>
releases
=
releaseRepository
.
findByAppIdAndClusterNameAndNamespaceName
(
appId
,
clusterName
,
namespaceName
);
if
(
releases
==
null
)
{
return
Collections
.
emptyList
();
}
return
releases
;
}
@Transactional
public
Release
buildRelease
(
String
name
,
String
comment
,
Namespace
namespace
,
String
owner
)
{
List
<
Item
>
items
=
itemRepository
.
findByNamespaceIdOrderByLineNumAsc
(
namespace
.
getId
());
...
...
apollo-biz/src/main/java/com/ctrip/apollo/biz/service/ViewService.java
deleted
100644 → 0
View file @
cf7feb30
package
com
.
ctrip
.
apollo
.
biz
.
service
;
import
java.util.Collections
;
import
java.util.List
;
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.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.NamespaceRepository
;
import
com.ctrip.apollo.biz.repository.ItemRepository
;
import
com.ctrip.apollo.biz.repository.ReleaseRepository
;
import
com.google.common.base.Strings
;
/**
* config service for admin
*/
@Service
public
class
ViewService
{
@Autowired
private
ClusterRepository
clusterRepository
;
@Autowired
private
NamespaceRepository
namespaceRepository
;
@Autowired
private
ItemRepository
itemRepository
;
@Autowired
private
ReleaseRepository
releaseRepository
;
public
List
<
Cluster
>
findClusters
(
String
appId
)
{
if
(
Strings
.
isNullOrEmpty
(
appId
))
{
return
Collections
.
emptyList
();
}
List
<
Cluster
>
clusters
=
clusterRepository
.
findByAppId
(
appId
);
if
(
clusters
==
null
)
{
return
Collections
.
emptyList
();
}
return
clusters
;
}
public
List
<
Namespace
>
findNamespaces
(
String
appId
,
String
clusterName
)
{
List
<
Namespace
>
groups
=
namespaceRepository
.
findByAppIdAndClusterName
(
appId
,
clusterName
);
if
(
groups
==
null
)
{
return
Collections
.
emptyList
();
}
return
groups
;
}
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
{
return
Collections
.
emptyList
();
}
}
public
List
<
Item
>
findItems
(
Long
namespaceId
)
{
List
<
Item
>
items
=
itemRepository
.
findByNamespaceIdOrderByLineNumAsc
(
namespaceId
);
if
(
items
==
null
)
{
return
Collections
.
emptyList
();
}
return
items
;
}
public
List
<
Release
>
findReleases
(
String
appId
,
String
clusterName
,
String
namespaceName
)
{
List
<
Release
>
releases
=
releaseRepository
.
findByAppIdAndClusterNameAndNamespaceName
(
appId
,
clusterName
,
namespaceName
);
if
(
releases
==
null
)
{
return
Collections
.
emptyList
();
}
return
releases
;
}
}
apollo-biz/src/test/java/com/ctrip/apollo/biz/service/AdminServiceTest.java
View file @
d521b24f
...
...
@@ -17,7 +17,9 @@ import com.ctrip.apollo.biz.entity.App;
import
com.ctrip.apollo.biz.entity.Audit
;
import
com.ctrip.apollo.biz.entity.Cluster
;
import
com.ctrip.apollo.biz.entity.Namespace
;
import
com.ctrip.apollo.biz.repository.AppRepository
;
import
com.ctrip.apollo.core.ConfigConsts
;
import
com.ctrip.apollo.core.exception.ServiceException
;
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@SpringApplicationConfiguration
(
classes
=
BizTestConfiguration
.
class
)
...
...
@@ -29,10 +31,16 @@ public class AdminServiceTest {
private
AdminService
adminService
;
@Autowired
private
ViewService
view
Service
;
private
AuditService
audit
Service
;
@Autowired
private
AuditService
auditService
;
private
AppRepository
appRepository
;
@Autowired
private
ClusterService
clsuterService
;
@Autowired
private
NamespaceService
namespaceService
;
@Test
public
void
testCreateNewApp
()
{
...
...
@@ -50,11 +58,11 @@ public class AdminServiceTest {
app
=
adminService
.
createNewApp
(
app
);
Assert
.
assertEquals
(
appId
,
app
.
getAppId
());
List
<
Cluster
>
clusters
=
view
Service
.
findClusters
(
app
.
getAppId
());
List
<
Cluster
>
clusters
=
clsuter
Service
.
findClusters
(
app
.
getAppId
());
Assert
.
assertEquals
(
1
,
clusters
.
size
());
Assert
.
assertEquals
(
ConfigConsts
.
CLUSTER_NAME_DEFAULT
,
clusters
.
get
(
0
).
getName
());
List
<
Namespace
>
namespaces
=
view
Service
.
findNamespaces
(
appId
,
clusters
.
get
(
0
).
getName
());
List
<
Namespace
>
namespaces
=
namespace
Service
.
findNamespaces
(
appId
,
clusters
.
get
(
0
).
getName
());
Assert
.
assertEquals
(
1
,
namespaces
.
size
());
Assert
.
assertEquals
(
appId
,
namespaces
.
get
(
0
).
getNamespaceName
());
...
...
@@ -62,4 +70,22 @@ public class AdminServiceTest {
Assert
.
assertEquals
(
4
,
audits
.
size
());
}
@Test
(
expected
=
ServiceException
.
class
)
public
void
testCreateDuplicateApp
()
{
String
appId
=
"someAppId"
;
App
app
=
new
App
();
app
.
setAppId
(
appId
);
app
.
setName
(
"someAppName"
);
String
owner
=
"someOwnerName"
;
app
.
setOwnerName
(
owner
);
app
.
setOwnerEmail
(
"someOwnerName@ctrip.com"
);
app
.
setDataChangeCreatedBy
(
owner
);
app
.
setDataChangeLastModifiedBy
(
owner
);
app
.
setDataChangeCreatedTime
(
new
Date
());
appRepository
.
save
(
app
);
adminService
.
createNewApp
(
app
);
}
}
apollo-biz/src/test/java/com/ctrip/apollo/biz/service/ClusterServiceTest.java
0 → 100644
View file @
d521b24f
package
com
.
ctrip
.
apollo
.
biz
.
service
;
import
java.util.Date
;
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.annotation.Rollback
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.transaction.annotation.Transactional
;
import
com.ctrip.apollo.biz.BizTestConfiguration
;
import
com.ctrip.apollo.biz.entity.App
;
import
com.ctrip.apollo.core.exception.ServiceException
;
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@SpringApplicationConfiguration
(
classes
=
BizTestConfiguration
.
class
)
@Transactional
@Rollback
public
class
ClusterServiceTest
{
@Autowired
private
AdminService
adminService
;
@Autowired
private
ClusterService
clusterService
;
@Test
(
expected
=
ServiceException
.
class
)
public
void
testCreateDuplicateCluster
()
{
String
appId
=
"someAppId"
;
App
app
=
new
App
();
app
.
setAppId
(
appId
);
app
.
setName
(
"someAppName"
);
String
owner
=
"someOwnerName"
;
app
.
setOwnerName
(
owner
);
app
.
setOwnerEmail
(
"someOwnerName@ctrip.com"
);
app
.
setDataChangeCreatedBy
(
owner
);
app
.
setDataChangeLastModifiedBy
(
owner
);
app
.
setDataChangeCreatedTime
(
new
Date
());
adminService
.
createNewApp
(
app
);
clusterService
.
createDefaultCluster
(
appId
,
owner
);
}
}
apollo-biz/src/test/java/com/ctrip/apollo/biz/service/PrivilegeServiceTest.java
View file @
d521b24f
...
...
@@ -28,9 +28,12 @@ public class PrivilegeServiceTest {
private
AdminService
adminService
;
@Autowired
private
ViewService
view
Service
;
private
ClusterService
cluster
Service
;
@Autowired
private
NamespaceService
namespaceService
;
@Autowired
private
PrivilegeService
privilService
;
@Test
...
...
@@ -46,9 +49,9 @@ public class PrivilegeServiceTest {
app
.
setDataChangeCreatedTime
(
new
Date
());
App
newApp
=
adminService
.
createNewApp
(
app
);
List
<
Cluster
>
clusters
=
view
Service
.
findClusters
(
newApp
.
getAppId
());
List
<
Cluster
>
clusters
=
cluster
Service
.
findClusters
(
newApp
.
getAppId
());
List
<
Namespace
>
namespaces
=
view
Service
.
findNamespaces
(
newApp
.
getAppId
(),
clusters
.
get
(
0
).
getName
());
namespace
Service
.
findNamespaces
(
newApp
.
getAppId
(),
clusters
.
get
(
0
).
getName
());
Namespace
namespace
=
namespaces
.
get
(
0
);
privilService
.
addPrivilege
(
namespace
.
getId
(),
newApp
.
getOwnerName
(),
...
...
@@ -76,9 +79,9 @@ public class PrivilegeServiceTest {
app
.
setDataChangeLastModifiedBy
(
owner
);
app
.
setDataChangeCreatedTime
(
new
Date
());
App
newApp
=
adminService
.
createNewApp
(
app
);
List
<
Cluster
>
clusters
=
view
Service
.
findClusters
(
newApp
.
getAppId
());
List
<
Cluster
>
clusters
=
cluster
Service
.
findClusters
(
newApp
.
getAppId
());
List
<
Namespace
>
namespaces
=
view
Service
.
findNamespaces
(
newApp
.
getAppId
(),
clusters
.
get
(
0
).
getName
());
namespace
Service
.
findNamespaces
(
newApp
.
getAppId
(),
clusters
.
get
(
0
).
getName
());
Namespace
namespace
=
namespaces
.
get
(
0
);
privilService
.
addPrivilege
(
namespace
.
getId
(),
newApp
.
getOwnerName
(),
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment