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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
# This software is distributed under the terms and conditions of the 'Apache-2.0'
# license which can be found in the file 'LICENSE' in this package distribution
# or at 'http://www.apache.org/licenses/LICENSE-2.0'.
"""
Authz is the endpoint to get authorization response
"""
import logging
import json
import flask
from flask import request
from flask_restful import Resource
import requests
from python_moonutilities import exceptions
__version__ = "0.1.0"
LOGGER = logging.getLogger("moon.wrapper.api." + __name__)
class SlaveUpdate(Resource):
"""
Endpoint for authz requests
"""
__urls__ = (
"/update",
"/update/",
)
def __init__(self, **kwargs):
self.port = kwargs.get("port")
self.CACHE = kwargs.get("cache", {})
self.TIMEOUT = 5
def put(self):
LOGGER.warning("PUT {}".format(request.form))
response = flask.make_response("False")
try:
if self.update_slave():
response = flask.make_response("True")
except Exception as exception:
LOGGER.error(exception, exc_info=True)
response.headers['content-type'] = 'application/octet-stream'
return response
def get_interface_url(self, pdp_id):
LOGGER.debug("pdp_id {}".format(pdp_id))
for containers in self.CACHE.containers.values():
LOGGER.info("containers0 {}".format(containers))
for container in containers:
if container.get("pdp_id") == pdp_id:
if "pipeline" in container['name']:
yield "http://{}:{}".format(
container['name'],
container['port'])
self.CACHE.update()
# Note (asteroide): test an other time after the update
for containers in self.CACHE.containers.values():
LOGGER.info("containers1 {}".format(containers))
for container in containers:
if container.get("pdp_id") == pdp_id:
if "pipeline" in container['name']:
yield "http://{}:{}".format(
container['name'],
container['port'])
def update_slave(self):
result = {}
result_list = []
for _pdp_id in self.CACHE.pdp:
result[_pdp_id] = {}
for interface_url in self.get_interface_url(_pdp_id):
req = requests.put("{}/update".format(interface_url), request.form)
if req.status_code == 200:
if req.json().get("result", False):
result[_pdp_id][interface_url] = True
result_list.append(True)
continue
LOGGER.warning("Error in {} {}: {}".format(_pdp_id, interface_url, req.text))
result[_pdp_id][interface_url] = False
result_list.append(False)
return all(result_list)
|