From ebba12d23d1ef4778fdea75a5927972ecb409623 Mon Sep 17 00:00:00 2001 From: Nikhil Malik Date: Thu, 7 Mar 2024 12:46:37 +0900 Subject: [PATCH] BFD session config with yaml file --- cmd/create/create_file.go | 11 +++++++++++ cmd/delete/delete_bfd.go | 2 +- cmd/delete/delete_file.go | 28 ++++++++++++++++++++++++++++ cmd/dump/apply_create.go | 4 ++++ cmd/set/set_bfd.go | 5 ++--- pkg/api/bfd.go | 19 ++++++++++++------- 6 files changed, 58 insertions(+), 11 deletions(-) diff --git a/cmd/create/create_file.go b/cmd/create/create_file.go index 47ed697..1641093 100644 --- a/cmd/create/create_file.go +++ b/cmd/create/create_file.go @@ -193,3 +193,14 @@ func VxlanBridgeCreateWithFile(restOptions *api.RESTOptions, byteBuf []byte) err } return nil } + +func BFDCreateWithFile(restOptions *api.RESTOptions, byteBuf []byte) error { + var c api.ConfigurationBFDFile + if err := yaml.Unmarshal(byteBuf, &c); err != nil { + return err + } + if _, err := CreateBFDAPICall(restOptions, c.Spec); err != nil { + return err + } + return nil +} \ No newline at end of file diff --git a/cmd/delete/delete_bfd.go b/cmd/delete/delete_bfd.go index d95f8de..442a356 100644 --- a/cmd/delete/delete_bfd.go +++ b/cmd/delete/delete_bfd.go @@ -68,7 +68,7 @@ ex) loxicmd delete bfd 32.32.32.2 --instance=default" resp, err := client.BFDSession().SubResources(subResources).Query(qmap).Delete(ctx) if err != nil { - fmt.Printf("Error: Failed to delete Firewall") + fmt.Printf("Error: Failed to delete bfd session") return } defer resp.Body.Close() diff --git a/cmd/delete/delete_file.go b/cmd/delete/delete_file.go index 7427b94..3ece260 100644 --- a/cmd/delete/delete_file.go +++ b/cmd/delete/delete_file.go @@ -73,6 +73,8 @@ func DeleteFileConfig(file string, restOptions *api.RESTOptions) error { err = VxlanPeerDeleteWithFile(restOptions, byteBuf) case "Vxlan", "vxlan": err = VxlanDeleteWithFile(restOptions, byteBuf) + case "bfd", "BFD": + err = BFDDeleteWithFile(restOptions, byteBuf) default: fmt.Printf("Not Supported\n") } @@ -432,3 +434,29 @@ func VxlanPeerDeleteWithFile(restOptions *api.RESTOptions, byteBuf []byte) error } return nil } + +func BFDDeleteWithFile(restOptions *api.RESTOptions, byteBuf []byte) error { + var c api.ConfigurationBFDFile + if err := yaml.Unmarshal(byteBuf, &c); err != nil { + return err + } + client, ctx, cancel := GetClientWithCtx(restOptions) + if restOptions.Timeout > 0 { + defer cancel() + } + + subResources := []string{ + "remoteIP", c.Spec.RemoteIP, + } + + qmap := map[string]string{} + qmap["instance"] = c.Spec.Instance + + resp, err := client.BFDSession().SubResources(subResources).Query(qmap).Delete(ctx) + if err != nil { + fmt.Printf("Error: Failed to delete bfd session") + return nil + } + defer resp.Body.Close() + return nil +} diff --git a/cmd/dump/apply_create.go b/cmd/dump/apply_create.go index 3f60465..c67c9af 100644 --- a/cmd/dump/apply_create.go +++ b/cmd/dump/apply_create.go @@ -16,6 +16,7 @@ package dump import ( + "errors" "fmt" "loxicmd/cmd/create" "loxicmd/pkg/api" @@ -71,8 +72,11 @@ func ApplyFileConfig(file string, restOptions *api.RESTOptions) error { err = create.VxlanPeerCreateWithFile(restOptions, byteBuf) case "Vxlan", "vxlan": err = create.VxlanBridgeCreateWithFile(restOptions, byteBuf) + case "BFD", "bfd": + err = create.BFDCreateWithFile(restOptions, byteBuf) default: fmt.Printf("Not Supported\n") + return errors.New("not supported") } if err != nil { fmt.Println(err.Error()) diff --git a/cmd/set/set_bfd.go b/cmd/set/set_bfd.go index 1780ef2..19510de 100644 --- a/cmd/set/set_bfd.go +++ b/cmd/set/set_bfd.go @@ -40,7 +40,7 @@ func NewSetBFDCmd(restOptions *api.RESTOptions) *cobra.Command { Aliases: []string{"bfd-session"}, Run: func(cmd *cobra.Command, args []string) { - + // Make bfdMod if err := ReadSetBfdOptions(&o, args); err != nil { fmt.Printf("Error: %s\n", err.Error()) @@ -60,7 +60,7 @@ func NewSetBFDCmd(restOptions *api.RESTOptions) *cobra.Command { } SetBFDCmd.Flags().StringVarP(&o.Instance, "instance", "", "default", "Specify the cluster instance name") SetBFDCmd.Flags().Uint64VarP(&o.Interval, "interval", "", 0, "Specify the BFD packet tx interval (in microseconds)") - SetBFDCmd.Flags().Uint8VarP(&o.RetryCount, "retryCount", "", 0, "Specify the number of reties") + SetBFDCmd.Flags().Uint8VarP(&o.RetryCount, "retryCount", "", 0, "Specify the number of retries") return SetBFDCmd } @@ -81,7 +81,6 @@ func ReadSetBfdOptions(o *api.BFDSessionInfo, args []string) error { return nil } - func SetBFDAPICall(restOptions *api.RESTOptions, bfdModel api.BFDSessionInfo) (*http.Response, error) { client := api.NewLoxiClient(restOptions) ctx := context.TODO() diff --git a/pkg/api/bfd.go b/pkg/api/bfd.go index 51af8d7..0a40454 100644 --- a/pkg/api/bfd.go +++ b/pkg/api/bfd.go @@ -28,24 +28,29 @@ type BFDSessionGet struct { type BFDSessionInfo struct { // Instance name - Instance string `json:"instance,omitempty"` + Instance string `json:"instance" yaml:"instance"` // RemoteIP - Remote IP for BFD session - RemoteIP string `json:"remoteIp,omitempty"` + RemoteIP string `json:"remoteIp" yaml:"remoteIp"` // Interval - Tx Interval between BFD packets - SourceIP string `json:"sourceIp,omitempty"` + SourceIP string `json:"sourceIp" yaml:"sourceIp"` // Port - BFD session port - Port uint16 `json:"port,omitempty"` + Port uint16 `json:"port" yaml:"port"` // Interval - Tx Interval between BFD packets - Interval uint64 `json:"interval,omitempty"` + Interval uint64 `json:"interval" yaml:"interval"` // RetryCount - Retry Count for detecting failure - RetryCount uint8 `json:"retryCount,omitempty"` + RetryCount uint8 `json:"retryCount" yaml:"retryCount"` // Current BFD State - State string `json:"state,omitempty"` + State string `json:"state" yaml:"state"` } +type ConfigurationBFDFile struct { + TypeMeta `yaml:",inline"` + ObjectMeta `yaml:"metadata,omitempty"` + Spec BFDSessionInfo `yaml:"spec"` +}