I am currently working on a POC, creating the labels for instance with respect to a particular zone. This code should create labels for the instance.Here we are using a method setLabelsAsync()
for creating labels.
I am getting Invalid Arguments Exception and the code is not working as expected.
Java code
public void addLabelsToInstance(String projectId, String zone,String instanceName,String labelKey, String labelVal) throws IOException, ApiException {
try (InstancesClient instancesClient = InstancesClient.create()) {
Instance instance = instancesClient.get(projectId, zone, instanceName);
// Add the label to the current instance
instance = instance.toBuilder().putLabels(labelKey, labelVal).build();
int labelsCount = instance.getLabelsCount();
Map<String, String> labelsMap = instance.getLabelsMap();
SetLabelsInstanceRequest request = SetLabelsInstanceRequest.newBuilder().setProject(projectId).setZone(zone).build();
// Update the labels for the instance
ApiFuture<Operation> operationFuture = instancesClient.setLabelsAsync(request).getMetadata();
Operation operation = operationFuture.get();
if (operation.hasError()) {
System.err.println("Error updating instance labels: " + operation.getError());
} else {
System.out.println("Instance labels updated successfully.");
}
// Print the updated instance with labels
System.out.println("Labels Count :"+labelsCount);
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
}
Exception StackTrace
Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.RuntimeException: java.util.concurrent.ExecutionException: com.google.api.gax.rpc.InvalidArgumentException: Bad Request
at com.technotack.GCPHealthCheck.addLabelsToInstance(GCPHealthCheck.java:486)
at com.technotack.GcpHealthCheckApplication.main(GcpHealthCheckApplication.java:47)
... 5 more
Caused by: java.util.concurrent.ExecutionException: com.google.api.gax.rpc.InvalidArgumentException: Bad Request
at com.google.common.util.concurrent.AbstractFuture.getDoneValue(AbstractFuture.java:588)
at com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:567)
at com.google.common.util.concurrent.FluentFuture$TrustedFuture.get(FluentFuture.java:92)
at com.google.common.util.concurrent.ForwardingFuture.get(ForwardingFuture.java:66)
at com.technotack.GCPHealthCheck.addLabelsToInstance(GCPHealthCheck.java:476)
... 6 more
Caused by: com.google.api.gax.rpc.InvalidArgumentException: Bad Request
at com.google.api.gax.rpc.ApiExceptionFactory.createException(ApiExceptionFactory.java:87)
at com.google.api.gax.httpjson.HttpJsonApiExceptionFactory.createApiException(HttpJsonApiExceptionFactory.java:76)
at com.google.api.gax.httpjson.HttpJsonApiExceptionFactory.create(HttpJsonApiExceptionFactory.java:54)
at com.google.api.gax.httpjson.HttpJsonExceptionCallable$ExceptionTransformingFuture.onFailure(HttpJsonExceptionCallable.java:97)
at com.google.api.core.ApiFutures$1.onFailure(ApiFutures.java:68)
at com.google.common.util.concurrent.Futures$CallbackListener.run(Futures.java:1133)
at com.google.common.util.concurrent.DirectExecutor.execute(DirectExecutor.java:31)
at com.google.common.util.concurrent.AbstractFuture.executeListener(AbstractFuture.java:1277)
at com.google.common.util.concurrent.AbstractFuture.complete(AbstractFuture.java:1038)
at com.google.common.util.concurrent.AbstractFuture.setException(AbstractFuture.java:808)
at com.google.api.core.AbstractApiFuture$InternalSettableFuture.setException(AbstractApiFuture.java:95)
at com.google.api.core.AbstractApiFuture.setException(AbstractApiFuture.java:77)
at com.google.api.gax.httpjson.HttpJsonClientCalls$HttpJsonFuture.setException(HttpJsonClientCalls.java:112)
at com.google.api.gax.httpjson.HttpJsonClientCalls$FutureListener.onClose(HttpJsonClientCalls.java:133)
at com.google.api.gax.httpjson.HttpJsonClientCallImpl$OnCloseNotificationTask.call(HttpJsonClientCallImpl.java:500)
at com.google.api.gax.httpjson.HttpJsonClientCallImpl.notifyListeners(HttpJsonClientCallImpl.java:349)
at com.google.api.gax.httpjson.HttpJsonClientCallImpl.deliver(HttpJsonClientCallImpl.java:276)
at com.google.api.gax.httpjson.HttpJsonClientCallImpl.setResult(HttpJsonClientCallImpl.java:154)
at com.google.api.gax.httpjson.HttpRequestRunnable.run(HttpRequestRunnable.java:147)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: com.google.api.client.http.HttpResponseException: 400 Bad Request
POST https://compute.googleapis.com:443/compute/v1/projects/sab-dev-avps-1715/zones/us-central1-a/instances//setLabels
{
"error": {
"code": 400,
"message": "Invalid value for field 'instance': ''. Must be a match of regex '[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?|[1-9][0-9]{0,19}'",
"errors": [
{
"message": "Invalid value for field 'instance': ''. Must be a match of regex '[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?|[1-9][0-9]{0,19}'",
"domain": "global",
"reason": "invalid"
}
]
}
}
at com.google.api.client.http.HttpResponseException$Builder.build(HttpResponseException.java:293)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1118)
at com.google.api.gax.httpjson.HttpRequestRunnable.run(HttpRequestRunnable.java:118)
... 6 more
Well, any idea How to resolve this?