Merge "Add --hwlb options as a command line argument for SampleVNF"
[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 from yardstick.common import openstack_utils
15 from yardstick.common import exceptions
16
17
18 LOG = logging.getLogger(__name__)
19
20
21 class CreateFloatingIp(base.Scenario):
22     """Create an OpenStack floating ip"""
23
24     __scenario_type__ = "CreateFloatingIp"
25
26     def __init__(self, scenario_cfg, context_cfg):
27         self.scenario_cfg = scenario_cfg
28         self.context_cfg = context_cfg
29         self.ext_net_id = os.getenv("EXTERNAL_NETWORK", "external")
30         self.options = self.scenario_cfg["options"]
31
32         self.network_name_or_id = self.options.get("network_name_or_id", self.ext_net_id)
33         self.server = self.options.get("server")
34         self.fixed_address = self.options.get("fixed_address")
35         self.nat_destination = self.options.get("nat_destination")
36         self.port = self.options.get("port")
37         self.wait = self.options.get("wait", False)
38         self.timeout = self.options.get("timeout", 60)
39
40         self.shade_client = openstack_utils.get_shade_client()
41
42         self.setup_done = False
43
44     def setup(self):
45         """scenario setup"""
46
47         self.setup_done = True
48
49     def run(self, result):
50         """execute the test"""
51
52         if not self.setup_done:
53             self.setup()
54
55         floating_info = openstack_utils.create_floating_ip(
56             self.shade_client, network_name_or_id=self.network_name_or_id,
57             server=self.server, fixed_address=self.fixed_address,
58             nat_destination=self.nat_destination, port=self.port,
59             wait=self.wait, timeout=self.timeout)
60
61         if not floating_info:
62             result.update({"floating_ip_create": 0})
63             LOG.error("Creating floating ip failed!")
64             raise exceptions.ScenarioCreateFloatingIPError
65
66         result.update({"floating_ip_create": 1})
67         LOG.info("Creating floating ip successful!")
68         keys = self.scenario_cfg.get("output", '').split()
69         values = [floating_info["fip_id"], floating_info["fip_addr"]]
70         return self._push_to_outputs(keys, values)