From 501b2da51fd009026be72c34f14cd70e36677378 Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Wed, 20 May 2026 11:39:31 +0800 Subject: [PATCH] chore: make volcengine/volc-sdk-golang sdk lightweight --- .../volcengine-vod/volcengine_vod.go | 20 +- .../volc-sdk-golang/service/vod/config.go | 73 +++++++ .../service/vod/vod_cdn_service.go | 191 ++++++++++++++++++ 3 files changed, 274 insertions(+), 10 deletions(-) create mode 100644 pkg/sdk3rd-trimmed/github.com/volc-sdk-golang/service/vod/config.go create mode 100644 pkg/sdk3rd-trimmed/github.com/volc-sdk-golang/service/vod/vod_cdn_service.go diff --git a/pkg/core/deployer/providers/volcengine-vod/volcengine_vod.go b/pkg/core/deployer/providers/volcengine-vod/volcengine_vod.go index 9ecee9cf1..a1cae7bf0 100644 --- a/pkg/core/deployer/providers/volcengine-vod/volcengine_vod.go +++ b/pkg/core/deployer/providers/volcengine-vod/volcengine_vod.go @@ -8,14 +8,14 @@ import ( "strings" "github.com/samber/lo" - vevod "github.com/volcengine/volc-sdk-golang/service/vod" - vevodbusiness "github.com/volcengine/volc-sdk-golang/service/vod/models/business" - vevodrequest "github.com/volcengine/volc-sdk-golang/service/vod/models/request" + vevodbiz "github.com/volcengine/volc-sdk-golang/service/vod/models/business" + vevodreq "github.com/volcengine/volc-sdk-golang/service/vod/models/request" ve "github.com/volcengine/volcengine-go-sdk/volcengine" "github.com/certimate-go/certimate/pkg/core/certmgr" certmgrimpl "github.com/certimate-go/certimate/pkg/core/certmgr/providers/volcengine-certcenter" "github.com/certimate-go/certimate/pkg/core/deployer" + vevod "github.com/certimate-go/certimate/pkg/sdk3rd-trimmed/github.com/volc-sdk-golang/service/vod" xcerthostname "github.com/certimate-go/certimate/pkg/utils/cert/hostname" ) @@ -182,7 +182,7 @@ func (d *Deployer) getAllDomains(ctx context.Context) ([]string, error) { default: } - listDomainReq := &vevodrequest.VodListDomainRequest{ + listDomainReq := &vevodreq.VodListDomainRequest{ SpaceName: d.config.SpaceName, DomainType: d.config.DomainType, SourceStationType: 1, @@ -199,7 +199,7 @@ func (d *Deployer) getAllDomains(ctx context.Context) ([]string, error) { break } - var domainInstances []*vevodbusiness.VodDomainInstanceInfo + var domainInstances []*vevodbiz.VodDomainInstanceInfo switch d.config.DomainType { case DOMAIN_TYPE_PLAY: domainInstances = listDomainResp.GetResult().GetPlayInstanceInfo().GetByteInstances() @@ -231,15 +231,15 @@ func (d *Deployer) getAllDomains(ctx context.Context) ([]string, error) { func (d *Deployer) updateDomainCertificate(ctx context.Context, domain string, cloudCertId string) error { // 更新域名配置 // REF: https://www.volcengine.com/docs/4/1317310 - updateDomainConfigReq := &vevodrequest.VodUpdateDomainConfigRequest{ + updateDomainConfigReq := &vevodreq.VodUpdateDomainConfigRequest{ SpaceName: d.config.SpaceName, DomainType: d.config.DomainType, Domain: domain, - Config: &vevodbusiness.VodDomainConfig{ - HTTPS: &vevodbusiness.HTTPS{ + Config: &vevodbiz.VodDomainConfig{ + HTTPS: &vevodbiz.HTTPS{ Switch: ve.Bool(true), - CertInfo: &vevodbusiness.CertInfo{ - CertId: &cloudCertId, + CertInfo: &vevodbiz.CertInfo{ + CertId: ve.String(cloudCertId), }, }, }, diff --git a/pkg/sdk3rd-trimmed/github.com/volc-sdk-golang/service/vod/config.go b/pkg/sdk3rd-trimmed/github.com/volc-sdk-golang/service/vod/config.go new file mode 100644 index 000000000..00a8eb5fb --- /dev/null +++ b/pkg/sdk3rd-trimmed/github.com/volc-sdk-golang/service/vod/config.go @@ -0,0 +1,73 @@ +package vod + +import ( + "fmt" + "net/http" + "sync" + "time" + + "github.com/volcengine/volc-sdk-golang/base" + vevod "github.com/volcengine/volc-sdk-golang/service/vod" +) + +type Vod struct { + *base.Client + DomainCache map[string]map[string]int + Lock sync.RWMutex + disableLog bool +} + +type config struct { + disableLog bool +} + +type Option func(c *config) + +func NewInstance(opts ...Option) *Vod { + cfg := &config{} + for _, opt := range opts { + opt(cfg) + } + instance := &Vod{ + DomainCache: make(map[string]map[string]int), + Client: base.NewClient(ServiceInfoMap[base.RegionCnNorth1], ApiInfoList), + disableLog: cfg.disableLog, + } + return instance +} + +func NewInstanceWithRegion(region string, opts ...Option) *Vod { + cfg := &config{} + for _, opt := range opts { + opt(cfg) + } + var serviceInfo *base.ServiceInfo + var ok bool + if serviceInfo, ok = ServiceInfoMap[region]; !ok { + serviceInfo = &base.ServiceInfo{ + Timeout: 60 * time.Second, + Scheme: "https", + Host: fmt.Sprintf("vod.%s.volcengineapi.com", region), + Header: http.Header{ + "Accept": []string{"application/json"}, + }, + Credentials: base.Credentials{Region: region, Service: "vod"}, + } + } + + instance := &Vod{ + DomainCache: make(map[string]map[string]int), + Client: base.NewClient(serviceInfo, ApiInfoList), + disableLog: cfg.disableLog, + } + return instance +} + +var ( + ServiceInfoMap = vevod.ServiceInfoMap + + ApiInfoList = map[string]*base.ApiInfo{ + "ListDomain": vevod.ApiInfoList["ListDomain"], + "UpdateDomainConfig": vevod.ApiInfoList["UpdateDomainConfig"], + } +) diff --git a/pkg/sdk3rd-trimmed/github.com/volc-sdk-golang/service/vod/vod_cdn_service.go b/pkg/sdk3rd-trimmed/github.com/volc-sdk-golang/service/vod/vod_cdn_service.go new file mode 100644 index 000000000..971667e37 --- /dev/null +++ b/pkg/sdk3rd-trimmed/github.com/volc-sdk-golang/service/vod/vod_cdn_service.go @@ -0,0 +1,191 @@ +package vod + +import ( + "encoding/json" + "net/http" + "net/url" + "strconv" + + "google.golang.org/protobuf/encoding/protojson" + + "github.com/pkg/errors" + + "github.com/volcengine/volc-sdk-golang/service/vod/models/request" + "github.com/volcengine/volc-sdk-golang/service/vod/models/response" +) + +func (p *Vod) ListDomain(req *request.VodListDomainRequest) (*response.VodListDomainResponse, int, error) { + query := url.Values{} + form := url.Values{} + marshaler := protojson.MarshalOptions{ + Multiline: false, + AllowPartial: false, + UseProtoNames: true, + UseEnumNumbers: false, + EmitUnpopulated: false, + } + jsonData := marshaler.Format(req) + reqMap := map[string]interface{}{} + err := json.Unmarshal([]byte(jsonData), &reqMap) + if err != nil { + return nil, 0, err + } + for k, v := range reqMap { + var sv string + switch ov := v.(type) { + case string: + sv = ov + case int: + sv = strconv.FormatInt(int64(ov), 10) + case uint: + sv = strconv.FormatUint(uint64(ov), 10) + case int8: + sv = strconv.FormatInt(int64(ov), 10) + case uint8: + sv = strconv.FormatUint(uint64(ov), 10) + case int16: + sv = strconv.FormatInt(int64(ov), 10) + case uint16: + sv = strconv.FormatUint(uint64(ov), 10) + case int32: + sv = strconv.FormatInt(int64(ov), 10) + case uint32: + sv = strconv.FormatUint(uint64(ov), 10) + case int64: + sv = strconv.FormatInt(ov, 10) + case uint64: + sv = strconv.FormatUint(ov, 10) + case bool: + sv = strconv.FormatBool(ov) + case float32: + sv = strconv.FormatFloat(float64(ov), 'f', -1, 32) + case float64: + sv = strconv.FormatFloat(ov, 'f', -1, 64) + case []byte: + sv = string(ov) + default: + v2, e2 := json.Marshal(ov) + if e2 != nil { + return nil, 0, e2 + } + sv = string(v2) + } + query.Set(k, sv) + form.Add(k, sv) + } + + respBody, status, err := p.Query("ListDomain", query) + + output := &response.VodListDomainResponse{} + unmarshaler := protojson.UnmarshalOptions{ + DiscardUnknown: true, + } + errUnmarshal := unmarshaler.Unmarshal(respBody, output) + if err != nil || status != http.StatusOK { + // if exist http err,check whether the respBody's type is defined struct, + // if it is , + // return struct, + // otherwise return nil body + // if httpCode is not 200,check whether the respBody's type is defined struct, + // if it is , + // use errorCode as err and return struct, + // otherwise use respBody string as error and return + if errUnmarshal != nil || len(output.GetResponseMetadata().GetError().GetCode()) == 0 { + if err == nil { + err = errors.New(string(respBody)) + } + return nil, status, err + } else { + return output, status, errors.New(output.GetResponseMetadata().GetError().GetCode()) + } + } + return output, status, nil +} + +func (p *Vod) UpdateDomainConfig(req *request.VodUpdateDomainConfigRequest) (*response.VodUpdateDomainConfigResponse, int, error) { + query := url.Values{} + form := url.Values{} + marshaler := protojson.MarshalOptions{ + Multiline: false, + AllowPartial: false, + UseProtoNames: true, + UseEnumNumbers: false, + EmitUnpopulated: false, + } + jsonData := marshaler.Format(req) + reqMap := map[string]interface{}{} + err := json.Unmarshal([]byte(jsonData), &reqMap) + if err != nil { + return nil, 0, err + } + for k, v := range reqMap { + var sv string + switch ov := v.(type) { + case string: + sv = ov + case int: + sv = strconv.FormatInt(int64(ov), 10) + case uint: + sv = strconv.FormatUint(uint64(ov), 10) + case int8: + sv = strconv.FormatInt(int64(ov), 10) + case uint8: + sv = strconv.FormatUint(uint64(ov), 10) + case int16: + sv = strconv.FormatInt(int64(ov), 10) + case uint16: + sv = strconv.FormatUint(uint64(ov), 10) + case int32: + sv = strconv.FormatInt(int64(ov), 10) + case uint32: + sv = strconv.FormatUint(uint64(ov), 10) + case int64: + sv = strconv.FormatInt(ov, 10) + case uint64: + sv = strconv.FormatUint(ov, 10) + case bool: + sv = strconv.FormatBool(ov) + case float32: + sv = strconv.FormatFloat(float64(ov), 'f', -1, 32) + case float64: + sv = strconv.FormatFloat(ov, 'f', -1, 64) + case []byte: + sv = string(ov) + default: + v2, e2 := json.Marshal(ov) + if e2 != nil { + return nil, 0, e2 + } + sv = string(v2) + } + query.Set(k, sv) + form.Add(k, sv) + } + + respBody, status, err := p.Json("UpdateDomainConfig", url.Values{}, jsonData) + + output := &response.VodUpdateDomainConfigResponse{} + unmarshaler := protojson.UnmarshalOptions{ + DiscardUnknown: true, + } + errUnmarshal := unmarshaler.Unmarshal(respBody, output) + if err != nil || status != http.StatusOK { + // if exist http err,check whether the respBody's type is defined struct, + // if it is , + // return struct, + // otherwise return nil body + // if httpCode is not 200,check whether the respBody's type is defined struct, + // if it is , + // use errorCode as err and return struct, + // otherwise use respBody string as error and return + if errUnmarshal != nil || len(output.GetResponseMetadata().GetError().GetCode()) == 0 { + if err == nil { + err = errors.New(string(respBody)) + } + return nil, status, err + } else { + return output, status, errors.New(output.GetResponseMetadata().GetError().GetCode()) + } + } + return output, status, nil +}