Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
apollo
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
Administrator
apollo
Commits
43495f99
Commit
43495f99
authored
Dec 13, 2018
by
Jared.Tan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactoring
parent
dcc49834
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
58 deletions
+66
-58
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/controller/NamespaceController.java
...amework/apollo/portal/controller/NamespaceController.java
+14
-34
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/NamespaceService.java
...rip/framework/apollo/portal/service/NamespaceService.java
+52
-24
No files found.
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/controller/NamespaceController.java
View file @
43495f99
package
com
.
ctrip
.
framework
.
apollo
.
portal
.
controller
;
package
com
.
ctrip
.
framework
.
apollo
.
portal
.
controller
;
import
com.ctrip.framework.apollo.common.dto.AppNamespaceDTO
;
import
static
com
.
ctrip
.
framework
.
apollo
.
common
.
utils
.
RequestPrecondition
.
checkModel
;
import
com.ctrip.framework.apollo.common.http.MultiResponseEntity
;
import
com.ctrip.framework.apollo.common.http.RichResponseEntity
;
import
com.ctrip.framework.apollo.common.utils.BeanUtils
;
import
com.ctrip.framework.apollo.portal.api.AdminServiceAPI
;
import
com.ctrip.framework.apollo.portal.component.PermissionValidator
;
import
com.ctrip.framework.apollo.portal.listener.AppNamespaceDeletionEvent
;
import
com.google.common.collect.Sets
;
import
com.ctrip.framework.apollo.common.dto.AppNamespaceDTO
;
import
com.ctrip.framework.apollo.common.dto.NamespaceDTO
;
import
com.ctrip.framework.apollo.common.dto.NamespaceDTO
;
import
com.ctrip.framework.apollo.common.entity.AppNamespace
;
import
com.ctrip.framework.apollo.common.entity.AppNamespace
;
import
com.ctrip.framework.apollo.common.exception.BadRequestException
;
import
com.ctrip.framework.apollo.common.exception.BadRequestException
;
import
com.ctrip.framework.apollo.common.http.MultiResponseEntity
;
import
com.ctrip.framework.apollo.common.http.RichResponseEntity
;
import
com.ctrip.framework.apollo.common.utils.BeanUtils
;
import
com.ctrip.framework.apollo.common.utils.InputValidator
;
import
com.ctrip.framework.apollo.common.utils.InputValidator
;
import
com.ctrip.framework.apollo.common.utils.RequestPrecondition
;
import
com.ctrip.framework.apollo.common.utils.RequestPrecondition
;
import
com.ctrip.framework.apollo.core.enums.Env
;
import
com.ctrip.framework.apollo.core.enums.Env
;
import
com.ctrip.framework.apollo.portal.api.AdminServiceAPI
;
import
com.ctrip.framework.apollo.portal.component.PermissionValidator
;
import
com.ctrip.framework.apollo.portal.component.config.PortalConfig
;
import
com.ctrip.framework.apollo.portal.component.config.PortalConfig
;
import
com.ctrip.framework.apollo.portal.constant.RoleType
;
import
com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO
;
import
com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO
;
import
com.ctrip.framework.apollo.portal.entity.model.NamespaceCreationModel
;
import
com.ctrip.framework.apollo.portal.entity.model.NamespaceCreationModel
;
import
com.ctrip.framework.apollo.portal.listener.AppNamespaceCreationEvent
;
import
com.ctrip.framework.apollo.portal.listener.AppNamespaceCreationEvent
;
import
com.ctrip.framework.apollo.portal.listener.AppNamespaceDeletionEvent
;
import
com.ctrip.framework.apollo.portal.service.AppNamespaceService
;
import
com.ctrip.framework.apollo.portal.service.AppNamespaceService
;
import
com.ctrip.framework.apollo.portal.service.NamespaceService
;
import
com.ctrip.framework.apollo.portal.service.NamespaceService
;
import
com.ctrip.framework.apollo.portal.service.RoleInitializationService
;
import
com.ctrip.framework.apollo.portal.service.RoleInitializationService
;
import
com.ctrip.framework.apollo.portal.service.RolePermissionService
;
import
com.ctrip.framework.apollo.portal.spi.UserInfoHolder
;
import
com.ctrip.framework.apollo.portal.spi.UserInfoHolder
;
import
com.ctrip.framework.apollo.portal.util.RoleUtils
;
import
com.ctrip.framework.apollo.tracer.Tracer
;
import
com.ctrip.framework.apollo.tracer.Tracer
;
import
com.google.common.collect.Sets
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.Set
;
import
java.util.stream.Collectors
;
import
java.util.stream.Collectors
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
...
@@ -44,11 +43,6 @@ import org.springframework.web.bind.annotation.RequestMethod;
...
@@ -44,11 +43,6 @@ import org.springframework.web.bind.annotation.RequestMethod;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.bind.annotation.RestController
;
import
java.util.List
;
import
java.util.Map
;
import
static
com
.
ctrip
.
framework
.
apollo
.
common
.
utils
.
RequestPrecondition
.
checkModel
;
@RestController
@RestController
public
class
NamespaceController
{
public
class
NamespaceController
{
...
@@ -65,8 +59,6 @@ public class NamespaceController {
...
@@ -65,8 +59,6 @@ public class NamespaceController {
@Autowired
@Autowired
private
RoleInitializationService
roleInitializationService
;
private
RoleInitializationService
roleInitializationService
;
@Autowired
@Autowired
private
RolePermissionService
rolePermissionService
;
@Autowired
private
PortalConfig
portalConfig
;
private
PortalConfig
portalConfig
;
@Autowired
@Autowired
private
PermissionValidator
permissionValidator
;
private
PermissionValidator
permissionValidator
;
...
@@ -145,7 +137,7 @@ public class NamespaceController {
...
@@ -145,7 +137,7 @@ public class NamespaceController {
}
}
}
}
assignNamespaceRoleToOperator
(
appId
,
namespaceName
);
namespaceService
.
assignNamespaceRoleToOperator
(
appId
,
namespaceName
,
userInfoHolder
.
getUser
().
getUserId
()
);
return
ResponseEntity
.
ok
().
build
();
return
ResponseEntity
.
ok
().
build
();
}
}
...
@@ -199,7 +191,8 @@ public class NamespaceController {
...
@@ -199,7 +191,8 @@ public class NamespaceController {
AppNamespace
createdAppNamespace
=
appNamespaceService
.
createAppNamespaceInLocal
(
appNamespace
,
appendNamespacePrefix
);
AppNamespace
createdAppNamespace
=
appNamespaceService
.
createAppNamespaceInLocal
(
appNamespace
,
appendNamespacePrefix
);
if
(
portalConfig
.
canAppAdminCreatePrivateNamespace
()
||
createdAppNamespace
.
isPublic
())
{
if
(
portalConfig
.
canAppAdminCreatePrivateNamespace
()
||
createdAppNamespace
.
isPublic
())
{
assignNamespaceRoleToOperator
(
appId
,
appNamespace
.
getName
());
namespaceService
.
assignNamespaceRoleToOperator
(
appId
,
appNamespace
.
getName
(),
userInfoHolder
.
getUser
().
getUserId
());
}
}
publisher
.
publishEvent
(
new
AppNamespaceCreationEvent
(
createdAppNamespace
));
publisher
.
publishEvent
(
new
AppNamespaceCreationEvent
(
createdAppNamespace
));
...
@@ -282,17 +275,4 @@ public class NamespaceController {
...
@@ -282,17 +275,4 @@ public class NamespaceController {
return
Sets
.
union
(
missingAppNamespaceNames
,
missingNamespaceNames
);
return
Sets
.
union
(
missingAppNamespaceNames
,
missingNamespaceNames
);
}
}
private
void
assignNamespaceRoleToOperator
(
String
appId
,
String
namespaceName
)
{
//default assign modify、release namespace role to namespace creator
String
operator
=
userInfoHolder
.
getUser
().
getUserId
();
rolePermissionService
.
assignRoleToUsers
(
RoleUtils
.
buildNamespaceRoleName
(
appId
,
namespaceName
,
RoleType
.
MODIFY_NAMESPACE
),
Sets
.
newHashSet
(
operator
),
operator
);
rolePermissionService
.
assignRoleToUsers
(
RoleUtils
.
buildNamespaceRoleName
(
appId
,
namespaceName
,
RoleType
.
RELEASE_NAMESPACE
),
Sets
.
newHashSet
(
operator
),
operator
);
}
}
}
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/NamespaceService.java
View file @
43495f99
package
com
.
ctrip
.
framework
.
apollo
.
portal
.
service
;
package
com
.
ctrip
.
framework
.
apollo
.
portal
.
service
;
import
com.ctrip.framework.apollo.portal.component.config.PortalConfig
;
import
com.google.common.collect.Maps
;
import
com.google.gson.Gson
;
import
com.ctrip.framework.apollo.common.constants.GsonType
;
import
com.ctrip.framework.apollo.common.constants.GsonType
;
import
com.ctrip.framework.apollo.common.dto.ItemDTO
;
import
com.ctrip.framework.apollo.common.dto.ItemDTO
;
import
com.ctrip.framework.apollo.common.dto.NamespaceDTO
;
import
com.ctrip.framework.apollo.common.dto.NamespaceDTO
;
...
@@ -17,20 +12,28 @@ import com.ctrip.framework.apollo.core.enums.Env;
...
@@ -17,20 +12,28 @@ import com.ctrip.framework.apollo.core.enums.Env;
import
com.ctrip.framework.apollo.core.utils.StringUtils
;
import
com.ctrip.framework.apollo.core.utils.StringUtils
;
import
com.ctrip.framework.apollo.portal.api.AdminServiceAPI
;
import
com.ctrip.framework.apollo.portal.api.AdminServiceAPI
;
import
com.ctrip.framework.apollo.portal.component.PortalSettings
;
import
com.ctrip.framework.apollo.portal.component.PortalSettings
;
import
com.ctrip.framework.apollo.portal.component.config.PortalConfig
;
import
com.ctrip.framework.apollo.portal.constant.RoleType
;
import
com.ctrip.framework.apollo.portal.constant.TracerEventType
;
import
com.ctrip.framework.apollo.portal.constant.TracerEventType
;
import
com.ctrip.framework.apollo.portal.entity.bo.ItemBO
;
import
com.ctrip.framework.apollo.portal.entity.bo.ItemBO
;
import
com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO
;
import
com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO
;
import
com.ctrip.framework.apollo.portal.spi.UserInfoHolder
;
import
com.ctrip.framework.apollo.portal.spi.UserInfoHolder
;
import
com.ctrip.framework.apollo.portal.util.RoleUtils
;
import
com.ctrip.framework.apollo.tracer.Tracer
;
import
com.ctrip.framework.apollo.tracer.Tracer
;
import
com.google.common.collect.Maps
;
import
com.google.common.collect.Sets
;
import
com.google.gson.Gson
;
import
java.util.HashMap
;
import
java.util.LinkedList
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
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
org.springframework.transaction.annotation.Transactional
;
import
org.springframework.transaction.annotation.Transactional
;
import
java.util.*
;
@Service
@Service
public
class
NamespaceService
{
public
class
NamespaceService
{
...
@@ -55,6 +58,8 @@ public class NamespaceService {
...
@@ -55,6 +58,8 @@ public class NamespaceService {
private
InstanceService
instanceService
;
private
InstanceService
instanceService
;
@Autowired
@Autowired
private
NamespaceBranchService
branchService
;
private
NamespaceBranchService
branchService
;
@Autowired
private
RolePermissionService
rolePermissionService
;
public
NamespaceDTO
createNamespace
(
Env
env
,
NamespaceDTO
namespace
)
{
public
NamespaceDTO
createNamespace
(
Env
env
,
NamespaceDTO
namespace
)
{
...
@@ -65,8 +70,8 @@ public class NamespaceService {
...
@@ -65,8 +70,8 @@ public class NamespaceService {
NamespaceDTO
createdNamespace
=
namespaceAPI
.
createNamespace
(
env
,
namespace
);
NamespaceDTO
createdNamespace
=
namespaceAPI
.
createNamespace
(
env
,
namespace
);
Tracer
.
logEvent
(
TracerEventType
.
CREATE_NAMESPACE
,
Tracer
.
logEvent
(
TracerEventType
.
CREATE_NAMESPACE
,
String
.
format
(
"%s+%s+%s+%s"
,
namespace
.
getAppId
(),
env
,
namespace
.
getClusterName
(),
String
.
format
(
"%s+%s+%s+%s"
,
namespace
.
getAppId
(),
env
,
namespace
.
getClusterName
(),
namespace
.
getNamespaceName
()));
namespace
.
getNamespaceName
()));
return
createdNamespace
;
return
createdNamespace
;
}
}
...
@@ -78,19 +83,24 @@ public class NamespaceService {
...
@@ -78,19 +83,24 @@ public class NamespaceService {
//1. check parent namespace has not instances
//1. check parent namespace has not instances
if
(
namespaceHasInstances
(
appId
,
env
,
clusterName
,
namespaceName
))
{
if
(
namespaceHasInstances
(
appId
,
env
,
clusterName
,
namespaceName
))
{
throw
new
BadRequestException
(
"Can not delete namespace because namespace has active instances"
);
throw
new
BadRequestException
(
"Can not delete namespace because namespace has active instances"
);
}
}
//2. check child namespace has not instances
//2. check child namespace has not instances
NamespaceDTO
childNamespace
=
branchService
.
findBranchBaseInfo
(
appId
,
env
,
clusterName
,
namespaceName
);
NamespaceDTO
childNamespace
=
branchService
.
findBranchBaseInfo
(
appId
,
env
,
clusterName
,
namespaceName
);
if
(
childNamespace
!=
null
&&
if
(
childNamespace
!=
null
&&
namespaceHasInstances
(
appId
,
env
,
childNamespace
.
getClusterName
(),
namespaceName
))
{
namespaceHasInstances
(
appId
,
env
,
childNamespace
.
getClusterName
(),
namespaceName
))
{
throw
new
BadRequestException
(
"Can not delete namespace because namespace's branch has active instances"
);
throw
new
BadRequestException
(
"Can not delete namespace because namespace's branch has active instances"
);
}
}
//3. check public namespace has not associated namespace
//3. check public namespace has not associated namespace
if
(
appNamespace
!=
null
&&
appNamespace
.
isPublic
()
&&
publicAppNamespaceHasAssociatedNamespace
(
namespaceName
,
env
))
{
if
(
appNamespace
!=
null
&&
appNamespace
.
isPublic
()
&&
publicAppNamespaceHasAssociatedNamespace
(
throw
new
BadRequestException
(
"Can not delete public namespace which has associated namespaces"
);
namespaceName
,
env
))
{
throw
new
BadRequestException
(
"Can not delete public namespace which has associated namespaces"
);
}
}
String
operator
=
userInfoHolder
.
getUser
().
getUserId
();
String
operator
=
userInfoHolder
.
getUser
().
getUserId
();
...
@@ -98,7 +108,8 @@ public class NamespaceService {
...
@@ -98,7 +108,8 @@ public class NamespaceService {
namespaceAPI
.
deleteNamespace
(
env
,
appId
,
clusterName
,
namespaceName
,
operator
);
namespaceAPI
.
deleteNamespace
(
env
,
appId
,
clusterName
,
namespaceName
,
operator
);
}
}
public
NamespaceDTO
loadNamespaceBaseInfo
(
String
appId
,
Env
env
,
String
clusterName
,
String
namespaceName
)
{
public
NamespaceDTO
loadNamespaceBaseInfo
(
String
appId
,
Env
env
,
String
clusterName
,
String
namespaceName
)
{
NamespaceDTO
namespace
=
namespaceAPI
.
loadNamespace
(
appId
,
env
,
clusterName
,
namespaceName
);
NamespaceDTO
namespace
=
namespaceAPI
.
loadNamespace
(
appId
,
env
,
clusterName
,
namespaceName
);
if
(
namespace
==
null
)
{
if
(
namespace
==
null
)
{
throw
new
BadRequestException
(
"namespaces not exist"
);
throw
new
BadRequestException
(
"namespaces not exist"
);
...
@@ -125,7 +136,7 @@ public class NamespaceService {
...
@@ -125,7 +136,7 @@ public class NamespaceService {
namespaceBOs
.
add
(
namespaceBO
);
namespaceBOs
.
add
(
namespaceBO
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
logger
.
error
(
"parse namespace error. app id:{}, env:{}, clusterName:{}, namespace:{}"
,
logger
.
error
(
"parse namespace error. app id:{}, env:{}, clusterName:{}, namespace:{}"
,
appId
,
env
,
clusterName
,
namespace
.
getNamespaceName
(),
e
);
appId
,
env
,
clusterName
,
namespace
.
getNamespaceName
(),
e
);
throw
e
;
throw
e
;
}
}
}
}
...
@@ -137,12 +148,14 @@ public class NamespaceService {
...
@@ -137,12 +148,14 @@ public class NamespaceService {
return
namespaceAPI
.
findNamespaceByCluster
(
appId
,
env
,
clusterName
);
return
namespaceAPI
.
findNamespaceByCluster
(
appId
,
env
,
clusterName
);
}
}
public
List
<
NamespaceDTO
>
getPublicAppNamespaceAllNamespaces
(
Env
env
,
String
publicNamespaceName
,
int
page
,
public
List
<
NamespaceDTO
>
getPublicAppNamespaceAllNamespaces
(
Env
env
,
String
publicNamespaceName
,
int
size
)
{
int
page
,
int
size
)
{
return
namespaceAPI
.
getPublicAppNamespaceAllNamespaces
(
env
,
publicNamespaceName
,
page
,
size
);
return
namespaceAPI
.
getPublicAppNamespaceAllNamespaces
(
env
,
publicNamespaceName
,
page
,
size
);
}
}
public
NamespaceBO
loadNamespaceBO
(
String
appId
,
Env
env
,
String
clusterName
,
String
namespaceName
)
{
public
NamespaceBO
loadNamespaceBO
(
String
appId
,
Env
env
,
String
clusterName
,
String
namespaceName
)
{
NamespaceDTO
namespace
=
namespaceAPI
.
loadNamespace
(
appId
,
env
,
clusterName
,
namespaceName
);
NamespaceDTO
namespace
=
namespaceAPI
.
loadNamespace
(
appId
,
env
,
clusterName
,
namespaceName
);
if
(
namespace
==
null
)
{
if
(
namespace
==
null
)
{
throw
new
BadRequestException
(
"namespaces not exist"
);
throw
new
BadRequestException
(
"namespaces not exist"
);
...
@@ -150,7 +163,8 @@ public class NamespaceService {
...
@@ -150,7 +163,8 @@ public class NamespaceService {
return
transformNamespace2BO
(
env
,
namespace
);
return
transformNamespace2BO
(
env
,
namespace
);
}
}
public
boolean
namespaceHasInstances
(
String
appId
,
Env
env
,
String
clusterName
,
String
namespaceName
)
{
public
boolean
namespaceHasInstances
(
String
appId
,
Env
env
,
String
clusterName
,
String
namespaceName
)
{
return
instanceService
.
getInstanceCountByNamepsace
(
appId
,
env
,
clusterName
,
namespaceName
)
>
0
;
return
instanceService
.
getInstanceCountByNamepsace
(
appId
,
env
,
clusterName
,
namespaceName
)
>
0
;
}
}
...
@@ -159,9 +173,10 @@ public class NamespaceService {
...
@@ -159,9 +173,10 @@ public class NamespaceService {
}
}
public
NamespaceBO
findPublicNamespaceForAssociatedNamespace
(
Env
env
,
String
appId
,
public
NamespaceBO
findPublicNamespaceForAssociatedNamespace
(
Env
env
,
String
appId
,
String
clusterName
,
String
namespaceName
)
{
String
clusterName
,
String
namespaceName
)
{
NamespaceDTO
namespace
=
NamespaceDTO
namespace
=
namespaceAPI
.
findPublicNamespaceForAssociatedNamespace
(
env
,
appId
,
clusterName
,
namespaceName
);
namespaceAPI
.
findPublicNamespaceForAssociatedNamespace
(
env
,
appId
,
clusterName
,
namespaceName
);
return
transformNamespace2BO
(
env
,
namespace
);
return
transformNamespace2BO
(
env
,
namespace
);
}
}
...
@@ -229,7 +244,8 @@ public class NamespaceService {
...
@@ -229,7 +244,8 @@ public class NamespaceService {
NamespaceDTO
namespaceDTO
=
namespace
.
getBaseInfo
();
NamespaceDTO
namespaceDTO
=
namespace
.
getBaseInfo
();
//先从当前appId下面找,包含私有的和公共的
//先从当前appId下面找,包含私有的和公共的
AppNamespace
appNamespace
=
AppNamespace
appNamespace
=
appNamespaceService
.
findByAppIdAndName
(
namespaceDTO
.
getAppId
(),
namespaceDTO
.
getNamespaceName
());
appNamespaceService
.
findByAppIdAndName
(
namespaceDTO
.
getAppId
(),
namespaceDTO
.
getNamespaceName
());
//再从公共的app namespace里面找
//再从公共的app namespace里面找
if
(
appNamespace
==
null
)
{
if
(
appNamespace
==
null
)
{
appNamespace
=
appNamespaceService
.
findPublicAppNamespace
(
namespaceDTO
.
getNamespaceName
());
appNamespace
=
appNamespaceService
.
findPublicAppNamespace
(
namespaceDTO
.
getNamespaceName
());
...
@@ -291,4 +307,16 @@ public class NamespaceService {
...
@@ -291,4 +307,16 @@ public class NamespaceService {
return
itemBO
;
return
itemBO
;
}
}
public
void
assignNamespaceRoleToOperator
(
String
appId
,
String
namespaceName
,
String
operator
)
{
//default assign modify、release namespace role to namespace creator
rolePermissionService
.
assignRoleToUsers
(
RoleUtils
.
buildNamespaceRoleName
(
appId
,
namespaceName
,
RoleType
.
MODIFY_NAMESPACE
),
Sets
.
newHashSet
(
operator
),
operator
);
rolePermissionService
.
assignRoleToUsers
(
RoleUtils
.
buildNamespaceRoleName
(
appId
,
namespaceName
,
RoleType
.
RELEASE_NAMESPACE
),
Sets
.
newHashSet
(
operator
),
operator
);
}
}
}
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