Add common openstack opertation scenarios: subnet & port
[yardstick.git] / yardstick / benchmark / scenarios / lib / create_server.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 CreateServer(base.Scenario):
22     """Create an OpenStack server"""
23
24     __scenario_type__ = "CreateSever"
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.image_name = self.options.get("image_name", None)
32         self.flavor_name = self.options.get("flavor_name", None)
33         self.openstack = self.options.get("openstack_paras", None)
34
35         self.glance_client = op_utils.get_glance_client()
36         self.neutron_client = op_utils.get_neutron_client()
37         self.nova_client = op_utils.get_nova_client()
38
39         self.setup_done = False
40
41     def setup(self):
42         """scenario setup"""
43
44         self.setup_done = True
45
46     def run(self, result):
47         """execute the test"""
48
49         if not self.setup_done:
50             self.setup()
51
52         if self.image_name is not None:
53             self.openstack['image'] = op_utils.get_image_id(self.glance_client,
54                                                             self.image_name)
55         if self.flavor_name is not None:
56             self.openstack['flavor'] = op_utils.get_flavor_id(self.nova_client,
57                                                               self.flavor_name)
58
59         vm = op_utils.create_instance_and_wait_for_active(self.openstack)
60
61         if vm:
62             result.update({"instance_create": 1})
63             LOG.info("Create server successful!")
64         else:
65             result.update({"instance_create": 0})
66             LOG.error("Create server failed!")
67
68         try:
69             keys = self.scenario_cfg.get('output', '').split()
70         except KeyError:
71             pass
72         else:
73             values = [vm.id]
74             return self._push_to_outputs(keys, values)