Closed torrezsae closed 1 week ago
For those struggling to get the endpoint to simply spin up an instance here is what I have done, I've made it as simple as possible due to not knowing how to overcomplicate it.
createService()
public void createService(String name){
try{
// I believe this pulls from GOOGLE_APPLICATION_CREDENTIALS env variable, attaching service account to server should do the trick. For local development use [gcloud auth application-default login] command to generate credentials.
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
String authToken = credentials.getAccessToken().getTokenValue();
// https://run.googleapis.com/v2/projects/{projectId}/locations/you-region/services?serviceId={name_for_instance}
cloudRunUrl = cloudRunUrl + "/projects/" + projectId + "/locations/us-east1/services?serviceId="+name;
RestClient restClient = RestClient.builder()
.baseUrl(cloudRunUrl)
.defaultHeader("Authorization", "Bearer "+authToken)
.build();
CreateServiceTemplate createServiceTemplate = new CreateServiceTemplate();
// Figuring out how to get message converter to work, in the meantime just turn it into a JSON payload
ObjectMapper obj = new ObjectMapper();
String payload = obj.writeValueAsString(createServiceTemplate);
String response = restClient.post().contentType(MediaType.APPLICATION_JSON).body(payload).retrieve().body(String.class);
logger.info(response);
}catch(Exception e){
e.printStackTrace();
}
}
CreateServiceTemplate
public class CreateServiceTemplate {
@Getter @Setter
private RevisionTemplate template;
public CreateServiceTemplate(){
template = new RevisionTemplate();
}
}
RevisionTemplate
public class RevisionTemplate {
@Getter @Setter
private List<Container> containers;
public RevisionTemplate(){
containers = new ArrayList<>();
containers.add(new Container());
}
public class Container{
@Getter @Setter
public String image="us-docker.pkg.dev/cloudrun/container/hello";
public Container(){
}
}
}
It's great to see you resolved it via the REST API.
(Note that the service property of CreateServiceRequest needs the template field. https://cloud.google.com/java/docs/reference/google-cloud-run/latest/com.google.cloud.run.v2.Service#com_google_cloud_run_v2_Service_getTemplate__)
Environment details
Code example
Stack trace
External references such as API reference guides
Used official documentation : https://cloud.google.com/java/docs/reference/google-cloud-run/latest/com.google.cloud.run.v2.ServicesClient
Any additional information below
I've used both latest and previous version of the maven dependency with no success. I've also tried with and without protobuf-core and protobuf-utils. From my understanding the only thing that is required is the serviceId, projectId, and region, all of which are being provided.
Thanks!