Search code examples
goibm-cloud-infrastructure

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


I am having trouble with the GetLocations call. Every time I try to execute it I receive the error:

SoftLayer_Exception: Object does not exist to execute method on. (SoftLayer_Location_Group::getLocations) (HTTP 200)

This makes me think that there is something wrong with the locationService object I created but I don't understand what. Does anyone see the issue?

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

Solution

  • The error that you got is because you need to use an identifier locationGroup ID.

    Add this locationGroupId to you go code, like this example:

    locationGroupId := 1
    
    // Create a session
    sess := session.New(username, apikey)
    
    // Get SoftLayer_Location_Group
    service := services.GetLocationGroupService(sess)
    
    result, err := service.Id(locationGroupId).GetLocations()
    

    Reference:

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

    To get all locationGroupIds available you can use this go code example:

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