diff --git a/Makefile b/Makefile index 87c0e7d..93de058 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ SHELL := /bin/bash loxilbid=$(shell docker ps -f name=$(dock) | grep -w $(dock) | cut -d " " -f 1 | grep -iv "CONTAINER") build: - @go build -o ${bin} -ldflags="-X 'loxicmd/cmd.BuildInfo=${shell date '+%Y_%m_%d'}-${shell git branch --show-current}-$(shell git show --pretty=format:%h --no-patch)' -X 'loxicmd/cmd.Version=${shell git describe --tags --abbrev=0}'" + @go build -o ${bin} -ldflags="-X 'loxicmd/cmd.BuildInfo=${shell date '+%Y_%m_%d'}-${shell git branch --show-current}-$(shell git show --pretty=format:%h --no-patch)'" test: go test diff --git a/cmd/create/create_loadbalancer.go b/cmd/create/create_loadbalancer.go index ad23140..78e0583 100644 --- a/cmd/create/create_loadbalancer.go +++ b/cmd/create/create_loadbalancer.go @@ -152,6 +152,8 @@ func NewCreateLoadBalancerCmd(restOptions *api.RESTOptions) *cobra.Command { persist - select the lb end-point based on sender n2 - select the lb end-point base on N2 interface params (only available with fullproxy mode) n3 - select the lb end-point base on N3 interface params + lc - select the lb end-point base on least connection + --mode value options onearm - LB put LB-IP as srcIP fullnat - LB put Service IP as scrIP diff --git a/cmd/get/get.go b/cmd/get/get.go index 85af116..597fcc1 100644 --- a/cmd/get/get.go +++ b/cmd/get/get.go @@ -69,6 +69,7 @@ func GetCmd(restOptions *api.RESTOptions) *cobra.Command { GetCmd.AddCommand(NewGetBGPNeighborCmd(restOptions)) GetCmd.AddCommand(NewGetHaStateCmd(restOptions)) GetCmd.AddCommand(NewGetBFDCmd(restOptions)) + GetCmd.AddCommand(NewGetVersionCmd(restOptions)) return GetCmd } diff --git a/cmd/get/get_lbversion.go b/cmd/get/get_lbversion.go new file mode 100644 index 0000000..8b80bd8 --- /dev/null +++ b/cmd/get/get_lbversion.go @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2022 NetLOX Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package get + +import ( + "context" + "encoding/json" + "fmt" + "io" + "loxicmd/pkg/api" + "net/http" + "time" + + "github.com/spf13/cobra" +) + +func NewGetVersionCmd(restOptions *api.RESTOptions) *cobra.Command { + var GetLBVersionCmd = &cobra.Command{ + Use: "lbversion", + Short: "Get a loxilb version ", + Long: `It shows version in the LoxiLB`, + Aliases: []string{"LBversion", "LBVersion", "llbversion"}, + Run: func(cmd *cobra.Command, args []string) { + client := api.NewLoxiClient(restOptions) + ctx := context.TODO() + var cancel context.CancelFunc + if restOptions.Timeout > 0 { + ctx, cancel = context.WithTimeout(context.TODO(), time.Duration(restOptions.Timeout)*time.Second) + defer cancel() + } + resp, err := client.LBVersion().Get(ctx) + if err != nil { + fmt.Printf("Error: %s\n", err.Error()) + return + } + if resp.StatusCode == http.StatusOK { + PrintGetVersionResult(resp, *restOptions) + return + } + + }, + } + + return GetLBVersionCmd +} + +func PrintGetVersionResult(resp *http.Response, o api.RESTOptions) { + Versionresp := api.LBVersionGet{} + var data [][]string + resultByte, err := io.ReadAll(resp.Body) + if err != nil { + fmt.Printf("Error: Failed to read HTTP response: (%s)\n", err.Error()) + return + } + + if err := json.Unmarshal(resultByte, &Versionresp); err != nil { + fmt.Printf("Error: Failed to unmarshal HTTP response: (%s)\n", err.Error()) + return + } + + // if json options enable, it print as a json format. + if o.PrintOption == "json" { + resultIndent, _ := json.MarshalIndent(Versionresp, "", " ") + fmt.Println(string(resultIndent)) + return + } + + // Table Init + table := TableInit() + + table.SetHeader(LBVERSION_TITLE) + data = append(data, []string{Versionresp.Version, Versionresp.BuildInfo}) + + // Rendering the load balance data to table + TableShow(data, table) +} diff --git a/cmd/get/type.go b/cmd/get/type.go index 01cae6c..2d68258 100644 --- a/cmd/get/type.go +++ b/cmd/get/type.go @@ -47,4 +47,5 @@ var ( HASTATE_TITLE = []string{"Instance", "HAState"} BFD_TITLE = []string{"Instance", "RemoteIP", "State"} BFD_WIDE_TITLE = []string{"Instance", "RemoteIP", "SourceIP", "Port", "Interval", "Retry Count", "State"} + LBVERSION_TITLE = []string{"LoxiLB Version", "LoxiLB Build Info"} ) diff --git a/cmd/root.go b/cmd/root.go index 91de712..4647291 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -30,7 +30,7 @@ import ( "github.com/spf13/cobra" ) -var Version string = "" +var Version string = "0.9.7-beta" var BuildInfo string = "" var VersionCmd = &cobra.Command{ @@ -39,7 +39,7 @@ var VersionCmd = &cobra.Command{ Long: `It shows Loxicmd version.`, Run: func(cmd *cobra.Command, args []string) { - fmt.Printf("%s %s\n", Version, BuildInfo) + fmt.Printf("Loxicmd version: %s\nLoxicmd build info: %s\n", Version, BuildInfo) }, } diff --git a/pkg/api/client.go b/pkg/api/client.go index 3d069a2..b793647 100644 --- a/pkg/api/client.go +++ b/pkg/api/client.go @@ -43,6 +43,7 @@ const ( loxiBGPNeighResource = "config/bgp/neigh" loxiStatusResource = "status" loxiBFDSessionResource = "config/bfd" + loxiVersionResource = "version" ) type LoxiClient struct { @@ -319,3 +320,15 @@ func (l *LoxiClient) BFDSession() *BFDSession { }, } } +func (l *LoxiClient) LBVersion() *LBVersion { + return &LBVersion{ + CommonAPI: CommonAPI{ + restClient: &l.restClient, + requestInfo: RequestInfo{ + provider: loxiProvider, + apiVersion: loxiApiVersion, + resource: loxiVersionResource, + }, + }, + } +} diff --git a/pkg/api/lbversion.go b/pkg/api/lbversion.go new file mode 100644 index 0000000..955b387 --- /dev/null +++ b/pkg/api/lbversion.go @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2022 NetLOX Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package api + + type LBVersion struct { + CommonAPI + } + + type LBVersionGet struct { + BuildInfo string `json:"buildInfo"` + Version string `json:"version"` + } + \ No newline at end of file