-
Notifications
You must be signed in to change notification settings - Fork 23
feat(alicloud): aggregate cloudfw policies and assets via DescribeAss… #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -42,6 +42,12 @@ func GetCloudFWResource() schema.Resource { | |||||||||||||||||
| func GetInstanceDetail(ctx context.Context, cancel context.CancelFunc, service schema.ServiceInterface, res chan<- any) error { | ||||||||||||||||||
|
|
||||||||||||||||||
| cli := service.(*collector.Services).Cloudfw | ||||||||||||||||||
|
|
||||||||||||||||||
| // Collect Internet boundary firewall assets by DescribeAssetList. | ||||||||||||||||||
| detail := &Detail{ | ||||||||||||||||||
| Assets: describeAssetList(ctx, cli), | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| direction := []string{"in", "out"} | ||||||||||||||||||
| for _, d := range direction { | ||||||||||||||||||
| page := 1 | ||||||||||||||||||
|
|
@@ -67,15 +73,10 @@ func GetInstanceDetail(ctx context.Context, cancel context.CancelFunc, service s | |||||||||||||||||
| bd := resp.Body | ||||||||||||||||||
| count += len(bd.Policys) | ||||||||||||||||||
| req.PageSize = tea.String(strconv.Itoa(size)) | ||||||||||||||||||
| for i := 0; i < len(bd.Policys); i++ { | ||||||||||||||||||
| res <- Detail{ | ||||||||||||||||||
| Policy: bd.Policys[i], | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
| detail.Policies = append(detail.Policies, bd.Policys...) | ||||||||||||||||||
|
|
||||||||||||||||||
| if bd.TotalCount == nil || strconv.Itoa(count) >= *bd.TotalCount { | ||||||||||||||||||
| cancel() | ||||||||||||||||||
| return nil | ||||||||||||||||||
| break | ||||||||||||||||||
| } | ||||||||||||||||||
|
||||||||||||||||||
|
|
||||||||||||||||||
| page += 1 | ||||||||||||||||||
|
|
@@ -85,9 +86,43 @@ func GetInstanceDetail(ctx context.Context, cancel context.CancelFunc, service s | |||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| res <- detail | ||||||||||||||||||
|
|
||||||||||||||||||
| return nil | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| type Detail struct { | ||||||||||||||||||
| Policy *cloudfw20171207.DescribeControlPolicyResponseBodyPolicys | ||||||||||||||||||
| Policies []*cloudfw20171207.DescribeControlPolicyResponseBodyPolicys | ||||||||||||||||||
| Assets []*cloudfw20171207.DescribeAssetListResponseBodyAssets | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| func describeAssetList(ctx context.Context, cli *cloudfw20171207.Client) (assets []*cloudfw20171207.DescribeAssetListResponseBodyAssets) { | ||||||||||||||||||
| page := 1 | ||||||||||||||||||
| pageSize := 50 | ||||||||||||||||||
| var collected int32 = 0 | ||||||||||||||||||
|
|
||||||||||||||||||
| for { | ||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||||||||||
| request := &cloudfw20171207.DescribeAssetListRequest{ | ||||||||||||||||||
| CurrentPage: tea.String(strconv.Itoa(page)), | ||||||||||||||||||
| PageSize: tea.String(strconv.Itoa(pageSize)), | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| resp, err := cli.DescribeAssetListWithOptions(request, collector.RuntimeObject) | ||||||||||||||||||
| if err != nil { | ||||||||||||||||||
| log.CtxLogger(ctx).Warn("describe asset list error", zap.Error(err)) | ||||||||||||||||||
| return assets | ||||||||||||||||||
| } | ||||||||||||||||||
| if resp == nil || resp.Body == nil { | ||||||||||||||||||
| return assets | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| assets = append(assets, resp.Body.Assets...) | ||||||||||||||||||
| collected += int32(len(resp.Body.Assets)) | ||||||||||||||||||
|
|
||||||||||||||||||
| if resp.Body.TotalCount == nil || collected >= *resp.Body.TotalCount || len(resp.Body.Assets) == 0 { | ||||||||||||||||||
| return assets | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| page += 1 | ||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The loop in page += 1
time.Sleep(1 * time.Second) |
||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+112
to
+135
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The pagination loop in for {
select {
case <-ctx.Done():
log.CtxLogger(ctx).Warn("context cancelled during asset list description")
return assets
default:
request := &cloudfw20171207.DescribeAssetListRequest{
CurrentPage: tea.String(strconv.Itoa(page)),
PageSize: tea.String(strconv.Itoa(pageSize)),
}
resp, err := cli.DescribeAssetListWithOptions(request, collector.RuntimeObject)
if err != nil {
log.CtxLogger(ctx).Warn("describe asset list error", zap.Error(err))
return assets
}
if resp == nil || resp.Body == nil {
return assets
}
assets = append(assets, resp.Body.Assets...)
collected += int32(len(resp.Body.Assets))
if resp.Body.TotalCount == nil || collected >= *resp.Body.TotalCount || len(resp.Body.Assets) == 0 {
return assets
}
page += 1
}
} |
||||||||||||||||||
| } | ||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is redundant. The
reqobject is re-initialized at the beginning of each loop iteration on line 62. Modifying it here has no effect on subsequent iterations and can be safely removed for clarity.