Update Intel Copyright for files edited in 2019
[yardstick.git] / yardstick / benchmark / scenarios / lib / create_sec_group.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
12 from yardstick.benchmark.scenarios import base
13 from yardstick.common import openstack_utils
14 from yardstick.common import exceptions
15
16 LOG = logging.getLogger(__name__)
17
18
19 class CreateSecgroup(base.Scenario):
20     """Create an OpenStack security group"""
21
22     __scenario_type__ = "CreateSecgroup"
23
24     def __init__(self, scenario_cfg, context_cfg):
25         self.scenario_cfg = scenario_cfg
26         self.context_cfg = context_cfg
27         self.options = self.scenario_cfg["options"]
28
29         self.sg_name = self.options["sg_name"]
30         self.description = self.options.get("description", "")
31         self.project_id = self.options.get("project_id")
32         self.shade_client = openstack_utils.get_shade_client()
33
34         self.setup_done = False
35
36     def setup(self):
37         """scenario setup"""
38
39         self.setup_done = True
40
41     def run(self, result):
42         """execute the test"""
43
44         if not self.setup_done:
45             self.setup()
46
47         sg_id = openstack_utils.create_security_group_full(
48             self.shade_client, self.sg_name, sg_description=self.description,
49             project_id=self.project_id)
50         if not sg_id:
51             result.update({"sg_create": 0})
52             LOG.error("Create security group failed!")
53             raise exceptions.ScenarioCreateSecurityGroupError
54
55         result.update({"sg_create": 1})
56         LOG.info("Create security group successful!")
57         keys = self.scenario_cfg.get("output", '').split()
58         values = [sg_id]
59         return self._push_to_outputs(keys, values)