class Service(object): name = "service" foo_proxy = RpcProxy("foo_service") bar_proxy = RpcProxy("bar_service") baz_proxy = RpcProxy("baz_service") @rpc def method(self, arg): self.foo_proxy.remote_method(arg) @rpc def foo(self): return "bar"
class ExampleService(object): name = 'exampleservice' logger = WorkerErrorLogger() translate = Translator() example_rpc = RpcProxy('exampleservice') unknown_rpc = RpcProxy('unknown_service') @rpc def task_a(self, *args, **kwargs): return "result_a" @rpc def task_b(self, *args, **kwargs): return "result_b" @rpc(expected_exceptions=ExampleError) def broken(self): raise ExampleError("broken") @rpc(expected_exceptions=(KeyError, ValueError)) def very_broken(self): raise AttributeError @rpc def call_async(self): res1 = self.example_rpc.task_a.call_async() res2 = self.example_rpc.task_b.call_async() res3 = self.example_rpc.echo.call_async() return [res2.result(), res1.result(), res3.result()] @rpc def deprecated_async(self): return self.example_rpc.echo. async ().result() @rpc def call_unknown(self): return self.unknown_rpc.any_method() @rpc def echo(self, *args, **kwargs): return args, kwargs @rpc def say_hello(self): return self.translate(hello) @event_handler('srcservice', 'eventtype') def async_task(self): pass # pragma: no cover
class WebServer: name = 'web_server' konnichiwa_service = RpcProxy('konnichiwa_service') @http('GET', '/') def home(self, request): return self.konnichiwa_service.konnichiwa()
class ChatService: name = "chat" player_rpc = RpcProxy("player") config = Config() @rpc def validate_message(self, sender, content): content_empty = len(content) == 0 if content_empty: return None cleaned_content = html.escape(content) return cleaned_content @rpc def create_room(self, room_name: str, creator: str): creator_exists = self.player_rpc.get_player_by_username( creator) is not None secret = self.config.get("SECRET", "secret") hash_string = f"{secret};{str(time.time())};{creator};{room_name}" signature = hashlib.sha256(hash_string.encode()).hexdigest() if not creator_exists: return None return json.dumps({"room": {"code": signature, "name": room_name}})
class AccessService: name = 'access' player_rpc = RpcProxy("player") player_rest = PlayerRESTProvider() config = Config() @rpc def login(self, username, password): player = self.player_rpc.get_player(username, password) if player is None: return None jwt_secret = self.config.get("JWT_SECRET", "secret") token = jwt.encode( { 'username': player["username"], "exp": time.time() + 60 }, jwt_secret, algorithm='HS256') return json.dumps({"jwt": token.decode(), "user": player}) @rpc def signup(self, username, password, country): return self.player_rpc.create_player(username, password, country, 1000)
class Gateway: name = "gateway" orders = RpcProxy("orders_service") statsd = StatsClient('statsd-agent', 8125, prefix='simplebank-demo.gateway') @http('POST', '/shares/sell') @statsd.timer('sell_shares') def sell_shares(self, request): req_id = uuid.uuid4() res = u"{}".format(req_id) req = self.__sell_shares(res) return Response(json.dumps( {"ok": "sell order {} placed".format(req_id)}), mimetype='application/json') @rpc def __sell_shares(self, payload): print("[{}] rpc to orders service : sell_shares".format(payload)) res = u"{}".format(payload) return self.orders.sell_shares(res) @http('GET', '/health') @statsd.timer('health') def health(self, _request): return json.dumps({'ok': datetime.datetime.utcnow().__str__()})
class Gateway: name = "gateway" orders = RpcProxy("orders_service") statsd = init_statsd('simplebank-demo.gateway', 'statsd') logger = init_logger() @http('POST', '/shares/sell') @statsd.timer('sell_shares') def sell_shares(self, request): req_id = uuid.uuid4() res = u"{}".format(req_id) self.logger.debug("this is a debug message from gateway", extra={"uuid": res}) self.logger.info("placing sell order", extra={"uuid": res}) self.__sell_shares(res) return Response(json.dumps( {"ok": "sell order {} placed".format(req_id)}), mimetype='application/json') @rpc def __sell_shares(self, uuid): self.logger.info("contacting orders service", extra={"uuid": uuid}) res = u"{}".format(uuid) return self.orders.sell_shares(res) @http('GET', '/health') @statsd.timer('health') def health(self, _request): return json.dumps({'ok': datetime.datetime.utcnow().__str__()})
class WebServer: name = 'web_server' message_service = RpcProxy('message_service') templates = Jinja2() @http('GET', '/') def home(self, request): messages = self.message_service.get_all_messages() rendered_template = self.templates.render_home(messages) html_response = create_html_response(rendered_template) return html_response @http('POST', '/messages') def post_message(self, request): data_as_text = request.get_data(as_text=True) try: data = json.loads(data_as_text) except json.JSONDecodeError: return 400, 'JSON payload expected' try: message = data['message'] except KeyError: return 400, 'No message given' self.message_service.save_message(message) return 204, '' @http('GET', '/messages') def get_messages(self, request): messages = self.message_service.get_all_messages() return create_json_response(messages)
class ApiService: name = 'api' query_rpc = RpcProxy('query_stack') @http('POST', '/user') def post(self, request): data = json.loads(request.get_data(as_text=True)) if not data: return 400, 'Invalid payload' try: with ClusterRpcProxy(CONFIG) as cluster_rpc: data['id'] = str(uuid.uuid1()) cluster_rpc.command_stack.create_user.call_async(data) location = { 'Location': 'http://localhost/users/{}'.format(data['id']) } return 202, location, 'ACCEPTED' except Exception as e: return 500, e @http('GET', '/users/<int:page>/<int:limit>') def get_users(self, request, page, limit): response = self.query_rpc.get_all_users(page, limit) return 200, {'Content-Type': 'application/json'}, response @http('GET', '/users/<string:user_id>') def get_user(self, request, user_id): response = self.query_rpc.get_user(user_id) return 200, {'Content-Type': 'application/json'}, response
class MLService(object): name = "mltask" dash_rpc = RpcProxy("dashtask") db_rpc = RpcProxy("dbtask") processor = PredictLife() @rpc def get_predict(self, data): result = self.processor.predict(data) self.dash_rpc.send_the_data_to_kafka(result) self.db_rpc.save_the_data_to_hbase(result) return result
class ConfluencePublish(object): name = "confluencepublish" mail = RpcProxy('mail') @rpc def confluencepublish(self): pass
class OpenflowService: """ Openflow Service Microservice that translates the information sent by the api to commands applicable in Openflow devices Receive: this function receives a python dictionary, with at least the following information for each processing Return: - The microservice activates the application module via ssh and returns the result. If any incorrect information in the dictionary, the error message is returned Translations for NAT1toN and Route have not yet been implemented """ name = "openflow_translator" zipcode_rpc = RpcProxy('openflow_service_translator') @rpc def translate_intent(self, dict_intent): if 'intent_type' in dict_intent: output = check_values(dict_intent) if output is True: if dict_intent['intent_type'] == 'acl': return process_acl(dict_intent) elif dict_intent['intent_type'] == 'nat_1to1': return process_nat11(dict_intent) elif dict_intent['intent_type'] == 'traffic_shaping': return process_traffic_shaping(dict_intent) elif dict_intent['intent_type'] == 'dst_route': return process_dst_route(dict_intent) elif dict_intent['intent_type'] == 'nat_nto1': return process_natn1(dict_intent) elif dict_intent['intent_type'] == 'url_filter': return process_url_filter(dict_intent) else: return output else: return 'OPENFLOW TRANSLATOR: the key "intent_type" is unavailable in the dictionary'
class publishFlowDock(object): name = "publishtoflowdock" mail = RpcProxy('mail') @rpc def exporttopdf(self): pass
class ExportToPDF(object): name = "exporttopdf" mail = RpcProxy('mail') @rpc def exporttopdf(self): pass
def test_regular_parameters(self, parameter, mock_container, producer): """ Verify that most parameters can be specified at RpcProxy instantiation time. """ mock_container.config = {'AMQP_URI': 'memory://localhost'} mock_container.shared_extensions = {} mock_container.service_name = "service" worker_ctx = Mock() worker_ctx.container = mock_container worker_ctx.context_data = {} value = Mock() rpc_proxy = RpcProxy("service-name", **{ parameter: value }).bind(mock_container, "service_rpc") rpc_proxy.setup() rpc_proxy.rpc_reply_listener.setup() service_rpc = rpc_proxy.get_dependency(worker_ctx) service_rpc.method.call_async() assert producer.publish.call_args[1][parameter] == value
class pagerduty(object): name = "pagerduty" mail = RpcProxy('mail') @rpc def pagerduty(self): pass
class Service(object): name = "service" proxy = RpcProxy("foo_service") @rpc def method(self): pass
class GatewayService: name = 'gateway' parameter_service = RpcProxy('parameter_service') @http('GET', '/parameter') def get_parameter(self, request): parameter = self.parameter_service.get_param() return json.dumps({'parameter': parameter}) @http('POST', '/process') def post_process(self, request): data_as_text = request.get_data(as_text=True) try: data = json.loads(data_as_text) except json.JSONDecodeError: return 400, 'JSON payload expected' try: trigger = int(data['trigger']) except KeyError: return 400, 'No message given' self.parameter_service.set_process_triggered(trigger) return 200, ''
class GatewayService: name = 'gateway' service_rpc = RpcProxy('service') @http('GET', '/pessoa/<string:pessoa_id>') def get_pessoa(self, request, pessoa_id): pessoa = self.service_rpc.get_pessoa(pessoa_id) return json.dumps({'pessoa': pessoa}) @http('POST', '/pessoa') def post_pessoa(self, request): data = json.load(request.get_data(as_text=True)) pessoa = self.service_rpc.create_pessoa(data['pessoa']) return pessoa @http('GET', '/divida/<string:pessoa_id>') def get_divida(self, request, pessoa_id): dividas = self.service_rpc.get_divida(pessoa_id) return json.dumps({'dividas': dividas}) @http('POST', '/divida') def post_divida(self, request): data = json.loads(request.get_data(as_text=True)) divida = self.service_rpc.create_divida(data['divida']) return divida
class SalesforceService: name = 'salesforce' contacts_rpc = RpcProxy('contacts') salesforce = SalesforceAPI() source_tracker = SourceTracker() @event_handler('contacts', 'contact_created') def handle_platform_contact_created(self, payload): if self.source_tracker.is_sourced_from_salesforce(): print("Ignoring event that was sourced from salesforce") return result = self.salesforce.Contact.create( {'LastName': payload['contact']['name']} ) print('Created {} on salesforce'.format(result)) @handle_sobject_notification( 'Contact', exclude_current_user=True, notify_for_operation_update=False ) def handle_sf_contact_created(self, sobject_type, record_type, notification): with self.source_tracker.sourced_from_salesforce(): contact = self.contacts_rpc.create_contact( {'name': notification['sobject']['Name']} ) print('Created {} on platform'.format(contact))
class OpenflowService: """ Openflow Service Microservice that translates the information sent by the api to commands applicable in Openflow devices Receive: this function receives a python dictionary, with at least the following information for each processing For acl process: ['from', 'to', 'rule', 'traffic', 'apply'] For nat11 process: ['from', 'to', 'protocol', 'apply'] For traffic_shaping process: ['from', 'to', 'for', 'with', 'traffic', 'apply'] Return: - The microservice activates the application module via ssh and returns the result. If any incorrect information in the dictionary, the error message is returned """ name = "openflow_translator" zipcode_rpc = RpcProxy('openflow_service_translator') @rpc def translate_intent(self, dict_intent): if 'intent_type' in dict_intent: output = check_values(dict_intent) if output is True: if dict_intent['intent_type'] == 'acl': return process_acl(dict_intent) elif dict_intent['intent_type'] == 'nat11': return process_nat11(dict_intent) elif dict_intent['intent_type'] == 'traffic_shaping': return process_traffic_shaping(dict_intent) else: return output else: return 'OPENFLOW TRANSLATOR: the key "intent_type" is unavailable in the dictionary'
class GatewayService: logger = logging.getLogger(__name__) name = 'gateway' addresses_rpc = RpcProxy('addresses') accounts_rpc = RpcProxy('accounts') listings_rpc = RpcProxy('listings') # Accounts stuff @http('GET', '/accounts/<string:account_id>') def get_account(self, request, account_id): account = self.accounts_rpc.get(account_id) if 'billing_address_id' in account: account['billing_address_id'] = self.addresses_rpc.get(account.get('billing_address_id')) return json.dumps({'account': account}) @http('POST', '/accounts') def post_account(self, request): data = json.loads(request.get_data(as_text=True)) self.logger.info(data) account_id = self.accounts_rpc.create(data) return json.dumps({'account': {'id': account_id}}) # Addresses stuff @http('GET', '/addresses/<string:address_id>') def get_address(self, request, address_id): address = self.addresses_rpc.get(address_id) return json.dumps({'address': address}) @http('POST', '/addresses') def post_address(self, request): data = json.loads(request.get_data(as_text=True)) address_id = self.addresses_rpc.create(**data) return json.dumps({'address': {'id': address_id}}) # Listing stuff @http('GET', '/listings/<string:listing_id>') def get_listing(self, request, listing_id): listing = self.listings_rpc.get(listing_id) self.logger.info(listing) return json.dumps({'listing': listing}) @http('POST', '/listings') def post_listing(self, request): data = json.loads(request.get_data(as_text=True)) listing_id = self.listings_rpc.create(**data) return json.dumps({'listing': {'id': listing_id}})
class JukeboxService: name = "jukebox_service" player_service = RpcProxy("player_service") # {"bla.mp3" : 12, ...} data = {} index = [] def __init__(self): self._update() def _update(self): old_data = self.data.copy() #built from scratch, so the deleted files disapear self.data.clear() for f_name in os.listdir("mp3"): if f_name not in old_data.keys(): #no likes yet self.data[f_name] = 0 else: self.data[f_name] = old_data[f_name] self._update_index() def _update_index(self): self.index = [] for k in self.data.keys(): for o in range(self.data[k] + 1): self.index.append(k) @timer(interval=1) def poller(self): # method executed every second if not self.player_service.is_playing(): self.play_next() @rpc def play_next(self): n = self.get_next_song() self.player_service.play(n) @rpc def get_index(self): return self.index @rpc def update(self): self._update() @rpc def get_all(self): return self.data @rpc def like(self, f_name): self.data[f_name] += 1 self._update_index() @rpc def get_next_song(self): return random.choice(self.index)
class Parent(object): name = "parent" child_service = RpcProxy('child') @rpc def parent_do(self): return self.child_service.child_do()
class ProxyService(object): name = "proxyservice" example_rpc = RpcProxy('exampleservice') @dummy def entrypoint(self, arg): return self.example_rpc.method(arg)
class Service2: name = "service2" log = LoggingDependency() other_rpc = RpcProxy("greeting_service") @rpc def hello_service2(self, name): return self.other_rpc.hello(name)
class Service(object): name = "client" server_rpc = RpcProxy("server") @dummy def echo(self, arg): return self.server_rpc.echo(arg)
class WebServer: name = 'web_server' message_service = RpcProxy('message_service') @http('GET', '/<message_id>') def root(self, request, message_id): return self.message_service.get_message(message_id)
class FRPC(): name = "FRPC" fs = RpcProxy("FileService") @rpc def remote_file(self, fname): return self.fs.getFileContents(fname)
class Grandparent(object): name = "grandparent" parent_service = RpcProxy('parent') @rpc def grandparent_do(self): return self.parent_service.parent_do()