Beispiel #1
0
    def run(self, object_names, object_type, vsphere=None):
        """
        Transform object_name and object_type to MOID (Managed Object Reference ID). If the objects
        are not found then this action will fail.

        Args:
        - object_name: name of object that is labeled to the target
        - object_type: vimType of convert object

        Returns:
        - dict: key value pair of object_name and moid.
        """
        vimtype = self.get_vim_type(object_type)

        self.establish_connection(vsphere)

        results = {}
        for name in object_names:
            try:
                # consult vSphere for the managed_entity object that is the specified name and type
                managed_entity = inventory.get_managed_entity(self.si_content, vimtype, name=name)

                # extract moid from vim.ManagedEntity object
                results[name] = str(managed_entity).split(':')[-1].replace("'", "")
            except Exception as e:
                self.logger.warning(str(e))

        # Raise an error if no MOIDs were found for the VMs
        if not results:
            return (False, {})

        return (True, results)
Beispiel #2
0
    def get_storage(self, host, datastore_filter_strategy, datastore_filter,
                    disks):
        """Return a datastore on the host that is either specified in the disks variable or
        has the most free space and a name that doesn't match any filters
        :param host: Host object to retrieve a datastore from
        :param datastore_filter: Object containing list of filtersto exclude certain datastores
          :example: ["string1", "string2"]
        :param disks: Object containing a list of disks to add to the VM
          :example:
            [{
               "size_gb": "string",
               "uuid": "string",
               "datastore": "string",
               "controller_bus": "string",
               "scsi_bus": "string"
            }]
        :returns datastore: Datastore object from the given host
        """
        datastore = None

        # First check the disks variable for a datastore to use
        if disks is not None:
            first_disk = disks[0]
            datastore_name = first_disk['datastore']
            # If the disks variable is given and it's datastore key is not "automatic" then
            # the datastore from the disks variable will be returned
            if datastore_name != "automatic":
                vimtype = self.get_vim_type("Datastore")
                datastore = inventory.get_managed_entity(self.si_content,
                                                         vimtype,
                                                         name=datastore_name)

        # If the disks variable is empty or the datastore is set to "automatic" then search
        # the available datastores on the host for the one with the most free space
        if datastore is None:
            storages = host.datastore
            most_space = 0
            for ds in storages:
                # only allow placing onto a datastore in "normal" mode
                # this prevents from being placed onto a datastore in "maintenance" mode
                # Valid values are:
                #  - enteringMaintenance
                #  - inMaintenance
                #  - normal
                #
                # https://vdc-repo.vmware.com/vmwb-repository/dcr-public/6b586ed2-655c-49d9-9029-bc416323cb22/fa0b429a-a695-4c11-b7d2-2cbc284049dc/doc/vim.Datastore.Summary.html
                # https://vdc-repo.vmware.com/vmwb-repository/dcr-public/6b586ed2-655c-49d9-9029-bc416323cb22/fa0b429a-a695-4c11-b7d2-2cbc284049dc/doc/vim.Datastore.Summary.MaintenanceModeState.html
                if ds.summary.maintenanceMode != 'normal':
                    continue

                # The following function returns False if the name of the datastore
                # matches any of the regex filters
                if self.filter_datastores(ds.name, datastore_filter_strategy,
                                          datastore_filter):
                    if ds.info.freeSpace > most_space:
                        datastore = ds
                        most_space = ds.info.freeSpace

        return datastore
Beispiel #3
0
    def get_storage(self, host, datastore_filter, disks):
        """Return a datastore on the host that is either specified in the disks variable or
        has the most free space and a name that doesn't match any filters
        :param host: Host object to retrieve a datastore from
        :param datastore_filter: Object containing list of filtersto exclude certain datastores
          :example: ["string1", "string2"]
        :param disks: Object containing a list of disks to add to the VM
          :example:
            [{
               "size_gb": "string",
               "uuid": "string",
               "datastore": "string",
               "controller_bus": "string",
               "scsi_bus": "string"
            }]
        :returns datastore: Datastore object from the given host
        """
        datastore = None

        # First check the disks variable for a datastotre to use
        if disks is not None:
            first_disk = disks[0]
            datastore_name = first_disk['datastore']
            # If the disks variable is given and it's datastore key is not "automatic" then
            # the datastore from the disks variable will be returned
            if datastore_name != "automatic":
                vimtype = self.get_vim_type("Datastore")
                datastore = inventory.get_managed_entity(self.si_content,
                                                         vimtype,
                                                         name=datastore_name)

        # If the disks variable is empty or the datastore is set to "automatic" then search
        # the available datastores on the host for the one with the most free space
        if datastore is None:
            storages = host.datastore
            most_space = 0
            for ds in storages:
                # The following function returns False if the name of the datastore
                # matches any of the regex filters
                if self.filter_datastores(ds.name, datastore_filter):
                    if ds.info.freeSpace > most_space:
                        datastore = ds
                        most_space = ds.info.freeSpace

        return datastore
