Exemplo n.º 1
0
Arquivo: v1_0.py Projeto: cchheda1/PAN
    def __init__(self,
                 api_version=None,
                 panrc_tag=None,
                 hostname=None,
                 api_key=None,
                 timeout=None,
                 verify_cert=True,
                 sleeper=None):
        self._log = logging.getLogger(__name__).log
        self.api_version = api_version
        self.panrc_tag = panrc_tag
        self.hostname = hostname
        self.api_key = api_key
        self.timeout = timeout
        self.verify_cert = verify_cert
        self.sleeper = _Sleeper if sleeper is None else sleeper

        self._log(DEBUG3, 'Python version: %s', sys.version)
        self._log(DEBUG3, 'ssl: %s', ssl.OPENSSL_VERSION)
        self._log(DEBUG3, 'pan-python version: %s', __version__)

        init_panrc = {}  # .panrc args from constructor
        if hostname is not None:
            init_panrc['hostname'] = hostname
        if api_key is not None:
            init_panrc['api_key'] = api_key

        try:
            panrc = pan.rc.PanRc(tag=self.panrc_tag, init_panrc=init_panrc)
        except pan.rc.PanRcError as e:
            raise PanAFapiError(e)

        if 'api_key' in panrc.panrc:
            self.api_key = panrc.panrc['api_key']
        if 'hostname' in panrc.panrc:
            self.hostname = panrc.panrc['hostname']
        else:
            self.hostname = _cloud_server

        if self.api_key is None:
            raise PanAFapiError('api_key required')

        self.uri = 'https://' + self.hostname
        self.base_uri = self.uri + '/api/' + str(api_version)
        self.headers = {'content-type': 'application/json'}

        try:
            self.http = pan.http.PanHttp(timeout=self.timeout,
                                         verify_cert=self.verify_cert)
        except pan.http.PanHttpError as e:
            raise PanAFapiError(e)

        if self.http.using_requests:
            s = 'using requests %s' % self.http.requests_version
        else:
            s = 'using urllib'
        self._log(DEBUG2, s)
Exemplo n.º 2
0
Arquivo: v1_0.py Projeto: cchheda1/PAN
 def _set_apikey(self, data):
     try:
         obj = json.loads(data)
         obj['apiKey'] = self.api_key
         return json.dumps(obj)
     except ValueError as e:
         raise PanAFapiError(str(e))
Exemplo n.º 3
0
    def _search_results(self, data, search, results, terminal):
        r = search(data=data)
        r.raise_for_status()

        if not terminal:
            yield r

        obj = r.json
        if obj is None:
            raise PanAFapiError('Response not JSON')

        af_cookie = obj.get('af_cookie')
        if af_cookie is None:
            raise PanAFapiError('No af_cookie in response')

        sleeper = self.sleeper(obj)

        while True:
            r = results(af_cookie=af_cookie)
            r.raise_for_status()

            if not terminal:
                yield r

            obj = r.json
            if obj is None:
                raise PanAFapiError('Response not JSON')

            in_progress = obj.get('af_in_progress')
            if in_progress is None:
                raise PanAFapiError('No af_in_progress in response')
            if not in_progress:
                if terminal:
                    yield r
                try:
                    self._log(DEBUG1, 'ZZZ total %.2f', float(sleeper))
                except AttributeError:
                    pass
                break

            x = sleeper.sleep(obj)
            self._log(DEBUG1, 'ZZZ %.2f', x)
Exemplo n.º 4
0
Arquivo: v1_0.py Projeto: cchheda1/PAN
    def raise_for_status(self):
        if self.http_code is None:
            return None

        if not (200 <= self.http_code < 300):
            e = 'HTTP Error %s' % self.http_code
            if self.http_reason is not None:
                e += ': %s' % self.http_reason
            if self.json is not None and 'message' in self.json:
                e += ' ' + self.json['message']
            raise PanAFapiError(e)

        return None
Exemplo n.º 5
0
Arquivo: v1_0.py Projeto: cchheda1/PAN
    def _api_request(self, url, headers, data=None, params=None):
        self._log(DEBUG1, url)
        if params is not None:
            self._log(DEBUG1, params)
        self._log(DEBUG1, data)

        data = '{}' if data is None else data
        data = self._set_apikey(data)

        try:
            self.http.http_request(url=url,
                                   headers=self.headers,
                                   data=data,
                                   params=params)
        except pan.http.PanHttpError as e:
            raise PanAFapiError(str(e))

        r = PanAFapiRequest(inspect.stack()[1][3])
        self._set_attributes(r)
        return r