| tierno | f3da29d | 2019-05-31 13:40:26 +0000 | [diff] [blame] | 1 | #! /usr/bin/python3 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 13 | # implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | import sys |
| 18 | import requests |
| 19 | import yaml |
| 20 | from os import getenv |
| 21 | |
| 22 | __author__ = "Alfonso Tierno, alfonso.tiernosepulveda@telefonica.com" |
| 23 | __date__ = "$2019-05-31$" |
| 24 | __version__ = "0.1" |
| 25 | version_date = "May 2019" |
| 26 | |
| 27 | |
| 28 | def usage(): |
| 29 | print("Usage: ", sys.argv[0], "topic key message") |
| 30 | print(" Sends a kafka message using URL test of NBI") |
| 31 | print(" host is defined by env OSMNBI_HOST (localhost by default)") |
| 32 | print(" port is defined by env OSMNBI_PORT (9999 by default)") |
| 33 | return |
| 34 | |
| 35 | |
| 36 | if __name__ == "__main__": |
| 37 | try: |
| 38 | if "--help" in sys.argv: |
| 39 | usage() |
| 40 | exit(0) |
| 41 | |
| 42 | if len(sys.argv) != 4: |
| 43 | print("missing parameters. Type --help for more information", file=sys.stderr) |
| 44 | exit(1) |
| 45 | |
| 46 | topic, key, message = sys.argv[1:] |
| 47 | host = getenv("OSMNBI_HOST", "localhost") |
| 48 | port = getenv("OSMNBI_PORT", "9999") |
| 49 | url = "https://{host}:{port}/osm/test/message/{topic}".format(host=host, port=port, topic=topic) |
| 50 | print(url) |
| 51 | data = {key: message} |
| 52 | |
| 53 | r = requests.post(url, data=yaml.safe_dump(data), verify=False) |
| 54 | if r.status_code not in (200, 201, 202, 204): |
| 55 | print("Received code={}, content='{}'".format(r.status_code, r.text)) |
| 56 | exit(1) |
| 57 | print("{} -> {}: {}".format(topic, key, message)) |
| 58 | |
| 59 | except Exception: |
| 60 | raise |