[英]Error: sink() got an unexpected keyword argument ‘parent’
I am trying to create an aggregated sink at organization level in GCP using python, Not sure what's wrong, getting an error sink() got an unexpected keyword 'parent'我正在尝试使用 python 在 GCP 中的组织级别创建聚合接收器,不确定出了什么问题,出现错误 sink() 得到了意外的关键字“parent”
I am passing parent = organizations/orgid我正在传递 parent = organizations/orgid
Below is the code下面是代码
import requests
import json
import re
import sys
import subprocess
import os
from googleapiclient import discovery
from google.cloud import logging
from google.oauth2 import service_account
from oauth2client.client import GoogleCredentials
from datetime import date, timedelta
from google.cloud import pubsub_v1
credentials = service_account.Credentials.from_service_account_file("")
filterstring = "protoPayload.methodName=google.cloud.orgpolicy.v2.OrgPolicy.CreatePolicy OR protoPayload.methodName=google.cloud.orgpolicy.v2.OrgPolicy.UpdatePolicy OR protoPayload.methodName=google.cloud.orgpolicy.v2.OrgPolicy.DeletePolicy"
orgid = ""
logging_client = logging.Client()
projectid = ""
publisher = pubsub_v1.PublisherClient()
def create_topic(projectid, topicid):
try:
topicpath = publisher.topic_path(projectid, topicid)
topic = publisher.create_topic(request={"name": topicpath})
print(f"Created topic: {topic.name}")
except Exception as e:
pass
def create_sink(sink_name, topicid, filter_, orgid):
destination = "pubsub.googleapis.com/projects/{}/topics/{}".format(projectid, topicid)
sink = logging_client.sink(sink_name, filter_=filter_, parent="organizations/orgid", destination=destination)
if sink.exists():
print("Sink {} already exists.".format(sink.name))
else:
sink.create()
print("Created sink {}".format(sink.name))
# def publishing_message(projectid, topicid):
# publisher = pubsub_v1.PublisherClient()
# topic_path = publisher.topic_path(projectid, topicid)
# for n in range(1, 10):
# data_str = f"Message number {n}"
# data = data_str.encode("utf-8")
# future = publisher.publish(topic_path, data)
# print(future.result())
# print(f"Published messages to {topic_path}.")
if __name__ == "__main__":
sink_name = "newsink"
filter_ = filterstring
topicid = "newtopic"
create_topic(projectid, topicid)
create_sink(sink_name, topicid, filter_, orgid)
sinks = list(logging_client.list_sinks())
for sink in sinks:
print(dir(sink))
Any idea what's wrong in the code.. want to create a sink at org level..知道代码中有什么问题..想在组织级别创建一个接收器..
Here's a working example:这是一个工作示例:
main.py
: main.py
:
from google.cloud import logging
from os import getenv
name = getenv("NAME")
bucket = getenv("BUCKET")
org = getenv("ORG")
parent = f"organizations/{org}"
destination = f"storage.googleapis.com/{bucket}"
# Uses Google Application (Default) Credentials
# Service Account "sinkator" has Org roles/logging.admin
client = logging.Client()
sink = logging.Sink(
name,
parent=parent,
destination=destination,
client=client)
sink.create()
print(f"{name} {'DOES' if sink.exists() else 'NOT'} exist")
Then:然后:
gcloud logging sinks describe ${NAME} \
--organization=${ORG} \
--format="value(createTime)"
Yields:产量:
2022-04-22T00:00:00.000000000Z
With:和:
Q="71970259"
PROJECT=$(whoami)-$(date +%y%m%d)-${Q}
BUCKET=${PROJECT}
ACCOUNT="sinkator"
NAME="Freddie"
ORG=$(gcloud organizations list \
--format="value(name)") && echo ${ORG}
gcloud project create ${PROJECT}
EMAIL=${ACCOUNT}@${PROJECT}.iam.gserviceaccount.com
gcloud iam service-accounts create ${ACCOUNT} \
--project=${PROJECT}
gcloud iam service-accounts keys create ${PWD}/${ACCOUNT}.json \
--iam-account=${EMAIL} \
--project=${PROJECT}
# Add Service Account to the Organizational IAM
gcloud organizations add-iam-policy-binding ${ORG} \
--member=serviceAccount:${EMAIL} \
--role=roles/logging.admin
export GOOGLE_APPLICATION_CREDENTIALS=${PWD}/${ACCOUNT}.json
export ORG
export BUCKET
export NAME
python3 main.py
Yields:产量:
Freddie DOES exist
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.