Example #1
0
    def _calculate_response(self, base_path, *_):
        """Reply with empty list of apps for the '/v2/apps' request

        Please refer to the description of the BaseHTTPRequestHandler class
        for details on the arguments and return value of this method.

        Raises:
            EndpointException: request URL path is unsupported
        """
        if base_path not in ['/v2/apps', "/v2/leader"]:
            msg = "Path `{}` is not supported yet".format(base_path)
            blob = msg.encode('utf-8')
            raise EndpointException(code=500, reason=blob)

        ctx = self.server.context

        with ctx.lock:
            if base_path == '/v2/apps':
                blob = self._convert_data_to_blob(ctx.data['endpoint-content'])
            elif base_path == '/v2/leader':
                if ctx.data['leader-content'] is None:
                    msg = "Marathon leader unknown"
                    blob = msg.encode('utf-8')
                    raise EndpointException(code=404, reason=blob)
                elif isinstance(ctx.data['leader-content'], str):
                    blob = ctx.data['leader-content'].encode('utf-8')
                    raise EndpointException(code=200, reason=blob)
                else:
                    blob = self._convert_data_to_blob(
                        ctx.data['leader-content'])

        return blob
Example #2
0
    def _calculate_response(self, base_path, url_args, body_args=None):
        """Reply with a static Mesos state-summary response.

        Please refer to the description of the BaseHTTPRequestHandler class
        for details on the arguments and return value of this method.

        Raises:
            EndpointException: request URL path is unsupported
        """
        if base_path == '/reflect/me':
            # A test URI that is used by tests. In some cases it is impossible
            # to reuse /master/state-summary path.
            return self._reflect_request(base_path, url_args, body_args)

        if base_path != '/master/state-summary':
            msg = "Path `{}` is not supported yet".format(base_path)
            blob = msg.encode('utf-8')
            raise EndpointException(code=500, reason=blob)

        ctx = self.server.context

        with ctx.lock:
            blob = self._convert_data_to_blob(ctx.data['endpoint-content'])

        return 200, 'application/json', blob
Example #3
0
    def __srv_permissions_request_handler(self, srvid):
        """Calculate reply for given service-ID

        Arguments:
            srvid (string): service ID to reply to
        """
        ctx = self.server.context

        if srvid not in ctx.data['services']:
            raise EndpointException(
                code=500, content="Service `{}` is unknown".format(srvid))

        blob = self._convert_data_to_blob(ctx.data['services'][srvid])
        return 200, 'application/json', blob
Example #4
0
File: iam.py Project: wtf-bj/dcos
    def _calculate_response(self, base_path, url_args, body_args=None):
        match = self.USERS_QUERY_REGEXP.search(base_path)
        if match:
            return self.__users_permissions_request_handler(match.group(1))

        if base_path == '/acs/api/v1/foo/bar':
            # A test URI that is used by tests. In some cases it is impossible
            # to reuse /acs/api/v1/users/ path.
            blob = self._convert_data_to_blob({})
            return 200, 'application/json', blob

        raise EndpointException(
            code=500,
            content="Path `{}` is not supported yet".format(base_path))
Example #5
0
    def _calculate_response(self, base_path, url_args, body_args=None):
        match = self.USERS_QUERY_REGEXP.search(base_path)
        if match:
            return self.__users_permissions_request_handler(match.group(1))

        stub_paths = [
            '/acs/api/v1/foo/bar',
        ]
        if base_path in stub_paths:
            blob = self._convert_data_to_blob({})
            return 200, 'application/json', blob

        raise EndpointException(
            code=500,
            content="Path `{}` is not supported yet".format(base_path))
Example #6
0
    def _calculate_response(self, base_path, *_):
        """Answer the query for user data basing on the endpoint context.

        Please refer to the description of the BaseHTTPRequestHandler class
        for details on the arguments and return value of this method.

        Raises:
            EndpointException: request URL path is unsupported
        """
        ctx = self.server.context

        with ctx.lock:
            # copy.deepcopy() can also be used here, instead of locking
            users = ctx.data['users']

            if not base_path.startswith(self.USERS_PATH_PREFIX) or \
                    base_path == self.USERS_PATH_PREFIX:
                msg = "Path `{}` is not supported yet".format(base_path)
                blob = msg.encode('utf-8')
                raise EndpointException(code=500, reason=blob)

            uid = base_path[len(self.USERS_PATH_PREFIX):]

            if uid not in users:
                res = {
                    "title": "Bad Request",
                    "description": "User `{}` not known.".format(uid),
                    "code": "ERR_UNKNOWN_USER_ID"
                }

                blob = self._convert_data_to_blob(res)
                raise EndpointException(code=400,
                                        reason=blob,
                                        content_type='application/json')

            return self._convert_data_to_blob(users[uid])
