blob: b0b4bc2c0914dd4540d1a64dcaf312a38f72b5dc [file] [log] [blame]
Jian Li3defa842019-02-12 00:31:35 +09001/*
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 */
16package org.onosproject.k8snode.cli;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.google.common.collect.Lists;
21import org.apache.karaf.shell.api.action.Command;
22import org.apache.karaf.shell.api.action.lifecycle.Service;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.k8snode.api.K8sApiConfig;
25import org.onosproject.k8snode.api.K8sApiConfigService;
26
27import java.util.Comparator;
28import java.util.List;
29
30import 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")
38public class K8sApiConfigListCommand extends AbstractShellCommand {
39
Jian Li1cee9882019-02-13 11:25:25 +090040 private static final String FORMAT = "%-10s%-25s%-10s%-10s";
Jian Li3defa842019-02-12 00:31:35 +090041
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 Li1cee9882019-02-13 11:25:25 +090051 print(FORMAT, "Scheme", "IpAddress", "Port", "State");
Jian Li3defa842019-02-12 00:31:35 +090052 for (K8sApiConfig config : configs) {
Jian Li1cee9882019-02-13 11:25:25 +090053 print(FORMAT, config.scheme().name(), config.ipAddress().toString(),
54 config.port(), config.state().name());
Jian Li3defa842019-02-12 00:31:35 +090055 }
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}