網頁

2024/1/8

Golang 取得GCP執行個體機器類型 Get GCP Compute VM instance machine types

Go以Google API Client Libraries來取得GCP執行個體的機器類型(machine types)。



事前要求

參考「GCP 設定本機應用程式存取憑證 Application Default Credentials」設定credential。


下載API modules

在專案根目錄執行以下命令下載需要的google-api-go-client modules。



取得機器類型

呼叫compute.MachineTypesService.List輸入project id、zone參數取得機器類型清單物件compute.MachineTypeList。範例中使用MachineTypesListCall.Filter只查詢機器類型名稱開頭為e2-的結果。

main.go

package main

import (
    "context"
    "fmt"

    compute "google.golang.org/api/compute/v1"
)

func main() {
    ctx := context.Background()
    computeService, err := compute.NewService(ctx)
    if err != nil {
        panic(err)
    }
    projectId := "project-id-1"
    zone := "asia-east1-b"

    machineTypeService := compute.NewMachineTypesService(computeService)
    call := machineTypeService.List(projectId, zone)
    machineTypeList, err := call.Filter("name:e2-*").Do()
    if err != nil {
        panic(err)
    }
    for _, item := range machineTypeList.Items {
        fmt.Println(item.Name)
        fmt.Println(item.SelfLink)
    }

}

github


測試

執行Go應用程式輸出以下結果。

e2-highcpu-16
https://www.googleapis.com/compute/v1/projects/project-id-1/zones/asia-east1-b/machineTypes/e2-highcpu-16
e2-highcpu-2
https://www.googleapis.com/compute/v1/projects/project-id-1/zones/asia-east1-b/machineTypes/e2-highcpu-2
e2-highcpu-32
https://www.googleapis.com/compute/v1/projects/project-id-1/zones/asia-east1-b/machineTypes/e2-highcpu-32
e2-highcpu-4
https://www.googleapis.com/compute/v1/projects/project-id-1/zones/asia-east1-b/machineTypes/e2-highcpu-4
e2-highcpu-8
https://www.googleapis.com/compute/v1/projects/project-id-1/zones/asia-east1-b/machineTypes/e2-highcpu-8
e2-highmem-16
https://www.googleapis.com/compute/v1/projects/project-id-1/zones/asia-east1-b/machineTypes/e2-highmem-16
e2-highmem-2
https://www.googleapis.com/compute/v1/projects/project-id-1/zones/asia-east1-b/machineTypes/e2-highmem-2
e2-highmem-4
https://www.googleapis.com/compute/v1/projects/project-id-1/zones/asia-east1-b/machineTypes/e2-highmem-4
e2-highmem-8
https://www.googleapis.com/compute/v1/projects/project-id-1/zones/asia-east1-b/machineTypes/e2-highmem-8
e2-medium
https://www.googleapis.com/compute/v1/projects/project-id-1/zones/asia-east1-b/machineTypes/e2-medium
e2-micro
https://www.googleapis.com/compute/v1/projects/project-id-1/zones/asia-east1-b/machineTypes/e2-micro
e2-small
https://www.googleapis.com/compute/v1/projects/project-id-1/zones/asia-east1-b/machineTypes/e2-small
e2-standard-16
https://www.googleapis.com/compute/v1/projects/project-id-1/zones/asia-east1-b/machineTypes/e2-standard-16
e2-standard-2
https://www.googleapis.com/compute/v1/projects/project-id-1/zones/asia-east1-b/machineTypes/e2-standard-2
e2-standard-32
https://www.googleapis.com/compute/v1/projects/project-id-1/zones/asia-east1-b/machineTypes/e2-standard-32
e2-standard-4
https://www.googleapis.com/compute/v1/projects/project-id-1/zones/asia-east1-b/machineTypes/e2-standard-4
e2-standard-8
https://www.googleapis.com/compute/v1/projects/project-id-1/zones/asia-east1-b/machineTypes/e2-standard-8


沒有留言:

張貼留言