Normalize external_name

Signed-off-by: Joffrey F <joffrey@docker.com>
This commit is contained in:
Joffrey F 2016-01-13 12:07:08 -08:00
parent 8616b2de51
commit d601199eb5
5 changed files with 22 additions and 14 deletions

View File

@ -273,6 +273,13 @@ def load_volumes(config_files):
for config_file in config_files: for config_file in config_files:
for name, volume_config in config_file.config.get('volumes', {}).items(): for name, volume_config in config_file.config.get('volumes', {}).items():
volumes.update({name: volume_config}) volumes.update({name: volume_config})
external = volume_config.get('external')
if external:
if isinstance(external, dict):
volume_config['external_name'] = external.get('name')
else:
volume_config['external_name'] = name
return volumes return volumes

View File

@ -79,7 +79,7 @@ class Project(object):
client=client, project=name, name=vol_name, client=client, project=name, name=vol_name,
driver=data.get('driver'), driver=data.get('driver'),
driver_opts=data.get('driver_opts'), driver_opts=data.get('driver_opts'),
external=data.get('external', False) external_name=data.get('external_name')
) )
) )
return project return project

View File

@ -6,18 +6,13 @@ from docker.errors import NotFound
class Volume(object): class Volume(object):
def __init__(self, client, project, name, driver=None, driver_opts=None, def __init__(self, client, project, name, driver=None, driver_opts=None,
external=False): external_name=None):
self.client = client self.client = client
self.project = project self.project = project
self.name = name self.name = name
self.driver = driver self.driver = driver
self.driver_opts = driver_opts self.driver_opts = driver_opts
self.external_name = None self.external_name = external_name
if external:
if isinstance(external, dict):
self.external_name = external.get('name')
else:
self.external_name = self.name
def create(self): def create(self):
return self.client.create_volume( return self.client.create_volume(

View File

@ -687,7 +687,9 @@ class ProjectTest(DockerClientTestCase):
'name': 'web', 'name': 'web',
'image': 'busybox:latest', 'image': 'busybox:latest',
'command': 'top' 'command': 'top'
}], volumes={vol_name: {'external': True}} }], volumes={
vol_name: {'external': True, 'external_name': vol_name}
}
) )
project = Project.from_config( project = Project.from_config(
name='composetest', name='composetest',
@ -706,7 +708,9 @@ class ProjectTest(DockerClientTestCase):
'name': 'web', 'name': 'web',
'image': 'busybox:latest', 'image': 'busybox:latest',
'command': 'top' 'command': 'top'
}], volumes={vol_name: {'external': True}} }], volumes={
vol_name: {'external': True, 'external_name': vol_name}
}
) )
project = Project.from_config( project = Project.from_config(
name='composetest', name='composetest',

View File

@ -18,10 +18,12 @@ class VolumeTest(DockerClientTestCase):
except DockerException: except DockerException:
pass pass
def create_volume(self, name, driver=None, opts=None, external=False): def create_volume(self, name, driver=None, opts=None, external=None):
if external and isinstance(external, bool):
external = name
vol = Volume( vol = Volume(
self.client, 'composetest', name, driver=driver, driver_opts=opts, self.client, 'composetest', name, driver=driver, driver_opts=opts,
external=external external_name=external
) )
self.tmp_volumes.append(vol) self.tmp_volumes.append(vol)
return vol return vol
@ -66,7 +68,7 @@ class VolumeTest(DockerClientTestCase):
def test_external_aliased_volume(self): def test_external_aliased_volume(self):
alias_name = 'composetest_alias01' alias_name = 'composetest_alias01'
vol = self.create_volume('volume01', external={'name': alias_name}) vol = self.create_volume('volume01', external=alias_name)
assert vol.external is True assert vol.external is True
assert vol.full_name == alias_name assert vol.full_name == alias_name
vol.create() vol.create()
@ -86,7 +88,7 @@ class VolumeTest(DockerClientTestCase):
assert vol.exists() is True assert vol.exists() is True
def test_exists_external_aliased(self): def test_exists_external_aliased(self):
vol = self.create_volume('volume01', external={'name': 'composetest_alias01'}) vol = self.create_volume('volume01', external='composetest_alias01')
assert vol.exists() is False assert vol.exists() is False
vol.create() vol.create()
assert vol.exists() is True assert vol.exists() is True