AWS SDK in Go. 解析数据

huangapple go评论151阅读模式
英文:

AWS SDK in Go. Parsing data

问题

我正在使用aws-sdk来进行Go语言开发。我想根据实例类型过滤器来描述EC2实例。
以下是我的代码:

  1. ec2Svc := ec2.New(sess, &aws.Config{Credentials: creds})
  2. params := &ec2.DescribeInstanceTypesInput{
  3. Filters: []*ec2.Filter{
  4. {
  5. Name: aws.String("instance-type"),
  6. Values: []*string{aws.String("t2*")},
  7. },
  8. },
  9. }
  10. result, err := ec2Svc.DescribeInstanceTypes(params)
  11. if err != nil {
  12. fmt.Println("Error", err)
  13. } else {
  14. fmt.Println(result.String())
  15. }

这段代码运行良好。result变量的类型是ec2.DescribeInstanceTypeOutput。以下是result的样式:

  1. {
  2. "InstanceTypes": [
  3. {
  4. "AutoRecoverySupported": true,
  5. "BareMetal": false,
  6. "BurstablePerformanceSupported": true,
  7. "CurrentGeneration": true,
  8. "DedicatedHostsSupported": false,
  9. "EbsInfo": {
  10. "EbsOptimizedSupport": "unsupported",
  11. "EncryptionSupport": "supported",
  12. "NvmeSupport": "unsupported"
  13. },
  14. "FreeTierEligible": false,
  15. "InstanceStorageSupported": false,
  16. "InstanceType": "t2.2xlarge",
  17. "MemoryInfo": {
  18. "SizeInMiB": 32768
  19. },
  20. "NetworkInfo": {
  21. "DefaultNetworkCardIndex": 0,
  22. "MaximumNetworkCards": 1,
  23. "MaximumNetworkInterfaces": 3,
  24. "NetworkCards": [
  25. {
  26. "MaximumNetworkInterfaces": 3
  27. }
  28. ],
  29. "NetworkPerformance": "Moderate"
  30. },
  31. "PlacementGroupInfo": {
  32. "SupportedStrategies": [
  33. "partition",
  34. "spread"
  35. ]
  36. },
  37. "ProcessorInfo": {
  38. "SupportedArchitectures": [
  39. "x86_64"
  40. ]
  41. },
  42. "SupportedBootModes": [
  43. "legacy-bios"
  44. ],
  45. "SupportedVirtualizationTypes": [
  46. "hvm"
  47. ],
  48. "VCpuInfo": {
  49. "DefaultCores": 8
  50. }
  51. }
  52. ],
  53. "NextToken": "asadasdasd"
  54. }

如你所见,这个JSON非常长,我已经删除了其中一些内容以便更易读。我想从中获取一个信息 - MemoryInfo.SizeInMiB,但我不知道如何做到。我尝试使用json.Marshaljson.Unmarshal,但由于ec2.DescribeInstanceTypeOutput类型的原因,我遇到了错误。
获取这个值的唯一方法是创建一个相应的结构,然后进行处理吗?

英文:

I'm using aws-sdk for go. I want to describe EC2 instance based on instance-type filter.
Here's my code:

  1. ec2Svc := ec2.New(sess, &aws.Config{Credentials: creds})
  2. params := &ec2.DescribeInstanceTypesInput{
  3. Filters: []*ec2.Filter{
  4. {
  5. Name: aws.String("instance-type"),
  6. Values: []*string{aws.String("t2*")},
  7. },
  8. },
  9. }
  10. result, err := ec2Svc.DescribeInstanceTypes(params)
  11. if err != nil {
  12. fmt.Println("Error", err)
  13. } else {
  14. fmt.Println(result.String())
  15. }

This works fine. The type of result variable is *ec2.DescribeInstanceTypeOutput. Here's how result looks like:

  1. {
  2. InstanceTypes: [{
  3. AutoRecoverySupported: true,
  4. BareMetal: false,
  5. BurstablePerformanceSupported: true,
  6. CurrentGeneration: true,
  7. DedicatedHostsSupported: false,
  8. EbsInfo: {
  9. EbsOptimizedSupport: "unsupported",
  10. EncryptionSupport: "supported",
  11. NvmeSupport: "unsupported"
  12. },
  13. FreeTierEligible: false,
  14. InstanceStorageSupported: false,
  15. InstanceType: "t2.2xlarge",
  16. MemoryInfo: {
  17. SizeInMiB: 32768
  18. },
  19. NetworkInfo: {
  20. DefaultNetworkCardIndex: 0,
  21. MaximumNetworkCards: 1,
  22. MaximumNetworkInterfaces: 3,
  23. NetworkCards: [{
  24. MaximumNetworkInterfaces: 3,
  25. }],
  26. NetworkPerformance: "Moderate"
  27. },
  28. PlacementGroupInfo: {
  29. SupportedStrategies: ["partition","spread"]
  30. },
  31. ProcessorInfo: {
  32. SupportedArchitectures: ["x86_64"],
  33. },
  34. SupportedBootModes: ["legacy-bios"],
  35. SupportedVirtualizationTypes: ["hvm"],
  36. VCpuInfo: {
  37. DefaultCores: 8,
  38. }
  39. }],
  40. NextToken: "asadasdasd"
  41. }

