Add search function for volumes
@@ -49,6 +49,8 @@ LXD_API_IMAGE_SOURCE_TYPE = ["image", "migration", "copy", "none"]
# occurred for a request
LXD_ERROR_STATUS_RESP = 'error'
+DUMMY_POOL_NAME = "DummyPool"
+
# helpers
def strip_http_prefix(host):
@@ -1237,6 +1239,46 @@ class LXDContainerDriver(ContainerDriver):
return self._to_storage_volume(pool_id=pool_id,
metadata=response_dict["metadata"])
+ def ex_get_volume_by_name(self, name, vol_type="custom"):
+ """
+ Returns a storage volume that has the given name.
+ The function will loop over all storage-polls available
+ and will pick the first volume from the first storage poll
+ that matches the given name. Thus this function can be
+ quite expensive
+
+ :param name: The name of the volume to look for
+ :type name: str
+
+ :param vol_type: The type of the volume default is custom
+ :type vol_type: str
+
+ :return: A StorageVolume representing a storage volume
+ """
+
+ req = '/%s/storage-pools' % self.version
+ response = self.connection.request(req)
+ response_dict = response.parse_body()
+ assert_response(response_dict=response_dict, status_code=200)
+
+ pools = response_dict['metadata']
+
+ for pool in pools:
+
+ pool_id = pool.split('/')[-1]
+
+ volumes = self.ex_list_storage_pool_volumes(pool_id=pool_id)
+
+ for vol in volumes:
+ if vol.name == name:
+ return vol
+
+ return self._to_storage_volume(pool_id=None,
+ metadata={"pool_id": None,
+ "type": None,
+ "used_by": None,
+ "config": None})
+
def create_volume(self, pool_id, definition, **kwargs):
"""
@@ -1427,7 +1469,6 @@ class LXDContainerDriver(ContainerDriver):
:param name: The name of the new network
:type name: str
-
"""
data = json.dumps(kwargs)
@@ -1438,7 +1479,6 @@ class LXDContainerDriver(ContainerDriver):
assert_response(response_dict=response_dict, status_code=200)
return self.ex_get_network(name=name)
-
def ex_delete_network(self, name):
"""
Delete the network with the given name
@@ -1458,7 +1498,6 @@ class LXDContainerDriver(ContainerDriver):
return True
-
def _to_container(self, metadata):
"""
Returns Container instance built from the given metadata
