Warm tip: This article is reproduced from serverfault.com, please click

What is the proper way to split a source into resource name/zone/project?

发布于 2020-11-30 15:31:37

I'm listing instances using the google cloud python library method: service.instances().list()

This returns a dict of instances, for each instance it returns a list of disks, and for each disk the source of the disk is available in the following format:

https://www.googleapis.com/compute/v1/projects/<project name>/zones/<zone>/disks/<disk name>

There is no other "name" in the disks dict, so that is the closest thing I have to retrieve the disk name.

After looking into other methods many of them return resources in a similar way.

However, if I want to use any google disk methods from the library, it's expected that I supply the disk name, project and zone as separate arguments to the library's method.

Is there a common method I can write to split the resource parameters?

In this example this would be project name, zone and disk name, but other resources might have different resources.

I could not find any method in the library that would do the split for me, so I guess it's expected that I write my own.

Questioner
Tahvok
Viewed
0
1 2020-12-01 08:07:45

There is no specific API in GCP that helps you to give you such a result, although considering that the URL you are getting is constant (the order of what you want is constant), I think the easiest way to do it is by applying the next code ,

disk_url = "https://www.googleapis.com/compute/v1/projects/<project name>/zones/<zone>/disks/<disk name>".split('/')
project = disk_url[6]
zone = disk_url[8]
disk = disk_url[10]

I think it would be helpful but If you need something more specific I believe you have more work to do with "handling strings in python" by your own.