Merge "Cleanup CGNAPT unit tests"
[yardstick.git] / yardstick / benchmark / scenarios / lib / get_server.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 GetServer(base.Scenario):
19     """Get a server instance
20
21     Parameters:
22     name_or_id - Name or ID of the server
23         type: string
24     filters - meta data to use for further filtering
25         type: dict
26     detailed: Whether or not to add detailed additional information.
27         type: bool
28     bare: Whether to skip adding any additional information to the server
29           record.
30         type: bool
31     all_projects: Whether to get server from all projects or just the current
32                   auth scoped project.
33         type: bool
34
35     Outputs:
36     rc - response code of getting server instance
37         1 for success
38         0 for failure
39         type:    int
40     server - instance of the server
41         type:    dict
42
43     """
44
45     __scenario_type__ = 'GetServer'
46
47     def __init__(self, scenario_cfg, context_cfg):
48         self.scenario_cfg = scenario_cfg
49         self.context_cfg = context_cfg
50         self.options = self.scenario_cfg['options']
51
52         self.server_name_or_id = self.options.get('name_or_id')
53         self.filters = self.options.get('filters')
54         self.detailed = self.options.get('detailed', False)
55         self.bare = self.options.get('bare', False)
56
57         self.shade_client = openstack_utils.get_shade_client()
58
59     def run(self, result):
60         """execute the test"""
61
62         server = openstack_utils.get_server(
63             self.shade_client, name_or_id=self.server_name_or_id,
64             filters=self.filters, detailed=self.detailed, bare=self.bare)
65
66         if not server:
67             result.update({'get_server': 0})
68             LOG.error('Get Server failed!')
69             raise exceptions.ScenarioGetServerError
70
71         result.update({'get_server': 1})
72         LOG.info('Get Server successful!')
73         keys = self.scenario_cfg.get('output', '').split()
74         values = [server]
75         return self._push_to_outputs(keys, values)