Replace neutron get network id with shade.
[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 import logging
11 import os
12
13 from yardstick.benchmark.scenarios import base
14 import yardstick.common.openstack_utils as op_utils
15
16
17 LOG = logging.getLogger(__name__)
18
19
20 class CreateFloatingIp(base.Scenario):
21     """Create an OpenStack floating ip"""
22
23     __scenario_type__ = "CreateFloatingIp"
24
25     def __init__(self, scenario_cfg, context_cfg):
26         self.scenario_cfg = scenario_cfg
27         self.context_cfg = context_cfg
28         self.ext_net_id = os.getenv("EXTERNAL_NETWORK", "external")
29
30         self.neutron_client = op_utils.get_neutron_client()
31         self.shade_client = op_utils.get_shade_client()
32         self.setup_done = False
33
34     def setup(self):
35         """scenario setup"""
36
37         self.setup_done = True
38
39     def run(self, *args):
40         """execute the test"""
41
42         if not self.setup_done:
43             self.setup()
44
45         net_id = op_utils.get_network_id(self.shade_client, self.ext_net_id)
46         floating_info = op_utils.create_floating_ip(self.neutron_client,
47                                                     extnet_id=net_id)
48
49         if not floating_info:
50             LOG.error("Creating floating ip failed!")
51             return
52
53         LOG.info("Creating floating ip successful!")
54         keys = self.scenario_cfg.get('output', '').split()
55         values = [floating_info["fip_id"], floating_info["fip_addr"]]
56         return self._push_to_outputs(keys, values)