diff --git a/.gitignore b/.gitignore index f3d74a9..9b4d590 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ *.pyc *~ +*venv +.idea diff --git a/cinder/tests/unit/volume/drivers/test_ebs.py b/cinder/tests/unit/volume/drivers/test_ebs.py index 93e9836..09229b0 100644 --- a/cinder/tests/unit/volume/drivers/test_ebs.py +++ b/cinder/tests/unit/volume/drivers/test_ebs.py @@ -17,9 +17,9 @@ from cinder import context from cinder import test from cinder.exception import APITimeout, NotFound, VolumeNotFound from cinder.volume.drivers.aws import ebs +from cinder.volume.drivers.aws.exception import AvailabilityZoneNotFound from moto import mock_ec2 -import boto class EBSVolumeTestCase(test.TestCase): @@ -61,6 +61,14 @@ class EBSVolumeTestCase(test.TestCase): ss['display_name'] = kwargs.get('display_name', 'snapshot_007') return ss + @mock_ec2 + def test_availability_zone_config(self): + ebs.CONF.AWS.az = 'hgkjhgkd' + driver = ebs.EBSDriver() + ctxt = context.get_admin_context() + self.assertRaises(AvailabilityZoneNotFound, driver.do_setup, ctxt) + ebs.CONF.AWS.az = 'us-east-1a' + @mock_ec2 def test_volume_create_success(self): self.assertIsNone(self._driver.create_volume(self._stub_volume())) @@ -129,4 +137,4 @@ class EBSVolumeTestCase(test.TestCase): @mock_ec2 def test_volume_from_non_existing_snapshot(self): self.assertRaises(NotFound, self._driver.create_volume_from_snapshot, - self._stub_volume(), self._stub_snapshot()) \ No newline at end of file + self._stub_volume(), self._stub_snapshot()) diff --git a/cinder/volume/drivers/aws/ebs.py b/cinder/volume/drivers/aws/ebs.py index 7fecacb..5079dd4 100644 --- a/cinder/volume/drivers/aws/ebs.py +++ b/cinder/volume/drivers/aws/ebs.py @@ -22,6 +22,7 @@ from cinder.i18n import _LE from cinder.exception import VolumeNotFound, NotFound, APITimeout, InvalidConfigurationValue from cinder.volume.driver import BaseVD +from exception import AvailabilityZoneNotFound aws_group = cfg.OptGroup(name='AWS', title='Options to connect to an AWS environment') aws_opts = [ @@ -56,19 +57,6 @@ class EBSDriver(BaseVD): self.VERSION = '1.0.0' self._wait_time_sec = 60 * (CONF.AWS.wait_time_min) - self._check_config() - region_name = CONF.AWS.region_name - endpoint = '.'.join(['ec2', region_name, 'amazonaws.com']) - region = RegionInfo(name=region_name, endpoint=endpoint) - self._conn = ec2.EC2Connection(aws_access_key_id=CONF.AWS.access_key, - aws_secret_access_key=CONF.AWS.secret_key, - region=region) - # resort to first AZ for now. TODO: expose this through API - az = CONF.AWS.az - self._zone = filter(lambda z: z.name == az, - self._conn.get_all_zones())[0] - self.set_initialized() - def _check_config(self): tbl = dict([(n, eval(n)) for n in ['CONF.AWS.access_key', 'CONF.AWS.secret_key', @@ -79,7 +67,24 @@ class EBSDriver(BaseVD): raise InvalidConfigurationValue(value=None, option=k) def do_setup(self, context): - pass + self._check_config() + region_name = CONF.AWS.region_name + endpoint = '.'.join(['ec2', region_name, 'amazonaws.com']) + region = RegionInfo(name=region_name, endpoint=endpoint) + self._conn = ec2.EC2Connection(aws_access_key_id=CONF.AWS.access_key, + aws_secret_access_key=CONF.AWS.secret_key, + region=region) + # resort to first AZ for now. TODO: expose this through API + az = CONF.AWS.az + + try: + self._zone = filter(lambda z: z.name == az, + self._conn.get_all_zones())[0] + except IndexError: + raise AvailabilityZoneNotFound(az=az) + + self.set_initialized() + def _wait_for_create(self, id, final_state): def _wait_for_status(start_time): diff --git a/cinder/volume/drivers/aws/exception.py b/cinder/volume/drivers/aws/exception.py new file mode 100644 index 0000000..d9bbce9 --- /dev/null +++ b/cinder/volume/drivers/aws/exception.py @@ -0,0 +1,19 @@ +""" +Copyright 2017 Platform9 Systems Inc.(http://www.platform9.com) + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +""" + +from cinder.exception import CinderException +from cinder.i18n import _ + +class AvailabilityZoneNotFound(CinderException): + message = _("Availability Zone %(az)s was not found")