blob: c0a54838cca45978005c626e677e9761742f9086 [file] [log] [blame]
Federico Ressi21a10d32020-01-31 07:43:30 +01001#!/usr/bin/env python3
Brant Knudsone1fa0702015-06-21 08:54:43 -05002
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15# Update the clouds.yaml file.
16
Brant Knudsone1fa0702015-06-21 08:54:43 -050017import argparse
18import os.path
Stephen Finucane85576bb2025-02-26 18:04:09 +000019import sys
Brant Knudsone1fa0702015-06-21 08:54:43 -050020
21import yaml
22
23
Stephen Finucane85576bb2025-02-26 18:04:09 +000024class UpdateCloudsYaml:
Brant Knudsone1fa0702015-06-21 08:54:43 -050025 def __init__(self, args):
26 if args.file:
27 self._clouds_path = args.file
28 self._create_directory = False
29 else:
30 self._clouds_path = os.path.expanduser(
31 '~/.config/openstack/clouds.yaml')
32 self._create_directory = True
yatin82c30cd2023-11-15 12:44:50 +000033 self._clouds = {}
Brant Knudsone1fa0702015-06-21 08:54:43 -050034
Stephen Finucane85576bb2025-02-26 18:04:09 +000035 if args.os_identity_api_version != '3':
36 print("ERROR: Only identity API v3 is supported")
37 sys.exit(1)
38
39 if args.os_volume_api_version != '3':
40 print("ERROR: Only block storage API v3 is supported")
41 sys.exit(1)
42
Brant Knudsone1fa0702015-06-21 08:54:43 -050043 self._cloud = args.os_cloud
44 self._cloud_data = {
45 'region_name': args.os_region_name,
46 'identity_api_version': args.os_identity_api_version,
Monty Taylor5bf6a942015-12-31 16:22:04 -060047 'volume_api_version': args.os_volume_api_version,
Brant Knudsone1fa0702015-06-21 08:54:43 -050048 'auth': {
49 'auth_url': args.os_auth_url,
50 'username': args.os_username,
Stephen Finucane85576bb2025-02-26 18:04:09 +000051 'user_domain_id': 'default',
Brant Knudsone1fa0702015-06-21 08:54:43 -050052 'password': args.os_password,
Brant Knudsone1fa0702015-06-21 08:54:43 -050053 },
54 }
Stephen Finucane85576bb2025-02-26 18:04:09 +000055
Monty Taylor56905822019-01-08 15:29:16 +000056 if args.os_project_name and args.os_system_scope:
57 print(
Stephen Finucane85576bb2025-02-26 18:04:09 +000058 "WARNING: os_project_name and os_system_scope were both "
59 "given. os_system_scope will take priority."
60 )
61
62 if args.os_system_scope: # system-scoped
Monty Taylor56905822019-01-08 15:29:16 +000063 self._cloud_data['auth']['system_scope'] = args.os_system_scope
Stephen Finucane85576bb2025-02-26 18:04:09 +000064 elif args.os_project_name: # project-scoped
65 self._cloud_data['auth']['project_name'] = args.os_project_name
66 self._cloud_data['auth']['project_domain_id'] = 'default'
67
Brant Knudsone1fa0702015-06-21 08:54:43 -050068 if args.os_cacert:
69 self._cloud_data['cacert'] = args.os_cacert
70
71 def run(self):
72 self._read_clouds()
73 self._update_clouds()
74 self._write_clouds()
75
76 def _read_clouds(self):
77 try:
78 with open(self._clouds_path) as clouds_file:
yatin82c30cd2023-11-15 12:44:50 +000079 self._clouds = yaml.safe_load(clouds_file)
Brant Knudsone1fa0702015-06-21 08:54:43 -050080 except IOError:
81 # The user doesn't have a clouds.yaml file.
82 print("The user clouds.yaml file didn't exist.")
yatin82c30cd2023-11-15 12:44:50 +000083 self._clouds = {}
Brant Knudsone1fa0702015-06-21 08:54:43 -050084
85 def _update_clouds(self):
yatin82c30cd2023-11-15 12:44:50 +000086 self._clouds.setdefault('clouds', {})[self._cloud] = self._cloud_data
Brant Knudsone1fa0702015-06-21 08:54:43 -050087
88 def _write_clouds(self):
89
90 if self._create_directory:
91 clouds_dir = os.path.dirname(self._clouds_path)
92 os.makedirs(clouds_dir)
93
94 with open(self._clouds_path, 'w') as clouds_file:
yatin82c30cd2023-11-15 12:44:50 +000095 yaml.dump(self._clouds, clouds_file, default_flow_style=False)
Brant Knudsone1fa0702015-06-21 08:54:43 -050096
97
98def main():
99 parser = argparse.ArgumentParser('Update clouds.yaml file.')
100 parser.add_argument('--file')
101 parser.add_argument('--os-cloud', required=True)
102 parser.add_argument('--os-region-name', default='RegionOne')
103 parser.add_argument('--os-identity-api-version', default='3')
Akihiro Motokie0b375c2018-12-14 17:29:27 +0900104 parser.add_argument('--os-volume-api-version', default='3')
Brant Knudsone1fa0702015-06-21 08:54:43 -0500105 parser.add_argument('--os-cacert')
106 parser.add_argument('--os-auth-url', required=True)
107 parser.add_argument('--os-username', required=True)
108 parser.add_argument('--os-password', required=True)
Monty Taylor56905822019-01-08 15:29:16 +0000109 parser.add_argument('--os-project-name')
110 parser.add_argument('--os-system-scope')
Brant Knudsone1fa0702015-06-21 08:54:43 -0500111
112 args = parser.parse_args()
113
114 update_clouds_yaml = UpdateCloudsYaml(args)
115 update_clouds_yaml.run()
116
117
118if __name__ == "__main__":
119 main()