如何检索 SoftLayer 机器的全部成本,包括带宽超额等任何额外成本?

How to retrieve entire cost for a SoftLayer machine, including any extra costs such as bandwidth overages?

我使用 Ruby softlayer gem 在我们的 SoftLayer 帐户上检索月度发票成本信息已经有一段时间了。但是,团队中有人担心我们可能会遗漏某些成本,例如网络利用率的任何超额成本。我想知道我正在做的是正确地收集所有费用并且我们没有遗漏任何东西。这是我的 code/query:

account = SoftLayer::Service.new("SoftLayer_Account",:username => user, :api_key => api_key, :timeout => 999999999)
softlayer_client = SoftLayer::Client.new(:username => user, :api_key => api_key, :timeout => 999999999)
billing_invoice_service = softlayer_client.service_named("Billing_Invoice")

object_filter = SoftLayer::ObjectFilter.new
object_filter.set_criteria_for_key_path('invoices.createDate', 'operation' => 'betweenDate', 'options' => [{'name' => 'startDate', 'value' => ["#{startTime}"]}, {'name' => 'endDate', 'value' => ["#{endTime}"]}])

# Set startDate and endDate around the beginning of the month in search of the "Recurring" invoice that should appear on the 1st.

invoices = account.result_limit(0,10000).object_filter(object_filter).object_mask("mask[id,typeCode,itemCount,invoiceTotalAmount,closedDate,createDate]").getInvoices

invoices.each do | invoice |
  if invoice["typeCode"] == "RECURRING"
    invoice_reference = billing_invoice_service.object_with_id(invoice["id"])
    invoice_object = invoice_reference.object_mask("mask[itemCount]").getObject
    billing_items_count = invoice_object["itemCount"]
    billing_machines_map = Hash.new
    all_billing_items = Array.new

    # Search for billing items containing a hostName value.  
    # The corresponding billing item ID will become the key of a new hash.  
    # Child costs will be added to the existing costs.
    billing_items_retrieval_operation = proc {
      for i in 0..(billing_items_count/8000.0).ceil - 1
        billing_items = invoice_reference.result_limit(i*8000, 8000).object_mask("mask[id,resourceTableId,billingItemId,parentId,categoryCode,hostName,domainName,hourlyRecurringFee,laborFee,oneTimeFee,recurringFee,recurringTaxAmount,setupFee,setupTaxAmount,location[name]]").getItems()
        billing_items.each do | billing_item |
          if billing_item["hostName"]
            billing_machines_map[billing_item["id"]] = billing_item
          end
        end
        all_billing_items.concat(billing_items)
      end
    }

    # Look for items with parentIds or resourceTableIds.
    # Both Ids represent a "parent" of the item.
    # Give higher importance to parentId.
    billing_items_retrieval_callback = proc {
      cost_of_billing_items_without_parent = BigDecimal.new("0.00")
      all_billing_items.each do | billing_item |
        if billing_item["parentId"] != ""
          parent_billing_machine = billing_machines_map[billing_item["parentId"]]
          if parent_billing_machine                                parent_billing_machine["recurringFee"] = (BigDecimal.new(parent_billing_machine["recurringFee"]) + BigDecimal.new(billing_item["recurringFee"])).to_s('F')
            parent_billing_machine["setupFee"] = (BigDecimal.new(parent_billing_machine["setupFee"]) + BigDecimal.new(billing_item["setupFee"])).to_s('F')
            parent_billing_machine["laborFee"] = (BigDecimal.new(parent_billing_machine["laborFee"]) + BigDecimal.new(billing_item["laborFee"])).to_s('F')
            parent_billing_machine["oneTimeFee"] = (BigDecimal.new(parent_billing_machine["oneTimeFee"]) + BigDecimal.new(billing_item["oneTimeFee"])).to_s('F')
          end
        elsif billing_item["resourceTableId"] != ""
          parent_billing_machine = billing_machines_map[billing_item["resourceTableId"]]
          if parent_billing_machine
            parent_billing_machine["recurringFee"] = (BigDecimal.new(parent_billing_machine["recurringFee"]) + BigDecimal.new(billing_item["recurringFee"])).to_s('F')
            parent_billing_machine["setupFee"] = (BigDecimal.new(parent_billing_machine["setupFee"]) + BigDecimal.new(billing_item["setupFee"])).to_s('F')
            parent_billing_machine["laborFee"] = (BigDecimal.new(parent_billing_machine["laborFee"]) + BigDecimal.new(billing_item["laborFee"])).to_s('F')
            parent_billing_machine["oneTimeFee"] = (BigDecimal.new(parent_billing_machine["oneTimeFee"]) + BigDecimal.new(billing_item["oneTimeFee"])).to_s('F')
          end
        else
          cost_of_billing_items_without_parent = (BigDecimal.new(cost_of_billing_items_without_parent) + BigDecimal.new(billing_item["recurringFee"])).to_s('F')
          cost_of_billing_items_without_parent = (BigDecimal.new(cost_of_billing_items_without_parent) + BigDecimal.new(billing_item["setupFee"])).to_s('F')
          cost_of_billing_items_without_parent = (BigDecimal.new(cost_of_billing_items_without_parent) + BigDecimal.new(billing_item["laborFee"])).to_s('F')
          cost_of_billing_items_without_parent = (BigDecimal.new(cost_of_billing_items_without_parent) + BigDecimal.new(billing_item["oneTimeFee"])).to_s('F')
        end
      end
      pp "INVOICE: Total cost of devices for account without a parent is:"
      pp cost_of_billing_items_without_parent
    end
  end
end

在上述之后,我调用了 getVirtualGuests 和 getHardware 来为每台机器获取一些额外的元信息(我根据 billingItem.id 将它们联系在一起。示例:

billingItemId = billing_machine["billingItemId"]
account_service = softlayer_client.service_named("Account")
filter = SoftLayer::ObjectFilter.new {|f| f.accept("virtualGuests.billingItem.id").when_it is(billingItemId)}
virtual_guests_array = account_service.object_filter(filter).object_mask("mask[id, hostname, datacenter[name], billingItem[orderItem[order[userRecord[username]]]], tagReferences[tagId, tag[name]], primaryIpAddress, primaryBackendIpAddress]").getVirtualGuests()

如您所见,我没有拨打任何电话来收取带宽超额费用。我已经打印出从上述查询中获得的各种 "category" 值,但我没有看到任何特定于网络利用率的信息(可能没有额外的网络利用率成本,但我不确定)。

谢谢。

任何额外费用,例如带宽超支,都将包含在服务器的计费项目中。因此,您无需再调用 api 即可获取它。