該項目是可以在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)