delete-namespace-modal-directive.js 6.74 KB
Newer Older
lepdou committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
directive_module.directive('deletenamespacemodal', deleteNamespaceModalDirective);

function deleteNamespaceModalDirective($window, $q, toastr, AppUtil, EventManager,
                                       PermissionService, UserService, NamespaceService) {
    return {
        restrict: 'E',
        templateUrl: '../../views/component/delete-namespace-modal.html',
        transclude: true,
        replace: true,
        scope: {
            env: '='
        },
        link: function (scope) {

            scope.doDeleteNamespace = doDeleteNamespace;

            EventManager.subscribe(EventManager.EventType.PRE_DELETE_NAMESPACE, function (context) {
                var toDeleteNamespace = context.namespace;
                scope.toDeleteNamespace = toDeleteNamespace;

                //1. check namespace is not private
                if (!checkNotPrivateNamespace(toDeleteNamespace)) {
                    return;
                }

                //2. check operator has master permission
                checkPermission(toDeleteNamespace).then(function () {

                    //3. check namespace's master branch has not instances
                    if (!checkMasterInstance(toDeleteNamespace)) {
                        return;
                    }

                    //4. check namespace's gray branch has not instances
                    if (!checkBranchInstance(toDeleteNamespace)) {
                        return;
                    }

                    if (toDeleteNamespace.isLinkedNamespace) {
                        showDeleteNamespaceConfirmDialog();
                    } else {
                        //5. check public namespace has not associated namespace
                        checkPublicNamespace(toDeleteNamespace).then(function () {
                            showDeleteNamespaceConfirmDialog();
                        });
                    }
                })

            });

            function checkNotPrivateNamespace(namespace) {
                if (!namespace.isPublic) {
                    toastr.error("不能删除私有的Namespace", "删除失败");
                    return false;
                }

                return true;
            }

            function checkPermission(namespace) {
                var d = $q.defer();

                UserService.load_user().then(function (currentUser) {

                    var isAppMasterUser = false;

                    PermissionService.get_app_role_users(namespace.baseInfo.appId)
                        .then(function (appRoleUsers) {

                            var masterUsers = [];

                            appRoleUsers.masterUsers.forEach(function (user) {
                                masterUsers.push(user.userId);

                                if (currentUser.userId == user.userId) {
                                    isAppMasterUser = true;
                                }
                            });

                            scope.masterUsers = masterUsers;
                            scope.isAppMasterUser = isAppMasterUser;

                            if (!isAppMasterUser) {
                                toastr.error("您没有项目管理员权限,只有管理员才能删除Namespace,请找项目管理员 [" + scope.masterUsers.join(",")
                                             + "] 删除Namespace", "删除失败");
                                d.reject();
                            } else {
                                d.resolve();
                            }
                        });
                });

                return d.promise;
            }

            function checkMasterInstance(namespace) {
                if (namespace.instancesCount > 0) {
                    EventManager.emit(EventManager.EventType.DELETE_NAMESPACE_FAILED, {
                        namespace: namespace,
                        reason: 'master_instance'
                    });

                    return false;
                }

                return true;
            }

            function checkBranchInstance(namespace) {
                if (namespace.hasBranch && namespace.branch.latestReleaseInstances.total > 0) {
                    EventManager.emit(EventManager.EventType.DELETE_NAMESPACE_FAILED, {
                        namespace: namespace,
                        reason: 'branch_instance'
                    });

                    return false;
                }

                return true;
            }

            function checkPublicNamespace(namespace) {
                var d = $q.defer();

                var publicAppId = namespace.baseInfo.appId;
                NamespaceService.getPublicAppNamespaceAllNamespaces(scope.env,
                                                                    namespace.baseInfo.namespaceName,
                                                                    0, 20)
                    .then(function (associatedNamespaces) {
                        var otherAppAssociatedNamespaces = [];
                        associatedNamespaces.forEach(function (associatedNamespace) {
                            if (associatedNamespace.appId != publicAppId) {
                                otherAppAssociatedNamespaces.push(associatedNamespace);
                            }
                        });

                        if (otherAppAssociatedNamespaces.length) {
                            EventManager.emit(EventManager.EventType.DELETE_NAMESPACE_FAILED, {
                                namespace: namespace,
                                reason: 'public_namespace',
                                otherAppAssociatedNamespaces: otherAppAssociatedNamespaces
                            });
                            d.reject();
                        } else {
                            d.resolve();
                        }

                    });

                return d.promise;

            }

            function showDeleteNamespaceConfirmDialog() {
                AppUtil.showModal('#deleteNamespaceModal');

            }

            function doDeleteNamespace() {
                var toDeleteNamespace = scope.toDeleteNamespace;
                NamespaceService.deleteNamespace(toDeleteNamespace.baseInfo.appId, scope.env,
                                                 toDeleteNamespace.baseInfo.clusterName,
                                                 toDeleteNamespace.baseInfo.namespaceName)
                    .then(function () {
                        toastr.success("删除成功");

                        setTimeout(function () {
                            $window.location.reload();
                        }, 1000);

                    }, function (result) {
                        AppUtil.showErrorMsg(result, "删除失败");
                    })

            }

        }
    }
}