From c5804d89de3a67072e23c028a6b09330d5a1b0d1 Mon Sep 17 00:00:00 2001 From: Yujun Zhang Date: Wed, 5 Jul 2017 10:52:25 +0800 Subject: [PATCH] Add command for healthcheck Change-Id: I874809435b9afbb7282b7012a8b720c30b523a2a --- tools/vitrage.bash_completion | 3 ++- vitrageclient/shell.py | 2 ++ vitrageclient/v1/cli/healthcheck.py | 23 ++++++++++++++++++ vitrageclient/v1/client.py | 2 ++ vitrageclient/v1/healthcheck.py | 36 +++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 vitrageclient/v1/cli/healthcheck.py create mode 100644 vitrageclient/v1/healthcheck.py diff --git a/tools/vitrage.bash_completion b/tools/vitrage.bash_completion index 7ed113e..b569af5 100644 --- a/tools/vitrage.bash_completion +++ b/tools/vitrage.bash_completion @@ -5,12 +5,13 @@ _vitrage() _get_comp_words_by_ref -n : cur prev words # Command data: - cmds='alarm complete event help rca resource template topology' + cmds='alarm complete event healthcheck help rca resource template topology' cmds_alarm='list' cmds_alarm_list='-h --help -f --format -c --column --max-width --print-empty --noindent --quote --all-tenants' cmds_complete='-h --help --name --shell' cmds_event='post' cmds_event_post='-h --help --type --time --details' + cmds_healthcheck='-h --help -f --format -c --column --max-width --print-empty --noindent --variable --prefix' cmds_help='-h --help' cmds_rca='show' cmds_rca_show='-h --help -f --format -c --column --max-width --print-empty --noindent --variable --prefix --all-tenants' diff --git a/vitrageclient/shell.py b/vitrageclient/shell.py index 6a11746..5e39b1c 100644 --- a/vitrageclient/shell.py +++ b/vitrageclient/shell.py @@ -34,6 +34,7 @@ from vitrageclient import client from vitrageclient.v1.cli import alarm from vitrageclient.v1.cli import event +from vitrageclient.v1.cli import healthcheck from vitrageclient.v1.cli import rca from vitrageclient.v1.cli import resource from vitrageclient.v1.cli import template @@ -51,6 +52,7 @@ class VitrageCommandManager(commandmanager.CommandManager): 'template list': template.TemplateList, 'template show': template.TemplateShow, 'event post': event.EventPost, + 'healthcheck': healthcheck.HealthCheck } def load_commands(self, namespace): diff --git a/vitrageclient/v1/cli/healthcheck.py b/vitrageclient/v1/cli/healthcheck.py new file mode 100644 index 0000000..9838dbd --- /dev/null +++ b/vitrageclient/v1/cli/healthcheck.py @@ -0,0 +1,23 @@ +# Copyright 2017 - ZTE Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from cliff import show + + +# noinspection PyAbstractClass +class HealthCheck(show.ShowOne): + """Check api health status""" + + def take_action(self, parsed_args): + return self.dict2columns(self.app.client.healthcheck.get()) diff --git a/vitrageclient/v1/client.py b/vitrageclient/v1/client.py index 29ce7a5..38c6608 100644 --- a/vitrageclient/v1/client.py +++ b/vitrageclient/v1/client.py @@ -14,6 +14,7 @@ from vitrageclient import client from vitrageclient.v1 import alarm from vitrageclient.v1 import event +from vitrageclient.v1 import healthcheck from vitrageclient.v1 import rca from vitrageclient.v1 import resource from vitrageclient.v1 import template @@ -31,3 +32,4 @@ class Client(object): self.rca = rca.Rca(self._api) self.template = template.Template(self._api) self.event = event.Event(self._api) + self.healthcheck = healthcheck.HealthCheck(self._api) diff --git a/vitrageclient/v1/healthcheck.py b/vitrageclient/v1/healthcheck.py new file mode 100644 index 0000000..f679801 --- /dev/null +++ b/vitrageclient/v1/healthcheck.py @@ -0,0 +1,36 @@ +# Copyright 2017 - ZTE Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from vitrageclient.exc import ClientException + + +class HealthCheck(object): + URL = 'healthcheck/' + STATUS_CODE_OK = 200 + + def __init__(self, api): + self.api = api + + def get(self): + """Get healthcheck result""" + try: + resp = self.api.get(self.URL) + except ClientException as e: + return {"passed": False, + "message": e.message, + "url": e.url, + "status_code": e.code} + + return {"passed": resp.status_code == self.STATUS_CODE_OK, + "status_code": resp.status_code}