1
votes

I'm trying to create a disk snapshot using python api client of google cloud platform, Heres what's I have tried: From views.py

        if request.method == 'POST':
        form = forms.SnapshotForm(request.POST)
        if form.is_valid():
            obj = snapy()
            obj.project = form.cleaned_data['project']
            obj.instance = form.cleaned_data['instance']
            obj.zone = form.cleaned_data['zone']
            obj.snapshot = form.cleaned_data['snap_name']
            # Google Cloud stuff for Snapshot
            service = discovery.build('compute', 'v1',
                                      credentials=credentials)
            project_id = obj.project
            zone = obj.zone
            disk = obj.instance
            snapshot_body = {}
            request = service.disks().createSnapshot(project=project_id, zone=zone,
                                                     disk=disk, body=snapshot_body)
            response = request.execute()
            pprint(response)
            obj.save()
            print(obj)
    return HttpResponse('Good', status=200)

It returns no error but snapshot not created on gcp console. Is there something wrong I have configured? help me, please!

Here's the response:

[10/Sep/2017 08:09:46] "GET /snap/ HTTP/1.1" 200 2740

{'id': '8462873153659819689', 
 'insertTime': '2017-09-13T01:11:51.025-07:00',
 'kind': 'compute#operation',
 'name': 'operation-1505290310763-5590db641c1f8-9be2938e-9e1b974f',
 'operationType': 'createSnapshot',
 'progress': 0,
 'selfLink': 'https://www.googleapis.com/compute/v1/projects/istiodep/zones/asia-northeast1-c/operations/operation-1505290310763-5590db641c1f8-9be2938e-9e1b974f',
 'status': 'PENDING',
 'targetId': '1514253394633906452',
 'targetLink': 'https://www.googleapis.com/compute/v1/projects/istiodep/zones/asia-northeast1-c/disks/instance-1',
 'user': '[email protected]',
 'zone': 'https://www.googleapis.com/compute/v1/projects/istiodep/zones/asia-northeast1-c'
}
Snapshot object
1

1 Answers

0
votes

I think, your problem might be arising because of empty snapshot_body.

In google-cloud, the name for snapshot is mandatory field. If you check the operation status using service.zoneOperations, then you may see its status and you may probably get reason as well.

I was able to create snapshot with following snippet:

body = {
'name':'test-snap'
}

compute.disks().createSnapshot(
 project=project, zone=zone, body=body).execute()

Let me know if this doesn't work for you.