Esempio n. 1
0
def _update_sqlalchemy_object_fields(obj, updateable_fields, values_to_update):
    """ Updates the given 'values_to_update' on the provided sqlalchemy object
    as long as they are included as 'updateable_fields'.
    :param obj: object: sqlalchemy object
    :param updateable_fields: list(str): list of fields which are updateable
    :param values_to_update: dict: dict with the key/vals to update
    """
    if not isinstance(values_to_update, dict):
        raise exception.InvalidInput(
            "Properties to update for DB object of type '%s' must be a dict, "
            "got the following (type %s): %s" % (
                type(obj), type(values_to_update), values_to_update))

    non_updateable_fields = set(
        values_to_update.keys()).difference(
            set(updateable_fields))
    if non_updateable_fields:
        raise exception.Conflict(
            "Fields %s for '%s' database cannot be updated. "
            "Only updateable fields are: %s" % (
                non_updateable_fields, type(obj), updateable_fields))

    for field_name, field_val in values_to_update.items():
        if not hasattr(obj, field_name):
            raise exception.InvalidInput(
                "No region field named '%s' to update." % field_name)
        setattr(obj, field_name, field_val)
    LOG.debug(
        "Successfully updated the following fields on DB object "
        "of type '%s': %s" % (type(obj), values_to_update.keys()))
Esempio n. 2
0
    def _do_req(self,
                method_name,
                resource,
                body=None,
                response_key=None,
                raw_response=False,
                appliance_scoped=True):
        method = getattr(requests, method_name.lower(), None)
        if not method:
            raise ValueError("No such HTTP method '%s'" % method_name)

        url = self._get_url_for_resource(resource)
        if appliance_scoped:
            url = self._get_url_for_appliance_resource(resource)

        kwargs = {
            "verify": self._verify,
            "timeout": CONF.default_requests_timeout
        }
        if body:
            if not isinstance(body, (str, bytes)):
                body = json.dumps(body)
            kwargs["data"] = body

        LOG.debug("Making '%s' call to licensing server at '%s' with body: %s",
                  method_name, url, kwargs.get('data'))
        resp = method(url, **kwargs)

        if raw_response:
            return resp

        if not resp.ok:
            # try to extract error from licensing server:
            error = None
            try:
                error = resp.json().get('error', {})
            except (Exception, KeyboardInterrupt):
                LOG.debug(
                    "Exception occured during error extraction from licensing "
                    "response: '%s'\nException:\n%s", resp.text,
                    utils.get_exception_details())
            if error and all([x in error for x in ['code', 'message']]):
                raise exception.Conflict(message=error['message'],
                                         code=int(error['code']))
            else:
                resp.raise_for_status()

        resp_data = resp.json()
        if response_key:
            if response_key not in resp_data:
                raise ValueError("No response key '%s' in response body: %s" %
                                 (response_key, resp_data))
            resp_data = resp_data[response_key]

        return resp_data