Google Cloud Golang:如何从实例URL中解析项目/区域/实例?

huangapple go评论123阅读模式
英文:

Google Cloud Golang: How to parse project/zone/instance from instance URL?

问题

你可以使用URL解析函数来解析实例URL,以获取其项目、区域和实例部分。在Go语言中,你可以使用url.Parse函数来解析URL。以下是一个示例代码:

  1. import (
  2. "fmt"
  3. "net/url"
  4. )
  5. func main() {
  6. instanceURL := "https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-b/instances/instance-group-z0hf"
  7. parsedURL, err := url.Parse(instanceURL)
  8. if err != nil {
  9. fmt.Println("Failed to parse instance URL:", err)
  10. return
  11. }
  12. project := parsedURL.PathSegments()[3]
  13. zone := parsedURL.PathSegments()[5]
  14. instance := parsedURL.PathSegments()[7]
  15. fmt.Println("Project:", project)
  16. fmt.Println("Zone:", zone)
  17. fmt.Println("Instance:", instance)
  18. }

在上面的示例中,我们使用url.Parse函数解析实例URL,并使用PathSegments方法获取URL路径的各个部分。根据示例URL的结构,项目位于索引3,区域位于索引5,实例位于索引7。你可以根据实际URL的结构进行相应的调整。

希望这可以帮助到你!

英文:

I'm using compute.NewRegionInstanceGroupManagersService's ListManagedInstances call which returns ManagedInstance's.

ManagedInstance has a field Instance which is an instance url, like https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-b/instances/instance-group-z0hf

Now I would like to get more details about this particular instance. So using InstanceService's Get call, the function signature looks like this:

  1. func (r *InstancesService) Get(project string, zone string, instance string) *InstancesGetCall

What's the best way to parse the instance URL (see above) into its project, zone and instance parts? Or is there a way of using another method to pass the instance URL directly?

答案1

得分: 2

你可以这样做:

  • 解析URL以获取其路径
  • 通过斜杠组件拆分路径
  • 迭代各个部分
  • 定位静态字符串
  • 获取下一个值并相应地赋值。
  1. package main
  2. import (
  3. "fmt"
  4. "net/url"
  5. "strings"
  6. )
  7. func main() {
  8. s := "https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-b/instances/instance-group-z0hf"
  9. u, err := url.Parse(s)
  10. if err != nil {
  11. panic(err)
  12. }
  13. parts := strings.Split(u.Path, "/")
  14. var project string
  15. var zone string
  16. var inst string
  17. for i := 0; i < len(parts); i++ {
  18. if parts[i] == "projects" && i+1 < len(parts) {
  19. project = parts[i+1]
  20. i++
  21. } else if parts[i] == "zones" && i+1 < len(parts) {
  22. zone = parts[i+1]
  23. i++
  24. } else if parts[i] == "instances" && i+1 < len(parts) {
  25. inst = parts[i+1]
  26. i++
  27. }
  28. }
  29. fmt.Println(project, zone, inst)
  30. }
  31. //输出:
  32. //my-project us-central1-b instance-group-z0hf

或者,使用gorilla的路由引擎创建一个新的模式,将该模式应用于URL路径并收集输出结果。但这更复杂,可能没有必要。

英文:

you could do something like this,

  • parse the URL to get its path
  • split the path by slash component
  • iterate the parts,
  • locate static strings
  • take the next value and assign it appropriately.
  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;net/url&quot;
  5. &quot;strings&quot;
  6. )
  7. func main() {
  8. s := &quot;https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-b/instances/instance-group-z0hf&quot;
  9. u, err := url.Parse(s)
  10. if err != nil {
  11. panic(err)
  12. }
  13. parts := strings.Split(u.Path, &quot;/&quot;)
  14. var project string
  15. var zone string
  16. var inst string
  17. for i := 0; i &lt; len(parts); i++ {
  18. if parts[i] == &quot;projects&quot; &amp;&amp; i+1 &lt; len(parts) {
  19. project = parts[i+1]
  20. i++
  21. } else if parts[i] == &quot;zones&quot; &amp;&amp; i+1 &lt; len(parts) {
  22. zone = parts[i+1]
  23. i++
  24. } else if parts[i] == &quot;instances&quot; &amp;&amp; i+1 &lt; len(parts) {
  25. inst = parts[i+1]
  26. i++
  27. }
  28. }
  29. fmt.Println(project, zone, inst)
  30. }
  31. //Ouptput:
  32. //my-project us-central1-b instance-group-z0hf

Alternatively, use the route engine from gorilla to create a new pattern, apply the route to the url path and collect output results. But it is more complex and probably not justified.

答案2

得分: 0

URL是复杂的结构,最好的方法是使用url.Parse库。然后,你可以使用正则表达式或拆分函数从路径部分提取所需的数据。

英文:

URLs are complex animals, the best way is using the library url.Parse. Then you can use a regex or split to extract the data you need from the path part.

huangapple
  • 本文由 发表于 2021年8月6日 09:44:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/68675179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定