Beispiel #1
0
    def firePost(token=None):
        '''
        Fire event(s)
        Each event is a dict of the form
        {
          tag: 'tagstring',
          data: {datadict},
        }
        Post body is either list of events or single event
        '''
        if not token:
            token = bottle.request.get_header('X-Auth-Token')
        if not token:
            bottle.abort(401, "Missing token.")

        client = salt.client.api.APIClient()

        if not client.verify_token(token): #auth.get_tok(token):
            bottle.abort(401, "Invalid token.")

        events = bottle.request.json
        if not events:
            bottle.abort(code=400, text='Missing event(s).')

        if hasattr(events, 'get'): #convert to list if not
            events = [events]

        results = [dict(tag=event['tag'],
                        result=client.fire_event(event['data'], event['tag']))
                   for event in events]

        bottle.response.set_header('Content-Type',  'application/json')
        return json.dumps(results)
Beispiel #2
0
    def firePost(token=None):
        '''
        Fire event(s)
        Each event is a dict of the form
        {
          tag: 'tagstring',
          data: {datadict},
        }
        Post body is either list of events or single event
        '''
        if not token:
            token = bottle.request.get_header('X-Auth-Token')
        if not token:
            bottle.abort(401, "Missing token.")

        client = salt.client.api.APIClient(opts)

        if not client.verify_token(token): #auth.get_tok(token):
            bottle.abort(401, "Invalid token.")

        events = bottle.request.json
        if not events:
            bottle.abort(code=400, text='Missing event(s).')

        if hasattr(events, 'get'): #convert to list if not
            events = [events]

        results = [dict(tag=event['tag'],
                        result=client.fire_event(event['data'], event['tag']))
                   for event in events]

        bottle.response.set_header('Content-Type',  'application/json')
        return json.dumps(results)
Beispiel #3
0
    def eventGet(token):
        '''
        Create server sent event stream from salt
        and authenticate with the given token
        Also optional query arg tag allows with
        filter events based on tag
        '''
        if not token:
            bottle.abort(401, "Missing token.")

        client = salt.client.api.APIClient()

        if not client.verify_token(token): #auth.get_tok(token):
            bottle.abort(401, "Invalid token.")

        tag = bottle.request.query.get('tag', '')

        bottle.response.set_header('Content-Type',  'text/event-stream') #text
        bottle.response.set_header('Cache-Control',  'no-cache')

        # Set client-side auto-reconnect timeout, ms.
        yield 'retry: 250\n\n'

        while True:
            data =  client.get_event(wait=0.025, tag=tag, full=True)
            if data:
                yield 'data: {0}\n\n'.format(json.dumps(data))
            else:
                sleep(0.1)
Beispiel #4
0
    def eventGet(token):
        '''
        Create server sent event stream from salt
        and authenticate with the given token
        Also optional query arg tag allows with
        filter events based on tag
        '''
        if not token:
            bottle.abort(401, "Missing token.")

        client = salt.client.api.APIClient(opts)

        if not client.verify_token(token): #auth.get_tok(token):
            bottle.abort(401, "Invalid token.")

        tag = bottle.request.query.get('tag', '')

        bottle.response.set_header('Content-Type',  'text/event-stream') #text
        bottle.response.set_header('Cache-Control',  'no-cache')

        # Set client-side auto-reconnect timeout, ms.
        yield 'retry: 250\n\n'

        while True:
            data =  client.get_event(wait=0.025, tag=tag, full=True)

            if data:
                try: #work around try to decode catch unicode errors
                    yield 'data: {0}\n\n'.format(json.dumps(data))
                except UnicodeDecodeError as ex:
                    logger.error("Error: Salt event has non UTF-8 data:\n{0}".format(data))

            else:
                sleep(0.1)