如何使用 Google Cloud 中的 python 添加人员到此项目?
How to ADD PEOPLE TO THIS PROJECT using python in Google Cloud?
我正在使用来自 Snowflake 的 this 文档在 Jupyter Notebook 中构建一个管道模板,并且已经到达了文档声明将人员添加到该项目 的部分。
我已经手动构建了 Pipeline 并开始工作,但我需要将它放在一个易于部署到其他实例上的模板中。我在 snowflake 中创建了通知集成,并使用 desc notification integration my_int
获取了我的服务帐户。我需要为此服务帐户分配 Monitoring Viewer 角色,但找不到允许我这样做的库。
编辑(感谢@erhard):
我意识到我在 discovery.build
中使用了不同的 serviceName
。
这是工作代码:
from googleapiclient import discovery
gccreds = json.loads(open('gccreds.json', 'rt').read())
credentials = service_account.Credentials.from_service_account_info(gccreds)
crm_service = discovery.build(
serviceName='cloudresourcemanager',
version='v1',
credentials=credentials)
gcp_pubsub_service_account = '<VALUE FROM SNOWFLAKE>'
policy = (
crm_service.projects()
.getIamPolicy(
resource=project_id
)
.execute()
)
_ = [x for x in policy["bindings"] if f"serviceAccount:{gcp_pubsub_service_account}" in x["members"]] == []
if _:
binding = {"role": "roles/monitoring.viewer", "members": [f"serviceAccount:{gcp_pubsub_service_account}"]}
policy["bindings"].append(binding)
print(f"Added '{gcp_pubsub_service_account}' with role 'roles/monitoring.viewer' to project")
else:
print(f"'{gcp_pubsub_service_account}' with role 'roles/monitoring.viewer' to project already exists.")
我正在使用来自 Snowflake 的 this 文档在 Jupyter Notebook 中构建一个管道模板,并且已经到达了文档声明将人员添加到该项目 的部分。
我已经手动构建了 Pipeline 并开始工作,但我需要将它放在一个易于部署到其他实例上的模板中。我在 snowflake 中创建了通知集成,并使用 desc notification integration my_int
获取了我的服务帐户。我需要为此服务帐户分配 Monitoring Viewer 角色,但找不到允许我这样做的库。
编辑(感谢@erhard):
我意识到我在 discovery.build
中使用了不同的 serviceName
。
这是工作代码:
from googleapiclient import discovery
gccreds = json.loads(open('gccreds.json', 'rt').read())
credentials = service_account.Credentials.from_service_account_info(gccreds)
crm_service = discovery.build(
serviceName='cloudresourcemanager',
version='v1',
credentials=credentials)
gcp_pubsub_service_account = '<VALUE FROM SNOWFLAKE>'
policy = (
crm_service.projects()
.getIamPolicy(
resource=project_id
)
.execute()
)
_ = [x for x in policy["bindings"] if f"serviceAccount:{gcp_pubsub_service_account}" in x["members"]] == []
if _:
binding = {"role": "roles/monitoring.viewer", "members": [f"serviceAccount:{gcp_pubsub_service_account}"]}
policy["bindings"].append(binding)
print(f"Added '{gcp_pubsub_service_account}' with role 'roles/monitoring.viewer' to project")
else:
print(f"'{gcp_pubsub_service_account}' with role 'roles/monitoring.viewer' to project already exists.")