Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 1 | import functools |
| 2 | from xosapi.xos_grpc_client import SecureClient, InsecureClient |
| 3 | from twisted.internet import reactor, defer |
| 4 | from tosca.resources import RESOURCES |
| 5 | from xosconfig import Config |
| 6 | |
| 7 | LOCAL_CERT = '/Users/teone/Sites/opencord/orchestration/xos-tosca/local_certs.crt' |
| 8 | |
| 9 | class GRPC_Client: |
| 10 | def __init__(self): |
| 11 | self.client = None |
| 12 | |
| 13 | self.grpc_secure_endpoint = Config.get('grpc.secure_endpoint') |
| 14 | self.grpc_insecure_endpoint = Config.get('grpc.insecure_endpoint') |
| 15 | self.username = Config.get('grpc.admin_username') |
| 16 | self.password = Config.get('grpc.admin_password') |
| 17 | |
| 18 | def setup_resources(self, client): |
| 19 | print "[XOS-TOSCA] Loading resources" |
| 20 | for k in client.xos_orm.all_model_names: |
| 21 | RESOURCES[k] = getattr(client.xos_orm, k) |
| 22 | |
| 23 | def start(self): |
| 24 | print "[XOS-TOSCA] Connecting to xos-core" |
| 25 | |
| 26 | deferred = defer.Deferred() |
| 27 | |
| 28 | if self.client: |
| 29 | self.client.stop() |
| 30 | self.client.session_change = True |
| 31 | |
| 32 | if self.username and self.password: |
| 33 | # NOTE if we authenticate users given the parameters in the rest request, do we need this? |
| 34 | self.client = SecureClient(endpoint=self.grpc_secure_endpoint, username=self.username, password=self.password, cacert=LOCAL_CERT) |
| 35 | else: |
| 36 | self.client = InsecureClient(endpoint=self.grpc_insecure_endpoint) |
| 37 | |
| 38 | self.client.set_reconnect_callback(functools.partial(self.setup_resources, self.client)) |
| 39 | self.client.start() |
| 40 | |
| 41 | # TODO can we call this once the client is setted up? |
| 42 | reactor.callLater(12, deferred.callback, self.client) |
| 43 | return deferred |