Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
muripic opened this issue Dec 8, 2023 · 2 comments
Labels
api: compute Issues related to the Compute Engine API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: question Request for information or clarification. Not an issue.

Comments

@muripic
Copy link

muripic commented Dec 8, 2023

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.

@product-auto-label product-auto-label bot added the api: compute Issues related to the Compute Engine API. label Dec 8, 2023
@dazuma
Copy link
Member

dazuma commented Dec 11, 2023

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.

@dazuma dazuma added type: question Request for information or clarification. Not an issue. priority: p2 Moderately-important priority. Fix may not be included in next release. labels Dec 11, 2023
@muripic
Copy link
Author

muripic commented Dec 12, 2023

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 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: compute Issues related to the Compute Engine API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

2 participants