Skip to content

Commit

Permalink
use paginator for list accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
kaiyan-sheng committed Jun 22, 2021
1 parent 0e7fcad commit a540a42
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 71 deletions.
32 changes: 3 additions & 29 deletions x-pack/metricbeat/module/aws/billing/_meta/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,9 @@
"@timestamp": "2017-10-12T08:05:34.853Z",
"aws": {
"billing": {
"AmortizedCost": {
"amount": 0.6972584049,
"unit": "USD"
},
"BlendedCost": {
"amount": 0.6972584049,
"unit": "USD"
},
"NormalizedUsageAmount": {
"amount": 12,
"unit": "N/A"
},
"UnblendedCost": {
"amount": 0.6972584049,
"unit": "USD"
},
"UsageQuantity": {
"amount": 312.7242056683,
"unit": "N/A"
},
"end_date": "2021-06-16",
"group_by": {
"AZ": "eu-central-1"
},
"group_definition": {
"key": "AZ",
"type": "DIMENSION"
},
"start_date": "2021-06-15"
"Currency": "USD",
"EstimatedCharges": 39.26,
"ServiceName": "AmazonEKS"
}
},
"cloud": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"aws": {
"billing": {
"Currency": "USD",
"EstimatedCharges": 0.42,
"ServiceName": "AWSCostExplorer"
"EstimatedCharges": 39.26,
"ServiceName": "AmazonEKS"
}
},
"cloud": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,34 @@
"aws": {
"billing": {
"AmortizedCost": {
"amount": 4.896,
"amount": 51.6,
"unit": "USD"
},
"BlendedCost": {
"amount": 4.896,
"amount": 51.6,
"unit": "USD"
},
"NormalizedUsageAmount": {
"amount": 768,
"amount": 672,
"unit": "N/A"
},
"UnblendedCost": {
"amount": 4.896,
"amount": 51.6,
"unit": "USD"
},
"UsageQuantity": {
"amount": 48,
"amount": 168,
"unit": "N/A"
},
"end_date": "2021-06-16",
"end_date": "2021-06-22",
"group_by": {
"INSTANCE_TYPE": "a1.2xlarge"
"INSTANCE_TYPE": "db.r5.large"
},
"group_definition": {
"key": "INSTANCE_TYPE",
"type": "DIMENSION"
},
"start_date": "2021-06-15"
"start_date": "2021-06-21"
}
},
"cloud": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,34 @@
"aws": {
"billing": {
"AmortizedCost": {
"amount": 84.2729296703,
"amount": 86.0273421729,
"unit": "USD"
},
"BlendedCost": {
"amount": 84.2395483165,
"amount": 86.003330576,
"unit": "USD"
},
"NormalizedUsageAmount": {
"amount": 1896,
"unit": "N/A"
},
"UnblendedCost": {
"amount": 84.2729296703,
"amount": 86.0273421729,
"unit": "USD"
},
"UsageQuantity": {
"amount": 1821718.8538252518,
"amount": 1813824.6596604364,
"unit": "N/A"
},
"end_date": "2021-06-16",
"end_date": "2021-06-22",
"group_by": {
"LINKED_ACCOUNT": "428152502467"
},
"group_definition": {
"key": "LINKED_ACCOUNT",
"type": "DIMENSION"
},
"start_date": "2021-06-15"
"start_date": "2021-06-21"
}
},
"cloud": {
Expand Down
38 changes: 12 additions & 26 deletions x-pack/metricbeat/module/aws/billing/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,35 +426,21 @@ func generateEventID(eventID string) string {
}

func (m *MetricSet) getAccountName(svc organizationsiface.ClientAPI) map[string]string {
init := true
nextToken := ""
accounts := map[string]string{}
for nextToken != "" || init {
init = false

// construct ListAccountsInput
ListAccountsInput := &organizations.ListAccountsInput{}
if nextToken != "" {
ListAccountsInput.NextToken = awssdk.String(nextToken)
}

// make API request
req := svc.ListAccountsRequest(ListAccountsInput)
resp, err := req.Send(context.TODO())
if err != nil {
m.logger.Warnf("failed ListAccountsRequest, organizations:ListAccounts permission is required", err)
return accounts
}
// construct ListAccountsInput
ListAccountsInput := &organizations.ListAccountsInput{}
req := svc.ListAccountsRequest(ListAccountsInput)
p := organizations.NewListAccountsPaginator(req)

// get token for next API call, if resp.NextToken is nil, nextToken set to ""
nextToken = ""
if resp.NextToken != nil {
nextToken = *resp.NextToken
}

for _, a := range resp.Accounts {
accounts := map[string]string{}
for p.Next(context.TODO()) {
page := p.CurrentPage()
for _, a := range page.Accounts {
accounts[*a.Id] = *a.Name
}
}

if err := p.Err(); err != nil {
m.logger.Warnf("failed ListAccountsRequest", err)
}
return accounts
}

0 comments on commit a540a42

Please sign in to comment.