0

我正在运行一个程序来检索我最新的定期发票,然后我想查看发票中的每个项目并获取主机名列表。这是简化的形式:

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)

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' => ["01/01/2016"]}, {'name' => 'endDate', 'value' => ["01/02/2016"]}])

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"]

    for i in 0..(billing_items_count/10000.0).ceil - 1
      billing_items = invoice_reference.result_limit(i*10000, 10000).object_mask("mask[id,hostName]").getItems()
      billing_items.each do | billing_item |
        if billing_item["hostName"]
          pp billing_item
        end
      end
    end
  end
end

这是文档的相应链接:http: //sldn.softlayer.com/reference/services/SoftLayer_Billing_Invoice/getItems

结果是主机名列表,但值已被小写。这阻止了我,因为我稍后会调用 find_servers 并从上面传递这个“hostName”值。由于大小写不匹配,因此无法找到服务器详细信息。我已经进行了如下测试以确认此行为:

softlayer_client = SoftLayer::Client.new(:username => user, :api_key => api_key)

found_downcased_version = SoftLayer::VirtualServer.find_servers({ :client => softlayer_client, :hostname => "host-hadr-1" })

if !found_downcased_version.empty?
  pp "FOUND DOWNCASED VERSION OF HOSTNAME"
end

found_unchanged_case_version = SoftLayer::VirtualServer.find_servers({ :client => softlayer_client, :hostname => "host-HADR-1" })

if !found_unchanged_case_version.empty?
  pp "FOUND UNCHANGED CASE VERSION OF HOSTNAME"
end

这似乎是一个问题,SoftLayer 将返回一个更改了主机名大小写的结果。

1)有没有办法让我的 find_servers 调用不区分大小写?

或者

2) 有没有办法从发票中获取 virtual_guest/hardware ID?因此,我可以使用 server_with_id 而不是使用带有主机名参数的 find_server 。

谢谢!

4

1 回答 1

0

getItems 方法返回一个 SoftLayer_Billing_Invoice_Item 数组,该对象具有属性 billingItemId(请参阅http://sldn.softlayer.com/reference/datatypes/SoftLayer_Billing_Invoice_Item),这是生成此发票项目的计费项目。现在 billingItemId 必须与您帐户中的服务器或 VSI 相关联的 billingItemId 匹配。因此,您可以使用 billingItemId 找到您的服务器或 VSI

看看这段代码:

# To get the VSI
billingItemId = invoiceItem["billingItemId"]
account_service = softlayer_client.service_named("Account")
filter = SoftLayer::ObjectFilter.new {|f| f.accept("virtualGuests.billingItem.id").when_it is(billingItemId)}
# The method returns an array of VSIs, but because the filter the arrays should contain only one VSI
vsis = account_service.object_filter(filter).getVirtualGuests()
myVSI = vsi[0]


# To get the Server
billingItemId = invoiceItem["billingItemId"]
filter = SoftLayer::ObjectFilter.new {|f| f.accept("hardware.billingItem.id").when_it is(billingItemId)}
hardwares = account_service.object_filter(filter).getHardware()
myHardware = hardwares[0]

我希望它有帮助

于 2016-01-30T02:07:31.003 回答