MichalMa
MichalMa

Reputation: 1230

Why Google ComputeEngine Rest API does not return qualified values for Instance Templates which are necessary to create Instances

I'm trying to create VMs on Google ComputeEngine, using Java api and sourcing data from instance templates. Getting instance template is simple:

try (InstanceTemplateClient instanceClient = InstanceTemplateClient.create()) {
            ProjectGlobalInstanceTemplateName globalInstanceTemplateName = ProjectGlobalInstanceTemplateName.of(instanceTemplateName, projectName);
            instanceTemplate = instanceClient.getInstanceTemplate(globalInstanceTemplateName);
        }

however there is no obvious way how to use instanceTemplate then, without specifying all properties separately, e.g.

try (InstanceClient instanceClient = InstanceClient.create()) {
            ProjectZoneName zone = ProjectZoneName.of(projectName, zoneName);
            InstanceProperties instanceProperties = instanceTemplate.getProperties();

            Instance instanceResource = Instance.newBuilder()
                    .setShieldedInstanceConfig(instanceProperties.getShieldedInstanceConfig())
                    .setCanIpForward(instanceProperties.getCanIpForward())
...

but what is even worse, instanceTemplate has not qualified values for several propeties (e.g. machineType, diskType), so I need to update them (I get error otherwise - "Invalid value for field 'resource.disks[0].initializeParams.diskType': 'pd-standard'. The URL is malformed.)

.setMachineType("zones/" + zoneName + "/machineTypes/" + instanceProperties.getMachineType())

Manually qualifying those parameters feels very fragile. Is there better way to do it? I'm using google-cloud-compute 0.99.0-alpha

Upvotes: 0

Views: 117

Answers (1)

mensi
mensi

Reputation: 9826

Creating a single instance based on an instance template is a bit "special" in the API. The instance template is passed as an optional query string parameter (sourceInstanceTemplate) in addition to the normal instance resource definition in the POST body.

As far as I can see, the Java client library does not currently model this concept, so you're left with either the workaround you already have or - alternatively - you could perform this API request yourself as it is relatively simple as long as you do not customize a lot of values for your instance. An API call example for this approach can be found here.

Upvotes: 1

Related Questions