def show_external_zdb_payment( self, bot, farm_name, size=ZDB_STARTING_SIZE, no_nodes=1, expiry=5, wallet_name=None, disk_type=DiskType.HDD ): discount = FARM_DISCOUNT.get() duration = self.calculate_expiration_value() - j.data.time.utcnow().timestamp month = 60 * 60 * 24 * 30 if duration > month: duration = month zos = j.sals.zos.get() farm_id = zos._explorer.farms.get(farm_name=farm_name).id zdb = ZdbNamespace() zdb.size = size zdb.disk_type = disk_type amount = j.tools.zos.consumption.cost(zdb, duration, farm_id) + TRANSACTION_FEES amount *= no_nodes prepaid_balance = self._get_wallet_balance(self.prepaid_wallet) if prepaid_balance >= amount: if bot: result = bot.single_choice( f"Do you want to use your existing balance to pay {round(amount,4)} TFT? (This will impact the overall expiration of your plan)", ["Yes", "No"], required=True, ) if result == "Yes": amount = 0 else: amount = 0 elif not bot: # Not enough funds in prepaid wallet and no bot passed to use to view QRcode return False, amount, None payment_id, _ = j.sals.billing.submit_payment( amount=amount, wallet_name=wallet_name or self.prepaid_wallet.instance_name, refund_extra=False, expiry=expiry, description=j.data.serializers.json.dumps( {"type": "VDC_ZDB_EXTEND", "owner": self.owner_tname, "solution_uuid": self.solution_uuid} ), ) if amount > 0: notes = [] if discount: notes = ["For testing purposes, we applied a discount of {:.0f}%".format(discount * 100)] return j.sals.billing.wait_payment(payment_id, bot=bot, notes=notes), amount, payment_id else: return True, amount, payment_id
def test10_extend_zdb(self): """Test case for extending zdbs. **Test Scenario** - Deploy VDC. - Get the zdbs total size. - Calculate the price of added zdb and fund the provisioning wallet. - Extend zdbs. - Check that zdbs has been extended. """ self.info("Get the zdbs total size") zdb_monitor = self.vdc.get_zdb_monitor() old_zdb_total_size = zdb_monitor.zdb_total_size self.info( "Calculate the price of added zdb and fund the provisioning wallet." ) zdb = ZdbNamespace() zdb.size = 10 # In case of all tests runs, it will be deployed for an hour and renewed by a day. zos = j.sals.zos.get() farm_name = self.get_farm_name() farm_id = zos._explorer.farms.get(farm_name=farm_name).id price = j.tools.zos.consumption.cost( zdb, 25 * 60 * 60, farm_id) + TRANSACTION_FEES # transactions fees. self.vdc.transfer_to_provisioning_wallet(round(price, 6), "test_wallet") self.info("Extend zdbs") vdc_identity = f"vdc_ident_{self.vdc.solution_uuid}" if j.core.identity.find(vdc_identity): j.core.identity.set_default(vdc_identity) farm = self.get_farm_name() zdb_monitor.extend(10, [farm]) self.info("Check that zdbs has been extended") self.vdc.load_info() zdb_monitor = self.vdc.get_zdb_monitor() self.assertEqual(zdb_monitor.zdb_total_size, old_zdb_total_size + 10)
def test_zdb_ru(): z = ZdbNamespace() tt = [ { "size": 1, "type": DiskType.SSD, "cru": 0, "mru": 0, "sru": 1, "hru": 0 }, { "size": 1, "type": DiskType.HDD, "cru": 0, "mru": 0, "sru": 0, "hru": 1 }, { "size": 12, "type": DiskType.SSD, "cru": 0, "mru": 0, "sru": 12, "hru": 0 }, ] for tc in tt: z.size = tc["size"] z.disk_type = tc["type"] ru = z.resource_units() assert ru.cru == tc["cru"] assert ru.mru == tc["mru"] assert ru.sru == tc["sru"] assert ru.hru == tc["hru"]
def create( self, node_id: str, size: int, mode: Union[str, ZDBMode], password: str, pool_id: int, disk_type: Union[str, DiskType] = DiskType.SSD, public: bool = False, ) -> ZdbNamespace: """create 0-DB namespace workload Args: node_id(str): the ID of the node where to deploy the namespace size(int): the size of the namespace in GiB mode(Union[str,ZDBMode]): the mode of the 0-DB. It can be 'seq' or 'user' password(str): password of the namespace. if you don't want password use an empty string pool_id(int): the capacity pool ID disk_type(DiskType, optional): type of disk,can be SSD or HDD, defaults to DiskType.SSD public(bool, optional Returns: ZdbNamespace: ZdbNamespace """ if isinstance(disk_type, str): disk_type = getattr(DiskType, disk_type) if isinstance(mode, str): mode = getattr(ZDBMode, mode.title()) zdb = ZdbNamespace() zdb.info.node_id = node_id zdb.info.pool_id = pool_id zdb.info.workload_type = WorkloadType.Zdb zdb.size = size zdb.mode = mode if password: node = self._nodes.get(node_id) zdb.password = encrypt_for_node(node.public_key_hex, password).decode() zdb.disk_type = disk_type return zdb
def calculate_vdc_price(flavor, farm_name=None): """calculate the workloads price for vdcs in TFT Args: flavor (str): vdc flavor in [silver, gold, platinum, diamond] Returns: str: vdc price """ all_cus = 0 all_sus = 0 all_ipv4us = 0 # get the flavor enum for item in VDC_SIZE.VDC_FLAVORS.keys(): if flavor == item.value: flavor = item # calculate cloud units enough for a month def get_cloud_units(workload): ru = workload.resource_units() cloud_units = ru.cloud_units() return ( cloud_units.cu * 60 * 60 * 24 * 30, cloud_units.su * 60 * 60 * 24 * 30, cloud_units.ipv4u * 60 * 60 * 24 * 30, ) # get zdbs usage zdb = ZdbNamespace() zdb.size = VDC_SIZE.S3_ZDB_SIZES[VDC_SIZE.VDC_FLAVORS[flavor]["s3"] ["size"]]["sru"] zdb.disk_type = DiskType.HDD _, sus, _ = get_cloud_units(zdb) all_cus += sus # get kubernetes controller usage master_size = VDC_SIZE.VDC_FLAVORS[flavor]["k8s"]["controller_size"] k8s = K8s() k8s.size = master_size.value cus, sus, ipv4us = get_cloud_units(k8s) all_cus += cus all_sus += sus all_ipv4us += ipv4us # get kubernetes workers usage no_nodes = VDC_SIZE.VDC_FLAVORS[flavor]["k8s"]["no_nodes"] worker_size = VDC_SIZE.VDC_FLAVORS[flavor]["k8s"]["size"] k8s = K8s() k8s.size = worker_size.value cus, sus, ipv4us = get_cloud_units(k8s) all_cus += cus * (no_nodes + 1) all_sus += sus * (no_nodes + 1) # get 3bot container usage cont2 = Container() cont2.capacity.cpu = THREEBOT_CPU cont2.capacity.memory = THREEBOT_MEMORY cont2.capacity.disk_size = THREEBOT_DISK cont2.capacity.disk_type = DiskType.SSD n_cus, n_sus, _ = get_cloud_units(cont2) all_cus += n_cus all_sus += n_sus # create empty pool and get the payment amount zos = j.sals.zos.get() farm_name = farm_name or zos._explorer.farms.list()[0].name pool = zos.pools.create(round(all_cus), round(all_sus), round(all_ipv4us), farm_name) amount = pool.escrow_information.amount total_amount_dec = Decimal(amount) / Decimal(1e7) total_amount = "{0:f}".format(total_amount_dec) return total_amount
def calculate_vdc_price(flavor, farm_name=None): """calculate the workloads price for vdcs in TFT Args: flavor (str): vdc flavor in [silver, gold, platinum, diamond] Returns: str: vdc price """ all_cus = 0 all_sus = 0 all_ipv4us = 0 # get the flavor enum for item in VDC_SIZE.VDC_FLAVORS.keys(): if flavor == item.value: flavor = item # calculate cloud units enough for a month def get_cloud_units(workload): ru = workload.resource_units() cloud_units = ru.cloud_units() return ( cloud_units.cu * 60 * 60 * 24 * 30, cloud_units.su * 60 * 60 * 24 * 30, cloud_units.ipv4u * 60 * 60 * 24 * 30, ) # get zdbs usage zdb = ZdbNamespace() zdb.size = VDC_SIZE.S3_ZDB_SIZES[VDC_SIZE.VDC_FLAVORS[flavor]["s3"] ["size"]]["sru"] zdb.disk_type = DiskType.HDD _, sus, _ = get_cloud_units(zdb) all_sus += sus # get kubernetes controller usage master_size = VDC_SIZE.VDC_FLAVORS[flavor]["k8s"]["controller_size"] k8s = K8s() k8s.size = master_size.value cus, sus, ipv4us = get_cloud_units(k8s) all_cus += cus all_sus += sus all_ipv4us += 60 * 60 * 24 * 30 # hardcoded since not calculated in the explorer client # get kubernetes workers usage no_nodes = VDC_SIZE.VDC_FLAVORS[flavor]["k8s"]["no_nodes"] worker_size = VDC_SIZE.VDC_FLAVORS[flavor]["k8s"]["size"] k8s = K8s() k8s.size = worker_size.value cus, sus, ipv4us = get_cloud_units(k8s) all_cus += cus * (no_nodes) all_sus += sus * (no_nodes) # get 3bot container usage cont2 = Container() cont2.capacity.cpu = THREEBOT_CPU cont2.capacity.memory = THREEBOT_MEMORY cont2.capacity.disk_size = THREEBOT_DISK cont2.capacity.disk_type = DiskType.SSD n_cus, n_sus, _ = get_cloud_units(cont2) all_cus += n_cus all_sus += n_sus # etcd containers usage etcd_cont = Container() etcd_cont.capacity.cpu = ETCD_CPU etcd_cont.capacity.memory = ETCD_MEMORY etcd_cont.capacity.disk_size = ETCD_DISK etcd_cont.capacity.disk_type = DiskType.SSD n_cus, n_sus, _ = get_cloud_units(etcd_cont) all_cus += n_cus * ETCD_CLUSTER_SIZE all_sus += n_sus * ETCD_CLUSTER_SIZE zos = j.sals.zos.get() farm_prices = zos._explorer.farms.get_deal_for_threebot( 1, j.core.identity.me.tid)["custom_cloudunits_price"] total_amount = zos._explorer.prices.calculate(cus=all_cus, sus=all_sus, ipv4us=all_ipv4us, farm_prices=farm_prices) total_amount = round(total_amount, 6) return total_amount