PyScripts 评级模块¶
PyScripts 模块允许您创建自己的评级模块。脚本应该处理给定的数据并设置不同的价格。
警告:如果您添加多个 PyScripts,则执行顺序不保证。
自定义模块示例¶
价格定义¶
import decimal
# Price for each flavor. These are equivalent to hashmap field mappings.
flavors = {
'm1.micro': decimal.Decimal(0.65),
'm1.nano': decimal.Decimal(0.35),
'm1.large': decimal.Decimal(2.67)
}
# Price per MB / GB for images and volumes. These are equivalent to
# hashmap service mappings.
image_mb_price = decimal.Decimal(0.002)
volume_gb_price = decimal.Decimal(0.35)
价格计算函数¶
# These functions return the price of a service usage on a collect period.
# The price is always equivalent to the price per unit multiplied by
# the quantity.
def get_instance_price(item):
if not item['metadata']['flavor_name'] in flavors:
return 0
else:
return (decimal.Decimal(item['vol']['qty'])
* flavors[item['metadata']['flavor_name']])
def get_image_price(item):
if not item['vol']['qty']:
return 0
else:
return decimal.Decimal(item['vol']['qty']) * image_mb_price
def get_volume_price(item):
if not item['vol']['qty']:
return 0
else:
return decimal.Decimal(item['vol']['qty']) * volume_gb_price
# Mapping each service to its price calculation function
services = {
'instance': get_instance_price,
'volume': get_volume_price,
'image': get_image_price
}
处理数据¶
def process(data):
# The 'data' is a dictionary with the usage entries for each service
# in a given period.
usage_data = data['usage']
for service_name, service_data in usage_data.items():
# Do not calculate the price if the service has no
# price calculation function
if service_name in services.keys():
# A service can have several items. For example,
# each running instance is an item of the compute service
for item in service_data:
item['rating'] = {'price': services[service_name](item)}
return data
# 'data' is passed as a global variable. The script is supposed to set the
# 'rating' element of each item in each service
data = process(data)
将您的脚本用于评级¶
启用 PyScripts 模块¶
要将您的脚本用于评级,您需要启用 pyscripts 模块
$ cloudkitty module enable pyscripts
+-----------+---------+----------+
| Module | Enabled | Priority |
+-----------+---------+----------+
| pyscripts | True | 1 |
+-----------+---------+----------+
将脚本添加到 CloudKitty¶
创建脚本并指定其名称、描述、开始和结束日期。如果未提供 start 和 end,则 start 将设置为创建日期,end 设置为 None。脚本从 start 时间到 end 时间有效,如果 end 时间为 None,则脚本将无限期有效。
$ cloudkitty pyscript create my_awesome_script script.py
+-------------------+--------------------------------------+---------------------+---------------------+------+-------------+---------+----------------------------------+------------+------------+------------------------------------------+---------------------------------------+
| Name | Script ID | Created At | Start | End | Description | Deleted | Created By | Updated By | Deleted By | Checksum | Data |
+-------------------+--------------------------------------+---------------------+---------------------+------+-------------+---------+----------------------------------+------------+------------+------------------------------------------+---------------------------------------+
| my_awesome_script | 78e1955a-4e7e-47e3-843c-524d8e6ad4c4 | 2023-01-01T10:00:00 | 2023-01-01T10:00:00 | None | None | None | 7977999e2e2511e6a8b2df30b233ffcb | None | None | 49e889018eb86b2035437ebb69093c0b6379f18c | from __future__ import print_function |
| | | | | | | | | | | | from cloudkitty import rating |
| | | | | | | | | | | | |
| | | | | | | | | | | | import decimal |
| | | | | | | | | | | | |
| | | | | | | | | | | | {...} |
| | | | | | | | | | | | |
| | | | | | | | | | | | data = process(data) |
| | | | | | | | | | | | |
+-------------------+--------------------------------------+---------------------+---------------------+------+-------------+---------+----------------------------------+------------+------------+------------------------------------------+---------------------------------------+