diff --git a/pkg/config/datastore.go b/pkg/config/datastore.go index d914baae..08069e21 100644 --- a/pkg/config/datastore.go +++ b/pkg/config/datastore.go @@ -76,7 +76,8 @@ type SBI struct { } type SBIGnmiOptions struct { - Encoding string `yaml:"encoding,omitempty" json:"encoding,omitempty"` + Encoding string `yaml:"encoding,omitempty" json:"encoding,omitempty"` + TargetName string `yaml:"target,omitempty" json:"target,omitempty"` } type SBINetconfOptions struct { diff --git a/pkg/datastore/target/gnmi/gnmi.go b/pkg/datastore/target/gnmi/gnmi.go index 9dccc65a..5974eff2 100644 --- a/pkg/datastore/target/gnmi/gnmi.go +++ b/pkg/datastore/target/gnmi/gnmi.go @@ -217,6 +217,12 @@ func (t *gnmiTarget) Set(ctx context.Context, source targetTypes.TargetSource) ( Delete: make([]*gnmi.Path, 0, len(deletes)), Update: make([]*gnmi.Update, 0, len(upds)), } + + // check if the target name is set, then add the prefix field + if t.cfg.GnmiOptions.TargetName != "" { + setReq.Prefix = &gnmi.Path{Target: t.cfg.GnmiOptions.TargetName} + } + for _, del := range deletes { gdel := utils.ToGNMIPath(del) setReq.Delete = append(setReq.Delete, gdel) diff --git a/pkg/server/datastore.go b/pkg/server/datastore.go index de1b3550..10746556 100644 --- a/pkg/server/datastore.go +++ b/pkg/server/datastore.go @@ -102,52 +102,56 @@ func (s *Server) CreateDataStore(ctx context.Context, req *sdcpb.CreateDataStore return nil, status.Errorf(codes.InvalidArgument, "datastore %s already exists", name) } + reqTarget := req.GetTarget() sbi := &config.SBI{ - Type: req.GetTarget().GetType(), - Port: req.GetTarget().GetPort(), - Address: req.GetTarget().GetAddress(), + Type: reqTarget.GetType(), + Port: reqTarget.GetPort(), + Address: reqTarget.GetAddress(), } - switch strings.ToLower(req.GetTarget().GetType()) { + switch strings.ToLower(reqTarget.GetType()) { case "netconf": + netconfOpts := reqTarget.GetNetconfOpts() commitDatastore := "candidate" - switch req.GetTarget().GetNetconfOpts().GetCommitCandidate() { + switch netconfOpts.GetCommitCandidate() { case sdcpb.CommitCandidate_COMMIT_CANDIDATE: case sdcpb.CommitCandidate_COMMIT_RUNNING: commitDatastore = "running" default: - log.Error(nil, "unknown commitDatastore", "datastore", req.GetTarget().GetNetconfOpts().GetCommitCandidate()) - return nil, fmt.Errorf("unknown commitDatastore: %v", req.GetTarget().GetNetconfOpts().GetCommitCandidate()) + log.Error(nil, "unknown commitDatastore", "datastore", netconfOpts.GetCommitCandidate()) + return nil, fmt.Errorf("unknown commitDatastore: %v", netconfOpts.GetCommitCandidate()) } sbi.NetconfOptions = &config.SBINetconfOptions{ - IncludeNS: req.GetTarget().GetNetconfOpts().GetIncludeNs(), - OperationWithNamespace: req.GetTarget().GetNetconfOpts().GetOperationWithNs(), - UseOperationRemove: req.GetTarget().GetNetconfOpts().GetUseOperationRemove(), + IncludeNS: netconfOpts.GetIncludeNs(), + OperationWithNamespace: netconfOpts.GetOperationWithNs(), + UseOperationRemove: netconfOpts.GetUseOperationRemove(), CommitDatastore: commitDatastore, } case "gnmi": + gnmiOpts := reqTarget.GetGnmiOpts() sbi.GnmiOptions = &config.SBIGnmiOptions{ - Encoding: req.GetTarget().GetGnmiOpts().GetEncoding(), + Encoding: gnmiOpts.GetEncoding(), + TargetName: gnmiOpts.GetTargetName(), } default: - log.Error(nil, "unknowm targetconnection protocol type", "type", req.GetTarget().GetType()) - return nil, fmt.Errorf("unknowm targetconnection protocol type %s", req.GetTarget().GetType()) + log.Error(nil, "unknowm targetconnection protocol type", "type", reqTarget.GetType()) + return nil, fmt.Errorf("unknowm targetconnection protocol type %s", reqTarget.GetType()) } - if req.GetTarget().GetTls() != nil { + if tls := reqTarget.GetTls(); tls != nil { sbi.TLS = &config.TLS{ - CA: req.GetTarget().GetTls().GetCa(), - Cert: req.GetTarget().GetTls().GetCert(), - Key: req.GetTarget().GetTls().GetKey(), - SkipVerify: req.GetTarget().GetTls().GetSkipVerify(), + CA: tls.GetCa(), + Cert: tls.GetCert(), + Key: tls.GetKey(), + SkipVerify: tls.GetSkipVerify(), } } - if req.GetTarget().GetCredentials() != nil { + if creds := reqTarget.GetCredentials(); creds != nil { sbi.Credentials = &config.Creds{ - Username: req.GetTarget().GetCredentials().GetUsername(), - Password: req.GetTarget().GetCredentials().GetPassword(), - Token: req.GetTarget().GetCredentials().GetToken(), + Username: creds.GetUsername(), + Password: creds.GetPassword(), + Token: creds.GetToken(), } }