Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build set request from a prototext file #373

Merged
merged 1 commit into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/app/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func (a *App) InitSetFlags(cmd *cobra.Command) {
cmd.Flags().StringArrayVarP(&a.Config.LocalFlags.SetRequestFile, "request-file", "", []string{}, "set request template file(s)")
cmd.Flags().StringVarP(&a.Config.LocalFlags.SetRequestVars, "request-vars", "", "", "set request variables file")
cmd.Flags().BoolVarP(&a.Config.LocalFlags.SetDryRun, "dry-run", "", false, "prints the set request without initiating a gRPC connection")
cmd.Flags().StringArrayVarP(&a.Config.LocalFlags.SetRequestProtoFile, "proto-file", "", []string{}, "set request from prototext file")
//
cmd.Flags().StringArrayVarP(&a.Config.LocalFlags.SetReplaceCli, "replace-cli", "", []string{}, "a cli command to be sent as a set replace request")
cmd.Flags().StringVarP(&a.Config.LocalFlags.SetReplaceCliFile, "replace-cli-file", "", "", "path to a file containing a list of commands that will be sent as a set replace request")
Expand Down
7 changes: 6 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ type LocalFlags struct {
SetTarget string `mapstructure:"set-target,omitempty" json:"set-target,omitempty" yaml:"set-target,omitempty"`
SetRequestFile []string `mapstructure:"set-request-file,omitempty" json:"set-request-file,omitempty" yaml:"set-request-file,omitempty"`
SetRequestVars string `mapstructure:"set-request-vars,omitempty" json:"set-request-vars,omitempty" yaml:"set-request-vars,omitempty"`
SetRequestProtoFile []string `mapstructure:"set-proto-request-file,omitempty" yaml:"set-proto-request-file,omitempty" json:"set-proto-request-file,omitempty"`
SetDryRun bool `mapstructure:"set-dry-run,omitempty" json:"set-dry-run,omitempty" yaml:"set-dry-run,omitempty"`
SetReplaceCli []string `mapstructure:"set-replace-cli,omitempty" yaml:"set-replace-cli,omitempty" json:"set-replace-cli,omitempty"`
SetReplaceCliFile string `mapstructure:"set-replace-cli-file,omitempty" yaml:"set-replace-cli-file,omitempty" json:"set-replace-cli-file,omitempty"`
Expand Down Expand Up @@ -583,6 +584,9 @@ func (c *Config) execValueTemplate(tplString string, input interface{}) (string,
}

func (c *Config) CreateSetRequest(targetName string) ([]*gnmi.SetRequest, error) {
if len(c.SetRequestProtoFile) > 0 {
return c.CreateSetRequestFromProtoFile()
}
if len(c.SetRequestFile) > 0 {
return c.CreateSetRequestFromFile(targetName)
}
Expand Down Expand Up @@ -854,7 +858,8 @@ func (c *Config) ValidateSetInput() error {
len(c.LocalFlags.SetReplaceCli) == 0 &&
len(c.LocalFlags.SetUpdateCli) == 0 &&
len(c.LocalFlags.SetReplaceCliFile) == 0 &&
len(c.LocalFlags.SetUpdateCliFile) == 0 {
len(c.LocalFlags.SetUpdateCliFile) == 0 &&
len(c.LocalFlags.SetRequestProtoFile) == 0 {
return errors.New("no paths or request file provided")
}
if len(c.LocalFlags.SetUpdateFile) > 0 && len(c.LocalFlags.SetUpdateValue) > 0 {
Expand Down
18 changes: 18 additions & 0 deletions pkg/config/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"strings"
"text/template"

"google.golang.org/protobuf/encoding/prototext"
"gopkg.in/yaml.v2"

"github.com/openconfig/gnmi/proto/gnmi"
Expand Down Expand Up @@ -235,3 +236,20 @@ type templateInput struct {
TargetName string
Vars map[string]interface{}
}

func (c *Config) CreateSetRequestFromProtoFile() ([]*gnmi.SetRequest, error) {
reqs := make([]*gnmi.SetRequest, 0, len(c.SetRequestProtoFile))
for _, r := range c.SetRequestProtoFile {
b, err := os.ReadFile(r)
if err != nil {
return nil, err
}
req := new(gnmi.SetRequest)
err = prototext.Unmarshal(b, req)
if err != nil {
return nil, err
}
reqs = append(reqs, req)
}
return reqs, nil
}