Example #1
0
    def handle_messages(self, messages, env):
        retries = int(self.config_get("retries"))
        interval = int(self.config_get("interval"))
        max_wait = int(self.config_get("max_wait"))
        failures_before_reauth = int(self.config_get("failures_before_reauth"))
        connection = HttpConnection(self)
        results = env.setdefault('cufpub.results', dict())

        for payload in self.iterate_payloads(messages, env):
            msgid = payload["message_id"]
            try:
                deployment_info_string = yagi.config.get(
                    'event_feed', 'atom_categories')
                deployment_info = self.get_deployment_info(
                    deployment_info_string)
                payload_body = ""
                if "instance.exists" in payload['event_type']:
                    service = 'nova'
                    payload_body = self.nova_cuf(deployment_info, payload)
                elif "image.exists" in payload['event_type']:
                    service = 'glance'
                    payload_body = self.glance_cuf(deployment_info, payload)
                elif "ip.exists" in payload['event_type']:
                    service = 'neutron'
                    payload_body = self.neutron_pub_ipv4_cuf(
                        deployment_info, payload)
            except KeyError, e:
                error_msg = "Malformed Notification: %s" % payload
                LOG.error(error_msg)
                LOG.exception(e)
                results[msgid] = dict(error=True,
                                      code=0,
                                      message=error_msg,
                                      service=service)
                continue

            endpoint = self.config_get("url")
            tries = 0
            failures = 0
            code = 0
            ah_event_id = None
            while True:
                try:
                    response_details = connection.send_notification(
                        endpoint, endpoint, payload_body)
                    code = response_details.get("status")
                    ah_event_id = response_details.get("ah_event_id")
                    break
                except InvalidContentException, e:
                    LOG.exception(e)
                    LOG.error(payload_body)
                    results[msgid] = dict(error=False,
                                          code=code,
                                          message=e.msg)
                    break
                except UnauthorizedException, e:
                    LOG.exception(e)
                    connection = None
                    code = 401
                    error_msg = "Unauthorized"
Example #2
0
 def _send_notification(self, env, notification_payload, payload):
     retries = int(self.config_get("retries"))
     interval = int(self.config_get("interval"))
     max_wait = int(self.config_get("max_wait"))
     failures_before_reauth = int(self.config_get("failures_before_reauth"))
     endpoint = self.config_get("url")
     ah_event_id = ""
     tries = 0
     failures = 0
     code = 0
     error_msg = ''
     connection = HttpConnection(self)
     while True:
         try:
             response_details = connection.send_notification(
                 endpoint, endpoint, notification_payload)
             code = response_details.get("status")
             ah_event_id = response_details.get("ah_event_id")
             error = False
             msg = ''
             break
         except UnauthorizedException, e:
             LOG.exception(e)
             conn = None
             code = 401
             error_msg = "Unauthorized"
         except MessageDeliveryFailed, e:
             LOG.exception(e)
             code = e.code
             error_msg = e.msg
Example #3
0
 def _send_notification(self, env, notification_payload, payload):
     retries = int(self.config_get("retries"))
     interval = int(self.config_get("interval"))
     max_wait = int(self.config_get("max_wait"))
     failures_before_reauth = int(self.config_get("failures_before_reauth"))
     endpoint = self.config_get("url")
     ah_event_id = ""
     tries = 0
     failures = 0
     code = 0
     error_msg = ''
     connection = HttpConnection(self)
     while True:
         try:
             response_details = connection.send_notification(
                 endpoint, endpoint, notification_payload)
             code = response_details.get("status")
             ah_event_id = response_details.get("ah_event_id")
             error = False
             msg = ''
             break
         except UnauthorizedException, e:
             LOG.exception(e)
             conn = None
             code = 401
             error_msg = "Unauthorized"
         except MessageDeliveryFailed, e:
             LOG.exception(e)
             code = e.code
             error_msg = e.msg
