このプロジェクトは、Pythonスクリプトやアプリケーションで使用できるCrayonのCloudiq APIのSDKです。 OAUTH2を使用してAPIで認証するための簡単なインターフェイスを提供します。テナントの作成、ライセンスサブスクリプションの作成、請求を監視するために使用できます。 Cloud-IQポータルで実行できるものはすべて、このパッケージを使用して自動化できます。
いくつかの事前に構成されたデータスキーマおよびAPIメソッドが含まれています。データのカスタムブロックは、Python辞書としてAPIに投稿できます。 RESTメソッド: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)
ENV変数は、コンテナやパイプラインを使用する場合、またはAzure KeyVaultなどのシークレットマネージャーを介して注入などのさまざまな方法を使用して設定できます。 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パイプラインを使用した認証デモの例フォルダーを参照してください
APIによって返されるデータは、応答オブジェクトに保存されます(GetTokenおよびValidAteTokenを除く)。応答オブジェクトには、Status_Code、ヘッダー、Cookie、API呼び出しによって返されるテキストなどの値が含まれます。
応答からJSONデータを返すには、response.json()クラスメソッドを使用します
ステータスコードを返すには、respons.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)
応答オブジェクト内のフィールドの完全な説明については、次のリンクの情報を確認してください。
公式リクエスト。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())
RAW GETリクエストを作成します:
# 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())
SubsionDetailed Schemaを使用して、テナントの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)