Visitar URL original
Compute: `disks` is a required param even when creating an instance from an instance template · Issue #23642 · googleapis/google-cloud-ruby · GitHub
Skip to content

Compute: disks is a required param even when creating an instance from an instance template #23642

Description

@muripic

Not sure if this is an actual bug or if this is the intended behaviour and it's more of a feature request.

When using gcloud cli we are perfectly able to create machines using an instance template, and we do not need to specify disks. Because of this, we would expect the same behaviour.
However, the code examples for this use case in the documentation do not include Ruby (https://cloud.google.com/compute/docs/instances/create-vm-from-instance-template#create_a_vm_instance_from_an_instance_template), which indicates that maybe this is indeed a missing feature.

Environment details

  • OS: Debian Bullseye
  • Ruby version: 3.2.2
  • Gem name and version: google-cloud-compute (1.2.0), google-cloud-compute-v1 (2.5.0)

Steps to reproduce

  • Run the code below to create an instance using an instance template
  • Get an error saying disks are a required field

Code example

  Google::Cloud::Compute.configure do |config|
    config.credentials = '/path/to/credentials.json'
  end
  instances_client = Google::Cloud::Compute.instances
  instance_templates_client = Google::Cloud::Compute.instance_templates

  zone = 'europe-west4-c'
  machine_type = 'e2-medium'
  project = 'my_project'
  source_instance_template = 'my_instance_template'
  
  instance_template = instance_templates_client.get(project: project, instance_template: source_instance_template)

  instance = Google::Cloud::Compute::V1::Instance.new(
    name: 'my_test_machine',
    machine_type: "zones/#{zone}/machineTypes/#{machine_type}",
    disks: [ ],   # also tried not including it
    network_interfaces: [
      {
        name: "projects/my-project/global/networks/mynet",
        subnetwork: "projects/my-project/regions/europe-west4/subnetworks/mysubnet",
      }
    ],
  )

  request = { project: project, zone: zone, instance_resource: instance, source_instance_template: instance_template.self_link }

  puts "Creating the instance..."
  begin
    # Send the insert request.
    operation = instances_client.insert(request)

Full backtrace

Creating the instance...
Exception during creation:
An error has occurred when making a REST request: Invalid value for field 'resource.disks': ''. No disks are specified.

Also tried setting disks: instance_template.properties.disks.to_a to see if I could work around this, but in that case the request also failed with the following error:

An error has occurred when making a REST request: Invalid value for field 'resource.disks[0].initializeParams.diskType': 'pd-standard'. The URL is malformed.

This suggests that the disks need to be defined all over again, which defeats the purpose of having instance templates.

Activity

  1. dazuma commented on Dec 11, 2023

    @dazuma
    Contributor

    Thanks for your report! We are aware we're missing some samples for Ruby. In general, the Ruby client should have access to exactly the same functionality as any of the other programming language clients, so in many cases you may be able to look at the samples for another language such as Python and translate to Ruby.

    We'll have an engineer take a quick look at your code example to see if we can suggest any fixes or notice any issues with the client.

  2. added
    type: questionRequest for information or clarification. Not an issue.
    priority: p2Moderately-important priority. Fix may not be included in next release.
    on Dec 11, 2023
  3. muripic commented on Dec 12, 2023

    @muripic
    Author

    Thank you @dazuma ! I managed to work around it by retrieving the instance template, getting its disks and then using this method to overwrite the disk_type field with the full path so it would be valid for the insert instance request:

        def disks_from_instance_template(template, zone)
          disks = template.properties.disks.each do |d|
            d.initialize_params.disk_type = "zones/#{zone}/diskTypes/#{d.initialize_params.disk_type}"
          end
          disks.to_a
        end

    While it works, it would be nice if it could just be possible to create the instance using the instance template, instead of having to specify the disks 🙏

  4. amanda-tarafa commented on Oct 9, 2026

    @amanda-tarafa

    This seems like a feature request for the API team, looking at the Java sample, the template disks are also being manipulated there similar to comment. It's possible that gcloud achieves this by internally manipulating the disks, but that's unlikely to happen in libraries as we prefer to rely on generated code as much as possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api: computeIssues related to the Compute Engine API.priority: p2Moderately-important priority. Fix may not be included in next release.type: questionRequest for information or clarification. Not an issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions