SoftLayer_User_Customer :: GetObject softlayer因异常而失败

时间:2018-05-23 18:08:22

标签: api ibm-cloud-infrastructure

当我调用service.GetObject()方法时,它将返回Softlayer   例外。   任何人都可以帮助我解决我做错了什么。

sess := session.New(s.UserName, s.Apikey)

service := services.GetUserCustomerService(sess)
user, err := service.GetObject()

enter image description here

2 个答案:

答案 0 :(得分:1)

您遇到的错误是因为您需要使用用户ID标识符。

将此userId添加到您的代码中,例如:

   userId := 11111

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

   // Get SoftLayer_User_Customer service
   service := services.GetUserCustomerService(sess)

   result, err := service.Id(userId).GetObject()

参考:

https://softlayer.github.io/reference/services/SoftLayer_User_Customer/getObject/

如果您想获得帐户的特定userId,请使用以下代码示例: 你必须将你的用户名放在对象过滤器中¨Eq(“set me”)¨来获取特定的userId。

package main

/*
GetUsers

Retrieve an account’s portal users.

Important manual pages:
https://softlayer.github.io/reference/services/SoftLayer_Account/
https://softlayer.github.io/reference/services/SoftLayer_Account/getUsers/

License: http://sldn.softlayer.com/article/License
Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
*/

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

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

       filter := filter.Build(
                     filter.Path("users.username").Eq("set me"),
              )
       mask := "id"

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

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

       result, err := service.Mask(mask).Filter(filter).GetUsers()

       if err != nil {
              fmt.Printf("\n Unable to retrieve User:\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))
}

答案 1 :(得分:0)

我建议您使用primaryRouter.hostname而不是podName,过滤器如下所示:

package main

/*
GetPrivateSubnets

Retrieve all private subnets associated with an account.

Important manual pages:
https://softlayer.github.io/reference/services/SoftLayer_Account/
https://softlayer.github.io/reference/services/SoftLayer_Account/getPrivateSubnets/

License: http://sldn.softlayer.com/article/License
Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
*/

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

    "github.com/softlayer/softlayer-go/filter"
)

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

    mask := "mask[podName,datacenter[name],networkVlan]"

    filter := filter.Build( filter.Path("privateSubnets.datacenter.name").Eq("dal09"),
        filter.Path("privateSubnets.networkVlan.primaryRouter.hostname").Contains("bcr06"),
        )


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

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

    result, err := service.Mask(mask).Filter(filter).GetPrivateSubnets()
    if err != nil {
        fmt.Printf("\n Unable to retrieve the private subnets:\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))
}

bcr06表示子网通过pod06的后端网络(私有)进行连接。

响应如下:

"datacenter": {
            "name": "dal09"
        },
        "networkVlan": {
            "id": 111333322,
            "modifyDate": "2016-09-14T13:24:10-04:00",
            "primarySubnetId": 1225544,
            "vlanNumber": 111,
            "primaryRouter": {
                "domain": "softlayer.com",
                "fullyQualifiedDomainName": "bcr06a.dal09.softlayer.com",
                                "hostname": "bcr06a.dal09",
            }
        },
        "podName": "dal09.pod06"