Closed JonasKs closed 1 year ago
Hi, I wonder, have you tried creating the request with gnmic API, since you're using gnmic API to create the target.
https://gnmic.openconfig.net/user_guide/golang_package/intro/#get-request https://gnmic.openconfig.net/user_guide/golang_package/examples/get/
This works:
package main
import (
"context"
"fmt"
"log"
"github.com/openconfig/gnmic/api"
"google.golang.org/protobuf/encoding/prototext"
)
func main() {
// create a target
tg, err := api.NewTarget(
api.Name("Router"),
api.Address("10.242.0.33:4126"),
api.Username("x"),
api.Password("x"),
api.Insecure(true),
api.SkipVerify(true),
)
if err != nil {
log.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create a gNMI client
err = tg.CreateGNMIClient(ctx)
if err != nil {
log.Fatal(err)
}
defer tg.Close()
// create a GetRequest
getReq, err := api.NewGetRequest(
api.Path("Cisco-IOS-XR-um-vrf-cfg:vrfs/vrf"),
api.Encoding("json_ietf"))
if err != nil {
log.Fatal(err)
}
fmt.Println(prototext.Format(getReq))
// send the created gNMI GetRequest to the created target
getResp, err := tg.Get(ctx, getReq)
if err != nil {
log.Fatal(err)
}
fmt.Println(prototext.Format(getResp))
}
Result:
This also work:
package main
import (
"context"
"fmt"
"log"
"github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/gnmic/api"
"google.golang.org/protobuf/encoding/prototext"
)
func main() {
// create a target
tg, err := api.NewTarget(
api.Name("Router"),
api.Address("10.242.0.33:4126"),
api.Username("x"),
api.Password("x"),
api.Insecure(true),
api.SkipVerify(true),
)
if err != nil {
log.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create a gNMI client
err = tg.CreateGNMIClient(ctx)
if err != nil {
log.Fatal(err)
}
defer tg.Close()
// create a GetRequest
getReq := &gnmi.GetRequest{
Path: []*gnmi.Path{
{
Origin: "Cisco-IOS-XR-um-vrf-cfg",
Elem: []*gnmi.PathElem{
{Name: "vrfs"},
{Name: "vrf"},
},
},
},
Type: gnmi.GetRequest_STATE,
Encoding: gnmi.Encoding_JSON_IETF,
}
if err != nil {
log.Fatal(err)
}
fmt.Println(prototext.Format(getReq))
// send the created gNMI GetRequest to the created target
getResp, err := tg.Get(ctx, getReq)
if err != nil {
log.Fatal(err)
}
fmt.Println(prototext.Format(getResp))
}
Result:
So I should use this instead? π€
Thanks for the quick reply!
You should use target.Get(ctx, req)
not target.Client.Get(ctx, req)
.
The former adds the credentials to the metadata (ctx), the latter does not.
You should also create the gNMI client (target.CreateGNMIClient()
) before calling the RPC functions.
PS: you don't need both api.Skipverify(true)
and api.Insecure(true)
You were right, that was the issue! Thank you so much. π
Also thank you for your talk on gNMIc, it is what made me look further than Python and NETCONF and eventually landed on using go and all these awesome libraries for this project. Stumbled upon it when looking for Python gNMI resources on youtube. π
You should also create the gNMI client (target.CreateGNMIClient()) before calling the RPC functions.
Yes, we do this, sorry I forgot to attach that.
PS: you don't need both api.Skipverify(true) and api.Insecure(true)
Thanks! Initially only had api.Skipverify(true)
, but tried everything when troubleshooting π
Hi π
I'm unable to perform GET request through gnmi, even though it works with gNMIc.
We're sending this request with gNMIc:
Attempting the same with gnmi(and ygot), generating the path with this:
and then calling the Cisco IOS XR device:
we keep getting the error
Error: failed to fetch from target: rpc error: code = Unauthenticated desc = gNMI: get: metadata credentials not present
.The target is created like this:
and the request body is formatted like this, when printed with
fmt.Println(prototext.Format(gnmiRequest))
looks correct:We're not sure what happens. Printing
Target.ConnState()
also saysREADY
, so it seems like it is connected, but the GET request fails for some reason. Anyone experienced this?