0
votes

I'm trying to create a disk from a snapshot using google cloud python API:

def createDisk(compute, project, zone):
  config = {
    'name': disk_name
  }

  return compute.disks().insert(
    project=project,
    zone=zone,
    sourceSnapshot='global/snapshots/' + snap_name,
    body=config).execute()

But it throws:

TypeError: Got an unexpected keyword argument "sourceSnapshot"

According to the docs it should be possible:

Creates a persistent disk in the specified project using the data in the request. You can create a disk with a sourceImage, a sourceSnapshot, or create an empty 500 GB data disk by omitting all properties. You can also create a disk that is larger than the default size by specifying the sizeGb property.

I need it to automate an image creation which I want to base on the 'dummy' instance. The image should then be used to create a disk which in turn will be used in the instance template for the auto scaling.

Any tips on that one? Is it possible? If no, the sourceSnapshot reference be in the docs is really misleading?

Thank you in advance.

1
Despite what the text says, the Parameters box only shows sourceImage. - Barmar
Maybe you use the sourceImage parameter for both, and it figures out whether it's an image or a snapshot automatically. - Barmar
@Barmar: I tried also, but: "Invalid value for field 'resource.sourceImage': ''. Must be a URL to a valid image resource." - Michał Lisowski

1 Answers

0
votes

It came out that sourceSnapshot should be part of the body request, not an argument. So that would work:

def createDisk(compute, project, zone):
  config = {
    'name': disk_name,
    'sourceSnapshot': 'global/snapshots/' + snap_name,
  }

  return compute.disks().insert(
    project=project,
    zone=zone,
    body=config).execute()