获取资源 SKU 以在 GCP 中查找资源价格(使用 Go 语言)

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

Get resource SKU to find resource price in GCP (go)

问题

我正在尝试找到一种方法,当给定一个资源时,可以确定其 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/<SKU>",
	}
	result := client.ListSkus(ctx, &req)

    // ... 处理 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,
	}
英文:

I'm trying to figure out a way when given a resource to determine it's SKU so I can look up it's pricing information.

package main

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

func main() {
	
	ctx := context.Background()
	client, err := billing.NewCloudCatalogClient(ctx)
	if err != nil {
		return nil, err
	}
	req := billingpb.ListSkusRequest{
		Parent: &quot;services/&lt;SKU&gt;&quot;,
	}
	result := client.ListSkus(ctx, &amp;req)

    // ... handle result
	
}

If for example we examine a compute.Disk there isn't any information about it's SKU, only it's type (pd-ssd, pd-standard).

    computepb &quot;google.golang.org/genproto/googleapis/cloud/compute/v1&quot;
	
    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,
	}

答案1

得分: 1

result := client.ListSkus(ctx, &req)

result 将是一个 *SkuIterator

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"`
}

其中有一个字段 SkusSku 的数组

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.PricingInfoPricingInfo 的数组

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"`
}

这些信息应该是你想要的。

英文:

result := client.ListSkus(ctx, &amp;req)

result will be a *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)
}

where SkuIterator.Response will be type ListSkusResponse

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

	Skus []*Sku `protobuf:&quot;bytes,1,rep,name=skus,proto3&quot; json:&quot;skus,omitempty&quot;`
	
	NextPageToken string `protobuf:&quot;bytes,2,opt,name=next_page_token,json=nextPageToken,proto3&quot; json:&quot;next_page_token,omitempty&quot;`
}

which has field Skus which is an array of Sku

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

	Name string `protobuf:&quot;bytes,1,opt,name=name,proto3&quot; json:&quot;name,omitempty&quot;`

	SkuId string `protobuf:&quot;bytes,2,opt,name=sku_id,json=skuId,proto3&quot; json:&quot;sku_id,omitempty&quot;`

	Description string `protobuf:&quot;bytes,3,opt,name=description,proto3&quot; json:&quot;description,omitempty&quot;`

	Category *Category `protobuf:&quot;bytes,4,opt,name=category,proto3&quot; json:&quot;category,omitempty&quot;`
https://cloud.google.com/about/locations/
	
ServiceRegions []string `protobuf:&quot;bytes,5,rep,name=service_regions,json=serviceRegions,proto3&quot; json:&quot;service_regions,omitempty&quot;`

	PricingInfo []*PricingInfo `protobuf:&quot;bytes,6,rep,name=pricing_info,json=pricingInfo,proto3&quot; json:&quot;pricing_info,omitempty&quot;`

	ServiceProviderName string `protobuf:&quot;bytes,7,opt,name=service_provider_name,json=serviceProviderName,proto3&quot; json:&quot;service_provider_name,omitempty&quot;`
}

where Sku.PricingInfo is an array of PricingInfo

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

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

	Summary string `protobuf:&quot;bytes,2,opt,name=summary,proto3&quot; json:&quot;summary,omitempty&quot;`

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

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

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

which should give you the info you want.

huangapple
  • 本文由 发表于 2021年9月20日 08:23:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/69248007.html
匿名

发表评论

匿名网友

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

确定