def test_fipset_get_unit_price_with_again_event(self): rate_limit = 10240 quantity = pricing.rate_limit_to_unit(rate_limit) expected_price = pricing.calculate_price( quantity, self.product.unit_price) payload = self.build_floatingipset_payload( self.fipset, rate_limit, self.admin_account.project_id) payload = payload['floatingipset'] fipset = neutron.FloatingIpSet( id=payload['id'], name=payload['uos:name'], size=payload['rate_limit'], project_id=payload['tenant_id'], providers=payload['uos:service_provider'], resource_type=gring_const.RESOURCE_FLOATINGIPSET, status=payload['status'], is_reserved=True) message = fipset.to_message() handle = floatingip.FloatingIpCreateEnd() handle.process_notification(message) resource_id = payload['id'] order = self.dbconn.get_order_by_resource_id( self.admin_req_context, resource_id) price = handle.get_unit_price(order.order_id, message, gring_const.STATE_RUNNING) self.assertDecimalEqual(expected_price, price)
def _get_floatingip_collection(self, message): floatingip = message['payload']['floatingip'] service = const.SERVICE_NETWORK region_id = cfg.CONF.region_name product_name = generate_product_name(message) if product_name != const.PRODUCT_FLOATINGIP: product = self.gclient.get_product( product_name, service, region_id) if not product: product_name = const.PRODUCT_FLOATINGIP resource_id = floatingip['id'] resource_name = floatingip['uos:name'] resource_type = const.RESOURCE_FLOATINGIP resource_volume = pricing.rate_limit_to_unit( floatingip['rate_limit']) user_id = None project_id = floatingip['tenant_id'] return Collection(product_name=product_name, service=service, region_id=region_id, resource_id=resource_id, resource_name=resource_name, resource_type=resource_type, resource_volume=resource_volume, user_id=user_id, project_id=project_id)
def test_floatingip_change_unit_price(self): product = self.product_fixture.ip_products[0] project_id = self.admin_account.project_id end_time = self.utcnow() start_time = end_time - datetime.timedelta(hours=1) # rate_limit = 1024 rate_limit = 1024 resource_id = self.create_floatingip( rate_limit, project_id, timestamp=start_time) order = self.dbconn.get_order_by_resource_id( self.admin_req_context, resource_id) # change rate_limit to 1024 * 11 rate_limit = 1024 * 11 quantity = pricing.rate_limit_to_unit(rate_limit) expected_price = pricing.calculate_price(quantity, product.unit_price) payload = self.build_floatingip_payload( self.floating_ip_address, rate_limit, project_id, id=resource_id) message = self.build_notification_message( self.admin_account.user_id, self.event_resized, payload, timestamp=end_time) handle = floatingip.FloatingIpCreateEnd() handle.change_unit_price( message, gring_const.STATE_RUNNING, order.order_id) order = self.dbconn.get_order(self.admin_req_context, order.order_id) self.assertDecimalEqual(expected_price, order.unit_price) subs_list = list(self.dbconn.get_subscriptions_by_order_id( self.admin_req_context, order.order_id)) for subs in subs_list: self.assertEqual(quantity, subs.quantity)
def test_floatingip_create_end(self): product = self.product_fixture.ip_products[0] rate_limit = 1024 quantity = pricing.rate_limit_to_unit(rate_limit) price = pricing.calculate_price(quantity, product.unit_price) project_id = self.admin_account.project_id start_time = self.utcnow() resource_id = self.create_floatingip( rate_limit, project_id, timestamp=start_time) order = self.dbconn.get_order_by_resource_id( self.admin_req_context, resource_id) self.assertDecimalEqual(price, order.unit_price) subs_list = list(self.dbconn.get_subscriptions_by_order_id( self.admin_req_context, order.order_id)) self.assertEqual(1, len(subs_list)) for subs in subs_list: self.assertEqual(gring_const.STATE_RUNNING, subs.type) self.assertEqual(quantity, subs.quantity) bill = self.dbconn.get_latest_bill(self.admin_req_context, order.order_id) self.assertEqual(resource_id, bill.resource_id) self.assertEqual(self.datetime_to_str(start_time), self.datetime_to_str(bill.start_time)) self.assertDecimalEqual(price, bill.unit_price)
def get_resource_volume(self, body): if 'floatingip' in body: rate_limit = body['floatingip'].get('rate_limit') elif 'floatingipset' in body: rate_limit = body['floatingipset'].get('rate_limit') else: rate_limit = 1024 return pricing.rate_limit_to_unit(rate_limit)
def change_unit_price(self, resource, status, order_id): quantity = None if resource.resource_type == const.RESOURCE_LISTENER: quantity = int(resource.connection_limit) / 1000 elif resource.resource_type == const.RESOURCE_FLOATINGIP: quantity = pricing.rate_limit_to_unit(resource.size) if quantity is not None: self.change_order_unit_price(order_id, quantity, status)
def _test_floatingip_created_unit_price(rate_limit): price = pricing.calculate_price( pricing.rate_limit_to_unit(rate_limit), product.unit_price) resource_id = self.create_floatingip(rate_limit, project_id) order = self.dbconn.get_order_by_resource_id( self.admin_req_context, resource_id) self.assertDecimalEqual(price, order.unit_price) bill = self.dbconn.get_latest_bill(self.admin_req_context, order.order_id) self.assertDecimalEqual(price, bill.unit_price)
def process_notification(self, message): payload = get_payload(message) LOG.warn('Do action for event: %s, resource_id: %s', message['event_type'], payload['id']) quantity = pricing.rate_limit_to_unit(payload['rate_limit']) # Get the order of this resource resource_id = payload['id'] order = self.get_order_by_resource_id(resource_id) if order.get('unit') in ['month', 'year']: return # Notify master action_time = message['timestamp'] remarks = 'Floating IP Has Been Resized' self.resource_resized(order['order_id'], action_time, quantity, remarks)
def resize_resource_order(self, env, body, start_response, order_id, resource_id, resource_type): if resource_type == const.RESOURCE_FLOATINGIP: quantity = pricing.rate_limit_to_unit( body['floatingip']['rate_limit']) elif resource_type == const.RESOURCE_LISTENER: quantity = int(body['listener']['connection_limit']) / 1000 try: self.gclient.resize_resource_order(order_id, quantity=quantity, resource_type=resource_type) except Exception as e: msg = "Unable to resize the order: %s" % order_id LOG.exception(msg) return False, self._reject_request_500(env, start_response) return True, None
def test_floatingip_resize_end(self): product = self.product_fixture.ip_products[0] project_id = self.admin_account.project_id end_time = self.utcnow() start_time = end_time - datetime.timedelta(hours=1) # rate_limit = 1024 rate_limit = 1024 resource_id = self.create_floatingip( rate_limit, project_id, timestamp=start_time) order = self.dbconn.get_order_by_resource_id( self.admin_req_context, resource_id) bill1 = self.dbconn.get_latest_bill(self.admin_req_context, order.order_id) # change rate_limit to 1024 * 11 rate_limit = 1024 * 11 quantity = pricing.rate_limit_to_unit(rate_limit) payload = self.build_floatingip_payload( self.floating_ip_address, rate_limit, project_id, id=resource_id) message = self.build_notification_message( self.admin_account.user_id, self.event_resized, payload, timestamp=end_time) handle = floatingip.FloatingIpResizeEnd() handle.process_notification(message) price = pricing.calculate_price(quantity, product.unit_price) order = self.dbconn.get_order(self.admin_req_context, order.order_id) self.assertDecimalEqual(price, order.unit_price) subs_list = list(self.dbconn.get_subscriptions_by_order_id( self.admin_req_context, order.order_id)) self.assertEqual(1, len(subs_list)) for subs in subs_list: self.assertEqual(quantity, subs.quantity) bill1 = self.dbconn.get_bill(self.admin_req_context, bill1.bill_id) self.assertEqual(self.datetime_to_str(end_time), self.datetime_to_str(bill1.end_time)) bill2 = self.dbconn.get_latest_bill(self.admin_req_context, order.order_id) self.assertEqual(self.datetime_to_str(end_time), self.datetime_to_str(bill2.start_time)) self.assertDecimalEqual(price, bill2.unit_price)
def _get_floatingipset_collection(self, message): floatingipset = message['payload']['floatingipset'] product_name = generate_product_name(message) service = const.SERVICE_NETWORK region_id = cfg.CONF.region_name resource_id = floatingipset['id'] resource_name = floatingipset['uos:name'] resource_type = const.RESOURCE_FLOATINGIPSET resource_volume = pricing.rate_limit_to_unit( floatingipset['rate_limit']) user_id = None project_id = floatingipset['tenant_id'] return Collection(product_name=product_name, service=service, region_id=region_id, resource_id=resource_id, resource_name=resource_name, resource_type=resource_type, resource_volume=resource_volume, user_id=user_id, project_id=project_id)
def test_fipset_change_unit_price_with_again_event(self): project_id = self.admin_account.project_id end_time = self.utcnow() start_time = end_time - datetime.timedelta(hours=1) # rate_limit = 1024 rate_limit = 1024 resource_id = self.create_floatingipset( rate_limit, project_id, timestamp=start_time) order = self.dbconn.get_order_by_resource_id( self.admin_req_context, resource_id) # change rate_limit to 1024 * 11 rate_limit = 1024 * 11 quantity = pricing.rate_limit_to_unit(rate_limit) expected_price = pricing.calculate_price( quantity, self.product.unit_price) payload = self.build_floatingipset_payload( self.fipset, rate_limit, self.admin_account.project_id) payload = payload['floatingipset'] fipset = neutron.FloatingIpSet( id=payload['id'], name=payload['uos:name'], size=payload['rate_limit'], project_id=payload['tenant_id'], providers=payload['uos:service_provider'], resource_type=gring_const.RESOURCE_FLOATINGIPSET, status=payload['status'], is_reserved=True) message = fipset.to_message() handle = floatingip.FloatingIpCreateEnd() handle.change_unit_price( message, gring_const.STATE_RUNNING, order.order_id) order = self.dbconn.get_order(self.admin_req_context, order.order_id) self.assertDecimalEqual(expected_price, order.unit_price) subs_list = list(self.dbconn.get_subscriptions_by_order_id( self.admin_req_context, order.order_id)) for subs in subs_list: self.assertEqual(quantity, subs.quantity)
def test_floatingip_get_unit_price(self): product = self.product_fixture.ip_products[0] rate_limit = 10240 quantity = pricing.rate_limit_to_unit(rate_limit) expected_price = pricing.calculate_price(quantity, product.unit_price) payload = self.build_floatingip_payload( self.floating_ip_address, rate_limit, self.admin_account.project_id) message = self.build_notification_message( self.admin_account.user_id, self.event_created, payload) handle = floatingip.FloatingIpCreateEnd() handle.process_notification(message) resource_id = payload['floatingip']['id'] order = self.dbconn.get_order_by_resource_id( self.admin_req_context, resource_id) price = handle.get_unit_price(order.order_id, message, gring_const.STATE_RUNNING) self.assertDecimalEqual(expected_price, price)
def change_unit_price(self, message, status, order_id): payload = get_payload(message) quantity = pricing.rate_limit_to_unit(payload['rate_limit']) self.change_order_unit_price(order_id, quantity, status)
def get_resource_volume(self, env, body): if 'floatingip' in body: rate_limit = body['floatingip'].get('rate_limit') else: rate_limit = 1024 return pricing.rate_limit_to_unit(rate_limit)