def test_patch_works(self):
        headers = {
            'Content-Type': 'application/json',
            'If-Match': '27f88f9749259b53ccaf48331074fa54d092e1cc'
        }
        data = {'state': 'UP'}

        backend = Backend()

        with HTTMock(response_patch):
            resp = backend.method_patch('http://alignakbackend.local/livehost/55d113976376e9835e1b2feb', ujson.dumps(data), headers)

        self.assertEqual({"_updated": "Wed, 19 Aug 2015 07:59:51 GMT", "_links": {"self": {"href": "livehost/55d113976376e9835e1b2feb", "title": "Livehost"}}, "_created": "Sun, 16 Aug 2015 22:49:59 GMT", "_status": "OK", "_id": "55d113976376e9835e1b2feb", "_etag": "fff582e398e47bce29e7317f25eb5068aaac3c4a"}, resp)
    def test_patch_etag_notok_notok(self):
        """
        Test Patch method with _etag not ok + 1 retry _etag not ok

        :return: None
        """

        headers = {
            'Content-Type': 'application/json',
            'If-Match': '27f88f9749259b53ccaf48331074fa54d092e1cd'
        }
        data = {'state': 'UP'}

        backend = Backend()

        with HTTMock(response_patch_notok):
            resp = backend.method_patch('http://alignakbackend.local/livehost/55d113976376e9835e1b2feb', ujson.dumps(data), headers)

        self.assertEqual('{}', resp)
Example #3
0
    def send_to_backend(self, type_data, name, data):
        """
        Send data to alignak backend livehost or liveservice

        :param type_data: one of ['livehost', 'liveservice', 'loghost', 'logservice']
        :type type_data: str
        :param name: name of host or service
        :type name: str
        :param data: dictionary with data to add / update
        :type data: dict
        :return: True if send is ok, False otherwise
        :rtype: bool
        """
        backend = Backend()
        headers = {
            'Content-Type': 'application/json',
        }
        ret = True
        if type_data == 'livehost':
            headers['If-Match'] = self.ref_live['host'][self.mapping['host'][name]]['_etag']
            response = backend.method_patch(
                self.endpoint('livehost/%s'
                              % self.ref_live['host'][self.mapping['host'][name]]['_id']),
                ujson.dumps(data),
                headers=headers
            )
            if response['_status'] == 'ERR':
                logger.error(response['_issues'])
                ret = False
            else:
                self.ref_live['host'][self.mapping['host'][name]]['_etag'] = response['_etag']
        elif type_data == 'liveservice':
            headers['If-Match'] = self.ref_live['service'][self.mapping['service'][name]]['_etag']
            response = backend.method_patch(
                self.endpoint('liveservice/%s'
                              % self.ref_live['service'][self.mapping['service'][name]]['_id']),
                ujson.dumps(data),
                headers=headers
            )
            if response['_status'] == 'ERR':
                logger.error(response['_issues'])
                ret = False
            else:
                self.ref_live['service'][self.mapping['service'][name]]['_etag'] = response['_etag']
        elif type_data == 'loghost':
            data['host_name'] = self.mapping['host'][name]
            response = backend.method_post(
                self.endpoint('loghost'), ujson.dumps(data), headers=headers
            )
            if response['_status'] == 'ERR':
                logger.error(response['_issues'])
                ret = False
        elif type_data == 'logservice':
            data['service_description'] = self.mapping['service'][name]
            response = backend.method_post(
                self.endpoint('logservice'), ujson.dumps(data), headers=headers
            )
            if response['_status'] == 'ERR':
                logger.error(response['_issues'])
                ret = False
        return ret