Exemplo n.º 1
0
    def take_action(self, parsed_args):
        client = getattr(self.app.client_manager, "infra-optim")

        action_plan_uuid = parsed_args.action_plan

        if not uuidutils.is_uuid_like(action_plan_uuid):
            raise exceptions.ValidationError()

        try:
            action_plan = client.action_plan.get(action_plan_uuid)
        except exceptions.HTTPNotFound as exc:
            raise exceptions.CommandError(str(exc))

        if parsed_args.formatter == 'table':
            # Update the raw efficacy indicators with the formatted ones
            action_plan.efficacy_indicators = (self._format_indicators(
                action_plan, parsed_args))

            # Update the raw global efficacy with the formatted one
            action_plan.global_efficacy = self._format_global_efficacy(
                action_plan.global_efficacy, parsed_args)

        columns = res_fields.ACTION_PLAN_FIELDS
        column_headers = res_fields.ACTION_PLAN_FIELD_LABELS
        return column_headers, utils.get_item_properties(action_plan, columns)
Exemplo n.º 2
0
    def take_action(self, parsed_args):
        client = getattr(self.app.client_manager, "infra-optim")

        for action_plan in parsed_args.action_plans:
            if not uuidutils.is_uuid_like(action_plan):
                raise exceptions.ValidationError()

            client.action_plan.delete(action_plan)
Exemplo n.º 3
0
    def take_action(self, parsed_args):
        client = getattr(self.app.client_manager, "infra-optim")

        if not uuidutils.is_uuid_like(parsed_args.action_plan):
            raise exceptions.ValidationError()

        action_plan = client.action_plan.cancel(parsed_args.action_plan)

        columns = res_fields.ACTION_PLAN_FIELDS
        column_headers = res_fields.ACTION_PLAN_FIELD_LABELS

        return column_headers, utils.get_item_properties(action_plan, columns)
Exemplo n.º 4
0
    def take_action(self, parsed_args):
        client = getattr(self.app.client_manager, "infra-optim")

        if not uuidutils.is_uuid_like(parsed_args.action_plan):
            raise exceptions.ValidationError()

        patch = common_utils.args_array_to_patch(parsed_args.op,
                                                 parsed_args.attributes[0])

        action_plan = client.action_plan.update(parsed_args.action_plan, patch)

        columns = res_fields.ACTION_PLAN_FIELDS
        column_headers = res_fields.ACTION_PLAN_FIELD_LABELS

        return column_headers, utils.get_item_properties(action_plan, columns)
Exemplo n.º 5
0
    def _http_request(self, url, method, **kwargs):
        """Send an http request with the specified characteristics.

        Wrapper around request.Session.request to handle tasks such
        as setting headers and error handling.
        """
        # Copy the kwargs so we can reuse the original in case of redirects
        kwargs['headers'] = copy.deepcopy(kwargs.get('headers', {}))
        kwargs['headers'].setdefault('User-Agent', USER_AGENT)
        if self.os_watcher_api_version:
            kwargs['headers'].setdefault('X-OpenStack-Watcher-API-Version',
                                         self.os_watcher_api_version)
        if self.auth_token:
            kwargs['headers'].setdefault('X-Auth-Token', self.auth_token)

        self.log_curl_request(method, url, kwargs)

        # NOTE(aarefiev): This is for backwards compatibility, request
        # expected body in 'data' field, previously we used httplib,
        # which expected 'body' field.
        body = kwargs.pop('body', None)
        if body:
            kwargs['data'] = body

        conn_url = self._make_connection_url(url)
        try:
            resp = self.session.request(method, conn_url, **kwargs)

            # TODO(deva): implement graceful client downgrade when connecting
            # to servers that did not support microversions. Details here:
            # http://specs.openstack.org/openstack/watcher-specs/specs/kilo/api-microversions.html#use-case-3b-new-client-communicating-with-a-old-watcher-user-specified  # noqa

            if resp.status_code == http_client.NOT_ACCEPTABLE:
                negotiated_ver = self.negotiate_version(self.session, resp)
                kwargs['headers']['X-OpenStack-Watcher-API-Version'] = (
                    negotiated_ver)
                return self._http_request(url, method, **kwargs)

        except requests.exceptions.RequestException as e:
            message = (_("Error has occurred while handling "
                         "request for %(url)s: %(e)s") %
                       dict(url=conn_url, e=e))
            # NOTE(aarefiev): not valid request(invalid url, missing schema,
            # and so on), retrying is not needed.
            if isinstance(e, ValueError):
                raise exceptions.ValidationError(message)

            raise exceptions.ConnectionRefused(message)

        body_iter = resp.iter_content(chunk_size=CHUNKSIZE)

        # Read body into string if it isn't obviously image data
        body_str = None
        if resp.headers.get('Content-Type') != 'application/octet-stream':
            body_str = ''.join([chunk for chunk in body_iter])
            self.log_http_response(resp, body_str)
            body_iter = six.StringIO(body_str)
        else:
            self.log_http_response(resp)

        if resp.status_code >= http_client.BAD_REQUEST:
            error_json = _extract_error_json(body_str)
            raise exceptions.from_response(resp, error_json.get('faultstring'),
                                           error_json.get('debuginfo'), method,
                                           url)
        elif resp.status_code in (http_client.MOVED_PERMANENTLY,
                                  http_client.FOUND, http_client.USE_PROXY):
            # Redirected. Reissue the request to the new location.
            return self._http_request(resp['location'], method, **kwargs)
        elif resp.status_code == http_client.MULTIPLE_CHOICES:
            raise exceptions.from_response(resp, method=method, url=url)

        return resp, body_iter