Replace nova get flavor with shade client.
[yardstick.git] / yardstick / benchmark / scenarios / lib / get_flavor.py
1 ##############################################################################
2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 import logging
10
11 from yardstick.benchmark.scenarios import base
12 from yardstick.common import openstack_utils
13 from yardstick.common import exceptions
14
15 LOG = logging.getLogger(__name__)
16
17
18 class GetFlavor(base.Scenario):
19     """Get an OpenStack flavor by name"""
20
21     __scenario_type__ = "GetFlavor"
22
23     def __init__(self, scenario_cfg, context_cfg):
24         self.scenario_cfg = scenario_cfg
25         self.context_cfg = context_cfg
26         self.options = self.scenario_cfg["options"]
27         self.name_or_id = self.options["name_or_id"]
28         self.filters = self.options.get("filters")
29         self.get_extra = self.options.get("get_extra", True)
30         self.shade_client = openstack_utils.get_shade_client()
31
32         self.setup_done = False
33
34     def setup(self):
35         """scenario setup"""
36
37         self.setup_done = True
38
39     def run(self, result):
40         """execute the test"""
41
42         if not self.setup_done:
43             self.setup()
44
45         LOG.info("Querying flavor: %s", self.name_or_id)
46         flavor = openstack_utils.get_flavor(
47             self.shade_client, self.name_or_id, filters=self.filters,
48             get_extra=self.get_extra)
49
50         if not flavor:
51             result.update({"get_flavor": 0})
52             LOG.error("Get flavor failed!")
53             raise exceptions.ScenarioGetFlavorError
54
55         result.update({"get_flavor": 1})
56         LOG.info("Get flavor successful!")
57         values = [flavor]
58         keys = self.scenario_cfg.get("output", '').split()
59         return self._push_to_outputs(keys, values)