def _translate_response(self, response, has_body=None, error_message=None): if has_body is None: has_body = self.has_body # NOTE: we only use our own exception parser exc.raise_from_response(response, error_message=error_message) if has_body: try: body = response.json() if self.resource_key and self.resource_key in body: body = body[self.resource_key] body_attrs = self._consume_body_attrs(body) if self._store_unknown_attrs_as_properties: body_attrs = self._pack_attrs_under_properties( body_attrs, body) self._body.attributes.update(body_attrs) self._body.clean() if self.commit_jsonpatch or self.allow_patch: # We need the original body to compare against self._original_body = body_attrs.copy() except ValueError: # Server returned not parse-able response (202, 204, etc) # Do simply nothing pass headers = self._consume_header_attrs(response.headers) self._header.attributes.update(headers) self._header.clean() self._update_location() dict.update(self, self.to_dict())
def _translate_response(self, response, has_body=None, error_message=None): """Given a KSA response, inflate this instance with its data 'DELETE' operations don't return a body, so only try to work with a body when has_body is True. This method updates attributes that correspond to headers and body on this instance and clears the dirty set. """ if has_body is None: has_body = self.has_body exc.raise_from_response(response, error_message=error_message) if has_body: if response.status_code == 204: # Some bad APIs (i.e. DCS.Backup.List) return emptiness self.log.warn('API returned no content, while it was expected') return # NOTE: in difference to doc "GET" method return list with 1 item body = response.json() if isinstance(body, list): body = body[0] if self.resource_key and self.resource_key in body: body = body[self.resource_key] body = self._consume_body_attrs(body) self._body.attributes.update(body) self._body.clean() headers = self._consume_header_attrs(response.headers) self._header.attributes.update(headers) self._header.clean()
def batch_delete(cls, session, configs): """Batch delete auto-scaling configs :param session: openstack session. :param configs: The list item value can contain IDs of a config or a :class:`~otcextensions.sdk.auto_scaling.v1.config.Config` instance. :returns: None """ ids = [ config.id if isinstance(config, Config) else config for config in configs ] json_body = {'scaling_configuration_id': ids} response = session.post('/scaling_configurations', headers={'Accept': '*'}, json=json_body) if response.status_code == 400: # Check if failed due to not exist content = response.json() error = content.get('error', None) if error.get('code', None) == 'AS.1038': ids = [] message = error.get('message', None) if message.startswith('['): items = json.loads(message) for config_message in items: id = config_message.get('id', None) code = config_message.get('errorCode', None) if code == 'AS.1004': # Config does not exist ids.append(id) if len(ids) > 0: message = (_('AS Configurations (%(ids)s) not found') % { 'ids': ids }) raise exceptions.ResourceNotFound(message=message) # unknown failure exc.raise_from_response(response) return response