Skip to content

Commit ddbe754

Browse files
authored
Merge pull request #37 from Hanna623/dev
Add Print loxilb version #28
2 parents 9b7e2e2 + 19935ed commit ddbe754

File tree

8 files changed

+135
-3
lines changed

8 files changed

+135
-3
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ SHELL := /bin/bash
66
loxilbid=$(shell docker ps -f name=$(dock) | grep -w $(dock) | cut -d " " -f 1 | grep -iv "CONTAINER")
77

88
build:
9-
@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}'"
9+
@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)'"
1010

1111
test:
1212
go test

cmd/create/create_loadbalancer.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ func NewCreateLoadBalancerCmd(restOptions *api.RESTOptions) *cobra.Command {
152152
persist - select the lb end-point based on sender
153153
n2 - select the lb end-point base on N2 interface params (only available with fullproxy mode)
154154
n3 - select the lb end-point base on N3 interface params
155+
lc - select the lb end-point base on least connection
156+
155157
--mode value options
156158
onearm - LB put LB-IP as srcIP
157159
fullnat - LB put Service IP as scrIP

cmd/get/get.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func GetCmd(restOptions *api.RESTOptions) *cobra.Command {
6969
GetCmd.AddCommand(NewGetBGPNeighborCmd(restOptions))
7070
GetCmd.AddCommand(NewGetHaStateCmd(restOptions))
7171
GetCmd.AddCommand(NewGetBFDCmd(restOptions))
72+
GetCmd.AddCommand(NewGetVersionCmd(restOptions))
7273

7374
return GetCmd
7475
}

cmd/get/get_lbversion.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (c) 2022 NetLOX Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package get
17+
18+
import (
19+
"context"
20+
"encoding/json"
21+
"fmt"
22+
"io"
23+
"loxicmd/pkg/api"
24+
"net/http"
25+
"time"
26+
27+
"github.com/spf13/cobra"
28+
)
29+
30+
func NewGetVersionCmd(restOptions *api.RESTOptions) *cobra.Command {
31+
var GetLBVersionCmd = &cobra.Command{
32+
Use: "lbversion",
33+
Short: "Get a loxilb version ",
34+
Long: `It shows version in the LoxiLB`,
35+
Aliases: []string{"LBversion", "LBVersion", "llbversion"},
36+
Run: func(cmd *cobra.Command, args []string) {
37+
client := api.NewLoxiClient(restOptions)
38+
ctx := context.TODO()
39+
var cancel context.CancelFunc
40+
if restOptions.Timeout > 0 {
41+
ctx, cancel = context.WithTimeout(context.TODO(), time.Duration(restOptions.Timeout)*time.Second)
42+
defer cancel()
43+
}
44+
resp, err := client.LBVersion().Get(ctx)
45+
if err != nil {
46+
fmt.Printf("Error: %s\n", err.Error())
47+
return
48+
}
49+
if resp.StatusCode == http.StatusOK {
50+
PrintGetVersionResult(resp, *restOptions)
51+
return
52+
}
53+
54+
},
55+
}
56+
57+
return GetLBVersionCmd
58+
}
59+
60+
func PrintGetVersionResult(resp *http.Response, o api.RESTOptions) {
61+
Versionresp := api.LBVersionGet{}
62+
var data [][]string
63+
resultByte, err := io.ReadAll(resp.Body)
64+
if err != nil {
65+
fmt.Printf("Error: Failed to read HTTP response: (%s)\n", err.Error())
66+
return
67+
}
68+
69+
if err := json.Unmarshal(resultByte, &Versionresp); err != nil {
70+
fmt.Printf("Error: Failed to unmarshal HTTP response: (%s)\n", err.Error())
71+
return
72+
}
73+
74+
// if json options enable, it print as a json format.
75+
if o.PrintOption == "json" {
76+
resultIndent, _ := json.MarshalIndent(Versionresp, "", " ")
77+
fmt.Println(string(resultIndent))
78+
return
79+
}
80+
81+
// Table Init
82+
table := TableInit()
83+
84+
table.SetHeader(LBVERSION_TITLE)
85+
data = append(data, []string{Versionresp.Version, Versionresp.BuildInfo})
86+
87+
// Rendering the load balance data to table
88+
TableShow(data, table)
89+
}

cmd/get/type.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,5 @@ var (
4747
HASTATE_TITLE = []string{"Instance", "HAState"}
4848
BFD_TITLE = []string{"Instance", "RemoteIP", "State"}
4949
BFD_WIDE_TITLE = []string{"Instance", "RemoteIP", "SourceIP", "Port", "Interval", "Retry Count", "State"}
50+
LBVERSION_TITLE = []string{"LoxiLB Version", "LoxiLB Build Info"}
5051
)

cmd/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
"github.com/spf13/cobra"
3131
)
3232

33-
var Version string = ""
33+
var Version string = "0.9.7-beta"
3434
var BuildInfo string = ""
3535

3636
var VersionCmd = &cobra.Command{
@@ -39,7 +39,7 @@ var VersionCmd = &cobra.Command{
3939
Long: `It shows Loxicmd version.`,
4040

4141
Run: func(cmd *cobra.Command, args []string) {
42-
fmt.Printf("%s %s\n", Version, BuildInfo)
42+
fmt.Printf("Loxicmd version: %s\nLoxicmd build info: %s\n", Version, BuildInfo)
4343
},
4444
}
4545

pkg/api/client.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const (
4343
loxiBGPNeighResource = "config/bgp/neigh"
4444
loxiStatusResource = "status"
4545
loxiBFDSessionResource = "config/bfd"
46+
loxiVersionResource = "version"
4647
)
4748

4849
type LoxiClient struct {
@@ -319,3 +320,15 @@ func (l *LoxiClient) BFDSession() *BFDSession {
319320
},
320321
}
321322
}
323+
func (l *LoxiClient) LBVersion() *LBVersion {
324+
return &LBVersion{
325+
CommonAPI: CommonAPI{
326+
restClient: &l.restClient,
327+
requestInfo: RequestInfo{
328+
provider: loxiProvider,
329+
apiVersion: loxiApiVersion,
330+
resource: loxiVersionResource,
331+
},
332+
},
333+
}
334+
}

pkg/api/lbversion.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2022 NetLOX Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package api
17+
18+
type LBVersion struct {
19+
CommonAPI
20+
}
21+
22+
type LBVersionGet struct {
23+
BuildInfo string `json:"buildInfo"`
24+
Version string `json:"version"`
25+
}
26+

0 commit comments

Comments
 (0)