diff --git a/gnocchiclient/tests/functional/test_resource.py b/gnocchiclient/tests/functional/test_resource.py index 8d3ce6e..a96e861 100644 --- a/gnocchiclient/tests/functional/test_resource.py +++ b/gnocchiclient/tests/functional/test_resource.py @@ -72,6 +72,17 @@ class ResourceClientTest(base.ClientTestBase): self.assertEqual(self.RESOURCE_ID, resource_got["id"]) self.assertEqual(self.PROJECT_ID, resource_got["project_id"]) self.assertEqual(resource["started_at"], resource_got["started_at"]) + self.assertIn("temperature", resource_updated["metrics"]) + + # HISTORY + result = self.gnocchi( + 'resource', params="history generic %s" % self.RESOURCE_ID) + resource_history = self.parser.listing(result) + self.assertEqual(2, len(resource_history)) + self.assertEqual(self.RESOURCE_ID, resource_history[0]["id"]) + self.assertEqual(self.RESOURCE_ID, resource_history[1]["id"]) + self.assertEqual("None", resource_history[0]["project_id"]) + self.assertEqual(self.PROJECT_ID, resource_history[1]["project_id"]) # LIST result = self.gnocchi('resource', params="list generic") diff --git a/gnocchiclient/v1/resource.py b/gnocchiclient/v1/resource.py index 9e63739..ca964ab 100644 --- a/gnocchiclient/v1/resource.py +++ b/gnocchiclient/v1/resource.py @@ -33,16 +33,34 @@ class ResourceManager(base.Manager): resource_type, details, history)) return self.client.api.get(url).json() - def get(self, resource_type, resource_id): + def get(self, resource_type, resource_id, history=False): """Get a resource :param resource_type: Type of the resource :type resource_type: str :param resource_id: ID of the resource :type resource_id: str + :param history: Show the history of the resource + :type history: bool """ - url = self.client._build_url("resource/%s/%s" % ( - resource_type, resource_id)) + history = "/history" if history else "" + url = self.client._build_url("resource/%s/%s%s" % ( + resource_type, resource_id, history)) + return self.client.api.get(url).json() + + def history(self, resource_type, resource_id, details=False): + """Get a resource + + :param resource_type: Type of the resource + :type resource_type: str + :param resource_id: ID of the resource + :type resource_id: str + :param details: Show all attributes of resources + :type details: bool + """ + details = "true" if details else "false" + url = self.client._build_url("resource/%s/%s/history?details=%s" % ( + resource_type, resource_id, details)) return self.client.api.get(url).json() def create(self, resource_type, resource): diff --git a/gnocchiclient/v1/resourcecli.py b/gnocchiclient/v1/resourcecli.py index ca775a7..61aa4b1 100644 --- a/gnocchiclient/v1/resourcecli.py +++ b/gnocchiclient/v1/resourcecli.py @@ -26,12 +26,13 @@ class CliResourceList(lister.Lister): 'started_at', 'ended_at', 'revision_start', 'revision_end') - def get_parser(self, prog_name): + def get_parser(self, prog_name, history=True): parser = super(CliResourceList, self).get_parser(prog_name) parser.add_argument("--details", action='store_true', help="Show all attributes of generic resources"), - parser.add_argument("--history", action='store_true', - help="Show history of the resources"), + if history: + parser.add_argument("--history", action='store_true', + help="Show history of the resources"), parser.add_argument("resource_type", default="generic", nargs='?', @@ -50,6 +51,22 @@ class CliResourceList(lister.Lister): return tuple([resource[k] for k in cls.COLS]) +class CliResourceHistory(CliResourceList): + def get_parser(self, prog_name): + parser = super(CliResourceHistory, self).get_parser(prog_name, + history=False) + parser.add_argument("resource_id", + help="ID of a resource") + return parser + + def take_action(self, parsed_args): + resources = self.app.client.resource.history( + resource_type=parsed_args.resource_type, + resource_id=parsed_args.resource_id, + details=parsed_args.details) + return self.COLS, [self._resource2tuple(r) for r in resources] + + class CliResourceSearch(CliResourceList): def get_parser(self, prog_name): parser = super(CliResourceSearch, self).get_parser(prog_name) diff --git a/setup.cfg b/setup.cfg index d598d63..2247564 100644 --- a/setup.cfg +++ b/setup.cfg @@ -30,6 +30,7 @@ console_scripts = gnocchi.cli.v1 = resource_list = gnocchiclient.v1.resourcecli:CliResourceList resource_show = gnocchiclient.v1.resourcecli:CliResourceShow + resource_history = gnocchiclient.v1.resourcecli:CliResourceHistory resource_search = gnocchiclient.v1.resourcecli:CliResourceSearch resource_create = gnocchiclient.v1.resourcecli:CliResourceCreate resource_update = gnocchiclient.v1.resourcecli:CliResourceUpdate