Example #7
0
    def _calculate_response(self, base_path, url_args, body_args=None):
        """Reply with the currently set mock-reply for given SRV record query.

        Please refer to the description of the BaseHTTPRequestHandler class
        for details on the arguments and return value of this method.

        Raises:
            EndpointException: request URL path is unsupported
        """

        match = self.SRV_QUERY_REGEXP.search(base_path)
        if match:
            return self.__srv_permissions_request_handler(match.group(1))

        raise EndpointException(
            code=500,
            content="Path `{}` is not supported yet".format(base_path))
Example #8
0
    def _calculate_response(self, base_path, *_):
        """Reply with a static Mesos state-summary response.

        Please refer to the description of the BaseHTTPRequestHandler class
        for details on the arguments and return value of this method.

        Raises:
            EndpointException: request URL path is unsupported
        """
        if base_path != '/master/state-summary':
            msg = "Path `{}` is not supported yet".format(base_path)
            blob = msg.encode('utf-8')
            raise EndpointException(code=500, reason=blob)

        ctx = self.server.context

        with ctx.lock:
            blob = self._convert_data_to_blob(ctx.data['endpoint-content'])

        return 200, 'application/json', blob
Example #9
0
    def _calculate_response(self, base_path, url_args, body_args=None):
        """Reply with empty list of apps for the '/v2/apps' request

        Please refer to the description of the BaseHTTPRequestHandler class
        for details on the arguments and return value of this method.

        Raises:
            EndpointException: request URL path is unsupported
        """
        if base_path in ['/v2/reflect/me', '/']:
            # A test URI that is used by tests. In some cases it is impossible
            # to reuse /v2/apps path.
            return self._reflect_request(base_path, url_args, body_args)

        if base_path not in ['/v2/apps', "/v2/leader"]:
            msg = "Path `{}` is not supported yet".format(base_path)
            blob = msg.encode('utf-8')
            raise EndpointException(code=500, reason=blob)

        ctx = self.server.context

        status = 200
        content_type = 'application/json'
        with ctx.lock:
            if base_path == '/v2/apps':
                blob = self._convert_data_to_blob(ctx.data['endpoint-content'])
            elif base_path == '/v2/leader':
                if ctx.data['leader-content'] is None:
                    msg = "Marathon leader unknown"
                    blob = msg.encode('utf-8')
                    content_type = 'text/plain; charset=utf-8'
                    status = 404
                elif isinstance(ctx.data['leader-content'], str):
                    blob = ctx.data['leader-content'].encode('utf-8')
                    content_type = 'text/plain; charset=utf-8'
                else:
                    blob = self._convert_data_to_blob(
                        ctx.data['leader-content'])

        return status, content_type, blob
Example #10
0
    def _calculate_response(self, base_path, url_args, body_args=None):
        """Reply with the currently set mock-reply for given IAM user query.

        Please refer to the description of the BaseHTTPRequestHandler class
        for details on the arguments and return value of this method.

        Raises:
            EndpointException: request URL path is unsupported
        """
        match = self.USERS_QUERY_REGEXP.search(base_path)
        if match:
            return self.__users_permissions_request_handler(match.group(1))

        if base_path == '/acs/api/v1/foo/bar':
            # A test URI that is used by tests. In some cases it is impossible
            # to reuse /acs/api/v1/users/ path.
            blob = self._convert_data_to_blob({})
            return 200, 'application/json', blob

        raise EndpointException(
            code=500,
            content="Path `{}` is not supported yet".format(base_path))
Example #11
0
    def _calculate_response(self, base_path, url_args, body_args=None):
        """Reply with the currently set mock-reply for given IAM user query.

        Please refer to the description of the BaseHTTPRequestHandler class
        for details on the arguments and return value of this method.

        Raises:
            EndpointException: request URL path is unsupported
        """
        match = self.USERS_QUERY_REGEXP.search(base_path)
        if match:
            return self.__users_permissions_request_handler(match.group(1))

        reflecting_paths = ['/acs/api/v1/reflect/me', '/acs/api/v1/uiconfig']
        if base_path.rstrip('/') in reflecting_paths or \
                base_path.startswith('/acs/api/v1/auth/'):
            # A test URI that is used by tests. In some cases it is impossible
            # to reuse /acs/api/v1/users/ path.
            return self._reflect_request(base_path, url_args, body_args)

        raise EndpointException(
            code=500,
            content="Path `{}` is not supported yet".format(base_path))