def test_report_cost_success(self): client = utils.get_meteringmarketplace_client() with Stubber(client) as stubber: response = { "Results": [{ "UsageRecord": { "Timestamp": "2020-10-10", "CustomerIdentifier": "cust-123", "Dimension": "costs_accrued", "Quantity": 100 }, "MeteringRecordId": "rec-123", "Status": "Success" }], "UnprocessedRecords": [], "ResponseMetadata": { "RequestId": "066f8a74-f929-4595-bc19-1a16f605bed5", "HTTPStatusCode": 200, } } stubber.add_response('batch_meter_usage', response) utils.get_meteringmarketplace_client = MagicMock( return_value=client) status, result = utils.report_cost(1.0, "cust-123", "prod-123") self.assertEqual('Success', status) self.assertDictEqual(response['Results'][0], result)
def lambda_handler(event, context): synapse_ids = utils.get_marketplace_synapse_ids() log.debug(f'customers list: {synapse_ids}') num_customers = len(synapse_ids) num_failed_reports = 0 for synapse_id in synapse_ids: customer_info = utils.get_marketplace_customer_info(synapse_id) log.debug(f'marketplace customer info: {customer_info}') if len(customer_info) == 0: continue customer_id = customer_info['MarketplaceCustomerId'] product_code = customer_info['ProductCode'] yesterday = get_time_period_yesterday() cost, unit = utils.get_customer_cost(customer_id, yesterday, "DAILY") status, result = utils.report_cost(cost, customer_id, product_code) if status == 'Failed': num_failed_reports = num_failed_reports + 1 log.info(f'Failed to report cost for product {product_code} to customer {customer_id}') time.sleep(1) else: log.info(f'Successfully reported cost with record {result}') num_success_reports = num_customers - num_failed_reports message = f'Metering processed: failed reports ({num_failed_reports}), ' \ f'successful reports ({num_success_reports})' log.info(message) response = { "statusCode": 200, "body": json.dumps({ "message": message, }), } return response
def lambda_handler(event, context): synapse_ids = utils.get_marketplace_synapse_ids() log.debug(f'customers list: {synapse_ids}') for synapse_id in synapse_ids: customer_id = utils.get_marketplace_customer_id(synapse_id) log.debug(f'marketplace customer ID: {customer_id}') product_code = utils.get_marketplace_product_code(synapse_id) log.debug(f'marketplace product code: {product_code}') yesterday = get_time_period_yesterday() cost, unit = utils.get_customer_cost(customer_id, yesterday, "DAILY") utils.report_cost(cost, customer_id, product_code) response = { "statusCode": 200, "body": json.dumps({ "message": "Metering processed", }), } return response
def test_report_cost_default_attempts_failed(self): client = utils.get_meteringmarketplace_client() with Stubber(client) as stubber: responses = [{}, {}, {}] for response in responses: stubber.add_response('batch_meter_usage', response) utils.get_meteringmarketplace_client = MagicMock( return_value=client) status, result = utils.report_cost(1.0, "cust-123", "prod-123") expected = ("Failed", None) self.assertEqual('Failed', status) self.assertEqual(None, result)
def test_report_cost_invalid_cost_invalid_type_str(self): with self.assertRaises(ValueError): utils.report_cost("1.0", "cust-123", "prod-123")
def test_report_cost_invalid_cost_less_than_zero(self): with self.assertRaises(ValueError): utils.report_cost(-1.0, "cust-123", "prod-123")
def test_report_cost_invalid_attempts(self): with self.assertRaises(ValueError): utils.report_cost(1.0, "cust-123", "prod-123", 0)