Beispiel #4
0
    def run(self,
            custom_attr_name,
            custom_attr_value,
            object_id,
            object_type,
            vsphere=None):
        """
        Create a custom attribute if it doesn't exist and asssign it to a given object

        Args:
        - custom_attr_name: name of custom attribute to assign
        - custom_attr_value: value of custom attribute to assign
        - object_id: MOID of an object to assign a custom attribute to
        - object_type: vimType of convert object
        - vsphere: Pre-configured vsphere connection details (config.yaml)

        Returns:
        - string: string with success or error message
        """
        vimtype = self.get_vim_type(object_type)

        self.establish_connection(vsphere)

        # consult vSphere for the managed_entity object that is the specified name and type
        entity = inventory.get_managed_entity(self.si_content,
                                              vimtype,
                                              moid=object_id)

        cfm = self.si_content.customFieldsManager

        # Get the custom attribute object with the given name or 'None' if it isn't found
        field = next(
            (field for field in cfm.field if field.name == custom_attr_name),
            None)

        if field is None:
            field = cfm.AddCustomFieldDef(name=custom_attr_name)

        cfm.SetField(entity=entity, key=field.key, value=custom_attr_value)

        return (True, "Attribute: '%s' set on object: '%s' with value: '%s'" %
                (custom_attr_name, object_id, custom_attr_value))
Beispiel #5
0
    def run(self, object_names, object_type, vsphere=None):
        """
        Transform object_name and object_type to MOID (Managed Object Reference ID).

        Args:
        - object_name: name of object that is labeled to the target
        - object_type: vimType of convert object

        Returns:
        - dict: key value pair of object_name and moid.
        """

        if object_type == 'VirtualMachine':
            vimtype = vim.VirtualMachine
        elif object_type == 'Network':
            vimtype = vim.Network
        elif object_type == 'Datastore':
            vimtype = vim.Datastore
        elif object_type == 'Datacenter':
            vimtype = vim.Datacenter
        elif object_type == 'Host':
            vimtype = vim.HostSystem
        else:
            self.logger.warning("specified object_type ('%s') is not supported" % object_type)
            return (False, {})

        self.establish_connection(vsphere)

        results = {}
        for name in object_names:
            try:
                # consult vSphere for the managed_entity object that is the specified name and type
                managed_entity = inventory.get_managed_entity(self.si_content, vimtype, name=name)

                # extract moid from vim.ManagedEntity object
                results[name] = str(managed_entity).split(':')[-1].replace("'", "")
            except Exception as e:
                self.logger.warning(str(e))

        return (True, results)
Beispiel #6
0
    def run(self, custom_attr_name, object_id, object_type, vsphere=None):
        """
        Get the value of a given Custom Attribute on an object

        Args:
        - custom_attr_name: name of custom attribute to get the value of
        - object_id: MOID of an object with a given custom attribute
        - object_type: vimType of convert object
        - vsphere: Pre-configured vsphere connection details (config.yaml)

        Returns:
        - string: string with value of the given custom attribute name
        """
        vimtype = self.get_vim_type(object_type)

        self.establish_connection(vsphere)

        # consult vSphere for the managed_entity object that is the specified ID and type
        entity = inventory.get_managed_entity(self.si_content,
                                              vimtype,
                                              moid=object_id)

        cfm = self.si_content.customFieldsManager

        # Find the ID of the custom attribute from the given name
        attr_id = next(
            (field.key
             for field in cfm.field if field.name == custom_attr_name), None)

        if not attr_id:
            return (False, "Attribute: '%s' has no value on object: '%s'" %
                    (custom_attr_name, object_id))

        # Use the ID from above to find the value of the custom attribute on the object
        attr_value = next(
            (attr.value
             for attr in entity.summary.customValue if attr.key == attr_id),
            None)

        return (True, attr_value)