我在使用GetLocations调用时遇到了麻烦。每当我尝试执行它时,都会收到错误消息:

SoftLayer_Exception:对象不存在,无法在其上执行方法。
(SoftLayer_Location_Group::getLocations)(HTTP 200)

这使我认为我创建的locationService对象出了点问题,但是我不明白是什么。有人看到这个问题吗?

package main

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

func main() {
    sess := session.New("user", "password")
    locationService := services.GetLocationGroupService(sess)
    locations, err := locationService.GetLocations()
    if err != nil {
        fmt.Printf("%s\n",err.Error())
        return
    }
    fmt.Printf("%+v", locations)
}

最佳答案

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

将此示例添加到您的代码中,例如以下示例:

locationGroupId := 1

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

// Get SoftLayer_Location_Group
service := services.GetLocationGroupService(sess)

result, err := service.Id(locationGroupId).GetLocations()

参考:

https://softlayer.github.io/reference/services/SoftLayer_Location_Group/getLocations/

要获得所有可用的locationGroupId,可以使用以下go代码示例:
package main

/*
GetAllObjects

Retrieve all locationGroup objects.

Important manual pages:
https://softlayer.github.io/reference/services/SoftLayer_Location_Group/
https://softlayer.github.io/reference/services/SoftLayer_Location_Group/getAllObjects/

License: http://sldn.softlayer.com/article/License
Author: SoftLayer Technologies, Inc. <[email protected]>
*/

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.GetLocationGroupService(sess)

    result, err := service.GetAllObjects()
    if err != nil {
        fmt.Printf("\n Unable to retrieve all locationGroups:\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))
}

关于go - GetLocations失败,并显示“对象不存在,无法在其上执行方法”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50608572/

10-12 00:53