Example #4
0
    def handle_messages(self, messages,env):
        retries = int(self.config_get("retries"))
        interval = int(self.config_get("interval"))
        max_wait = int(self.config_get("max_wait"))
        failures_before_reauth = int(self.config_get("failures_before_reauth"))
        connection = HttpConnection(self)
        results = env.setdefault('cufpub.results', dict())

        for payload in self.iterate_payloads(messages, env):
            msgid = payload["message_id"]
            try:
                deployment_info_string = yagi.config.get('event_feed',
                                                  'atom_categories')
                deployment_info = self.get_deployment_info(
                    deployment_info_string)
                payload_body = ""
                if "instance.exists" in payload['event_type']:
                    service = 'nova'
                    payload_body = self.nova_cuf(deployment_info, payload)
                elif "image.exists" in payload['event_type']:
                    service = 'glance'
                    payload_body = self.glance_cuf(deployment_info, payload)
                elif "ip.exists" in payload['event_type']:
                    service = 'neutron'
                    payload_body = self.neutron_pub_ipv4_cuf(deployment_info, payload)
            except KeyError, e:
                error_msg = "Malformed Notification: %s" % payload
                LOG.error(error_msg)
                LOG.exception(e)
                results[msgid] = dict(error=True, code=0, message=error_msg,
                                      service=service)
                continue

            endpoint = self.config_get("url")
            tries = 0
            failures = 0
            code = 0
            ah_event_id = None
            while True:
                try:
                    response_details = connection.send_notification(endpoint, endpoint,
                                                         payload_body)
                    code = response_details.get("status")
                    ah_event_id = response_details.get("ah_event_id")
                    break
                except InvalidContentException, e:
                    LOG.exception(e)
                    LOG.error(payload_body)
                    results[msgid] = dict(error=False, code=code, message=e.msg)
                    break
                except UnauthorizedException, e:
                    LOG.exception(e)
                    connection = None
                    code = 401
                    error_msg = "Unauthorized"
Example #5
0
    def test_send_notification_successfully(self):
        self.called = False

        def mock_request(*args, **kwargs):
            self.called = True
            return MockResponse(201), None

        handler = CufPub()
        http_conn = HttpConnection(handler)
        endpoint = handler.config_get("url")
        payload_body = {"a": "b"}
        self.stubs.Set(httplib2.Http, 'request', mock_request)
        http_conn.send_notification(endpoint, endpoint, payload_body)
Example #6
0
    def test_send_notification_successfully(self):
        self.called = False

        def mock_request(*args, **kwargs):
            self.called = True
            return MockResponse(201), None

        handler = CufPub()
        http_conn = HttpConnection(handler)
        endpoint = handler.config_get("url")
        payload_body = {"a":"b"}
        self.stubs.Set(httplib2.Http, 'request', mock_request)
        http_conn.send_notification(endpoint, endpoint, payload_body)
Example #7
0
    def test_duplicate_entry_to_atomhopper_response_409(self):
        self.called = False

        def mock_request(*args, **kwargs):
            self.called = True
            return MockResponse(409), None

        handler = CufPub()
        http_conn = HttpConnection(handler)
        endpoint = handler.config_get("url")
        payload_body = {"a":"b"}
        self.stubs.Set(httplib2.Http, 'request', mock_request)

        status = http_conn.send_notification(endpoint, endpoint, payload_body)
        self.assertEqual(status,409)
Example #8
0
    def test_duplicate_entry_to_atomhopper_response_409(self):
        self.called = False

        def mock_request(*args, **kwargs):
            self.called = True
            return MockResponse(409), None

        handler = CufPub()
        http_conn = HttpConnection(handler)
        endpoint = handler.config_get("url")
        payload_body = {"a": "b"}
        self.stubs.Set(httplib2.Http, 'request', mock_request)

        status = http_conn.send_notification(endpoint, endpoint, payload_body)
        self.assertEqual(status, 409)
Example #9
0
    def test_response_too_large_error_and_status_is_201_does_not_raise_exception(self):
        self.called = False

        def mock_request(*args, **kwargs):
            self.called = True
            raise http_util.ResponseTooLargeError("desc",MockResponse(201),
                                                  "content")

        handler = CufPub()
        http_conn = HttpConnection(handler)
        endpoint = handler.config_get("url")
        payload_body = {"a":"b"}
        self.stubs.Set(httplib2.Http, 'request', mock_request)

        http_conn.send_notification(endpoint, endpoint, payload_body)
Example #10
0
    def test_response_too_large_error_and_status_is_201_does_not_raise_exception(
            self):
        self.called = False

        def mock_request(*args, **kwargs):
            self.called = True
            raise http_util.ResponseTooLargeError("desc", MockResponse(201),
                                                  "content")

        handler = CufPub()
        http_conn = HttpConnection(handler)
        endpoint = handler.config_get("url")
        payload_body = {"a": "b"}
        self.stubs.Set(httplib2.Http, 'request', mock_request)

        http_conn.send_notification(endpoint, endpoint, payload_body)
