summaryrefslogtreecommitdiff
path: root/nova/virt/vmwareapi/ds_util.py
diff options
context:
space:
mode:
authorArnaud Legendre <arnaudleg@gmail.com>2014-11-24 14:29:07 -0800
committerGary Kotton <gkotton@vmware.com>2015-03-20 07:53:20 -0700
commite7ae5bb7fbdd5b79bde8937958dd0a645554a5f0 (patch)
tree2eba4bf2054ec20ec731f8440f43dda1aef04dd7 /nova/virt/vmwareapi/ds_util.py
parent37a6c601f37790a94b6e2db7f4fa41f143b9caa8 (diff)
downloadnova-e7ae5bb7fbdd5b79bde8937958dd0a645554a5f0.tar.gz
VMware: Consume the oslo.vmware objects
oslo.vmware now contains the datastore and datacenter objects. This patch gets rid of the nova datastore object and leverages oslo.vmware. Co-authored-by: Gary Kotton <gkotton@vmware.com> Change-Id: I3f1e65dec1500afb58dcb841991ac976887de09d
Diffstat (limited to 'nova/virt/vmwareapi/ds_util.py')
-rw-r--r--nova/virt/vmwareapi/ds_util.py165
1 files changed, 7 insertions, 158 deletions
diff --git a/nova/virt/vmwareapi/ds_util.py b/nova/virt/vmwareapi/ds_util.py
index 118c14e4d0..d447715594 100644
--- a/nova/virt/vmwareapi/ds_util.py
+++ b/nova/virt/vmwareapi/ds_util.py
@@ -16,10 +16,9 @@
Datastore utility functions
"""
-import posixpath
-
from oslo_log import log as logging
from oslo_vmware import exceptions as vexc
+from oslo_vmware.objects import datastore as ds_obj
from oslo_vmware import pbm
from nova import exception
@@ -34,156 +33,6 @@ ALL_SUPPORTED_DS_TYPES = frozenset([constants.DATASTORE_TYPE_VMFS,
constants.DATASTORE_TYPE_VSAN])
-class Datastore(object):
-
- def __init__(self, ref, name, capacity=None, freespace=None):
- """Datastore object holds ref and name together for convenience.
-
- :param ref: a vSphere reference to a datastore
- :param name: vSphere unique name for this datastore
- :param capacity: (optional) capacity in bytes of this datastore
- :param freespace: (optional) free space in bytes of datastore
- """
- if name is None:
- raise ValueError(_("Datastore name cannot be None"))
- if ref is None:
- raise ValueError(_("Datastore reference cannot be None"))
- if freespace is not None and capacity is None:
- raise ValueError(_("Invalid capacity"))
- if capacity is not None and freespace is not None:
- if capacity < freespace:
- raise ValueError(_("Capacity is smaller than free space"))
-
- self._ref = ref
- self._name = name
- self._capacity = capacity
- self._freespace = freespace
-
- @property
- def ref(self):
- return self._ref
-
- @property
- def name(self):
- return self._name
-
- @property
- def capacity(self):
- return self._capacity
-
- @property
- def freespace(self):
- return self._freespace
-
- def build_path(self, *paths):
- """Constructs and returns a DatastorePath.
-
- :param paths: list of path components, for constructing a path relative
- to the root directory of the datastore
- :return: a DatastorePath object
- """
- return DatastorePath(self._name, *paths)
-
- def __str__(self):
- return '[%s]' % self._name
-
-
-class DatastorePath(object):
- """Class for representing a directory or file path in a vSphere datatore.
-
- This provides various helper methods to access components and useful
- variants of the datastore path.
-
- Example usage:
-
- DatastorePath("datastore1", "_base/foo", "foo.vmdk") creates an
- object that describes the "[datastore1] _base/foo/foo.vmdk" datastore
- file path to a virtual disk.
-
- Note:
-
- * Datastore path representations always uses forward slash as separator
- (hence the use of the posixpath module).
- * Datastore names are enclosed in square brackets.
- * Path part of datastore path is relative to the root directory
- of the datastore, and is always separated from the [ds_name] part with
- a single space.
-
- """
-
- VMDK_EXTENSION = "vmdk"
-
- def __init__(self, datastore_name, *paths):
- if datastore_name is None or datastore_name == '':
- raise ValueError(_("datastore name empty"))
- self._datastore_name = datastore_name
- self._rel_path = ''
- if paths:
- if None in paths:
- raise ValueError(_("path component cannot be None"))
- self._rel_path = posixpath.join(*paths)
-
- def __str__(self):
- """Full datastore path to the file or directory."""
- if self._rel_path != '':
- return "[%s] %s" % (self._datastore_name, self.rel_path)
- return "[%s]" % self._datastore_name
-
- def __repr__(self):
- return "%s(%s, %s)" % (self.__class__.__name__,
- self.datastore, self.rel_path)
-
- @property
- def datastore(self):
- return self._datastore_name
-
- @property
- def parent(self):
- return DatastorePath(self.datastore, posixpath.dirname(self._rel_path))
-
- @property
- def basename(self):
- return posixpath.basename(self._rel_path)
-
- @property
- def dirname(self):
- return posixpath.dirname(self._rel_path)
-
- @property
- def rel_path(self):
- return self._rel_path
-
- def join(self, *paths):
- if paths:
- if None in paths:
- raise ValueError(_("path component cannot be None"))
- return DatastorePath(self.datastore,
- posixpath.join(self._rel_path, *paths))
- return self
-
- def __eq__(self, other):
- return (isinstance(other, DatastorePath) and
- self._datastore_name == other._datastore_name and
- self._rel_path == other._rel_path)
-
- def __hash__(self):
- return str(self).__hash__()
-
- @classmethod
- def parse(cls, datastore_path):
- """Constructs a DatastorePath object given a datastore path string."""
- if not datastore_path:
- raise ValueError(_("datastore path empty"))
-
- spl = datastore_path.split('[', 1)[1].split(']', 1)
- path = ""
- if len(spl) == 1:
- datastore_name = spl[0]
- else:
- datastore_name, path = spl
- return cls(datastore_name, path.strip())
-
-
# NOTE(mdbooth): this convenience function is temporarily duplicated in
# vm_util. The correct fix is to handle paginated results as they are returned
# from the relevant vim_util function. However, vim_util is currently
@@ -224,7 +73,7 @@ def _select_datastore(session, data_stores, best_match, datastore_regex=None,
propdict = vm_util.propset_dict(obj_content.propSet)
if _is_datastore_valid(propdict, datastore_regex, allowed_ds_types):
- new_ds = Datastore(
+ new_ds = ds_obj.Datastore(
ref=obj_content.obj,
name=propdict['summary.name'],
capacity=propdict['summary.capacity'],
@@ -317,9 +166,9 @@ def get_datastore_by_ref(session, ds_ref):
props = session._call_method(vim_util, "get_object_properties",
None, ds_ref, "Datastore", lst_properties)
query = vm_util.get_values_from_object_properties(session, props)
- return Datastore(ds_ref, query["summary.name"],
- capacity=query["summary.capacity"],
- freespace=query["summary.freeSpace"])
+ return ds_obj.Datastore(ds_ref, query["summary.name"],
+ capacity=query["summary.capacity"],
+ freespace=query["summary.freeSpace"])
def _get_allowed_datastores(data_stores, datastore_regex):
@@ -333,8 +182,8 @@ def _get_allowed_datastores(data_stores, datastore_regex):
if _is_datastore_valid(propdict,
datastore_regex,
ALL_SUPPORTED_DS_TYPES):
- allowed.append(Datastore(ref=obj_content.obj,
- name=propdict['summary.name']))
+ allowed.append(ds_obj.Datastore(ref=obj_content.obj,
+ name=propdict['summary.name']))
return allowed