Merge "Adding latency test for vfw"
[yardstick.git] / yardstick / benchmark / scenarios / lib / create_floating_ip.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 import os
15
16 from yardstick.benchmark.scenarios import base
17 import yardstick.common.openstack_utils as op_utils
18
19 LOG = logging.getLogger(__name__)
20
21
22 class CreateFloatingIp(base.Scenario):
23     """Create an OpenStack floating ip"""
24
25     __scenario_type__ = "CreateFloatingIp"
26
27     def __init__(self, scenario_cfg, context_cfg):
28         self.scenario_cfg = scenario_cfg
29         self.context_cfg = context_cfg
30         self.ext_net_id = os.getenv("EXTERNAL_NETWORK", "external")
31
32         self.neutron_client = op_utils.get_neutron_client()
33         self.setup_done = False
34
35     def setup(self):
36         """scenario setup"""
37
38         self.setup_done = True
39
40     def run(self, result):
41         """execute the test"""
42
43         if not self.setup_done:
44             self.setup()
45
46         net_id = op_utils.get_network_id(self.neutron_client, self.ext_net_id)
47         floating_info = op_utils.create_floating_ip(self.neutron_client,
48                                                     extnet_id=net_id)
49         if floating_info:
50             LOG.info("Creating floating ip successful!")
51         else:
52             LOG.error("Creating floating ip failed!")
53
54         try:
55             keys = self.scenario_cfg.get('output', '').split()
56         except KeyError:
57             pass
58         else:
59             values = [floating_info["fip_id"], floating_info["fip_addr"]]
60             return self._push_to_outputs(keys, values)