def test_status_publish_connected(self, client_mock): """ Verifies the message published on pubsub when an org is connected. Args: client_mock(Mock): mock of the pubsub client """ Org(id='test', connected_at=datetime(2010, 1, 2)).put() publish_status('test', 'connection_status', 'connected') publish_mock = client_mock.return_value.topic.return_value.publish publish_mock.assert_called_with( json.dumps({ "meta": { "version": "2.0.0", "data_source_id": "test", "timestamp": "2010-01-01T00:00:00" }, "data": [{ "type": "connection_status", "id": "test", "attributes": { "status": "connected", "connected_at": "2010-01-02T00:00:00" } }] }))
def mark_as_connected(org_uid, also_linked=False): """ Flags an org as connected. The org will get included in update cycles from this point. Args: org_uid(str): org identifier """ logging.info( "marking the org as connected (status value {})".format(CONNECTED)) org = Org.get_by_id(org_uid) org.status = CONNECTED if also_linked: org.linked_at = datetime.utcnow() org.connected_at = datetime.utcnow() org.put() if also_linked: publish_status(org_uid, LINK_STATUS_TYPE, LINK_STATUS_LINKED) publish_status(org_uid, CONNECT_STATUS_TYPE, CONNECT_STATUS_CONNECTED) if is_changeset_in_progress(org): logging.info( "publishing syncing changeset status for changeset {}:{}".format( org_uid, org.changeset)) publish_changeset_status(org_uid, org.changeset, CHANGESET_STATUS_SYNCING)
def mark_as_disconnected(org_uid, deactivate_update_cycle): """ Flags an org as disconnected by changing its status to DISCONNECTED and completing current changeset. This is useful if the sync gives up because of authentication issues with the provider for example. This does not forcibly disconnect the org by deleting the auth keys. Publishes an error status for changeset currently being ingested. Args: org_uid(str): org identifier deactivate_update_cycle(bool): indicates if the update_cycle_active flag should be set to false """ logging.info("marking the org as disconnected (status value {})".format( DISCONNECTED)) org = Org.get_by_id(org_uid) org.status = DISCONNECTED if deactivate_update_cycle: org.update_cycle_active = False org.put() publish_status(org_uid, CONNECT_STATUS_TYPE, CONNECT_STATUS_DISCONNECTED) if is_changeset_in_progress(org): logging.info( "publishing error changeset status for changeset {}:{}".format( org_uid, org.changeset)) publish_changeset_status(org_uid, org.changeset, CHANGESET_STATUS_ERROR)
def _abort_link(org_uid): """ Aborts the in process link if an error occurs, disconnecting the org in the process. Args: org_uid (str): The org ID """ publish_status(org_uid, LINK_STATUS_TYPE, LINK_STATUS_UNLINKED) mark_as_disconnected(org_uid=org_uid, deactivate_update_cycle=False)
def perform_disconnect(org_uid): logging.info("disconnecting the org explicitly") org = Org.get_by_id(org_uid) if not org: logging.info("org {} not found".format(org_uid)) raise NotFoundException("org {} not found".format(org_uid)) publish_status(org_uid, LINK_STATUS_TYPE, LINK_STATUS_UNLINKED) mark_as_disconnected(org_uid=org_uid, deactivate_update_cycle=False)
def create_manual_provider_org(org_uid, provider): """ Creates the Org entry for a non-API based provider. Treates the org linked and connected. Args: org_uid(str): org identifier provider(str): data provider (eg. 'uploader') """ now = datetime.utcnow() Org(id=org_uid, provider=provider, status=CONNECTED, linked_at=now, connected_at=now).put() publish_status(org_uid, LINK_STATUS_TYPE, LINK_STATUS_LINKED) publish_status(org_uid, CONNECT_STATUS_TYPE, CONNECT_STATUS_CONNECTED)