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

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

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

问题

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

import (
    "fmt"
    "net/url"
)

func main() {
    instanceURL := "https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-b/instances/instance-group-z0hf"
    
    parsedURL, err := url.Parse(instanceURL)
    if err != nil {
        fmt.Println("Failed to parse instance URL:", err)
        return
    }
    
    project := parsedURL.PathSegments()[3]
    zone := parsedURL.PathSegments()[5]
    instance := parsedURL.PathSegments()[7]
    
    fmt.Println("Project:", project)
    fmt.Println("Zone:", zone)
    fmt.Println("Instance:", instance)
}

在上面的示例中,我们使用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:

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以获取其路径
  • 通过斜杠组件拆分路径
  • 迭代各个部分
  • 定位静态字符串
  • 获取下一个值并相应地赋值。
package main

import (
	"fmt"
	"net/url"
	"strings"
)

func main() {
	s := "https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-b/instances/instance-group-z0hf"
	u, err := url.Parse(s)
	if err != nil {
		panic(err)
	}

	parts := strings.Split(u.Path, "/")

	var project string
	var zone string
	var inst string

	for i := 0; i < len(parts); i++ {
		if parts[i] == "projects" && i+1 < len(parts) {
			project = parts[i+1]
			i++
		} else if parts[i] == "zones" && i+1 < len(parts) {
			zone = parts[i+1]
			i++
		} else if parts[i] == "instances" && i+1 < len(parts) {
			inst = parts[i+1]
			i++
		}
	}

	fmt.Println(project, zone, inst)
}
//输出:
//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.
package main

import (
	&quot;fmt&quot;
	&quot;net/url&quot;
	&quot;strings&quot;
)

func main() {
	s := &quot;https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-b/instances/instance-group-z0hf&quot;
	u, err := url.Parse(s)
	if err != nil {
		panic(err)
	}

	parts := strings.Split(u.Path, &quot;/&quot;)

	var project string
	var zone string
	var inst string

	for i := 0; i &lt; len(parts); i++ {
		if parts[i] == &quot;projects&quot; &amp;&amp; i+1 &lt; len(parts) {
			project = parts[i+1]
			i++
		} else if parts[i] == &quot;zones&quot; &amp;&amp; i+1 &lt; len(parts) {
			zone = parts[i+1]
			i++
		} else if parts[i] == &quot;instances&quot; &amp;&amp; i+1 &lt; len(parts) {
			inst = parts[i+1]
			i++
		}
	}

	fmt.Println(project, zone, inst)
}
//Ouptput:
//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:

确定