As you can see this json is quite long and I've removed some content from it to make more readable. I want to get one information from it - MemoryInfo.SizeInMiB but I don't know how to. I've tried using json.Marshal and json.Unmarshal but I get error because of *ec2.DescribeInstanceTypeOutput type.

Is the only way to get this value is to create some long structure and then using it somehow?

答案1

得分: 1

您不需要创建新的结构,SDK已经提供了这个功能。DescribeInstanceTypes的返回类型是一个指向DescribeInstanceTypesOutput对象的指针。

您可以通过以下方式访问MemoryInfo.SizeInMiB

  1. func main() {
  2. // 创建一个会话
  3. sess := session.Must(session.NewSessionWithOptions(session.Options{
  4. SharedConfigState: session.SharedConfigEnable,
  5. }))
  6. ec2Svc := ec2.New(sess)
  7. params := &ec2.DescribeInstanceTypesInput{
  8. Filters: []*ec2.Filter{
  9. {
  10. Name: aws.String("instance-type"),
  11. Values: []*string{aws.String("t2*")},
  12. },
  13. },
  14. }
  15. // 使用NextToken循环检索实例类型。实例的过滤将在本地进行
  16. for {
  17. result, err := ec2Svc.DescribeInstanceTypes(params)
  18. if err != nil {
  19. fmt.Println("错误", err)
  20. break
  21. } else {
  22. // InstanceTypes是一个切片,所以我们需要遍历它
  23. for _, instance := range result.InstanceTypes {
  24. // 获取实例的内存大小
  25. fmt.Printf("%s - %d MiB\n", *instance.InstanceType, *instance.MemoryInfo.SizeInMiB)
  26. }
  27. // 检查是否还有实例剩余
  28. if result.NextToken != nil {
  29. params.NextToken = result.NextToken
  30. } else {
  31. break
  32. }
  33. }
  34. }
  35. }

这应该会输出类似以下内容:

  1. t2.2xlarge - 32768 MiB
  2. t2.large - 8192 MiB
  3. t2.micro - 1024 MiB
  4. t2.medium - 4096 MiB
  5. t2.xlarge - 16384 MiB
  6. t2.small - 2048 MiB
  7. t2.nano - 512 MiB
英文:

You don't have to create a new structure, this is already provided by the SDK. The return type of DescribeInstanceTypes is a pointer to a DescribeInstanceTypesOutput object.

You can access MemoryInfo.SizeInMiB in the following way:

  1. func main() {
  2. // Build a session
  3. sess := session.Must(session.NewSessionWithOptions(session.Options{
  4. SharedConfigState: session.SharedConfigEnable,
  5. }))
  6. ec2Svc := ec2.New(sess)
  7. params := &ec2.DescribeInstanceTypesInput{
  8. Filters: []*ec2.Filter{
  9. {
  10. Name: aws.String("instance-type"),
  11. Values: []*string{aws.String("t2*")},
  12. },
  13. },
  14. }
  15. // Retrieve the instance types in a loop using NextToken. The filtering of instances will happen locally
  16. for {
  17. result, err := ec2Svc.DescribeInstanceTypes(params)
  18. if err != nil {
  19. fmt.Println("Error", err)
  20. break
  21. } else {
  22. // InstanceTypes is slice, so we have to iterate over it
  23. for _, instance := range result.InstanceTypes {
  24. // Grab the memory size of the instance
  25. fmt.Printf("%s - %d MiB\n", *instance.InstanceType, *instance.MemoryInfo.SizeInMiB)
  26. }
  27. // Check if they are any instances left
  28. if result.NextToken != nil {
  29. params.NextToken = result.NextToken
  30. } else {
  31. break
  32. }
  33. }
  34. }
  35. }

This should output something similar to this:

  1. t2.2xlarge - 32768 MiB
  2. t2.large - 8192 MiB
  3. t2.micro - 1024 MiB
  4. t2.medium - 4096 MiB
  5. t2.xlarge - 16384 MiB
  6. t2.small - 2048 MiB
  7. t2.nano - 512 MiB

huangapple
  • 本文由 发表于 2022年8月11日 18:38:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/73319268.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定