CloudScheduler 中的 projects.locations.jobs.create 因 InvalidArgument 和 PermissionDenied 而失败
projects.locations.jobs.create in CloudScheduler fails with InvalidArgument and PermissionDenied
我目前正在尝试从 golang 创建 CloudScheduler 作业。
然而,它一直卡在一个错误中,我想知道如何修复它。
当前代码如下所示,运行 来自 CloudRun。
const projectID = "hogehoge-project"
const locationID = "asia-northeast1"
const jobBaseUrl = "https://example.com/notify"
func StartToCheckRunnning(jobID string) error {
ctx := context.Background()
cloudschedulerService, err := cloudscheduler.NewCloudSchedulerClient(ctx)
if err != nil {
log.Fatalf("cloudscheduler.NewCloudSchedulerClient: %v", err)
return fmt.Errorf("cloudscheduler.NewCloudSchedulerClient: %v", err)
}
defer cloudschedulerService.Close()
queuePath := fmt.Sprintf("projects/%s/locations/%s", projectID, locationID)
req := &schedulerpb.CreateJobRequest{
Parent: queuePath,
Job: &schedulerpb.Job{
Name: jobID,
Description: "managed by the system",
Target: &schedulerpb.Job_HttpTarget{
HttpTarget: &schedulerpb.HttpTarget{
Uri: createJobUrl(jobBaseUrl, jobID),
HttpMethod: schedulerpb.HttpMethod_POST,
},
},
Schedule: "* * * * *",
TimeZone: "jst",
},
}
resp, err := cloudschedulerService.CreateJob(ctx, req)
if err != nil {
log.Fatalf("cloudschedulerService.CreateJob: %v", err)
return fmt.Errorf("cloudschedulerService.CreateJob: %v", err)
}
// TODO: Use resp.
_ = resp
return nil
}
当我运行它时,我会得到以下错误。
cloudschedulerService.CreateJob: rpc error: code = InvalidArgument desc = Job name must be formatted: "projects/<PROJECT_ID>/locations/<LOCATION_ID>/jobs/<JOB_ID>".
但是,当我将 queuePath 更改为以下内容时,我现在收到以下错误。
queuePath := fmt.Sprintf("projects/%s/locations/%s/jobs/%s", projectID, locationID, jobID)
cloudschedulerService.CreateJob: rpc error: code = PermissionDenied desc = The principal (user or service account) lacks IAM permission "cloudscheduler.jobs.create" for the resource "projects/hogehoge-project/locations/asia-northeast1/jobs/029321cb-467f-491e-852e-0c3df3d49db3" (or the resource may not exist).
既然我用的是CloudRun默认服务账号,应该不会缺少权限吧
顺便说一句,这是用这种格式写的:projects/PROJECT_ID/locations/LOCATION_ID
。
https://pkg.go.dev/google.golang.org/genproto/googleapis/cloud/scheduler/v1beta1#CreateJobRequest
如何正确执行创建请求?
谢谢。
我用这段代码解决了自己。
这是作业名称错误,而不是父名称。
queuePath := fmt.Sprintf("projects/%s/locations/%s", projectID, locationID)
namePath := fmt.Sprintf("projects/%s/locations/%s/jobs/%s", projectID, locationID, jobID)
req := &schedulerpb.CreateJobRequest{
Parent: queuePath,
Job: &schedulerpb.Job{
Name: namePath,
Description: "managed by system",
Target: &schedulerpb.Job_HttpTarget{
HttpTarget: &schedulerpb.HttpTarget{
Uri: createJobUrl(jobBaseUrl, jobID),
HttpMethod: schedulerpb.HttpMethod_POST,
},
},
Schedule: "* * * * *",
TimeZone: "Asia/Tokyo",
},
}
我目前正在尝试从 golang 创建 CloudScheduler 作业。
然而,它一直卡在一个错误中,我想知道如何修复它。
当前代码如下所示,运行 来自 CloudRun。
const projectID = "hogehoge-project"
const locationID = "asia-northeast1"
const jobBaseUrl = "https://example.com/notify"
func StartToCheckRunnning(jobID string) error {
ctx := context.Background()
cloudschedulerService, err := cloudscheduler.NewCloudSchedulerClient(ctx)
if err != nil {
log.Fatalf("cloudscheduler.NewCloudSchedulerClient: %v", err)
return fmt.Errorf("cloudscheduler.NewCloudSchedulerClient: %v", err)
}
defer cloudschedulerService.Close()
queuePath := fmt.Sprintf("projects/%s/locations/%s", projectID, locationID)
req := &schedulerpb.CreateJobRequest{
Parent: queuePath,
Job: &schedulerpb.Job{
Name: jobID,
Description: "managed by the system",
Target: &schedulerpb.Job_HttpTarget{
HttpTarget: &schedulerpb.HttpTarget{
Uri: createJobUrl(jobBaseUrl, jobID),
HttpMethod: schedulerpb.HttpMethod_POST,
},
},
Schedule: "* * * * *",
TimeZone: "jst",
},
}
resp, err := cloudschedulerService.CreateJob(ctx, req)
if err != nil {
log.Fatalf("cloudschedulerService.CreateJob: %v", err)
return fmt.Errorf("cloudschedulerService.CreateJob: %v", err)
}
// TODO: Use resp.
_ = resp
return nil
}
当我运行它时,我会得到以下错误。
cloudschedulerService.CreateJob: rpc error: code = InvalidArgument desc = Job name must be formatted: "projects/<PROJECT_ID>/locations/<LOCATION_ID>/jobs/<JOB_ID>".
但是,当我将 queuePath 更改为以下内容时,我现在收到以下错误。
queuePath := fmt.Sprintf("projects/%s/locations/%s/jobs/%s", projectID, locationID, jobID)
cloudschedulerService.CreateJob: rpc error: code = PermissionDenied desc = The principal (user or service account) lacks IAM permission "cloudscheduler.jobs.create" for the resource "projects/hogehoge-project/locations/asia-northeast1/jobs/029321cb-467f-491e-852e-0c3df3d49db3" (or the resource may not exist).
既然我用的是CloudRun默认服务账号,应该不会缺少权限吧
顺便说一句,这是用这种格式写的:projects/PROJECT_ID/locations/LOCATION_ID
。
https://pkg.go.dev/google.golang.org/genproto/googleapis/cloud/scheduler/v1beta1#CreateJobRequest
如何正确执行创建请求? 谢谢。
我用这段代码解决了自己。 这是作业名称错误,而不是父名称。
queuePath := fmt.Sprintf("projects/%s/locations/%s", projectID, locationID)
namePath := fmt.Sprintf("projects/%s/locations/%s/jobs/%s", projectID, locationID, jobID)
req := &schedulerpb.CreateJobRequest{
Parent: queuePath,
Job: &schedulerpb.Job{
Name: namePath,
Description: "managed by system",
Target: &schedulerpb.Job_HttpTarget{
HttpTarget: &schedulerpb.HttpTarget{
Uri: createJobUrl(jobBaseUrl, jobID),
HttpMethod: schedulerpb.HttpMethod_POST,
},
},
Schedule: "* * * * *",
TimeZone: "Asia/Tokyo",
},
}