You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Language runtime version: go version go1.21.0 darwin/arm64
Package version: 0.142.0
Steps to reproduce
I am trying to enable certain APIs for my project
Below is the code
funcenableRequiredAPIS(projectNamestring) error {
ctx:=context.Background()
projectParentName:=fmt.Sprintf("projects/%s", projectName)
serviceusageServiceClient, err:=serviceusage.NewService(ctx)
iferr!=nil {
returnfmt.Errorf("enableRequiredAPIS: Error getting serviceusage.NewService type: %w", err)
}
serviceUsageServicesServiceClient:=serviceusage.NewServicesService(serviceusageServiceClient)
// get project informationprojectBatchGet:=serviceUsageServicesServiceClient.BatchGet(projectParentName)
// all the required services we want to check and enableallServices:= []string{
fmt.Sprintf("%s/services/compute.googleapis.com", projectParentName),
fmt.Sprintf("%s/services/container.googleapis.com", projectParentName),
fmt.Sprintf("%s/services/secretmanager.googleapis.com", projectParentName),
fmt.Sprintf("%s/services/artifactregistry.googleapis.com", projectParentName),
}
// assigning service namesprojectBatchGet.Names(allServices...)
resp, err:=projectBatchGet.Do()
iferr!=nil {
returnfmt.Errorf("enableRequiredAPIS: Error getting batch GET for services %w", err)
}
varserviceToEnable []stringfor_, service:=rangeresp.Services {
ifservice.State=="DISABLED" {
serviceName:=strings.Replace(service.Name, fmt.Sprintf("%s/services/", service.Parent), "", 1)
utils.PrintInfoln(fmt.Sprintf("service %s not enabled for project %s", serviceName, projectName))
serviceToEnable=append(serviceToEnable, serviceName)
}
}
iflen(serviceToEnable) !=0 {
enableOperation, err:=serviceUsageServicesServiceClient.BatchEnable(projectParentName, &serviceusage.BatchEnableServicesRequest{
ServiceIds: serviceToEnable,
}).Do()
iferr!=nil {
returnfmt.Errorf("enableRequiredAPIS: Error enabling services %w", err)
}
for {
fmt.Println("Waiting for the services to get enabled...")
fmt.Println("done", enableOperation.Done)
fmt.Println("statuscode", enableOperation.ServerResponse.HTTPStatusCode)
ifenableOperation.Done {
fmt.Println("Successfully enabled all the services")
returnnil
} elseifopErr:=enableOperation.Error; opErr!=nil {
fmt.Println(fmt.Sprintf("Failed to enable services. error: %s %d", opErr.Message, opErr.Code))
returnfmt.Errorf("enableRequiredAPIS: error enabling services, error %s %d", opErr.Message, opErr.Code)
} else {
fmt.Println(string(enableOperation.Response))
time.Sleep(3*time.Second)
}
}
}
returnnil
}
When I am trying to execute the code to enable the APIs which are disabled, the for loop is running indefinitely.
When I check the API from the console it gets enabled after certain time which is the ideal behaviour.
The operation should return target and not ret, but I am not perfectly sure of this.
What should happen
I should either get an error or operation.Done should be marked as true
The text was updated successfully, but these errors were encountered:
dunefro
added
priority: p2
Moderately-important priority. Fix may not be included in next release.
type: bug
Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
labels
Sep 22, 2023
noahdietz
added
type: question
Request for information or clarification. Not an issue.
and removed
priority: p2
Moderately-important priority. Fix may not be included in next release.
type: bug
Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
labels
Sep 25, 2023
Hello @dunefro, thanks for the issue. I don't think there is a bug in the client, but rather in the code that you've shared.
The *Operation returned by the ServicesBatchEnableCall.Do does not automatically poll the Operation. So, the code shared above is checking the same Operation from the initial response, thus looping forever.
Instead, you need to include a polling request via OperationsService.Get in your for loop after your time.Sleep call to refresh the local Operation state.
operations=NewOperationsService(serviceusageServiceClient)
// ...for {
// ...
} else {
fmt.Println(string(enableOperation.Response))
time.Sleep(3*time.Second)
// Poll the operation state, loop again to same operation evaluation logic.enableOperation, err=operations.Get(enableOperation.Name).Do()
iferr!=nil {
fmt.Println("error polling operation:", err)
returnerr
}
}
}
I can't find a section in our top-level of docs that explain this, so I will add one.
Please close this if it works for you or I will close it in a few days as resolved.
Environment details
go version go1.21.0 darwin/arm64
Steps to reproduce
I am trying to enable certain APIs for my project
for
loop is running indefinitely.target
and notret
, but I am not perfectly sure of this.What should happen
I should either get an error or
operation.Done
should be marked astrue
The text was updated successfully, but these errors were encountered: