Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 110 additions & 30 deletions collector/baidu/collector/blb/appblb.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,75 @@
package blb

import (
"github.com/core-sdk/constant"
"github.com/core-sdk/log"
"github.com/core-sdk/schema"
"context"
"strconv"

"github.com/baidubce/bce-sdk-go/bce"
"github.com/baidubce/bce-sdk-go/services/appblb"
"github.com/cloudrec/baidu/collector"
"github.com/core-sdk/constant"
"github.com/core-sdk/log"
"github.com/core-sdk/schema"
"go.uber.org/zap"
)

// enrichedAppBLB embeds appblb.AppBLBModel so existing $.AppBLB.* paths stay
// intact; the trailing fields are returned by /v1/appblb but not modeled by
// the baidu SDK's AppBLBModel.
type enrichedAppBLB struct {
appblb.AppBLBModel
Type string `json:"type"`
UnderlayVip string `json:"underlayVip"`
ExpireTime string `json:"expireTime"`
BillingMethod string `json:"billingMethod"`
PaymentTiming string `json:"paymentTiming"`
PerformanceLevel string `json:"performanceLevel"`
AllowModify bool `json:"allowModify"`
ModificationProtectionReason string `json:"modificationProtectionReason"`
}

type enrichedAppListener struct {
appblb.AppAllListenerModel
Description string `json:"description"`
}

type enrichedAppServerGroupPort struct {
appblb.AppServerGroupPort
HealthCheckValid int `json:"healthCheckValid"`
}

// enrichedAppServerGroup mirrors appblb.AppServerGroup but swaps in
// enrichedAppServerGroupPort so the missing portList[].healthCheckValid
// is preserved. Fields kept verbatim from the SDK type.
type enrichedAppServerGroup struct {
Id string `json:"id"`
Name string `json:"name"`
Description string `json:"desc"`
Status appblb.BLBStatus `json:"status"`
PortList []enrichedAppServerGroupPort `json:"portList"`
}

type enrichedDescribeAppLoadBalancersResult struct {
BlbList []enrichedAppBLB `json:"blbList"`
appblb.DescribeResultMeta
}

type enrichedDescribeAppAllListenersResult struct {
ListenerList []enrichedAppListener `json:"listenerList"`
appblb.DescribeResultMeta
}

type enrichedDescribeAppServerGroupResult struct {
AppServerGroupList []enrichedAppServerGroup `json:"appServerGroupList"`
appblb.DescribeResultMeta
}

type AppBLBDetail struct {
AppBLB appblb.AppBLBModel
ListenerList []appblb.AppAllListenerModel
AppBLB enrichedAppBLB
ListenerList []enrichedAppListener
SecurityGroups []appblb.BlbSecurityGroupModel
EnterpriseSecurityGroups []appblb.BlbEnterpriseSecurityGroupModel
AppServerGroupList []appblb.AppServerGroup
AppServerGroupList []enrichedAppServerGroup
}

