GetLocations 失败 "Object does not exist to execute method on"

GetLocations fails with "Object does not exist to execute method on"

我在调用 GetLocations 时遇到问题。每次我尝试执行它时,我都会收到错误消息:

SoftLayer_Exception: Object does not exist to execute method on. (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 添加到你的代码中,就像这个例子:

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,您可以使用此代码示例:

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. <sldn@softlayer.com>
*/

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