forked from RedHatInsights/insights-host-delete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
54 lines (38 loc) · 1.71 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import requests
import traceback
from utils import config, host_delete_logging
from mq import kafka_consumer
logger = host_delete_logging.initialize_logging()
def handle_message(parsed):
try:
if parsed["type"] is "delete":
send_request(parsed["insights_id"], parsed["account"])
except KeyError as e:
logger.exception("Missing Key in Message: %s", e)
def send_request(insights_id, account):
logger.debug("sending delete request to legacy")
URL = "{0}/{1}?account_number={2}".format(config.LEGACY_URL, insights_id, account)
if config.NAMESPACE is "platform-qa":
r = requests.delete(URL, auth=(config.LEGACY_USERNAME, config.LEGACY_PASSWORD),
headers={config.PROXY_TOKEN_HEADER: config.PROXY_TOKEN})
else:
r = requests.delete(URL, auth=(config.LEGACY_USERNAME, config.LEGACY_PASSWORD))
if r.status_code not in [200, 204]:
logger.error("Request failed with error: [%s] %s", r.status_code, r.text)
elif r.status_code is 404:
logger.debug("Request completed with error: [%s] %s", r.status_code, r.text)
def main():
logger = host_delete_logging.initialize_logging()
logger.info("Starting legacy host deletion service")
config.log_config()
if not (config.LEGACY_USERNAME and config.LEGACY_PASSWORD):
raise ValueError("Legacy Username and Password Required")
consumer = kafka_consumer.init_consumer()
for data in consumer:
handle_message(data.value)
if __name__ == "__main__":
try:
main()
except Exception:
the_error = traceback.format_exc()
logger.error(f"Insights Host Delete Service failed with error: {the_error}")