我有兴趣获取 Azure 成本管理数据(尤其是摊销成本)。乍一看,AzureR 包集似乎是一个很好的起点。
是否有可能以某种方式使用 AzureAuth 来授权我,然后尝试使用 REST API?至于现在,我什至没有设法正确使用 AzureAuth,因为我不了解参数。
你们中有人在 R 中处理这些数据吗?你如何获得它?在 Azure 门户中保存 CSV 文件是一种糟糕的体验,因为设置正确的过滤器需要花费大量时间,而且 Microsoft 会不时更改导出架构。例如,今天按标签分组时,很少有列消失(ia 资源类型)。
任何帮助将不胜感激:)
编辑:感谢 Hong Ooi(https://github.com/Azure/AzureR/issues/6)的帮助,我现在设法使用 AzureRMR 连接到成本管理 API:
library(AzureRMR)
az <- create_azure_login()
sub1 <- az$get_subscription(my_sub_guid)
d1 <- sub1$do_operation("providers/Microsoft.CostManagement/query", api_version = "2019-11-01", http_verb = "POST", encode = "json",
body = list(
timeframe = "Custom",
timePeriod = list(
from = "2020-04-01",
to = "2020-04-01"
),
type = "AmortizedCost",
dataset = list(
granularity = "daily"
)
))
我仍然不知道的是:
- 如何将正文作为 JSON 而不是列表列表发送?
- 如何以 JSON 格式接收响应?
- 如何在响应中接收更多列?我特别需要资源名称和资源类型。
编辑 2:响应示例:
response_example <-
list(properties = list(
nextLink = NULL,
columns = list(
list(name = "UsageDate",
type = "Number"),
list(name = "Currency",
type = "String")
),
rows = list(
list(as.integer(20200401),
"EUR"),
list(as.integer(20200402),
"EUR")
)
))
我想要做的是从响应中获取数据框。我找到了可行的解决方案,但看起来很糟糕:
d1_ <- do.call(what = "rbind", args = lapply(d1$properties$rows, as_tibble, .name_repair = "unique"))
colnames(d1_) <- do.call(what = "rbind", args = lapply(d1$properties$columns, as_tibble, .name_repair = "unique")) %>%
select(name) %>% pull()
编辑 3:如果有人需要,我找到了一种方法来询问特定的列。身体是:
{
"type":"AmortizedCost",
"dataSet":{
"granularity":"Daily",
"aggregation":{
"PreTaxCost":{
"name":"PreTaxCost",
"function":"Sum"
}
},
"sorting":[
{
"direction":"ascending",
"name":"UsageDate"
}
],
"grouping":[
{
"type":"TagKey",
"name":"myTag"
},
{
"type":"Dimension",
"name":"ResourceType"
},
{
"type":"Dimension",
"name":"ResourceGroupName"
}
]
},
"timeframe":"Custom",
"timePeriod":{
"from":"2020-04-01T00:00:00+00:00",
"to":"2020-04-30T23:59:59+00:00"
}
}
有效参数(列名)是:
AccountName, BillingAccountId, BillingAccountName, BillingMonth, BillingPeriod, BillingProfileId, BillingProfileName, ChargeType, ConsumedService, CostAllocationRuleId, CostAllocationRuleName, CustomerName, CustomerTenantDomainName, CustomerTenantId, DepartmentName, EnrollmentAccountName, Frequency, InvoiceId, InvoiceNumber, InvoiceSection, InvoiceSectionId, InvoiceSectionName, MarkupRuleId, MarkupRuleName, Meter, MeterCategory, MeterId, MeterSubcategory, PartNumber, PartnerEarnedCreditApplied, PartnerName, PricingModel, Product, ProductOrderId, ProductOrderName, PublisherType, ResellerMPNId, ReservationId, ReservationName, ResourceGroup, ResourceGroupName, ResourceGuid, ResourceId, ResourceLocation, ResourceType, ServiceFamily, ServiceName, ServiceTier, SubscriptionId, SubscriptionName, UnitOfMeasure
另外,这就是我处理响应的方式: Turning Azure Cost Management API's response into data frame