Jian Li | f16e885 | 2019-01-22 22:55:31 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.k8snode.cli; |
| 17 | |
| 18 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 19 | import org.apache.karaf.shell.api.console.CommandLine; |
| 20 | import org.apache.karaf.shell.api.console.Completer; |
| 21 | import org.apache.karaf.shell.api.console.Session; |
| 22 | import org.apache.karaf.shell.support.completers.StringsCompleter; |
| 23 | import org.onosproject.k8snode.api.K8sNode; |
| 24 | import org.onosproject.k8snode.api.K8sNodeService; |
| 25 | |
| 26 | import java.util.List; |
| 27 | import java.util.Set; |
| 28 | import java.util.SortedSet; |
| 29 | import java.util.stream.Collectors; |
| 30 | |
| 31 | import static org.onosproject.cli.AbstractShellCommand.get; |
| 32 | |
| 33 | /** |
| 34 | * Kubernetes host completer. |
| 35 | */ |
| 36 | @Service |
| 37 | public class K8sHostnameCompleter implements Completer { |
| 38 | @Override |
| 39 | public int complete(Session session, CommandLine commandLine, List<String> candidates) { |
| 40 | StringsCompleter delegate = new StringsCompleter(); |
| 41 | K8sNodeService nodeService = get(K8sNodeService.class); |
| 42 | |
| 43 | Set<String> hostnames = nodeService.nodes().stream() |
| 44 | .map(K8sNode::hostname) |
| 45 | .collect(Collectors.toSet()); |
| 46 | SortedSet<String> strings = delegate.getStrings(); |
| 47 | |
| 48 | strings.addAll(hostnames); |
| 49 | |
| 50 | return delegate.complete(session, commandLine, candidates); |
| 51 | } |
| 52 | } |