2016-08-02 18 views
0

我已經獲取我們的SoftLayer帳戶的每月發票成本信息使用Ruby SOFTLAYER寶石相當長的一段時間。然而,團隊擔心我們可能會遺漏某些成本,例如網絡利用率的過高。我想有一些想法,我正在做的是正確收集所有費用,我們不會錯過任何東西。這裏是我的代碼/查詢:如何檢索整個成本的SoftLayer的機器,包括任何額外成本,如帶寬超額?

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() 

正如你可以看到我不作任何呼叫捕捉帶寬超齡費。我已經打印出來的各種「類別」的價值觀,我從上面的查詢得到,但我沒有看到具體到網絡利用率任何東西(這是可能的存在沒有額外的網絡使用費用,但我不能肯定)。

謝謝。

回答

0

任何額外成本,如帶寬超額將被包括在從服務器的計費項目。所以你不需要再通過其他的API來獲得它。