blob: 143257ac3d65559338a706309f8c4f2c632460a7 [file] [log] [blame]
Attila Fazekasa23f5002012-10-23 19:32:45 +02001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2012 OpenStack, LLC
4# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
Attila Fazekas1aed6202013-02-11 14:47:45 +010018import ConfigParser
19import contextlib
20import re
21import types
Attila Fazekasa23f5002012-10-23 19:32:45 +020022import urlparse
23
Attila Fazekas1aed6202013-02-11 14:47:45 +010024from tempest import exceptions
Matthew Treinisha83a16e2012-12-07 13:44:02 -050025
Attila Fazekas1aed6202013-02-11 14:47:45 +010026import boto
27import boto.ec2
28import boto.s3.connection
29
30
31class BotoClientBase(object):
32
33 ALLOWED_METHODS = set()
34
35 def __init__(self, config,
36 username=None, password=None,
37 auth_url=None, tenant_name=None,
38 *args, **kwargs):
39
40 self.connection_timeout = str(config.boto.http_socket_timeout)
41 self.num_retries = str(config.boto.num_retries)
42 self.build_timeout = config.boto.build_timeout
43 self.ks_cred = {"username": username,
44 "password": password,
45 "auth_url": auth_url,
46 "tenant_name": tenant_name}
47
48 def _keystone_aws_get(self):
49 import keystoneclient.v2_0.client
50
51 keystone = keystoneclient.v2_0.client.Client(**self.ks_cred)
52 ec2_cred_list = keystone.ec2.list(keystone.auth_user_id)
53 ec2_cred = None
54 for cred in ec2_cred_list:
55 if cred.tenant_id == keystone.auth_tenant_id:
56 ec2_cred = cred
57 break
58 else:
59 ec2_cred = keystone.ec2.create(keystone.auth_user_id,
60 keystone.auth_tenant_id)
61 if not all((ec2_cred, ec2_cred.access, ec2_cred.secret)):
62 raise exceptions.NotFound("Unable to get access and secret keys")
63 return ec2_cred
64
65 def _config_boto_timeout(self, timeout, retries):
66 try:
67 boto.config.add_section("Boto")
68 except ConfigParser.DuplicateSectionError:
69 pass
70 boto.config.set("Boto", "http_socket_timeout", timeout)
71 boto.config.set("Boto", "num_retries", retries)
72
73 def __getattr__(self, name):
74 """Automatically creates methods for the allowed methods set."""
75 if name in self.ALLOWED_METHODS:
76 def func(self, *args, **kwargs):
77 with contextlib.closing(self.get_connection()) as conn:
78 return getattr(conn, name)(*args, **kwargs)
79
80 func.__name__ = name
81 setattr(self, name, types.MethodType(func, self, self.__class__))
82 setattr(self.__class__, name,
83 types.MethodType(func, None, self.__class__))
84 return getattr(self, name)
85 else:
86 raise AttributeError(name)
87
88 def get_connection(self):
89 self._config_boto_timeout(self.connection_timeout, self.num_retries)
90 if not all((self.connection_data["aws_access_key_id"],
91 self.connection_data["aws_secret_access_key"])):
92 if all(self.ks_cred.itervalues()):
93 ec2_cred = self._keystone_aws_get()
94 self.connection_data["aws_access_key_id"] = \
95 ec2_cred.access
96 self.connection_data["aws_secret_access_key"] = \
97 ec2_cred.secret
98 else:
99 raise exceptions.InvalidConfiguration(
100 "Unable to get access and secret keys")
101 return self.connect_method(**self.connection_data)
Matthew Treinisha83a16e2012-12-07 13:44:02 -0500102
Attila Fazekasa23f5002012-10-23 19:32:45 +0200103
104class APIClientEC2(BotoClientBase):
105
106 def connect_method(self, *args, **kwargs):
107 return boto.connect_ec2(*args, **kwargs)
108
109 def __init__(self, config, *args, **kwargs):
110 super(APIClientEC2, self).__init__(config, *args, **kwargs)
111 aws_access = config.boto.aws_access
112 aws_secret = config.boto.aws_secret
113 purl = urlparse.urlparse(config.boto.ec2_url)
114
Attila Fazekas1aed6202013-02-11 14:47:45 +0100115 region = boto.ec2.regioninfo.RegionInfo(name=config.identity.region,
116 endpoint=purl.hostname)
Attila Fazekasa23f5002012-10-23 19:32:45 +0200117 port = purl.port
118 if port is None:
119 if purl.scheme is not "https":
120 port = 80
121 else:
122 port = 443
123 else:
124 port = int(port)
125 self.connection_data = {"aws_access_key_id": aws_access,
126 "aws_secret_access_key": aws_secret,
127 "is_secure": purl.scheme == "https",
128 "region": region,
129 "host": purl.hostname,
130 "port": port,
131 "path": purl.path}
132
133 ALLOWED_METHODS = set(('create_key_pair', 'get_key_pair',
134 'delete_key_pair', 'import_key_pair',
135 'get_all_key_pairs',
136 'create_image', 'get_image',
137 'register_image', 'deregister_image',
138 'get_all_images', 'get_image_attribute',
139 'modify_image_attribute', 'reset_image_attribute',
140 'get_all_kernels',
141 'create_volume', 'delete_volume',
142 'get_all_volume_status', 'get_all_volumes',
143 'get_volume_attribute', 'modify_volume_attribute'
144 'bundle_instance', 'cancel_spot_instance_requests',
145 'confirm_product_instanc',
146 'get_all_instance_status', 'get_all_instances',
147 'get_all_reserved_instances',
148 'get_all_spot_instance_requests',
149 'get_instance_attribute', 'monitor_instance',
150 'monitor_instances', 'unmonitor_instance',
151 'unmonitor_instances',
152 'purchase_reserved_instance_offering',
153 'reboot_instances', 'request_spot_instances',
154 'reset_instance_attribute', 'run_instances',
155 'start_instances', 'stop_instances',
156 'terminate_instances',
157 'attach_network_interface', 'attach_volume',
158 'detach_network_interface', 'detach_volume',
159 'get_console_output',
160 'delete_network_interface', 'create_subnet',
161 'create_network_interface', 'delete_subnet',
162 'get_all_network_interfaces',
163 'allocate_address', 'associate_address',
164 'disassociate_address', 'get_all_addresses',
165 'release_address',
166 'create_snapshot', 'delete_snapshot',
167 'get_all_snapshots', 'get_snapshot_attribute',
168 'modify_snapshot_attribute',
169 'reset_snapshot_attribute', 'trim_snapshots',
170 'get_all_regions', 'get_all_zones',
171 'get_all_security_groups', 'create_security_group',
172 'delete_security_group', 'authorize_security_group',
173 'authorize_security_group_egress',
174 'revoke_security_group',
175 'revoke_security_group_egress'))
176
177 def get_good_zone(self):
178 """
179 :rtype: BaseString
180 :return: Returns with the first available zone name
181 """
182 for zone in self.get_all_zones():
183 #NOTE(afazekas): zone.region_name was None
184 if (zone.state == "available" and
185 zone.region.name == self.connection_data["region"].name):
186 return zone.name
187 else:
188 raise IndexError("Don't have a good zone")
189
190
191class ObjectClientS3(BotoClientBase):
192
193 def connect_method(self, *args, **kwargs):
194 return boto.connect_s3(*args, **kwargs)
195
196 def __init__(self, config, *args, **kwargs):
197 super(ObjectClientS3, self).__init__(config, *args, **kwargs)
198 aws_access = config.boto.aws_access
199 aws_secret = config.boto.aws_secret
200 purl = urlparse.urlparse(config.boto.s3_url)
201 port = purl.port
202 if port is None:
203 if purl.scheme is not "https":
204 port = 80
205 else:
206 port = 443
207 else:
208 port = int(port)
209 self.connection_data = {"aws_access_key_id": aws_access,
210 "aws_secret_access_key": aws_secret,
211 "is_secure": purl.scheme == "https",
212 "host": purl.hostname,
213 "port": port,
Attila Fazekas1aed6202013-02-11 14:47:45 +0100214 "calling_format": boto.s3.connection.
215 OrdinaryCallingFormat()}
Attila Fazekasa23f5002012-10-23 19:32:45 +0200216
217 ALLOWED_METHODS = set(('create_bucket', 'delete_bucket', 'generate_url',
218 'get_all_buckets', 'get_bucket', 'delete_key',
219 'lookup'))