Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 1 | # 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 Fazekas | 1aed620 | 2013-02-11 14:47:45 +0100 | [diff] [blame] | 18 | import ConfigParser |
| 19 | import contextlib |
Attila Fazekas | 1aed620 | 2013-02-11 14:47:45 +0100 | [diff] [blame] | 20 | import types |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 21 | import urlparse |
| 22 | |
Attila Fazekas | 1aed620 | 2013-02-11 14:47:45 +0100 | [diff] [blame] | 23 | from tempest import exceptions |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 24 | |
Attila Fazekas | 1aed620 | 2013-02-11 14:47:45 +0100 | [diff] [blame] | 25 | import boto |
| 26 | import boto.ec2 |
| 27 | import boto.s3.connection |
| 28 | |
| 29 | |
| 30 | class BotoClientBase(object): |
| 31 | |
| 32 | ALLOWED_METHODS = set() |
| 33 | |
| 34 | def __init__(self, config, |
| 35 | username=None, password=None, |
| 36 | auth_url=None, tenant_name=None, |
| 37 | *args, **kwargs): |
| 38 | |
| 39 | self.connection_timeout = str(config.boto.http_socket_timeout) |
| 40 | self.num_retries = str(config.boto.num_retries) |
| 41 | self.build_timeout = config.boto.build_timeout |
| 42 | self.ks_cred = {"username": username, |
| 43 | "password": password, |
| 44 | "auth_url": auth_url, |
| 45 | "tenant_name": tenant_name} |
| 46 | |
| 47 | def _keystone_aws_get(self): |
| 48 | import keystoneclient.v2_0.client |
| 49 | |
| 50 | keystone = keystoneclient.v2_0.client.Client(**self.ks_cred) |
| 51 | ec2_cred_list = keystone.ec2.list(keystone.auth_user_id) |
| 52 | ec2_cred = None |
| 53 | for cred in ec2_cred_list: |
| 54 | if cred.tenant_id == keystone.auth_tenant_id: |
| 55 | ec2_cred = cred |
| 56 | break |
| 57 | else: |
| 58 | ec2_cred = keystone.ec2.create(keystone.auth_user_id, |
| 59 | keystone.auth_tenant_id) |
| 60 | if not all((ec2_cred, ec2_cred.access, ec2_cred.secret)): |
| 61 | raise exceptions.NotFound("Unable to get access and secret keys") |
| 62 | return ec2_cred |
| 63 | |
| 64 | def _config_boto_timeout(self, timeout, retries): |
| 65 | try: |
| 66 | boto.config.add_section("Boto") |
| 67 | except ConfigParser.DuplicateSectionError: |
| 68 | pass |
| 69 | boto.config.set("Boto", "http_socket_timeout", timeout) |
| 70 | boto.config.set("Boto", "num_retries", retries) |
| 71 | |
| 72 | def __getattr__(self, name): |
| 73 | """Automatically creates methods for the allowed methods set.""" |
| 74 | if name in self.ALLOWED_METHODS: |
| 75 | def func(self, *args, **kwargs): |
| 76 | with contextlib.closing(self.get_connection()) as conn: |
| 77 | return getattr(conn, name)(*args, **kwargs) |
| 78 | |
| 79 | func.__name__ = name |
| 80 | setattr(self, name, types.MethodType(func, self, self.__class__)) |
| 81 | setattr(self.__class__, name, |
| 82 | types.MethodType(func, None, self.__class__)) |
| 83 | return getattr(self, name) |
| 84 | else: |
| 85 | raise AttributeError(name) |
| 86 | |
| 87 | def get_connection(self): |
| 88 | self._config_boto_timeout(self.connection_timeout, self.num_retries) |
| 89 | if not all((self.connection_data["aws_access_key_id"], |
| 90 | self.connection_data["aws_secret_access_key"])): |
| 91 | if all(self.ks_cred.itervalues()): |
| 92 | ec2_cred = self._keystone_aws_get() |
| 93 | self.connection_data["aws_access_key_id"] = \ |
| 94 | ec2_cred.access |
| 95 | self.connection_data["aws_secret_access_key"] = \ |
| 96 | ec2_cred.secret |
| 97 | else: |
| 98 | raise exceptions.InvalidConfiguration( |
Sean Dague | 14c6818 | 2013-04-14 15:34:30 -0400 | [diff] [blame] | 99 | "Unable to get access and secret keys") |
Attila Fazekas | 1aed620 | 2013-02-11 14:47:45 +0100 | [diff] [blame] | 100 | return self.connect_method(**self.connection_data) |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 101 | |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 102 | |
| 103 | class APIClientEC2(BotoClientBase): |
| 104 | |
| 105 | def connect_method(self, *args, **kwargs): |
| 106 | return boto.connect_ec2(*args, **kwargs) |
| 107 | |
| 108 | def __init__(self, config, *args, **kwargs): |
| 109 | super(APIClientEC2, self).__init__(config, *args, **kwargs) |
| 110 | aws_access = config.boto.aws_access |
| 111 | aws_secret = config.boto.aws_secret |
| 112 | purl = urlparse.urlparse(config.boto.ec2_url) |
| 113 | |
Attila Fazekas | 1aed620 | 2013-02-11 14:47:45 +0100 | [diff] [blame] | 114 | region = boto.ec2.regioninfo.RegionInfo(name=config.identity.region, |
| 115 | endpoint=purl.hostname) |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 116 | port = purl.port |
| 117 | if port is None: |
| 118 | if purl.scheme is not "https": |
| 119 | port = 80 |
| 120 | else: |
| 121 | port = 443 |
| 122 | else: |
| 123 | port = int(port) |
| 124 | self.connection_data = {"aws_access_key_id": aws_access, |
| 125 | "aws_secret_access_key": aws_secret, |
| 126 | "is_secure": purl.scheme == "https", |
| 127 | "region": region, |
| 128 | "host": purl.hostname, |
| 129 | "port": port, |
| 130 | "path": purl.path} |
| 131 | |
| 132 | ALLOWED_METHODS = set(('create_key_pair', 'get_key_pair', |
| 133 | 'delete_key_pair', 'import_key_pair', |
| 134 | 'get_all_key_pairs', |
Burt Holzman | bb575e1 | 2013-07-14 00:23:30 -0500 | [diff] [blame] | 135 | 'get_all_tags', |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 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(): |
Attila Fazekas | a8b5fe7 | 2013-08-01 16:59:06 +0200 | [diff] [blame^] | 183 | # NOTE(afazekas): zone.region_name was None |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 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 | |
| 191 | class 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 Fazekas | 1aed620 | 2013-02-11 14:47:45 +0100 | [diff] [blame] | 214 | "calling_format": boto.s3.connection. |
| 215 | OrdinaryCallingFormat()} |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 216 | |
| 217 | ALLOWED_METHODS = set(('create_bucket', 'delete_bucket', 'generate_url', |
| 218 | 'get_all_buckets', 'get_bucket', 'delete_key', |
| 219 | 'lookup')) |