如何添加对象掩码以使用 golang 调用 GetNetworkVlans

How to add object mask to call GetNetworkVlans using golang

func getPrivateVlan(env string) (string, error) {
// 1. Create a session
sess := session.New(user, pass)
// 2. Get a service
accountService := services.GetAccountService(sess)
// 3. Invoke a method:
vlans, err := accountService.GetNetworkVlans()
vlan := vlans[0]
log.Println(*vlan.Name)           //works
log.Println(*vlan.PrimaryRouter)  //Doesn't work
}

返回的对象是SoftLayer_Network_Vlan、https://sldn.softlayer.com/reference/datatypes/softlayer_network_vlan类型的vlan数组。 我能够访问 "Local Properties" 列(即名称)中的属性,但无法访问 "Relational & Count Properties" 列(即 PrimaryRouter)中的属性。 如何将对象掩码添加到我的调用中以查看这些属性?

原因是为了从 API 中的对象获取关系数据,您必须在 API 调用中声明一个对象掩码,据我所知,它不是在您的代码中声明,它应该是这样的:

func getPrivateVlan(env string) (string, error) {
// 1. Create a session
sess := session.New(user, pass)
// 2. Get a service
accountService := services.GetAccountService(sess)

//declare Mask
object_mask := "id;name;primaryRouter"

// 3. Invoke a method including the Object mask:
vlans, err := accountService.Mask(object_mask).GetNetworkVlans()

vlan := vlans[0]
log.Println(*vlan.Name)           //works
log.Println(*vlan.PrimaryRouter)  //
}

例如尝试以下代码:

package main

import (
"fmt"
"github.com/softlayer/softlayer-go/services"
"github.com/softlayer/softlayer-go/session"
"encoding/json"
)

func main() {
// SoftLayer API username and key
username := "set-me"
apikey   := "set-me"

// Create a session
sess := session.New(username, apikey)

// Get SoftLayer_Account service
service := services.GetAccountService(sess)

// Object-Mask to get specific Vlan's information
mask := "id;name;primaryRouter"

// Call to getNetworkVlans in order to retrieve vlans according to filter.
result, err := service.Mask(mask).GetNetworkVlans()
if err != nil {
    fmt.Printf("\n Unable to retrieve vlans:\n - %s\n", err)
    return
}

// Following helps to print the result in json format.
jsonFormat, jsonErr := json.MarshalIndent(result,"","     ")
if jsonErr != nil {
    fmt.Println(jsonErr)
    return
}
fmt.Println(string(jsonFormat))
}

更多信息请看下面:

https://sldn.softlayer.com/article/object-masks#Property_Set

来源:

F.Ojeda: 默认端点是 REST 但在您的环境中您使用的是 xmlrpc,这可能是由于 ~ / .softlayer 文件的存在,并且在该文件中它被配置为 XMLRPC enpoint。

有关更多信息,您可以查看以下文档:https://github.com/softlayer/softlayer-go

此问题发生在 XMLRPC enpoint 上,您可以随时报告它 GitHub。 https://github.com/softlayer/softlayer-go/issues/

在您的代码中尝试使用 REST 端点,就像这个例子:

endpoint := "https://api.softlayer.com/rest/v3"

// Create a session
sess := session.New(username, apikey, endpoint)