Example #11
0
    def handle_messages(self, messages, env):
        retries = int(self.config_get("retries"))
        interval = int(self.config_get("interval"))
        max_wait = int(self.config_get("max_wait"))
        entity_links = self.config_get("generate_entity_links") == "True"
        failures_before_reauth = int(self.config_get("failures_before_reauth"))
        is_stacktach_down = self.config_get("stacktach_down")
        connection = HttpConnection(self)

        for payload in self.iterate_payloads(messages, env):
            try:
                event_type = self._get_event_type(is_stacktach_down, payload)
                entity = dict(content=payload,
                              id=payload["message_id"],
                              event_type=event_type)
                payload_body = yagi.serializer.atom.dump_item(
                    entity, entity_links=entity_links)
            except KeyError, e:
                error_msg = "Malformed Notification: %s" % payload
                LOG.error(error_msg)
                LOG.exception(e)
                self.note_result(env, payload, error=True, message=error_msg)
                continue

            endpoint = self.config_get("url")
            tries = 0
            failures = 0
            code = 0
            error_msg = ''

            while True:
                try:
                    code = connection.send_notification(
                        endpoint, endpoint, payload_body)
                    error = False
                    msg = ''
                    break
                except UnauthorizedException, e:
                    LOG.exception(e)
                    conn = None
                    code = 401
                    error_msg = "Unauthorized"
                except MessageDeliveryFailed, e:
                    LOG.exception(e)
                    code = e.code
                    error_msg = e.msg
Example #12
0
    def handle_messages(self, messages, env):
        retries = int(self.config_get("retries"))
        interval = int(self.config_get("interval"))
        max_wait = int(self.config_get("max_wait"))
        entity_links = self.config_get("generate_entity_links") == "True"
        failures_before_reauth = int(self.config_get("failures_before_reauth"))
        is_stacktach_down = self.config_get("stacktach_down")
        connection = HttpConnection(self)

        for payload in self.iterate_payloads(messages, env):
            try:
                event_type = self._get_event_type(is_stacktach_down, payload)
                entity = dict(content=payload,
                              id=payload["message_id"],
                              event_type=event_type)
                payload_body = yagi.serializer.atom.dump_item(entity,
                    entity_links=entity_links)
            except KeyError, e:
                error_msg = "Malformed Notification: %s" % payload
                LOG.error(error_msg)
                LOG.exception(e)
                self.note_result(env, payload, error=True, message=error_msg)
                continue

            endpoint = self.config_get("url")
            tries = 0
            failures = 0
            code = 0
            error_msg = ''

            while True:
                try:
                    code = connection.send_notification(endpoint, endpoint,
                                                        payload_body)
                    error = False
                    msg = ''
                    break
                except UnauthorizedException, e:
                    LOG.exception(e)
                    conn = None
                    code = 401
                    error_msg = "Unauthorized"
                except MessageDeliveryFailed, e:
                    LOG.exception(e)
                    code = e.code
                    error_msg = e.msg
Example #13
0
    def handle_messages(self, messages, env):
        retries = int(self.config_get("retries"))
        interval = int(self.config_get("interval"))
        max_wait = int(self.config_get("max_wait"))
        failures_before_reauth = int(self.config_get("failures_before_reauth"))
        connection = HttpConnection(self)
        results = env.setdefault('cufpub.results', dict())

        for payload in self.iterate_payloads(messages, env):
            msgid = payload["message_id"]
            try:
                deployment_info_string = yagi.config.get(
                    'event_feed', 'atom_categories')
                deployment_info = self.get_deployment_info(
                    deployment_info_string)
                payload_body = ""
                if "instance" in payload['event_type']:
                    payload_body = self.nova_cuf(deployment_info, payload)
                elif "image" in payload['event_type']:
                    payload_body = self.glance_cuf(deployment_info, payload)
            except KeyError, e:
                error_msg = "Malformed Notification: %s" % payload
                LOG.error(error_msg)
                LOG.exception(e)
                results[msgid] = dict(error=True, code=0, message=error_msg)
                continue

            endpoint = self.config_get("url")
            tries = 0
            failures = 0
            code = 0
            while True:
                try:
                    code = connection.send_notification(
                        endpoint, endpoint, payload_body)
                    break
                except UnauthorizedException, e:
                    LOG.exception(e)
                    connection = None
                    code = 401
                    error_msg = "Unauthorized"
                except MessageDeliveryFailed, e:
                    LOG.exception(e)
                    code = e.code
                    error_msg = e.msg
