使用Cloudcraft API渲染AWS和Azure图表的Python SDK。
项目描述
Cloudcraft API客户端
使用Datadog的Cloudcraft可视化您的云架构, 创建智能AWS和Azure图表的最佳方式。
Cloudcraft支持手动和程序化图表绘制,以及将现有云环境自动反向工程成漂亮的系统架构图。
此cloudcraftco
Python库提供了一种易于使用的本地Python SDK,用于与Cloudcraft API交互。
用例示例
- 在部署前后,在您的应用程序或自动化CI管道中,快照并可视化您的实时AWS或Azure环境
- 从链接的帐户下载所有云资源的清单作为JSON
- 编写从第三方数据格式到Cloudcraft图表的转换器
- 备份、导出和导入您的Cloudcraft数据
- 以编程方式创建Cloudcraft图表
此SDK使用时需要Cloudcraft API密钥。提供具有API访问权限的Cloudcraft Pro免费试用版。
要求
- Python 3.10
- Requests 2.28
安装
python3.10 -m pip install cloudcraftco
用法
通过Cloudcraft
类访问API。实例化Cloudcraft
时需要通过Cloudcraft用户界面提供的API密钥。它可以作为参数传递给类,或者通过CLOUDCRAFT_API_KEY
环境变量
from cloudcraftco.cloudcraft import Cloudcraft
# assumes CLOUDCRAFT_API_KEY exported
cloudcraft = Cloudcraft()
profile = cloudcraft.read_user_profile()
配置
使用配置对象初始化
该软件包可以初始化为几种选项
from cloudcraftco.cloudcraft import Cloudcraft
cloudcraft = Cloudcraft({"api_key": "api-key-value", "timeout": 30000})
api_key
必须通过配置对象或环境变量提供。
选项 | 默认值 | 描述 |
---|---|---|
api_key |
与Cloudcraft账户关联的API密钥 | |
maxNetworkRetries |
10 | 请求应该重试的次数 |
timeout |
80000 | 每个请求可以消耗的最大时间(毫秒) |
host |
'api.cloudcraft.co' |
请求的目标主机 |
port |
443 | 请求的目标端口 |
protocol |
'https' |
'https' 或'http' |
也可以通过环境变量指定选项...
选项 | 环境变量 |
---|---|
api_key |
CLOUDCRAFT_API_KEY |
maxNetworkRetries |
CLOUDCRAFT_MAX_NETWORK_RETRIES |
timeout |
CLOUDCRAFT_TIMEOUT |
host |
CLOUDCRAFT_HOST |
port |
CLOUDCRAFT_PORT |
protocol |
CLOUDCRAFT_PROTOCOL |
蓝图
列出蓝图
from cloudcraftco.cloudcraft import Cloudcraft
# assumes CLOUDCRAFT_API_KEY exported
cloudcraft = Cloudcraft()
blueprints = cloudcraft.list_blueprints()
检索蓝图
from cloudcraftco.cloudcraft import Cloudcraft
# assumes CLOUDCRAFT_API_KEY exported
cloudcraft = Cloudcraft()
blueprint_id = "BLUEPRINT-ID" # valid blueprint uuid
blueprint = cloudcraft.read_blueprint(blueprint_id)
创建蓝图
from cloudcraftco.cloudcraft import Cloudcraft
# assumes CLOUDCRAFT_API_KEY exported
cloudcraft = Cloudcraft()
data = {"data": {"grid": "standard", "name": "New blueprint"}}
blueprint = cloudcraft.create_blueprint(data)
更新蓝图
from cloudcraftco.cloudcraft import Cloudcraft
# assumes CLOUDCRAFT_API_KEY exported
cloudcraft = Cloudcraft()
blueprint_id = "BLUEPRINT-ID" # valid blueprint uuid
data = {"data": {"grid": "standard", "name": "Updated blueprint"}}
cloudcraft.update_blueprint(blueprint_id, data)
删除蓝图
from cloudcraftco.cloudcraft import Cloudcraft
# assumes CLOUDCRAFT_API_KEY exported
cloudcraft = Cloudcraft()
blueprint_id = "BLUEPRINT-ID" # valid blueprint uuid
cloudcraft.delete_blueprint(blueprint_id)
将蓝图导出为图片
from cloudcraftco.cloudcraft import Cloudcraft
# assumes CLOUDCRAFT_API_KEY exported
cloudcraft = Cloudcraft()
script_dir = os.path.dirname(os.path.realpath(__file__)) + os.sep
bp_id = "BLUEPRINT-ID" # valid blueprint uuid
bp_format = "svg"
bp_file = script_dir + bp_id + "." + bp_format
export = cloudcraft.export_blueprint(bp_id, bp_format)
with open(bp_file, "wb") as binary_file:
binary_file.write(export)
AWS账户
添加AWS账户
from cloudcraftco.cloudcraft import Cloudcraft
# assumes CLOUDCRAFT_API_KEY exported
cloudcraft = Cloudcraft()
# role must exist and match your api_key/account
role = "arn:aws:iam::{}:role/cloudcraft".format(aws_account_id)
data = {"name": "New AWS Account", "roleArn": role}
result = cloudcraft.create_aws_account(data)
列出AWS账户
from cloudcraftco.cloudcraft import Cloudcraft
# assumes CLOUDCRAFT_API_KEY exported
cloudcraft = Cloudcraft()
accounts = cloudcraft.list_aws_accounts()
更新AWS账户
from cloudcraftco.cloudcraft import Cloudcraft
# assumes CLOUDCRAFT_API_KEY exported
cloudcraft = Cloudcraft()
account_id = "AWS-ACCOUNT" # valid account uuid for api-key
role = "AWS-ROLE" # valid role for AWS Account
data = {"name": "Updated Playground AWS Account", "roleArn": role}
result = cloudcraft.update_aws_account(account_id, data)
删除AWS账户
from cloudcraftco.cloudcraft import Cloudcraft
# assumes CLOUDCRAFT_API_KEY exported
cloudcraft = Cloudcraft()
account_id = "AWS-ACCOUNT" # valid account uuid for api-key
cloudcraft.delete_aws_account(account_id)
获取我的AWS IAM角色参数
from cloudcraftco.cloudcraft import Cloudcraft
# assumes CLOUDCRAFT_API_KEY exported
cloudcraft = Cloudcraft()
iam_parameters = cloudcraft.read_aws_role_parameters()
快照AWS账户
from cloudcraftco.cloudcraft import Cloudcraft
# assumes CLOUDCRAFT_API_KEY exported
cloudcraft = Cloudcraft()
script_dir = os.path.dirname(os.path.realpath(__file__)) + os.sep
ss_account = "AWS-ACCOUNT" # valid account uuid for api-key
ss_region = "us-west-2"
ss_format = "png"
ss_file = script_dir + ss_region + "." + ss_format
snapshot = cloudcraft.snapshot_aws_account(ss_account, ss_region, ss_format)
with open(ss_file, "wb") as binary_file:
binary_file.write(snapshot)
Azure账户
添加Azure账户
from cloudcraftco.cloudcraft import Cloudcraft
# assumes CLOUDCRAFT_API_KEY exported
cloudcraft = Cloudcraft()
# id and secret values must be valid
data = {
"name": "Azure Account",
"subscriptionId": "subscriptionId",
"directoryId": "directoryId",
"applicationId": "applicationId",
"clientSecret": "clientSecret"
}
result = cloudcraft.create_azure_account(data)
列出Azure账户
from cloudcraftco.cloudcraft import Cloudcraft
# assumes CLOUDCRAFT_API_KEY exported
cloudcraft = Cloudcraft()
accounts = cloudcraft.list_azure_accounts()
更新Azure账户
from cloudcraftco.cloudcraft import Cloudcraft
# assumes CLOUDCRAFT_API_KEY exported
cloudcraft = Cloudcraft()
account_id = "AZURE-ACCOUNT" # valid account uuid for api-key
data = {
"name": "Updated Azure Account",
"subscriptionId": "subscriptionId",
"directoryId": "directoryId",
"applicationId": "applicationId",
}
result = cloudcraft.update_azure_account(account_id, data)
删除Azure账户
from cloudcraftco.cloudcraft import Cloudcraft
# assumes CLOUDCRAFT_API_KEY exported
cloudcraft = Cloudcraft()
account_id = "AZURE-ACCOUNT" # valid account uuid for api-key
cloudcraft.delete_azure_account(account_id)
快照Azure账户
from cloudcraftco.cloudcraft import Cloudcraft
# assumes CLOUDCRAFT_API_KEY exported
cloudcraft = Cloudcraft()
script_dir = os.path.dirname(os.path.realpath(__file__)) + os.sep
ss_account = "AZURE-ACCOUNT" # valid account uuid for api-key
ss_location = "canadaeast"
ss_format = "png"
ss_file = script_dir + ss_location + "." + ss_format
snapshot = cloudcraft.snapshot_azure_account(ss_account, ss_location, ss_format)
with open(ss_file, "wb") as binary_file:
binary_file.write(snapshot)
预算
导出蓝图预算
from cloudcraftco.cloudcraft import Cloudcraft
# assumes CLOUDCRAFT_API_KEY exported
cloudcraft = Cloudcraft()
script_dir = os.path.dirname(os.path.realpath(__file__)) + os.sep
bp_id = "BLUEPRINT-ID" # valid blueprint uuid
bp_format = "csv"
bp_file = script_dir + bp_id + "." + bp_format
export = cloudcraft.export_blueprint_budget(bp_id, bp_format)
with open(bp_file, "wb") as binary_file:
binary_file.write(export)
用户
获取Cloudcraft账户信息
from cloudcraftco.cloudcraft import Cloudcraft
# assumes CLOUDCRAFT_API_KEY exported
cloudcraft = Cloudcraft()
profile = cloudcraft.read_user_profile()
更多信息
开发
宿主环境
cloudcraft-python
使用以下工具开发...
- Python 3.7.15
- Poetry 1.1.14
- Tox 3.25.1
宿主环境为macOS 12,但其他环境也应该可以工作。
早期版本可能也可以工作,但Python 3.10是最小支持的版本。
运行Playground(示例)
运行源代码以展示Cloudcraft API如何工作的开发示例。
测试账户需要小心,因为创建账户需要有效的角色。
% cd {repo-directory}
% poetry env use python3.10
% poetry shell
% poetry install
% export CLOUDCRAFT_API_KEY={{ api-key }}
% python3 dev_playgrounds/blueprints.py
% python3 dev_playgrounds/budgets.py
% python3 dev_playgrounds/exports.py
% python3 dev_playgrounds/snapshots_aws.py
% python3 dev_playgrounds/snapshots_azure.py
% python3 dev_playgrounds/users.py
% export CLOUDCRAFT_TEST_ROLE={{ your-role-arn }}
% python3 dev_playgrounds/accounts_aws.py
% export CLOUDCRAFT_TEST_SUBSCRIPTION={{ your-subscription-id }}
% export CLOUDCRAFT_TEST_DIRECTORY={{ your-directory-id }}
% export CLOUDCRAFT_TEST_APPLICATION={{ your-application-id }}
% export CLOUDCRAFT_TEST_SECRET={{ your-client-secret }}
% python3 dev_playgrounds/accounts_azure.py
运行测试
% poetry run pytest tests/unit
% poetry run pytest tests/functional
% tox
格式化代码
% poetry run isort . --profile black
% poetry run black .
检查测试覆盖率
% poetry run coverage run --source=cloudcraftco --branch -m pytest .
% poetry run coverage html
项目详情
下载文件
下载适用于您平台的自定义文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。
源分布
cloudcraftco-1.1.0.tar.gz (13.6 KB 查看哈希值)
构建分布
cloudcraftco-1.1.0-py3-none-any.whl (12.7 KB 查看哈希值)