在 AWS Golang SDK 中解析/比较分页器的值

问题描述 投票:0回答:1

我正在尝试执行此处所示的操作,但对于使用分页器的帐户中所有区域的DescribeNetworkInterfaces。以下代码无法编译:

import (
    "context"
    "fmt"
    "log"
    "github.com/aws/aws-sdk-go-v2/config"
    "github.com/aws/aws-sdk-go-v2/service/ec2"
)

func main() {


    cfg, err := config.LoadDefaultConfig(context.TODO())
    if err != nil {
        log.Fatalf("Unable to load AWS SDK config, %v", err)
    }

    ec2Client := ec2.NewFromConfig(cfg)

    availableRegions, err := ec2Client.DescribeRegions(context.TODO(), &ec2.DescribeRegionsInput{})
    if err != nil {
        log.Fatalf("Unable to get AWS regions, %v", err)
    }

    for _, region := range availableRegions.Regions {
        fmt.Println("Region Name :", *region.RegionName)

        ec2Client := ec2.NewFromConfig(cfg, func(o *ec2.Options) {
            o.Region = (*region.RegionName)
        })

        describeIntefacesPaginator := ec2.NewDescribeNetworkInterfacesPaginator(ec2Client, &ec2.DescribeNetworkInterfacesInput{}, func(o *ec2.DescribeNetworkInterfacesPaginatorOptions) {
            o.Limit = 1000
        })

        for describeIntefacesPaginator.HasMorePages() {
            output, err := describeIntefacesPaginator.NextPage(context.TODO())
            if err != nil {
                fmt.Println("Error", err)
                break
            }

            for _, interface := range output.NetworkInterfaces {
               fmt.Println(*interface.NetworkInterfaceId, "-" *interface.InterfaceType)
            }

        }
    }

}

编译器抱怨 for 循环中有错误:

2024-04-01 16:52:08.993 [info] test.go:46:21 syntax error: unexpected :=, expected {

如果使用编码包删除 for 循环并输出到 JSON,我确实会得到输出(我目前在 ap-south-1 中有 2 个 ENI:

output2, err := json.Marshal(output)

if err != nil {
   fmt.Println("Unable to convert the struct to a JSON string")
} else {
   fmt.Println(string(output2) + "\n")
}
{"NetworkInterfaces":[{"Association":null,"Attachment":null,"AvailabilityZone":"ap-south-1a","ConnectionTrackingConfiguration":null,"DenyAllIgwTraffic":null,"Description":"test2","Groups":[{"GroupId":"sg-0242772b747b89a45","GroupName":"default"}],"InterfaceType":"interface","Ipv4Prefixes":null,"Ipv6Address":null,"Ipv6Addresses":[],"Ipv6Native":null,"Ipv6Prefixes":null,"MacAddress":"02:2d:44:db:66:9d","NetworkInterfaceId":"eni-069434f3d842b5ce4","OutpostArn":null,"OwnerId":"<redacted>","PrivateDnsName":"ip-172-31-34-44.ap-south-1.compute.internal","PrivateIpAddress":"172.31.34.44","PrivateIpAddresses":[{"Association":null,"Primary":true,"PrivateDnsName":"ip-172-31-34-44.ap-south-1.compute.internal","PrivateIpAddress":"172.31.34.44"}],"RequesterId":"<redacted>","RequesterManaged":false,"SourceDestCheck":true,"Status":"available","SubnetId":"subnet-04d1989d5b4bb8a93","TagSet":[],"VpcId":"vpc-0e9a0e28dad668f8a"},{"Association":null,"Attachment":null,"AvailabilityZone":"ap-south-1c","ConnectionTrackingConfiguration":null,"DenyAllIgwTraffic":null,"Description":"test","Groups":[{"GroupId":"sg-0242772b747b89a45","GroupName":"default"}],"InterfaceType":"interface","Ipv4Prefixes":null,"Ipv6Address":null,"Ipv6Addresses":[],"Ipv6Native":null,"Ipv6Prefixes":null,"MacAddress":"06:22:17:0b:8b:d3","NetworkInterfaceId":"eni-0c03afbd1d2638e74","OutpostArn":null,"OwnerId":"<redacted>","PrivateDnsName":"ip-172-31-24-26.ap-south-1.compute.internal","PrivateIpAddress":"172.31.24.26","PrivateIpAddresses":[{"Association":null,"Primary":true,"PrivateDnsName":"ip-172-31-24-26.ap-south-1.compute.internal","PrivateIpAddress":"172.31.24.26"}],"RequesterId":"<redacted>","RequesterManaged":false,"SourceDestCheck":true,"Status":"available","SubnetId":"subnet-09e693c955f141ea7","TagSet":[],"VpcId":"vpc-0e9a0e28dad668f8a"}],"NextToken":null,"ResultMetadata":{}}

最终目标是能够进行 if/else 或比较返回结构中的值。我应该将分页器的输出存储在地图中,然后对其执行操作吗?

amazon-web-services go pagination sdk
1个回答
0
投票

错误出现在 test.go 文件中,而不是代码本身。检查您在

test.go:46:21

中写的内容
© www.soinside.com 2019 - 2024. All rights reserved.