GetLocations失败,“对象不存在,无法执行方法”

时间:2018-05-30 15:34:35

标签: go ibm-cloud-infrastructure

我遇到了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)
}

1 个答案:

答案 0 :(得分:0)

您收到的错误是因为您需要使用标识符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))
}