/*
 * Copyright 2016 Code Above Lab LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.codeabovelab.dm.cluman.ds.clusters;

import com.codeabovelab.dm.cluman.cluster.filter.Filter;
import com.codeabovelab.dm.cluman.cluster.filter.FilterFactory;
import com.codeabovelab.dm.cluman.ds.nodes.NodeRegistration;
import com.codeabovelab.dm.cluman.model.DiscoveryStorage;
import com.codeabovelab.dm.cluman.model.NodesGroup;
import org.springframework.util.StringUtils;

/**
 */
class OrphansNodeFilterFactory implements FilterFactory.Factory {

    private static final String PROTO = "nodes-orphan";
    public static final String FILTER = PROTO + ":";

    private final DiscoveryStorage ds;

    OrphansNodeFilterFactory(DiscoveryStorage ds) {
        this.ds = ds;
    }

    @Override
    public Filter create(String expr) {
        return (o) -> {
            String cluster = ((NodeRegistration) o).getNodeInfo().getCluster();
            if(!StringUtils.hasText(cluster)) {
                return true;
            }
            //also we want see nodes which cluster has been deleted
            NodesGroup group = ds.getCluster(cluster);
            return group == null || !group.getFeatures().contains(NodesGroup.Feature.SWARM);
        };
    }

    @Override
    public String getProtocol() {
        return PROTO;
    }
}

@@ -937,16 +937,69 @@
 sGroup;%0A
+import com.codeabovelab.dm.common.security.TempAuth;%0A
 import o
@@ -1659,16 +1659,73 @@
 deleted%0A
+            try(TempAuth ta = TempAuth.asSystem()) %7B%0A    
         
@@ -1771,16 +1771,20 @@
 uster);%0A
+    
         
@@ -1868,16 +1868,30 @@
 SWARM);%0A
+            %7D%0A
         

fix OrphansNodeFilterFactory with unprivileged user