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