blob: d01c33aa143492391295b5db4971e16dc40ac23f [file] [log] [blame]
Jian Lic4604302020-12-25 02:24:16 +09001/*
2 * Copyright 2020-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.kubevirtnode.cli;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.lifecycle.Service;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.kubevirtnode.api.KubevirtApiConfig;
24import org.onosproject.kubevirtnode.api.KubevirtApiConfigService;
25
26import static org.onosproject.kubevirtnode.util.KubevirtNodeUtil.prettyJson;
27
28/**
29 * Lists all KubeVirt API server configs registered to the service.
30 */
31@Service
32@Command(scope = "onos", name = "kubevirt-api-configs",
33 description = "Lists all KubeVirt API server configs registered to the service")
Jian Li138f51f2021-01-06 03:29:58 +090034public class KubevirtListApiConfigsCommand extends AbstractShellCommand {
Jian Lic4604302020-12-25 02:24:16 +090035
36 private static final String FORMAT = "%-10s%-25s%-10s%-10s";
37
38 @Override
39 protected void doExecute() throws Exception {
40 KubevirtApiConfigService service = get(KubevirtApiConfigService.class);
41 KubevirtApiConfig config = service.apiConfig();
42
43 if (outputJson()) {
44 print("%s", json(config));
45 } else {
46 print(FORMAT, "Scheme", "IpAddress", "Port", "State");
Jian Li304dca42020-12-27 23:22:46 +090047 if (config != null) {
48 print(FORMAT, config.scheme().name(), config.ipAddress().toString(),
49 config.port(), config.state().name());
50 } else {
51 print("Kubevirt config not found!");
52 }
53
Jian Lic4604302020-12-25 02:24:16 +090054 }
55 }
56
57 private String json(KubevirtApiConfig config) {
58 ObjectMapper mapper = new ObjectMapper();
59 ObjectNode node = jsonForEntity(config, KubevirtApiConfig.class);
60 return prettyJson(mapper, node.toString());
61 }
62}