使用 Schematics API 更新变量
(1)要使用 IBM Cloud® Schematics Python API 更新变量,请创建两个 Python 文件,并为这些文件提供您选择的名称,例如 schematics_variables_update.py 和 schematics_env_class.py。
(2)将 schematics_variables_update.py 和 schematics_env_class.py Python 示例代码请求复制并粘贴到相应的 Python 文件中。
(3)将 config.json 模板文件 复制并粘贴到 JSON 文件,例如 config.json。
(4)在请求中更改以下参数:
·提供在以下两个函数中生成的 workspace ID w_id :
schematic_obj.get_workspace(w_id="
1)确保更新 config.json 文件中的必需参数,例如 api_key, ibm_customer_number, remote_allowed_ips, ssh_key_name和 zone 。
2)使用 python3 运行 Python 脚本,以更新 IBM Cloud中 Schematics 工作空间中的变量。
·注: 在 config.json 中可能不需要以下参数,因为 Schematics 更新变量 API 使用 workspace ID w_id 针对该工作空间更新变量。
{
"name": "Schematic Dev Workspace",
"type": [
"terraform_v0.13.7"
],
"location": "us-south",
"description": "Schematic Dev Workspace",
"tags": [],
"template_repo": {
"url": "<GitHub repo URL>",
"githubtoken": "<github-token>"
}
针对 schematics_variables_update.py 文件的示例 Python 请求
以下 Python 示例请求用于示例文件 schematics_variables_update.py。
import logging, os, json
logging.basicConfig()
logging.root.setLevel(logging.NOTSET)
logging.basicConfig(level=logging.NOTSET)
from schematics_env_class import HPCCEnvironmentValues
logging.info("Schematic Variable Update Started")if __name__ == '__main__':
json_files = os.path.join(os.path.abspath("."), "config.json")
with open(json_files, "r") as file:
config_data = json.load(file)
api_key = config_data["template_data"][0]["variablestore"][1]["value"]
schematic_obj = HPCCEnvironmentValues(api_key)
workspace_response =
schematic_obj.schematics_service.get_workspace(w_id="<workspace id>").get_result()
schematic_obj.update_variables(w_id="<workspace id>",
t_id=workspace_response["template_data"][0]["id"],
variablestore=config_data["template_data"][0]["variablestore"]
)
logging.info("Schematic Variable Update Completed")
针对 schematics_env_class.py 文件的示例 Python 请求
import json
import logging
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
from ibm_schematics import SchematicsV1class HPCCEnvironmentValues:
def __init__(self, api_key):
"""
create schematic services
""" self.authenticator = IAMAuthenticator(api_key)
self.schematics_service = SchematicsV1(authenticator=self.authenticator)
self.schematics_service.set_service_url('https://us.schematics.cloud.ibm.com')
logging.info("Schematic Services created")
def update_variables(self, w_id, t_id, variablestore):
print("Function Schematic Update Variables Started")
variables_update_result =
self.schematics_service.replace_workspace_inputs(w_id=w_id,
t_id=t_id, variablestore=variablestore
).get_result()
return variables_update_result
config.json 模板文件
在公共 GitHub 存储库中使用此模板文件: config.json
示例 Python 响应
INFO:root:Schematic Variable Update Started
INFO:root:Schematic Services created
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): iam.cloud.ibm.com:443
DEBUG:urllib3.connectionpool:https://iam.cloud.ibm.com:443 "POST /identity/token HTTP/1.1" 200 983
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): us.schematics.cloud.ibm.com:443
DEBUG:urllib3.connectionpool:https://us.schematics.cloud.ibm.com:443
"GET /v1/workspaces/us-south.workspace.Schematic-Sunil-Test-Workspace.5a4cbf11 HTTP/1.1" 200 None
Function Schematic Update Variables Started
DEBUG:urllib3.connectionpool:https://us.schematics.cloud.ibm.com:443
"PUT /v1/workspaces/us-south.workspace.Schematic-Sunil-Test-
Workspace.5a4cbf11/template_data/b80ea875-360c-4c/values HTTP/1.1" 200 None
INFO:root:Schematic Variable Update Completed