该项目是可以在Python脚本和应用程序中使用的Crayon Cloudiq API的SDK。提供了一个简单的接口,可以使用OAuth2与API进行身份验证。它可用于创建租户,创建许可订阅和监控账单。可以使用此软件包自动化在Cloud-IQ门户中可以执行的任何操作。
包括几种预配置的数据架构和API方法。自定义数据块可以作为Python词典发布到API。 REST方法:获取,发布,补丁,PUT和DELETE可以用API端点和数据字典作为参数调用。
pip install crayon-cloudiq-sdk
如何创建Cloud-IQ API客户端凭据
创建一个新的Python脚本
导入Cloudiq类
from cloudiq import CloudIQ
用有效的用户凭据初始化Cloudiq类的实例:
from cloudiq import CloudIQ
CLIENT_ID = xxxxxxx-xxxx-xxxx-xxxx-xxxxxx
CLIENT_SECRET = xxxxxxx-xxxx-xxxx-xxxx-xxxxxx
USERNAME = "[email protected]"
PASSWORD = "Password123456"
crayon_api = CloudIQ(CLIENT_ID,CLIENT_SECRET,USERNAME,PASSWORD)
导入凭据的首选方法是通过ENV变量。
from os import getenv
from cloudiq import CloudIQ
CLIENT_ID = getenv('CLIENT_ID')
CLIENT_SECRET = getenv('CLIENT_SECRET')
USERNAME = getenv('CLOUDIQ_USER')
PASSWORD = getenv('CLOUDIQ_PW')
crayon_api = CloudIQ(CLIENT_ID,CLIENT_SECRET,USERNAME,PASSWORD)
如果使用容器和管道或通过Azure KeyVault等Secrets Manager,则可以使用各种方法设置ENV变量。使用bash在本地系统上设置它们,请运行以下命令:
export CLIENT_ID="xxxxxxx-xxxx-xxxx-xxxx-xxxxxx"
export CLIENT_SECRET="xxxxxxx-xxxx-xxxx-xxxx-xxxxxx"
export USERNAME="[email protected]"
export PASSWORD="Password123456"
另一种方法是使用包含凭据的config.ini文件并使用configparser模块检索它们。
import configparser
from cloudiq import CloudIQ
# Parse configuration file
try:
config = configparser.ConfigParser()
config.read('config.ini')
ID = config['CRAYON_API']['ID']
SECRET = config['CRAYON_API']['SECRET']
USER = config['CRAYON_API']['USER']
PASS = config['CRAYON_API']['PASS']
except configparser.Error:
print("Configuration Error...config.ini not found")
exit()
except KeyError:
print("Configuration Error...configuration not found in config.ini")
exit()
crayon_api = CloudIQ(CLIENT_ID,CLIENT_SECRET,USERNAME,PASSWORD)
使用ConfigParser,Env变量和Azure DevOps Pipelines,请参阅示例文件夹以获取身份验证演示
API返回的数据被保存到响应对象中(除了GetToken和ValidateToken除外)。响应对象包含诸如status_code,标题,cookie和API调用返回的文本之类的值。
要从响应中返回JSON数据,请使用response.json()类方法
返回状态代码使用响应。STATUS_CODE变量
所有成功的API调用返回200 OK , 201创建或204个没有内容
大多数错误响应还以JSON形式提供详细的错误消息
如果您收到500个错误,则数据模式有效负载很可能是问题所在。它可能格式不正确,也可能缺少所需字段。
记住在编写自动化时处理错误状态
response = crayon_api.me()
if(int(response.status_code) == 200):
# Handle JSON data
print(response.json())
else:
# Handle Error
print(response.status_code)
exit(1)
有关响应对象中字段的完整说明,请在以下链接中查看信息:
官方请求S.Response类文档:https://docs.python-requests.org/en/latest/api/#requests.response
W3学校:https://www.w3schools.com/python/ref_requests_response.asp
对API进行未经验证的测试ping
response = crayon_api.ping()
print(response,json())
获取有关当前身份验证的用户的信息
response = crayon_api.me()
print(response.json())
提出原始的请求:
# retrieves all products in the Azure Active Directory product family within Org 123456
params = {
'OrganizationId': 123456,
'Include.ProductFamilyNames': 'Azure Active Directory'
}
# make a GET request to https://api.crayon.com/api/v1/AgreementProducts
response = crayon_api.get("https://api.crayon.com/api/v1/AgreementProducts",params)
print(response.json())
数据可以作为标准Python词典对象发送到API
检索有效的授权令牌:
response = crayon_api.getToken()
print(response)
使用CustomErtenantDeTailed模式创建租户:
# Set Unique Tenant Variables
tenant_name = "tenant_name"
domain_prefix = "domain_prefix"
# Initialize Tenant and Agreement objects
tenant = crayon_api.CustomerTenantDetailed(
tenant_name=tenant_name,
domain_prefix=domain_prefix,
org_id=111111,
org_name="Fake Org",
invoice_profile_id=80408, # Default
contact_firstname="First",
contact_lastname="Last",
contact_email="[email protected]",
contact_phone="5555555555",
address_firstname="First",
address_lastname="Last",
address_address="75 NoWhere Lane",
address_city="Boston",
address_countrycode="US",
address_region="MA",
address_zipcode="02109"
)
agreement = crayon_api.CustomerTenantAgreement(
firstname="First",
lastname="Last",
phone_number="5555555555",
email="[email protected]"
)
#Create New Tenant
new_tenant = crayon_api.createTenant(tenant.tenant)
print(new_tenant.json())
# Agree to Microsoft Customer Agreement
tenant_id = new_tenant["Tenant"]["Id"]
agreement = crayon_api.createTenantAgreement(tenant_id,agreement.agreement)
print(agreement.json())
使用订阅架模式购买Microsoft许可证:
tenant_id=123456
# Create Subscription objects
azure_subscription = crayon_api.SubscriptionDetailed(
name="Azure P2 Subscription",
tenant_id=tenant_id,
part_number="CFQ7TTC0LFK5:0001",
quantity=1,
billing_cycle=1,
duration="P1M"
)
# Create Azure P2 Subscription
subscription = crayon_api.createSubscription(azure_subscription.subscription)
print(subscription.json)
from cloudiq import CloudIQ
help(CloudIQ)