Example #14
0
    def handle_messages(self, messages,env):
        retries = int(self.config_get("retries"))
        interval = int(self.config_get("interval"))
        max_wait = int(self.config_get("max_wait"))
        failures_before_reauth = int(self.config_get("failures_before_reauth"))
        connection = HttpConnection(self)
        results = env.setdefault('cufpub.results', dict())

        for payload in self.iterate_payloads(messages, env):
            msgid = payload["message_id"]
            try:
                deployment_info_string = yagi.config.get('event_feed',
                                                  'atom_categories')
                deployment_info = self.get_deployment_info(
                    deployment_info_string)
                payload_body = ""
                if "instance" in payload['event_type']:
                    payload_body = self.nova_cuf(deployment_info, payload)
                elif "image" in payload['event_type']:
                    payload_body = self.glance_cuf(deployment_info, payload)
            except KeyError, e:
                error_msg = "Malformed Notification: %s" % payload
                LOG.error(error_msg)
                LOG.exception(e)
                results[msgid] = dict(error=True, code=0, message=error_msg)
                continue

            endpoint = self.config_get("url")
            tries = 0
            failures = 0
            code = 0
            while True:
                try:
                    code = connection.send_notification(endpoint, endpoint,
                                                         payload_body)
                    break
                except UnauthorizedException, e:
                    LOG.exception(e)
                    connection = None
                    code = 401
                    error_msg = "Unauthorized"
                except MessageDeliveryFailed, e:
                    LOG.exception(e)
                    code = e.code
                    error_msg = e.msg
Example #15
0
    def test_send_notification_successfully(self):
        self.called = False
        content = ("""<atom:entry xmlns:atom="http://www.w3.org/2005/Atom">"""
        """<atom:id>urn:uuid:95347e4d-4737-4438-b774-6a9219d78d2a</atom:id>"""
        """</atom:entry>""")

        def mock_request(*args, **kwargs):
            self.called = True
            return MockResponse(201), content

        handler = CufPub()
        http_conn = HttpConnection(handler)
        endpoint = handler.config_get("url")
        payload_body = {"a":"b"}
        self.mox.ReplayAll()

        self.stubs.Set(httplib2.Http, 'request', mock_request)
        http_conn.send_notification(endpoint, endpoint, payload_body)
        self.mox.VerifyAll()
Example #16
0
    def test_send_notification_successfully(self):
        self.called = False
        content = (
            """<atom:entry xmlns:atom="http://www.w3.org/2005/Atom">"""
            """<atom:id>urn:uuid:95347e4d-4737-4438-b774-6a9219d78d2a</atom:id>"""
            """</atom:entry>""")

        def mock_request(*args, **kwargs):
            self.called = True
            return MockResponse(201), content

        handler = CufPub()
        http_conn = HttpConnection(handler)
        endpoint = handler.config_get("url")
        payload_body = {"a": "b"}
        self.mox.ReplayAll()

        self.stubs.Set(httplib2.Http, 'request', mock_request)
        http_conn.send_notification(endpoint, endpoint, payload_body)
        self.mox.VerifyAll()
Example #17
0
    def test_response_401_raises_unauthorized_exception(self):
        self.called = False

        def mock_request(*args, **kwargs):
            self.called = True
            return MockResponse(401), None

        handler = CufPub()
        http_conn = HttpConnection(handler)
        endpoint = handler.config_get("url")
        payload_body = {"a": "b"}
        self.stubs.Set(httplib2.Http, 'request', mock_request)

        self.assertRaises(UnauthorizedException, http_conn.send_notification,
                          endpoint, endpoint, payload_body)
Example #18
0
    def test_response_other_than_201_raises_exception(self):
        self.called = False

        def mock_request(*args, **kwargs):
            self.called = True
            return MockResponse(400), None

        handler = CufPub()
        http_conn = HttpConnection(handler)
        endpoint = handler.config_get("url")
        payload_body = {"a": "b"}
        self.stubs.Set(httplib2.Http, 'request', mock_request)

        self.assertRaises(MessageDeliveryFailed, http_conn.send_notification,
                          endpoint, endpoint, payload_body)
Example #19
0
                    LOG.exception(e)

                #If we got here, something failed.
                stats.increment_stat(yagi.stats.failure_message())
                # Number of overall tries
                tries += 1
                # Number of tries between re-auth attempts
                failures += 1

                # Used primarily for testing, but it's possible we don't
                # care if we lose messages?
                if retries > 0:
                    if tries >= retries:
                        msg = "Exceeded retry limit. Error %s" % error_msg
                        self.note_result(env, payload, code=code, message=msg)
                        break
                wait = min(tries * interval, max_wait)
                LOG.error("Message delivery failed, going to sleep, will "
                          "try again in %s seconds" % str(wait))
                time.sleep(wait)

                if failures >= failures_before_reauth:
                    # Don't always try to reconnect, give it a few
                    # tries first
                    failures = 0
                    connection = None
                if connection is None:
                    connection = HttpConnection(self, force=True)

            self.note_result(env, payload, code=code)