Debian Codegraph @

config_tempest/services/object_storage.py

raw
1 # Copyright 2016, 2018 Red Hat, Inc.
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 import json
17
18 from six.moves import configparser
19 from tempest.lib import exceptions
20
21 from config_tempest.constants import LOG
22 from config_tempest.services.base import Service
23
24
25 class ObjectStorageService(Service):
26 def set_extensions(self):
27 if 'v3' not in self.service_url: # it's not a v3 url
28 capabilities = self.get_capabilities('info')
29 if not capabilities:
30 # Certain deploynments have /info API endpoint for
31 # listing active capabilities at /swift/info instead
32 # (e.g.: when ceph is on swift)
33 capabilities = self.get_capabilities('swift/info')
34
35 # Remove Swift general information from extensions list
36 capabilities.pop('swift', {})
37 self.extensions = capabilities.keys()
38 else:
39 self.extensions = []
40
41 def get_capabilities(self, path):
42 try:
43 body = self.do_get(self.service_url, top_level=True,
44 top_level_path=path)
45 except Exception as e:
46 body = '{}'
47 LOG.warning('Object storage %s API endpoint not discovered. '
48 'Error message: %s', path, e)
49
50 return json.loads(body)
51
52 def list_create_roles(self, conf, client):
53 try:
54 roles = client.list_roles()['roles']
55 for section_key in ["operator_role", "reseller_admin_role"]:
56 key_value = conf.get_defaulted("object-storage", section_key)
57 if key_value not in [r['name'] for r in roles]:
58 LOG.info("Creating %s role", key_value)
59 try:
60 client.create_role(name=key_value)
61 except exceptions.Conflict:
62 LOG.info("Role %s already exists", key_value)
63 conf.set('object-storage', 'operator_role', 'admin')
64 except exceptions.Forbidden:
65 LOG.info("Roles can't be listed or created. The user doesn't have "
66 "permissions.")
67 # If is not admin, we set the operator_role to Member
68 # otherwise we set to admin
69 conf.set('object-storage', 'operator_role', 'Member')
70
71 def get_feature_name(self):
72 return 'object-storage'
73
74 def get_service_extension_key(self):
75 return 'discoverable_apis'
76
77 def _check_health_check(self, path):
78 try:
79 self.client.accounts.skip_path()
80 resp, _ = self.client.accounts.get(path, {})
81 return resp['status'] == '200'
82 except Exception as e:
83 LOG.warning('Healthcheck API not discovered giving %s', e)
84 return False
85
86 def check_service_status(self, conf):
87 """Use healthcheck api to check the service status
88
89 :type conf: TempestConf object
90 """
91 # Check for swift discoverability if it is False
92 # check_service_status returns False
93 # Else above is True, then we can check for healthcheck
94 # API then we can find the service_status
95 try:
96 if not conf.get_bool_value(
97 conf.get(
98 'object-storage-feature-enabled',
99 'discoverability')):
100 return False
101 return True
102 except (configparser.NoSectionError, configparser.NoOptionError):
103 # On swift, healthcheck is under http://.../healthcheck, while in
104 # ceph is under http://.../swift/healthcheck, we check both cases
105 return_value = self._check_health_check('healthcheck')
106 if not return_value:
107 return_value = self._check_health_check('swift/healthcheck')
108 return return_value
109
110 def set_default_tempest_options(self, conf):
111 """Set default values for swift
112
113 :type conf: TempestConf object
114 """
115 swift_status = self.check_service_status(conf)
116 # Set roles based on service status
117 if swift_status:
118 self.list_create_roles(conf, self.client.roles)
119
120 @staticmethod
121 def get_service_type():
122 return ['object-store']
123
124 @staticmethod
125 def get_codename():
126 return 'swift'