英文:
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-ssd
,pd-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 (
"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)
// ... 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 "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,
}
答案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"`
}
其中有一个字段 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"`
}
这些信息应该是你想要的。
英文:
result := client.ListSkus(ctx, &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:"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"`
}
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:"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"`
}
where Sku.PricingInfo is an array of 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"`
}
which should give you the info you want.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论