#!/usr/bin/env python3 """Translate S3-owned state classifications without redefining their meaning.""" import argparse import json import sys import uuid from pathlib import Path from receiver import read_json, instant, STATES def translate(report, contract): if (set(report) != {'schema', 'cluster_uid', 'evaluated_at', 'signals', 'transport', 'guarantees', 'threshold_status', 'healthy'} or report['schema'] != 'railiance-platform.assurance-signal.v1' or report['cluster_uid'] != 'a553c742-0115-43d4-99a4-a5ca56fe0786' or report['transport'] != 'unmonitored' or report['guarantees'] != 'unsupported' or report['threshold_status'] != 'local-diagnostic-only' or set(report['signals']) != set(contract['signals'])): raise ValueError('unexpected platform report') instant(report['evaluated_at']) states = {} for name, value in report['signals'].items(): if (set(value) != {'state', 'owner'} or value['state'] not in STATES or value['owner'] not in ('railiance-platform', 'rapp-postgres')): raise ValueError('unexpected signal') states[name] = value['state'] if type(report['healthy']) is not bool or report['healthy'] != all(s == 'healthy' for s in states.values()): raise ValueError('inconsistent summary') return dict(schema='railiance-telemetry.signal.v1', id=str(uuid.uuid4()), stream=contract['stream'], producer=contract['producer'], observed_at=report['evaluated_at'], states=states) if __name__ == '__main__': p = argparse.ArgumentParser(description=__doc__) p.add_argument('--contract', required=True, type=Path) p.add_argument('report', type=Path) a = p.parse_args() try: print(json.dumps(translate(read_json(a.report), read_json(a.contract)))) except (OSError, ValueError, KeyError, TypeError, AttributeError): print(json.dumps({'status': 'rejected', 'error': 'invalid-platform-report'})) sys.exit(2)