Debian Codegraph @

config_tempest/accounts.py

raw
1 # Copyright 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 yaml
17
18
19 def create_accounts_file(create, accounts_path, conf):
20 if create:
21 section = 'auth'
22 prefix = 'admin_'
23 else:
24 section = 'identity'
25 prefix = ''
26 write_accounts_file(accounts_path,
27 conf.get(section, prefix + 'username'),
28 conf.get(section, prefix + 'password'),
29 conf.get(section, prefix + 'project_name'))
30
31
32 def write_accounts_file(path, username, password, project_name):
33 """Creates a minimal accounts.yaml file.
34
35 Dumps provided credentials in a yaml format.
36
37 :type path: string
38 :type username: string
39 :type password: string
40 :type project_name: string
41 """
42 comments = "# A minimal accounts.yaml file\n" \
43 "# Will likely not work with swift, since additional\n" \
44 "# roles are required. For more documentation see:\n" \
45 "# https://opendev.org/openstack/tempest/src/branch/master/" \
46 "etc/accounts.yaml.sample\n\n"
47 accounts = []
48 accounts.append({
49 'username': username,
50 'project_name': project_name,
51 'password': password
52 })
53
54 # Some deployments (like devstack) don't assign the reader role to the
55 # default user, but it's required by some tempest tests.
56 # To mitigate this issue, also add a <username>_reader user to the
57 # accounts file (same project and same password).
58 if username != "admin":
59 reader_username = f"{username}_reader"
60 accounts.append({
61 'username': reader_username,
62 "project_name": project_name,
63 "password": password,
64 'roles': ["reader"]
65 })
66
67 with open(path, 'w') as outfile:
68 for line in comments:
69 outfile.write(line)
70 yaml.safe_dump(accounts, outfile, default_flow_style=False)