netbox-community / go-netbox

The official Go API client for Netbox IPAM and DCIM service.
Other
195 stars 146 forks source link

Context Deadline #43

Closed kedare closed 4 years ago

kedare commented 5 years ago

Hello,

I've been trying to use the Go client for Netbox without any success, I cannot get anything else than context deadline exceeded (instantly) when trying to query the Netbox host

A very simple code

package main

import (
    "fmt"
    "github.com/digitalocean/go-netbox/netbox"
    "github.com/digitalocean/go-netbox/netbox/client/ipam"
    "log"
)

func main() {
    nbox := netbox.NewNetboxWithAPIKey("10.1.0.1", "xxx")

    ipString := "10.1.0.1"
    ipInIPAM, err := nbox.IPAM.IPAMIPAddressesList(
            &ipam.IPAMIPAddressesListParams{
                Q: &ipString,
            }, nil)
        if err != nil {
            log.Fatalf("Error: %v", err)
        }
        fmt.Println(ipInIPAM)
}

Would returns

2019/05/09 09:31:00 Error: Get http://10.1.0.1/api/ipam/ip-addresses/?q=10.1.0.1: context deadline exceeded

But the URL is perfectly reachable and working fine

$ curl http://10.1.0.1/api/ipam/ip-addresses/?q=10.1.0.1
{"count":0,"next":null,"previous":null,"results":[]}

Am I missing something ? Also when the Go code runs, the query doesn't run at all (Not visible in HTTP logs)

Thanks.

bvandewalle commented 5 years ago

If you give params as an option, you also need to give a context:

&ipam.IPAMPrefixesListParams{
        Context: context.Background(),
    }

The SDK should clearly check if context has been set by the user though

kedare commented 5 years ago

Oh, ok.

I don't have access to the system anymore so I can't test the solution, but if it's just that, the issue can be closed :)

Indeed it would be useful to check that a context has been injected.

Thank you.