blob: 68356541e4c4168c52e16c479c57fff26be9ecd2 [file] [log] [blame]
tiernof3da29d2019-05-31 13:40:26 +00001#! /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
17import sys
18import requests
19import yaml
20from os import getenv
21
22__author__ = "Alfonso Tierno, alfonso.tiernosepulveda@telefonica.com"
23__date__ = "$2019-05-31$"
24__version__ = "0.1"
25version_date = "May 2019"
26
27
28def 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
36if __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