dqd2800 2019-04-08 08:21
浏览 70

如何使用Go列出Google Cloud Platform上正在运行的实例

I'm trying to learn Go by managing Google Cloud Platform. I didn't understand how to use related functions about Compute. The goal is listing instances with some go code.

This is https://godoc.org/google.golang.org/api/compute/v1#InstancesService.List the related function.

func (r *InstancesService) List(project string, zone string) *InstancesListCall

There are two structs, InstancesService and InstancesListCall

As far as i understand i should define these structs but it's not clear the things should be defined in the structs. I've searched for examples but many of them using rest calls instead of golang api. Have any idea how to list instances with go?

  • 写回答

1条回答 默认 最新

  • douguwo2275 2019-04-23 12:27
    关注

    i had to write something like this today and googling for examples turned up surprisingly little. i've written up what i learned below, however, i'm quite new to golang so maybe smarter people can suggest improvements.

    my work in progress is at: https://github.com/grenade/rubberneck


    if you want to run your go program from a development pc that is not on the google compute platform:

    • set up the gcloud cli to run on your pc (instructions: https://cloud.google.com/sdk/gcloud)
    • create a service account for your go application to run under (instructions: https://cloud.google.com/docs/authentication/production#creating_a_service_account)
    • grant permissions to the service account (use the same instructions link above)
    • create a local key file containing your new service account credentials (use the same instructions link above)
    • set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path of your local key file
    • write your go application. something like this:
    package main
    
    import (
    
      "golang.org/x/net/context"
      "google.golang.org/api/compute/v1"
      "golang.org/x/oauth2/google"
    
      "fmt"
      "strings"
    )
    
    func main() {
      projects := [...]string{
        "my-project-one",
        "my-project-two",
      }
      filters := [...]string{
        "status = RUNNING",
        "name != my-uninteresting-instance-one",
        "name != my-uninteresting-instance-two",
      }
    
      ctx := context.Background()
      client, err := google.DefaultClient(ctx,compute.ComputeScope)
      if err != nil {
        fmt.Println(err)
      }
      computeService, err := compute.New(client)
      for _, project := range projects {
        zoneListCall := computeService.Zones.List(project)
        zoneList, err := zoneListCall.Do()
        if err != nil {
          fmt.Println("Error", err)
        } else {
          for _, zone := range zoneList.Items {
            instanceListCall := computeService.Instances.List(project, zone.Name)
            instanceListCall.Filter(strings.Join(filters[:], " "))
            instanceList, err := instanceListCall.Do()
            if err != nil {
              fmt.Println("Error", err)
            } else {
              for _, instance := range instanceList.Items {
                if workerType, isWorker := instance.Labels["worker-type"]; isWorker {
                  m := strings.Split(instance.MachineType, "/")
                  fmt.Printf("cloud: gcp, zone: %v, name: %v, instance id: %v, machine type: %v, worker type: %v, launch time: %v
    ",
                    zone.Name,
                    instance.Name,
                    instance.Id,
                    m[len(m)-1],
                    workerType,
                    instance.CreationTimestamp)
                }
              }
            }
          }
        }
      }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端