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
30 changes: 27 additions & 3 deletions collector/hws/collector/ecs/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"github.com/core-sdk/log"
"github.com/core-sdk/schema"
ecsModel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ecs/v2/model"
evs "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/evs/v2"
evsModel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/evs/v2/model"
vpc "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/vpc/v3"
vpcModel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/vpc/v3/model"
"go.uber.org/zap"
Expand All @@ -45,13 +47,17 @@ func GetInstanceResource() schema.Resource {
}

type InstanceDetail struct {
RegionId string
ServerDetail ecsModel.ServerDetail
SecurityGroup []*vpcModel.SecurityGroupInfo
Volumes []evsModel.VolumeDetail
}

func GetInstanceDetail(ctx context.Context, service schema.ServiceInterface, res chan<- any) error {
client := service.(*collector.Services).ECS
vpcClient := service.(*collector.Services).VPC
services := service.(*collector.Services)
ecsClient := services.ECS
evsClient := services.EVS
vpcClient := services.VPC

limit := int32(50)
offset := int32(1)
Expand All @@ -60,7 +66,7 @@ func GetInstanceDetail(ctx context.Context, service schema.ServiceInterface, res
Offset: &offset,
}
for {
response, err := client.ListServersDetails(request)
response, err := ecsClient.ListServersDetails(request)
if err != nil {
log.CtxLogger(ctx).Warn("ListServersDetails error", zap.Error(err))
return err
Expand All @@ -69,8 +75,10 @@ func GetInstanceDetail(ctx context.Context, service schema.ServiceInterface, res
for _, ecs := range *response.Servers {
ecs.OSEXTSRVATTRuserData = nil
res <- &InstanceDetail{
RegionId: services.Region,
ServerDetail: ecs,
SecurityGroup: getSecurityGroup(ecs.SecurityGroups, vpcClient),
Volumes: getVolume(ecs.OsExtendedVolumesvolumesAttached, evsClient),
}
}

Expand All @@ -97,3 +105,19 @@ func getSecurityGroup(securityGroups []ecsModel.ServerSecurityGroup, client *vpc
}
return result
}

func getVolume(volumes []ecsModel.ServerExtendVolumeAttachment, client *evs.EvsClient) []evsModel.VolumeDetail {
var result []evsModel.VolumeDetail
for _, volume := range volumes {
request := &evsModel.ListVolumesRequest{}
idRequest := volume.Id
request.Id = &idRequest
response, err := client.ListVolumes(request)
if err != nil {
log.GetWLogger().Error(fmt.Sprintf("list volumes error: %s", err.Error()))
return nil
}
result = append(result, (*response.Volumes)[0])
Comment on lines +117 to +120
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

Accessing (*response.Volumes)[0] without checking if response.Volumes is non-empty can lead to a runtime panic if the API returns an empty slice. Additionally, if client.ListVolumes encounters an error for a specific volume, the function currently returns nil immediately, discarding any volume details that might have been successfully retrieved for other volumes. It would be more robust to log the error for the failed volume and continue processing the remaining volumes, returning a partial list of successfully retrieved volume details.

        if err != nil {
            log.GetWLogger().Error(fmt.Sprintf("list volumes error for volume %s: %s", volume.Id, err.Error()))
            continue
        }
        if response.Volumes != nil && len(*response.Volumes) > 0 {
            result = append(result, (*response.Volumes)[0])
        } else {
            log.GetWLogger().Warn(fmt.Sprintf("no volume details returned for volume %s", volume.Id))
        }

}
return result
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Go source files should end with a newline character. This is a common convention and helps with version control systems and tools.

Suggested change
}
}

1 change: 1 addition & 0 deletions collector/hws/collector/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func (s *Services) InitServices(cloudAccountParam schema.CloudAccountParam) (err
case ECS:
s.ECS, err = s.ECSClient(param.Region)
s.VPC, err = s.VPCClient(param.Region)
s.EVS, err = s.EVSClient(param.Region)
case IAMUser:
s.IAM, err = s.IAMClient()
case VPC, SecurityGroup:
Expand Down
Loading