|
| 1 | +# Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). |
| 4 | +# You may not use this file except in compliance with the License. |
| 5 | +# A copy of the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0 |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is distributed |
| 10 | +# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +# express or implied. See the License for the specific language governing |
| 12 | +# permissions and limitations under the License. |
| 13 | + |
| 14 | +from awscrt.http import HttpClientConnection, HttpRequest |
| 15 | +from awscrt import io |
| 16 | +from awscrt.io import ClientBootstrap, ClientTlsContext, TlsConnectionOptions, SocketOptions |
| 17 | +import awsiot |
| 18 | +from concurrent.futures import Future |
| 19 | +import json |
| 20 | + |
| 21 | +class DiscoveryClient(object): |
| 22 | + __slots__ = ['_bootstrap', '_tls_context', '_socket_options', '_region', '_tls_connection_options', '_gg_server_name', 'gg_url', 'port'] |
| 23 | + |
| 24 | + def __init__(self, bootstrap, socket_options, tls_context, region): |
| 25 | + assert bootstrap is not None and isinstance(bootstrap, ClientBootstrap) |
| 26 | + assert socket_options is not None and isinstance(socket_options, SocketOptions) |
| 27 | + assert tls_context is not None and isinstance(tls_context, ClientTlsContext) |
| 28 | + assert region is not None and isinstance(region, str) |
| 29 | + |
| 30 | + self._bootstrap = bootstrap |
| 31 | + self._socket_options = socket_options |
| 32 | + self._region = region |
| 33 | + self._gg_server_name = 'greengrass-ats.iot.{}.amazonaws.com'.format(region) |
| 34 | + self._tls_connection_options = tls_context.new_connection_options() |
| 35 | + self._tls_connection_options.set_server_name(self._gg_server_name) |
| 36 | + self.port = 8443 |
| 37 | + |
| 38 | + if io.is_alpn_available(): |
| 39 | + self._tls_connection_options.set_alpn_list('x-amzn-http-ca') |
| 40 | + self.port = 443 |
| 41 | + |
| 42 | + def discover(self, thing_name): |
| 43 | + ret_future = Future() |
| 44 | + response_body = bytearray() |
| 45 | + request = None |
| 46 | + |
| 47 | + def on_incoming_body(response_chunk): |
| 48 | + response_body.extend(response_chunk) |
| 49 | + |
| 50 | + def on_request_complete(completion_future): |
| 51 | + global request |
| 52 | + try: |
| 53 | + response_code = request.response_code |
| 54 | + # marking request as global prevents the GC from reclaiming it, |
| 55 | + # so force it to do it here. |
| 56 | + request = None |
| 57 | + if response_code == 200: |
| 58 | + payload_str = response_body.decode('utf-8') |
| 59 | + discover_res = DiscoverResponse.from_payload(json.loads(payload_str)) |
| 60 | + ret_future.set_result(discover_res) |
| 61 | + else: |
| 62 | + ret_future.set_exception(DiscoveryException('Error during discover call: response code ={}'.format(response_code), response_code)) |
| 63 | + |
| 64 | + except Exception as e: |
| 65 | + ret_future.set_exception(e) |
| 66 | + |
| 67 | + def on_connection_completed(conn_future): |
| 68 | + global request |
| 69 | + try: |
| 70 | + connection = conn_future.result() |
| 71 | + request = connection.make_request( |
| 72 | + method='GET', |
| 73 | + uri_str='/greengrass/discover/thing/{}'.format(thing_name), |
| 74 | + outgoing_headers={'host':self._gg_server_name}, |
| 75 | + on_outgoing_body=None, |
| 76 | + on_incoming_body=on_incoming_body) |
| 77 | + |
| 78 | + request.response_completed.add_done_callback(on_request_complete) |
| 79 | + |
| 80 | + except Exception as e: |
| 81 | + # marking request as global prevents the GC from reclaiming it, |
| 82 | + # so force it to do it here. |
| 83 | + request = None |
| 84 | + ret_future.set_exception(e) |
| 85 | + |
| 86 | + connect_future = HttpClientConnection.new_connection(self._bootstrap, self._gg_server_name, self.port, self._socket_options, None, self._tls_connection_options) |
| 87 | + connect_future.add_done_callback(on_connection_completed) |
| 88 | + |
| 89 | + return ret_future |
| 90 | + |
| 91 | +class DiscoveryException(Exception): |
| 92 | + _slots_ = ['http_response_code', 'message'] |
| 93 | + |
| 94 | + def __init__(self, message, response_code): |
| 95 | + self.http_response_code = response_code |
| 96 | + self.message = message |
| 97 | + |
| 98 | + |
| 99 | +class ConnectivityInfo(awsiot.ModeledClass): |
| 100 | + __slots__ = ['id', 'host_address', 'metadata', 'port'] |
| 101 | + |
| 102 | + def ___init___(self): |
| 103 | + for slot in self.__slots__: |
| 104 | + setattr(self, slot, None) |
| 105 | + |
| 106 | + @classmethod |
| 107 | + def from_payload(cls, payload): |
| 108 | + # type: (typing.Dict[str, typing.Any]) -> ConnectivityInfo |
| 109 | + new = cls() |
| 110 | + val = payload.get('Id') |
| 111 | + if val is not None: |
| 112 | + new.id = val |
| 113 | + val = payload.get('HostAddress') |
| 114 | + if val is not None: |
| 115 | + new.host_address = val |
| 116 | + val = payload.get('PortNumber') |
| 117 | + if val is not None: |
| 118 | + new.port = val |
| 119 | + val = payload.get('Metadata') |
| 120 | + if val is not None: |
| 121 | + new.metadata = val |
| 122 | + return new |
| 123 | + |
| 124 | +class GGCore(awsiot.ModeledClass): |
| 125 | + __slots__ = ['thing_arn', 'connectivity'] |
| 126 | + |
| 127 | + def ___init___(self): |
| 128 | + for slot in self.__slots__: |
| 129 | + setattr(self, slot, None) |
| 130 | + |
| 131 | + @classmethod |
| 132 | + def from_payload(cls, payload): |
| 133 | + # type: (typing.Dict[str, typing.Any]) -> GGCore |
| 134 | + new = cls() |
| 135 | + val = payload.get('thingArn') |
| 136 | + if val is not None: |
| 137 | + new.thing_arn = val |
| 138 | + val = payload.get('Connectivity') |
| 139 | + if val is not None: |
| 140 | + new.connectivity = [ConnectivityInfo.from_payload(i) for i in val] |
| 141 | + |
| 142 | + return new |
| 143 | + |
| 144 | +class GGGroup(awsiot.ModeledClass): |
| 145 | + __slots__ = ['gg_group_id', 'cores', 'certificate_authorities'] |
| 146 | + |
| 147 | + def ___init___(self): |
| 148 | + for slot in self.__slots__: |
| 149 | + setattr(self, slot, None) |
| 150 | + |
| 151 | + @classmethod |
| 152 | + def from_payload(cls, payload): |
| 153 | + # type: (typing.Dict[str, typing.Any]) -> GGGroup |
| 154 | + new = cls() |
| 155 | + val = payload.get('GGGroupId') |
| 156 | + if val is not None: |
| 157 | + new.gg_group_id = val |
| 158 | + val = payload.get('Cores') |
| 159 | + if val is not None: |
| 160 | + new.cores = [GGCore.from_payload(i) for i in val] |
| 161 | + val = payload.get('CAs') |
| 162 | + if val is not None: |
| 163 | + new.certificate_authorities = val |
| 164 | + |
| 165 | + return new |
| 166 | + |
| 167 | +class DiscoverResponse(awsiot.ModeledClass): |
| 168 | + __slots__ = ['gg_groups'] |
| 169 | + |
| 170 | + def ___init___(self): |
| 171 | + for slot in self.__slots__: |
| 172 | + setattr(self, slot, None) |
| 173 | + |
| 174 | + @classmethod |
| 175 | + def from_payload(cls, payload): |
| 176 | + # type: (typing.Dict[str, typing.Any]) -> DiscoverResponse |
| 177 | + new = cls() |
| 178 | + val = payload.get('GGGroups') |
| 179 | + if val is not None: |
| 180 | + new.gg_groups = [GGGroup.from_payload(i) for i in val] |
| 181 | + |
| 182 | + return new |
0 commit comments