blob: 08621352ad9895dfa0d1c866be94fe27bd6afa56 [file] [log] [blame]
Brant Knudsone1fa0702015-06-21 08:54:43 -05001#!/usr/bin/env python
2
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
17
18import argparse
19import os.path
20
21import yaml
22
23
24class UpdateCloudsYaml(object):
25 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
33 self._clouds = {}
34
35 self._cloud = args.os_cloud
36 self._cloud_data = {
37 'region_name': args.os_region_name,
38 'identity_api_version': args.os_identity_api_version,
39 'auth': {
40 'auth_url': args.os_auth_url,
41 'username': args.os_username,
42 'password': args.os_password,
43 'project_name': args.os_project_name,
44 },
45 }
46 if args.os_cacert:
47 self._cloud_data['cacert'] = args.os_cacert
48
49 def run(self):
50 self._read_clouds()
51 self._update_clouds()
52 self._write_clouds()
53
54 def _read_clouds(self):
55 try:
56 with open(self._clouds_path) as clouds_file:
57 self._clouds = yaml.load(clouds_file)
58 except IOError:
59 # The user doesn't have a clouds.yaml file.
60 print("The user clouds.yaml file didn't exist.")
61 self._clouds = {}
62
63 def _update_clouds(self):
64 self._clouds.setdefault('clouds', {})[self._cloud] = self._cloud_data
65
66 def _write_clouds(self):
67
68 if self._create_directory:
69 clouds_dir = os.path.dirname(self._clouds_path)
70 os.makedirs(clouds_dir)
71
72 with open(self._clouds_path, 'w') as clouds_file:
73 yaml.dump(self._clouds, clouds_file, default_flow_style=False)
74
75
76def main():
77 parser = argparse.ArgumentParser('Update clouds.yaml file.')
78 parser.add_argument('--file')
79 parser.add_argument('--os-cloud', required=True)
80 parser.add_argument('--os-region-name', default='RegionOne')
81 parser.add_argument('--os-identity-api-version', default='3')
82 parser.add_argument('--os-cacert')
83 parser.add_argument('--os-auth-url', required=True)
84 parser.add_argument('--os-username', required=True)
85 parser.add_argument('--os-password', required=True)
86 parser.add_argument('--os-project-name', required=True)
87
88 args = parser.parse_args()
89
90 update_clouds_yaml = UpdateCloudsYaml(args)
91 update_clouds_yaml.run()
92
93
94if __name__ == "__main__":
95 main()