Jian Li | 3defa84 | 2019-02-12 00:31:35 +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 com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.google.common.collect.Lists; |
| 21 | import org.apache.karaf.shell.api.action.Command; |
| 22 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.k8snode.api.K8sApiConfig; |
| 25 | import org.onosproject.k8snode.api.K8sApiConfigService; |
| 26 | |
| 27 | import java.util.Comparator; |
| 28 | import java.util.List; |
| 29 | |
| 30 | import static org.onosproject.k8snode.util.K8sNodeUtil.prettyJson; |
| 31 | |
| 32 | /** |
| 33 | * Lists all kubernetes API server configs registered to the service. |
| 34 | */ |
| 35 | @Service |
| 36 | @Command(scope = "onos", name = "k8s-api-configs", |
| 37 | description = "Lists all kubernetes API server configs registered to the service") |
| 38 | public class K8sApiConfigListCommand extends AbstractShellCommand { |
| 39 | |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 40 | private static final String FORMAT = "%-10s%-25s%-10s%-10s"; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 41 | |
| 42 | @Override |
| 43 | protected void doExecute() { |
| 44 | K8sApiConfigService configService = get(K8sApiConfigService.class); |
| 45 | List<K8sApiConfig> configs = Lists.newArrayList(configService.apiConfigs()); |
| 46 | configs.sort(Comparator.comparing(K8sApiConfig::ipAddress)); |
| 47 | |
| 48 | if (outputJson()) { |
| 49 | print("%s", json(configs)); |
| 50 | } else { |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 51 | print(FORMAT, "Scheme", "IpAddress", "Port", "State"); |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 52 | for (K8sApiConfig config : configs) { |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 53 | print(FORMAT, config.scheme().name(), config.ipAddress().toString(), |
| 54 | config.port(), config.state().name()); |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 55 | } |
| 56 | print("Total %s API configs", configService.apiConfigs().size()); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | private String json(List<K8sApiConfig> configs) { |
| 61 | ObjectMapper mapper = new ObjectMapper(); |
| 62 | ArrayNode result = mapper.createArrayNode(); |
| 63 | for (K8sApiConfig config : configs) { |
| 64 | result.add(jsonForEntity(config, K8sApiConfig.class)); |
| 65 | } |
| 66 | return prettyJson(mapper, result.toString()); |
| 67 | } |
| 68 | } |