Jian Li | f248307 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.kubevirtnode.cli; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.apache.karaf.shell.api.action.Command; |
| 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.kubevirtnode.api.KubevirtApiConfig; |
| 24 | import org.onosproject.kubevirtnode.api.KubevirtApiConfigService; |
| 25 | |
| 26 | import 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 Li | 4fe40e5 | 2021-01-06 03:29:58 +0900 | [diff] [blame] | 34 | public class KubevirtListApiConfigsCommand extends AbstractShellCommand { |
Jian Li | f248307 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 35 | |
Jian Li | e0eaf5c | 2021-09-06 10:02:13 +0900 | [diff] [blame] | 36 | private static final String FORMAT = "%-10s%-20s%-10s%-25s%-10s"; |
Jian Li | f248307 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 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 { |
Jian Li | e0eaf5c | 2021-09-06 10:02:13 +0900 | [diff] [blame] | 46 | print(FORMAT, "Scheme", "Server IP", "Port", "Controller IP", "State"); |
| 47 | String controllerIp = "N/A"; |
Jian Li | aaf44b5 | 2020-12-27 23:22:46 +0900 | [diff] [blame] | 48 | if (config != null) { |
Jian Li | e0eaf5c | 2021-09-06 10:02:13 +0900 | [diff] [blame] | 49 | if (config.controllerIp() != null) { |
| 50 | controllerIp = config.controllerIp().toString(); |
| 51 | } |
Jian Li | aaf44b5 | 2020-12-27 23:22:46 +0900 | [diff] [blame] | 52 | print(FORMAT, config.scheme().name(), config.ipAddress().toString(), |
Jian Li | e0eaf5c | 2021-09-06 10:02:13 +0900 | [diff] [blame] | 53 | config.port(), controllerIp, config.state().name()); |
Jian Li | aaf44b5 | 2020-12-27 23:22:46 +0900 | [diff] [blame] | 54 | } else { |
| 55 | print("Kubevirt config not found!"); |
| 56 | } |
| 57 | |
Jian Li | f248307 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
| 61 | private String json(KubevirtApiConfig config) { |
| 62 | ObjectMapper mapper = new ObjectMapper(); |
| 63 | ObjectNode node = jsonForEntity(config, KubevirtApiConfig.class); |
| 64 | return prettyJson(mapper, node.toString()); |
| 65 | } |
| 66 | } |