def download_file(self, url):
        if self.session.cookies:
            self.session.auth = None
        headers = {'content-type': 'application/force-download'}
        r = self.session.get(url, headers=headers)
        if r.status_code != requests.codes.ok:
            response = utils.safe_json_load(r.content)
            raise ib_ex.InfobloxFileDownloadFailed(response=response, url=url)

        return r
    def create_object(self, obj_type, payload, return_fields=None):
        """Create an Infoblox object of type 'obj_type'

        Args:
            obj_type        (str): Infoblox object type,
                                  e.g. 'network', 'range', etc.
            payload       (dict): Payload with data to send
            return_fields (list): List of fields to be returned
        Returns:
            The object reference of the newly create object
        Raises:
            InfobloxException
        """
        self._validate_obj_type_or_die(obj_type)

        query_params = self._build_query_params(return_fields=return_fields)

        url = self._construct_url(obj_type, query_params)
        opts = self._get_request_options(data=payload)
        self._log_request('post', url, opts)
        if (self.session.cookies):
            # the first 'get' or 'post' action will generate a cookie
            # after that, we don't need to re-authenticate
            self.session.auth = None
        r = self.session.post(url, **opts)

        self._validate_authorized(r)

        if r.status_code != requests.codes.CREATED:
            response = utils.safe_json_load(r.content)
            already_assigned = 'is assigned to another network view'
            if response and already_assigned in response.get('text'):
                exception = ib_ex.InfobloxMemberAlreadyAssigned
            else:
                exception = ib_ex.InfobloxCannotCreateObject
            raise exception(response=response,
                            obj_type=obj_type,
                            content=r.content,
                            args=payload,
                            code=r.status_code)

        return self._parse_reply(r)
    def create_object(self, obj_type, payload, return_fields=None):
        """Create an Infoblox object of type 'obj_type'

        Args:
            obj_type        (str): Infoblox object type,
                                  e.g. 'network', 'range', etc.
            payload       (dict): Payload with data to send
            return_fields (list): List of fields to be returned
        Returns:
            The object reference of the newly create object
        Raises:
            InfobloxException
        """
        self._validate_obj_type_or_die(obj_type)

        query_params = self._build_query_params(return_fields=return_fields)

        url = self._construct_url(obj_type, query_params)
        opts = self._get_request_options(data=payload)
        self._log_request('post', url, opts)
        r = self.session.post(url, **opts)

        self._validate_authorized(r)

        if r.status_code != requests.codes.CREATED:
            response = utils.safe_json_load(r.content)
            already_assigned = 'is assigned to another network view'
            if response and already_assigned in response.get('text'):
                exception = ib_ex.InfobloxMemberAlreadyAssigned
            else:
                exception = ib_ex.InfobloxCannotCreateObject
            raise exception(
                response=response,
                obj_type=obj_type,
                content=r.content,
                args=payload,
                code=r.status_code)

        return self._parse_reply(r)
    def upload_file(self, url, files):
        """Upload file to fully-qualified upload url

        Args:
            url (str): upload url provided by infoblox fileop uploadinit
            files (dict): file contents payload
        Returns:
            The requests response
        Raises:
            InfobloxException
        """
        if self.session.cookies:
            self.session.auth = None
        r = self.session.post(url, files=files)
        if r.status_code != requests.codes.ok:
            response = utils.safe_json_load(r.content)
            raise ib_ex.InfobloxFileUploadFailed(
                response=response,
                url=url,
                content=response,
                code=r.status_code,
            )

        return r
Beispiel #5
0
 def test_safe_json_load(self):
     data = '{"array":[1,2,3]}'
     expected_data = {'array': [1, 2, 3]}
     self.assertEqual(expected_data, utils.safe_json_load(data))
Beispiel #6
0
 def test_safe_json_load_no_exception(self):
     data = 'Some regular not json text'
     self.assertEqual(None, utils.safe_json_load(data))