func GetAppBLBResource() schema.Resource {
Expand All @@ -52,9 +106,9 @@ func GetAppBLBResource() schema.Resource {
ResourceDetailFunc: func(ctx context.Context, service schema.ServiceInterface, res chan<- any) error {
client := service.(*collector.Services).APPBLBClient

args := &appblb.DescribeLoadBalancersArgs{}
marker := ""
for {
response, err := client.DescribeLoadBalancers(args)
response, err := describeAppLoadBalancersEnriched(ctx, client, marker)
if err != nil {
log.CtxLogger(ctx).Warn("DescribeLoadBalancers error", zap.Error(err))
return err
Expand All @@ -72,7 +126,7 @@ func GetAppBLBResource() schema.Resource {
if response.NextMarker == "" {
break
}
args.Marker = response.NextMarker
marker = response.NextMarker
}

return nil
Expand All @@ -86,25 +140,47 @@ func GetAppBLBResource() schema.Resource {
}
}

func describeAppAllListeners(ctx context.Context, client *appblb.Client, blbId string) (listenerList []appblb.AppAllListenerModel) {
args := &appblb.DescribeAppListenerArgs{
Marker: "",
MaxKeys: 50,
// describeAppLoadBalancersEnriched mirrors client.DescribeLoadBalancers but
// decodes into enrichedDescribeAppLoadBalancersResult so the 8 extra fields
// returned by /v1/appblb are preserved.
func describeAppLoadBalancersEnriched(ctx context.Context, client *appblb.Client, marker string) (*enrichedDescribeAppLoadBalancersResult, error) {
result := &enrichedDescribeAppLoadBalancersResult{}
rb := bce.NewRequestBuilder(client).
WithMethod("GET").
WithURL("/v1/appblb").
WithQueryParam("maxKeys", strconv.Itoa(1000)).
WithResult(result)
if marker != "" {
rb = rb.WithQueryParam("marker", marker)
}
if err := rb.Do(); err != nil {
return nil, err
}
return result, nil
}

func describeAppAllListeners(ctx context.Context, client *appblb.Client, blbId string) (listenerList []enrichedAppListener) {
marker := ""
for {
response, err := client.DescribeAppAllListeners(blbId, args)
if err != nil {
result := &enrichedDescribeAppAllListenersResult{}
rb := bce.NewRequestBuilder(client).
WithMethod("GET").
WithURL("/v1/appblb/" + blbId + "/listener").
WithQueryParam("maxKeys", strconv.Itoa(50)).
WithResult(result)
if marker != "" {
rb = rb.WithQueryParam("marker", marker)
}
if err := rb.Do(); err != nil {
log.CtxLogger(ctx).Warn("DescribeAppAllListeners error", zap.Error(err))
return
}
listenerList = append(listenerList, response.ListenerList...)
if response.NextMarker == "" {
listenerList = append(listenerList, result.ListenerList...)
if result.NextMarker == "" {
break
}
args.Marker = response.NextMarker
marker = result.NextMarker
}

return listenerList
}
Comment on lines +162 to 185

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The function describeAppAllListeners (and similarly describeAppServerGroup at line 207) swallows errors from the API call, returning a potentially empty or partial list without notifying the caller. This can lead to incomplete resource data being collected silently. Consider updating these functions to return an error along with the results, allowing the caller to handle API failures more robustly.


Expand All @@ -128,23 +204,27 @@ func describeAppBLBEnterpriseSecurityGroups(ctx context.Context, client *appblb.
return resp.BlbEnterpriseSecurityGroups
}

func describeAppServerGroup(ctx context.Context, client *appblb.Client, blbId string) (appServerGroupList []appblb.AppServerGroup) {
args := &appblb.DescribeAppServerGroupArgs{
Marker: "",
MaxKeys: 50,
}
func describeAppServerGroup(ctx context.Context, client *appblb.Client, blbId string) (appServerGroupList []enrichedAppServerGroup) {
marker := ""
for {
response, err := client.DescribeAppServerGroup(blbId, args)
if err != nil {
result := &enrichedDescribeAppServerGroupResult{}
rb := bce.NewRequestBuilder(client).
WithMethod("GET").
WithURL("/v1/appblb/" + blbId + "/appservergroup").
WithQueryParam("maxKeys", strconv.Itoa(50)).
WithResult(result)
if marker != "" {
rb = rb.WithQueryParam("marker", marker)
}
if err := rb.Do(); err != nil {
log.CtxLogger(ctx).Warn("describeAppServerGroup error", zap.Error(err))
return
}
appServerGroupList = append(appServerGroupList, response.AppServerGroupList...)
if response.NextMarker == "" {
appServerGroupList = append(appServerGroupList, result.AppServerGroupList...)
if result.NextMarker == "" {
break
}
args.Marker = response.NextMarker
marker = result.NextMarker
}

return appServerGroupList
}
94 changes: 76 additions & 18 deletions collector/baidu/collector/blb/blb.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,54 @@
package blb

import (
"github.com/core-sdk/constant"
"github.com/core-sdk/log"
"github.com/core-sdk/schema"
"context"
"strconv"

"github.com/baidubce/bce-sdk-go/bce"
"github.com/baidubce/bce-sdk-go/services/blb"
"github.com/cloudrec/baidu/collector"
"github.com/core-sdk/constant"
"github.com/core-sdk/log"
"github.com/core-sdk/schema"
"go.uber.org/zap"
)

// enrichedBLB embeds blb.BLBModel so all existing JSON paths under $.Blb.*
// remain unchanged. Fields below are returned by /v1/blb but not modeled by
// the baidu SDK's BLBModel — captured here so collector content stays a
// superset of the raw API response.
type enrichedBLB struct {
blb.BLBModel
Type string `json:"type"`
UnderlayVip string `json:"underlayVip"`
ExpireTime string `json:"expireTime"`
BillingMethod string `json:"billingMethod"`
PaymentTiming string `json:"paymentTiming"`
PerformanceLevel string `json:"performanceLevel"`
AllowModify bool `json:"allowModify"`
ModificationProtectionReason string `json:"modificationProtectionReason"`
}

// enrichedBLBListener adds two fields the SDK's AllListenerModel doesn't model.
type enrichedBLBListener struct {
blb.AllListenerModel
BackendPortType string `json:"backendPortType"`
HealthCheckValid int `json:"healthCheckValid"`
}

type enrichedDescribeLoadBalancersResult struct {
BlbList []enrichedBLB `json:"blbList"`
blb.DescribeResultMeta
}

type enrichedDescribeAllListenersResult struct {
ListenerList []enrichedBLBListener `json:"listenerList"`
blb.DescribeResultMeta
}

type Detail struct {
Blb blb.BLBModel
ListenerList []blb.AllListenerModel
Blb enrichedBLB
ListenerList []enrichedBLBListener
BlbSecurityGroups []blb.BlbSecurityGroupModel
BlbEnterpriseSecurityGroups []blb.BlbEnterpriseSecurityGroupModel
BackendServerList []blb.BackendServerModel
Expand All @@ -52,9 +88,9 @@ func GetResource() schema.Resource {
ResourceDetailFunc: func(ctx context.Context, service schema.ServiceInterface, res chan<- any) error {
client := service.(*collector.Services).BLBClient

args := &blb.DescribeLoadBalancersArgs{}
marker := ""
for {
response, err := client.DescribeLoadBalancers(args)
response, err := describeLoadBalancersEnriched(ctx, client, marker)
if err != nil {
log.CtxLogger(ctx).Warn("DescribeLoadBalancers error", zap.Error(err))
return err
Expand All @@ -72,7 +108,7 @@ func GetResource() schema.Resource {
if response.NextMarker == "" {
break
}
args.Marker = response.NextMarker
marker = response.NextMarker
}

return nil
Expand All @@ -86,25 +122,47 @@ func GetResource() schema.Resource {
}
}

func describeAllListeners(ctx context.Context, client *blb.Client, blbId string) (listenerList []blb.AllListenerModel) {
args := &blb.DescribeListenerArgs{
Marker: "",
MaxKeys: 50,
// describeLoadBalancersEnriched issues GET /v1/blb and decodes into a struct
// that captures both the SDK-modeled fields and the SDK-omitted ones. Pagination
// follows the same NextMarker convention as the SDK.
func describeLoadBalancersEnriched(ctx context.Context, client *blb.Client, marker string) (*enrichedDescribeLoadBalancersResult, error) {
result := &enrichedDescribeLoadBalancersResult{}
rb := bce.NewRequestBuilder(client).
WithMethod("GET").
WithURL("/v1/blb").
WithQueryParam("maxKeys", strconv.Itoa(1000)).
WithResult(result)
if marker != "" {
rb = rb.WithQueryParam("marker", marker)
}
if err := rb.Do(); err != nil {
return nil, err
}
return result, nil
}

func describeAllListeners(ctx context.Context, client *blb.Client, blbId string) (listenerList []enrichedBLBListener) {
marker := ""
for {
response, err := client.DescribeAllListeners(blbId, args)
if err != nil {
result := &enrichedDescribeAllListenersResult{}
rb := bce.NewRequestBuilder(client).
WithMethod("GET").
WithURL("/v1/blb/" + blbId + "/listener").
WithQueryParam("maxKeys", strconv.Itoa(50)).
WithResult(result)
if marker != "" {
rb = rb.WithQueryParam("marker", marker)
}
if err := rb.Do(); err != nil {
log.CtxLogger(ctx).Warn("DescribeAllListeners error", zap.Error(err))
return
}
listenerList = append(listenerList, response.AllListenerList...)
if response.NextMarker == "" {
listenerList = append(listenerList, result.ListenerList...)
if result.NextMarker == "" {
break
}
args.Marker = response.NextMarker
marker = result.NextMarker
}

return listenerList
}
Comment on lines +144 to 167

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The function describeAllListeners swallows errors from the rb.Do() call. If the API request fails, it returns an empty or partial list, which might result in incomplete data collection. It is recommended to return the error to the caller for better error handling and visibility.


Expand Down
2 changes: 2 additions & 0 deletions collector/baidu/collector/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ const (
BLS = "BLS"
CFW = "CFW"
CCERBAC = "CCERBAC"
ENI = "ENI"
ACL = "ACL"
)
Loading
Loading