登录
首页 >  Golang >  Go问答

在 GCP 中查询资源价格需获取资源 SKU

来源:stackoverflow

时间:2024-03-03 20:12:28 459浏览 收藏

目前golang学习网上已经有很多关于Golang的文章了,自己在初次阅读这些文章中,也见识到了很多学习思路;那么本文《在 GCP 中查询资源价格需获取资源 SKU》,也希望能帮助到大家,如果阅读完后真的对你学习Golang有帮助,欢迎动动手指,评论留言并分享~

问题内容

当给定资源时,我试图找出一种方法来确定其 sku,以便我可以查找其定价信息。

package main

import (
    "context"
    billingpb "google.golang.org/genproto/googleapis/cloud/billing/v1"
    billing "cloud.google.com/go/billing/apiv1"
)

func main() {
    
    ctx := context.background()
    client, err := billing.newcloudcatalogclient(ctx)
    if err != nil {
        return nil, err
    }
    req := billingpb.listskusrequest{
        parent: "services/",
    }
    result := client.listskus(ctx, &req)

    // ... handle result
    
}

例如,如果我们检查 compute.disk,则没有任何有关其 sku 的信息,只有其类型(pd-ssdpd-standard)。

    computepb "google.golang.org/genproto/googleapis/cloud/compute/v1"
    
    computepb.Disk{
        CreationTimestamp:           nil,
        Description:                 nil,
        DiskEncryptionKey:           nil,
        GuestOsFeatures:             nil,
        Id:                          nil,
        Kind:                        nil,
        LabelFingerprint:            nil,
        Labels:                      nil,
        LastAttachTimestamp:         nil,
        LastDetachTimestamp:         nil,
        LicenseCodes:                nil,
        Licenses:                    nil,
        LocationHint:                nil,
        Name:                        nil,
        Options:                     nil,
        PhysicalBlockSizeBytes:      nil,
        ProvisionedIops:             nil,
        Region:                      nil,
        ReplicaZones:                nil,
        ResourcePolicies:            nil,
        SatisfiesPzs:                nil,
        SelfLink:                    nil,
        SizeGb:                      nil,
        SourceDisk:                  nil,
        SourceDiskId:                nil,
        SourceImage:                 nil,
        SourceImageEncryptionKey:    nil,
        SourceImageId:               nil,
        SourceSnapshot:              nil,
        SourceSnapshotEncryptionKey: nil,
        SourceSnapshotId:            nil,
        SourceStorageObject:         nil,
        Status:                      nil,
        Type:                        nil,
        Users:                       nil,
        Zone:                        nil,
    }

正确答案


结果 := client.listskus(ctx, &req)

结果将为 *SkuInterator

type skuiterator struct {
    items    []*billingpb.sku
    pageinfo *iterator.pageinfo
    nextfunc func() error

    response interface{}

    internalfetch func(pagesize int, pagetoken string) (results []*billingpb.sku, nextpagetoken string, err error)
}

其中skuiterator.response将为类型ListSkusResponse

type listskusresponse struct {
    state         protoimpl.messagestate
    sizecache     protoimpl.sizecache
    unknownfields protoimpl.unknownfields

    skus []*sku `protobuf:"bytes,1,rep,name=skus,proto3" json:"skus,omitempty"`
    
    nextpagetoken string `protobuf:"bytes,2,opt,name=next_page_token,json=nextpagetoken,proto3" json:"next_page_token,omitempty"`
}

其中的字段 skus 是一个 Sku 的数组

type sku struct {
    state         protoimpl.messagestate
    sizecache     protoimpl.sizecache
    unknownfields protoimpl.unknownfields

    name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`

    skuid string `protobuf:"bytes,2,opt,name=sku_id,json=skuid,proto3" json:"sku_id,omitempty"`

    description string `protobuf:"bytes,3,opt,name=description,proto3" json:"description,omitempty"`

    category *category `protobuf:"bytes,4,opt,name=category,proto3" json:"category,omitempty"`
https://cloud.google.com/about/locations/
    
serviceregions []string `protobuf:"bytes,5,rep,name=service_regions,json=serviceregions,proto3" json:"service_regions,omitempty"`

    pricinginfo []*pricinginfo `protobuf:"bytes,6,rep,name=pricing_info,json=pricinginfo,proto3" json:"pricing_info,omitempty"`

    serviceprovidername string `protobuf:"bytes,7,opt,name=service_provider_name,json=serviceprovidername,proto3" json:"service_provider_name,omitempty"`
}

其中 sku.pricinginfo 是一个 PricingInfo 的数组

type PricingInfo struct {
    state         protoimpl.MessageState
    sizeCache     protoimpl.SizeCache
    unknownFields protoimpl.UnknownFields

    EffectiveTime *timestamppb.Timestamp `protobuf:"bytes,1,opt,name=effective_time,json=effectiveTime,proto3" json:"effective_time,omitempty"`

    Summary string `protobuf:"bytes,2,opt,name=summary,proto3" json:"summary,omitempty"`

    PricingExpression *PricingExpression `protobuf:"bytes,3,opt,name=pricing_expression,json=pricingExpression,proto3" json:"pricing_expression,omitempty"`

    AggregationInfo *AggregationInfo `protobuf:"bytes,4,opt,name=aggregation_info,json=aggregationInfo,proto3" json:"aggregation_info,omitempty"`

    CurrencyConversionRate float64 `protobuf:"fixed64,5,opt,name=currency_conversion_rate,json=currencyConversionRate,proto3" json:"currency_conversion_rate,omitempty"`
}

这应该会给你你想要的信息。

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《在 GCP 中查询资源价格需获取资源 SKU》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>