Пример #1
0
    def delete(self, uuid):
        """Delete a host label."""
        if self._from_ihosts:
            raise exception.OperationNotPermitted

        lbl_obj = objects.label.get_by_uuid(pecan.request.context, uuid)
        host = objects.host.get_by_uuid(pecan.request.context, lbl_obj.host_id)

        _check_host_locked(host, [lbl_obj.label_key])

        label_dict = {lbl_obj.label_key: None}

        try:
            pecan.request.rpcapi.update_kubernetes_label(
                pecan.request.context, host.uuid, label_dict)
        except rpc_common.RemoteError as e:
            raise wsme.exc.ClientSideError(str(e.value))
        except Exception as e:
            with excutils.save_and_reraise_exception():
                LOG.exception(e)

        try:
            pecan.request.dbapi.label_destroy(lbl_obj.uuid)
        except exception.HostLabelNotFound:
            msg = _("Delete host label failed: host %s label %s=%s" %
                    (host.hostname, lbl_obj.label_key, lbl_obj.label_value))
            raise wsme.exc.ClientSideError(msg)

        try:
            vim_api.vim_host_update(None, host.uuid, host.hostname,
                                    constants.VIM_DEFAULT_TIMEOUT_IN_SECS)
        except Exception as e:
            LOG.warn(_("No response vim_api host:%s e=%s" %
                       (host.hostname, e)))
            pass
Пример #2
0
    def post(self, uuid, body):
        """Assign label(s) to a host.
        """
        if self._from_ihosts:
            raise exception.OperationNotPermitted

        LOG.info("patch_data: %s" % body)
        host = objects.host.get_by_uuid(pecan.request.context, uuid)

        _check_host_locked(host)

        try:
            pecan.request.rpcapi.update_kubernetes_label(
                pecan.request.context,
                host.uuid,
                body
            )
        except rpc_common.RemoteError as e:
            raise wsme.exc.ClientSideError(str(e.value))
        except Exception as e:
            with excutils.save_and_reraise_exception():
                LOG.exception(e)

        new_records = []
        for key, value in body.items():
            values = {
                'host_id': host.id,
                'label_key': key,
                'label_value': value
            }

            try:
                new_label = pecan.request.dbapi.label_create(uuid, values)
                new_records.append(new_label)
            except exception.HostLabelAlreadyExists:
                pass

        try:
            vim_api.vim_host_update(
                None,
                uuid,
                host.hostname,
                constants.VIM_DEFAULT_TIMEOUT_IN_SECS)
        except Exception as e:
            LOG.warn(_("No response vim_api host:%s e=%s" %
                     (host.hostname, e)))
            pass

        return LabelCollection.convert_with_links(
            new_records, limit=None, url=None, expand=False,
            sort_key='id', sort_dir='asc')
Пример #3
0
    def post(self, uuid, overwrite=False, body=None):
        """Assign label(s) to a host.
        """
        if self._from_ihosts:
            raise exception.OperationNotPermitted

        LOG.info("patch_data: %s" % body)
        host = objects.host.get_by_uuid(pecan.request.context, uuid)

        _check_host_locked(host, body.keys())

        _semantic_check_worker_labels(body)

        _semantic_check_k8s_plugins_labels(host, body)

        existing_labels = {}
        for label_key in body.keys():
            label = None
            try:
                label = pecan.request.dbapi.label_query(host.id, label_key)
            except exception.HostLabelNotFoundByKey:
                pass
            if label:
                if overwrite:
                    existing_labels.update({label_key: label.uuid})
                else:
                    raise wsme.exc.ClientSideError(
                        _("Label %s exists for host %s. Use overwrite option to assign a new value."
                          % (label_key, host.hostname)))

        try:
            pecan.request.rpcapi.update_kubernetes_label(
                pecan.request.context, host.uuid, body)
        except rpc_common.RemoteError as e:
            raise wsme.exc.ClientSideError(str(e.value))
        except Exception as e:
            with excutils.save_and_reraise_exception():
                LOG.exception(e)

        new_records = []
        for key, value in body.items():
            values = {
                'host_id': host.id,
                'label_key': key,
                'label_value': value
            }
            try:
                if existing_labels.get(key, None):
                    # Update the value
                    label_uuid = existing_labels.get(key)
                    new_label = pecan.request.dbapi.label_update(
                        label_uuid, {'label_value': value})
                else:
                    new_label = pecan.request.dbapi.label_create(uuid, values)
                new_records.append(new_label)
            except exception.HostLabelAlreadyExists:
                # We should not be here
                raise wsme.exc.ClientSideError(
                    _("Error creating label %s") % label_key)

        try:
            vim_api.vim_host_update(None, uuid, host.hostname,
                                    constants.VIM_DEFAULT_TIMEOUT_IN_SECS)
        except Exception as e:
            LOG.warn(_("No response vim_api host:%s e=%s" %
                       (host.hostname, e)))
            pass

        return LabelCollection.convert_with_links(new_records,
                                                  limit=None,
                                                  url=None,
                                                  expand=False,
                                                  sort_key='id',
                                                  sort_dir='asc')