Add common openstack opertation scenarios: flavor & server
[yardstick.git] / yardstick / benchmark / scenarios / lib / create_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
10 from __future__ import print_function
11 from __future__ import absolute_import
12
13 import logging
14
15 from yardstick.benchmark.scenarios import base
16 import yardstick.common.openstack_utils as op_utils
17
18 LOG = logging.getLogger(__name__)
19
20
21 class CreateFlavor(base.Scenario):
22     """Create an OpenStack flavor"""
23
24     __scenario_type__ = "CreateFlavor"
25
26     def __init__(self, scenario_cfg, context_cfg):
27         self.scenario_cfg = scenario_cfg
28         self.context_cfg = context_cfg
29         self.options = self.scenario_cfg['options']
30
31         self.flavor_name = self.options.get("flavor_name", "TestFlavor")
32         self.vcpus = self.options.get("vcpus", 2)
33         self.ram = self.options.get("ram", 1024)
34         self.disk = self.options.get("disk", 100)
35         self.public = self.options.get("is_public", True)
36
37         self.setup_done = False
38
39     def setup(self):
40         """scenario setup"""
41
42         self.setup_done = True
43
44     def run(self, result):
45         """execute the test"""
46
47         if not self.setup_done:
48             self.setup()
49
50         LOG.info("Creating flavor %s with %s vcpus, %sMB ram and %sGB disk",
51                  self.flavor_name, self.vcpus, self.ram, self.disk)
52         paras = {'is_public': self.public}
53         flavor = op_utils.create_flavor(self.flavor_name,
54                                         self.ram, self.vcpus, self.disk,
55                                         **paras)
56
57         if flavor:
58             LOG.info("Create flavor successful!")
59             values = [flavor.id]
60         else:
61             LOG.info("Create flavor failed!")
62             values = []
63
64         keys = self.scenario_cfg.get('output', '').split()
65         return self._push_to_outputs(keys, values)