Skip to content
Open
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
72 changes: 39 additions & 33 deletions collector/alicloud/collector/fc/fc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ package fc

import (
"context"
"fmt"
fc20230330 "github.com/alibabacloud-go/fc-20230330/v4/client"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/cloudrec/alicloud/collector"
"github.com/core-sdk/constant"
"github.com/core-sdk/log"
Expand All @@ -35,8 +33,8 @@ func GetFCResource() schema.Resource {
Desc: "https://api.aliyun.com/product/FC",
ResourceDetailFunc: GetInstanceDetail,
RowField: schema.RowField{
ResourceId: "$.ResourceId",
ResourceName: "$.ResourceName",
ResourceId: "$.Function.functionId",
ResourceName: "$.Function.functionName",
},
Regions: []string{
"cn-qingdao",
Expand Down Expand Up @@ -68,52 +66,49 @@ func GetFCResource() schema.Resource {
func GetInstanceDetail(ctx context.Context, service schema.ServiceInterface, res chan<- any) error {
services := service.(*collector.Services)
cli := services.FC
domain := describeCustomDomain(ctx, cli)

function := describeFunction(ctx, cli)

if len(domain) == 0 && len(function) == 0 {
log.CtxLogger(ctx).Info("no fc resource found")
return nil
}

res <- Detail{
ResourceId: fmt.Sprintf("fc_%s_%s", *cli.RegionId, services.CloudAccountId),
ResourceName: fmt.Sprintf("fc_%s_%s", *cli.RegionId, services.CloudAccountId),
Domain: domain,
Function: function,
for _, f := range function {
res <- Detail{
Function: f,
Triggers: describeTriggers(ctx, cli, f.FunctionName),
}
}

return nil
}

type Detail struct {
ResourceId string

ResourceName string

// Custom domain name information
Domain []*fc20230330.CustomDomain

// Function Information
Function []*fc20230330.Function
}
Function *fc20230330.Function

func describeCustomDomain(ctx context.Context, cli *fc20230330.Client) []*fc20230330.CustomDomain {
listCustomDomainsRequest := &fc20230330.ListCustomDomainsRequest{}
runtime := &util.RuntimeOptions{}
headers := make(map[string]*string)
Triggers []*fc20230330.Trigger
}

result, err := cli.ListCustomDomainsWithOptions(listCustomDomainsRequest, headers, runtime)
func describeTriggers(ctx context.Context, cli *fc20230330.Client, name *string) (triggers []*fc20230330.Trigger) {
request := &fc20230330.ListTriggersRequest{}
resp, err := cli.ListTriggers(name, request)
if err != nil {
log.CtxLogger(ctx).Warn("ListCustomDomainsWithOptions error", zap.Error(err))
log.CtxLogger(ctx).Warn("ListTriggers error", zap.Error(err))
return nil
}
triggers = append(triggers, resp.Body.Triggers...)

for resp.Body.NextToken != nil {
request.NextToken = resp.Body.NextToken
resp, err = cli.ListTriggers(name, request)
if err != nil {
log.CtxLogger(ctx).Warn("ListTriggers error", zap.Error(err))
return nil
}
triggers = append(triggers, resp.Body.Triggers...)
}

return result.Body.CustomDomains
return triggers
}
Comment on lines +89 to 109
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.

critical

The pagination logic in this function has a critical bug and can be simplified.

  1. Bug: The final return resp.Body.Triggers on line 108 will only return the triggers from the last fetched page, discarding all triggers from previous pages that were appended to the triggers slice. This will result in incomplete data.
  2. Refactoring: The code for fetching and appending triggers is duplicated before and inside the loop. This can be consolidated into a single loop structure for better readability and maintainability.

I've suggested a refactoring that fixes the bug and cleans up the code.

func describeTriggers(ctx context.Context, cli *fc20230330.Client, name *string) (triggers []*fc20230330.Trigger) {
	request := &fc20230330.ListTriggersRequest{}
	for {
		resp, err := cli.ListTriggers(name, request)
		if err != nil {
			log.CtxLogger(ctx).Warn("ListTriggers error", zap.Error(err))
			return nil
		}

		if resp.Body != nil {
			triggers = append(triggers, resp.Body.Triggers...)
			if resp.Body.NextToken == nil {
				break
			}
			request.NextToken = resp.Body.NextToken
		} else {
			break
		}
	}
	return triggers
}


func describeFunction(ctx context.Context, cli *fc20230330.Client) []*fc20230330.Function {
func describeFunction(ctx context.Context, cli *fc20230330.Client) (functions []*fc20230330.Function) {
listFunctionsRequest := &fc20230330.ListFunctionsRequest{}
headers := make(map[string]*string)

Expand All @@ -122,6 +117,17 @@ func describeFunction(ctx context.Context, cli *fc20230330.Client) []*fc20230330
log.CtxLogger(ctx).Warn("ListFunctionsWithOptions error", zap.Error(err))
return nil
}
functions = append(functions, result.Body.Functions...)

if result.Body.NextToken != nil {
listFunctionsRequest.NextToken = result.Body.NextToken
result, err = cli.ListFunctionsWithOptions(listFunctionsRequest, headers, collector.RuntimeObject)
if err != nil {
log.CtxLogger(ctx).Warn("ListFunctionsWithOptions error", zap.Error(err))
return nil
}
functions = append(functions, result.Body.Functions...)
}

return result.Body.Functions
return functions
}
Loading