Skip to content

Commit

Permalink
use call list entries status instead of make one
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurHlt committed Sep 9, 2023
1 parent ca25341 commit 99cc8c6
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 89 deletions.
36 changes: 0 additions & 36 deletions cli/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import (
msg "github.com/ArthurHlt/messages"
"github.com/gonvenience/ytbx"
"github.com/homeport/dyff/pkg/dyff"
"github.com/mitchellh/mapstructure"
"github.com/olekukonko/tablewriter"
"github.com/orange-cloudfoundry/gsloc-cli/highlight"
"github.com/orange-cloudfoundry/gsloc-go-sdk/gsloc/api/config/entries/v1"
hcconf "github.com/orange-cloudfoundry/gsloc-go-sdk/gsloc/api/config/healthchecks/v1"
"github.com/orange-cloudfoundry/gsloc-go-sdk/helpers"
"google.golang.org/protobuf/encoding/protojson"
Expand All @@ -38,40 +36,6 @@ var emptyJsonRegex = regexp.MustCompile(`\{\s*\}`)
// helpers.TypeUrl[*cluster.ClusterEndpointStats](): "endpoints",
//}

func ListMapToMembers(lm []map[string]string) ([]*entries.Member, error) {
var members []*entries.Member
for _, m := range lm {
member, err := MapToMember(m)
if err != nil {
if member.Ip != "" {
return nil, fmt.Errorf("failed to parse member %s: %w", member.Ip, err)
}
return nil, err
}
members = append(members, member)
}
return members, nil
}

func MapToMember(m map[string]string) (*entries.Member, error) {
var mm MemberMap
err := mapstructure.WeakDecode(m, &mm)
if err != nil {
return nil, err
}
member := &entries.Member{
Ip: mm.Ip,
Ratio: uint32(mm.Ratio),
Dc: mm.DC,
Disabled: mm.Disabled,
}
err = member.Validate()
if err != nil {
return member, err
}
return member, nil
}

func FileToProto[T proto.Message](file string) (protoMsg T, loaded bool, err error) {
var protoMsgDef T
protoMsg = protoMsgDef.ProtoReflect().New().Interface().(T)
Expand Down
35 changes: 5 additions & 30 deletions cli/list_entries_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
msg "github.com/ArthurHlt/messages"
gslbsvc "github.com/orange-cloudfoundry/gsloc-go-sdk/gsloc/services/gslb/v1"
"github.com/sourcegraph/conc/pool"
"strings"
)

Expand All @@ -25,43 +24,19 @@ func (c *ListEntriesStatus) SetClient(client gslbsvc.GSLBClient) {
var listEntriesStatus ListEntriesStatus

func (c *ListEntriesStatus) Execute([]string) error {
entsResp, err := c.client.ListEntries(context.Background(), &gslbsvc.ListEntriesRequest{
entsResp, err := c.client.ListEntriesStatus(context.Background(), &gslbsvc.ListEntriesStatusRequest{
Tags: c.Tags,
Prefix: c.Prefix,
})
if err != nil {
return err
}
var entsStatus []*gslbsvc.GetEntryStatusResponse
var chanEntsStatus = make(chan *gslbsvc.GetEntryStatusResponse, 100)
p := pool.New().WithMaxGoroutines(10)
done := make(chan struct{})
go func() {
for entStatus := range chanEntsStatus {
entsStatus = append(entsStatus, entStatus)
}
done <- struct{}{}
}()
for _, ent := range entsResp.GetEntries() {
ent := ent
p.Go(func() {
entStatus, err := c.client.GetEntryStatus(context.Background(), &gslbsvc.GetEntryStatusRequest{
Fqdn: ent.GetEntry().GetFqdn(),
})
if err != nil {
msg.Fatal(err.Error())
}
chanEntsStatus <- entStatus
})
}
p.Wait()
close(chanEntsStatus)
<-done

if c.Json {
return PrintProtoListJson[*gslbsvc.GetEntryStatusResponse](entsStatus)
return PrintProtoListJson[*gslbsvc.GetEntryStatusResponse](entsResp.GetEntriesStatus())
}

if len(entsStatus) == 0 {
if len(entsResp.GetEntriesStatus()) == 0 {
msg.Info("No entries found.")
return nil
}
Expand All @@ -73,7 +48,7 @@ func (c *ListEntriesStatus) Execute([]string) error {

table := MakeTableWriter(append([]string{"FQDN"}, dcResp.GetDcs()...))
table.SetAutoWrapText(false)
for _, entStatus := range entsStatus {
for _, entStatus := range entsResp.GetEntriesStatus() {
line := []string{entStatus.GetFqdn()}
for _, dc := range dcResp.GetDcs() {
dcContent := c.makeDcContent(entStatus.GetMembersIpv4(), dc)
Expand Down
32 changes: 12 additions & 20 deletions cli/set_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ import (
type SetEntry struct {
File flags.Filename `short:"f" long:"file" description:"Path to a json or yml file definition of entry" required:"true" default:"entry.yml"`

FQDN *FQDN `positional-args:"true" positional-arg-name:"'fqdn'" required:"true"`
LBAlgoPreferred string `short:"p" long:"lb-algo-preferred" description:"LB algo preferred" choice:"ROUND_ROBIN" choice:"TOPOLOGY" choice:"RATIO" choice:"RANDOM" default:"ROUND_ROBIN"`
LBAlgoAlternate string `short:"a" long:"lb-algo-alternate" description:"LB algo alternate" choice:"ROUND_ROBIN" choice:"TOPOLOGY" choice:"RATIO" choice:"RANDOM" default:"ROUND_ROBIN"`
LBAlgoFallback string `short:"b" long:"lb-algo-fallback" description:"LB algo fallback" choice:"ROUND_ROBIN" choice:"TOPOLOGY" choice:"RATIO" choice:"RANDOM" default:"ROUND_ROBIN"`
MaxAnswerReturned uint32 `short:"m" long:"max-answer-returned" description:"Max answer returned" default:"0"`
MembersIPv4 []map[string]string `short:"4" long:"members-ipv4" description:"Members ipv4 (can be set multiple time)"`
MembersIPv6 []map[string]string `short:"6" long:"members-ipv6" description:"Members ipv6 (can be set multiple time)"`
TTL uint32 `long:"ttl" description:"TTL" default:"30"`
Tags []string `short:"T" long:"tag" description:"Tag (can be set multiple time)"`
FQDN *FQDN `positional-args:"true" positional-arg-name:"'fqdn'" required:"true"`
LBAlgoPreferred string `short:"p" long:"lb-algo-preferred" description:"LB algo preferred" choice:"ROUND_ROBIN" choice:"TOPOLOGY" choice:"RATIO" choice:"RANDOM" default:"ROUND_ROBIN"`
LBAlgoAlternate string `short:"a" long:"lb-algo-alternate" description:"LB algo alternate" choice:"ROUND_ROBIN" choice:"TOPOLOGY" choice:"RATIO" choice:"RANDOM" default:"ROUND_ROBIN"`
LBAlgoFallback string `short:"b" long:"lb-algo-fallback" description:"LB algo fallback" choice:"ROUND_ROBIN" choice:"TOPOLOGY" choice:"RATIO" choice:"RANDOM" default:"ROUND_ROBIN"`
MaxAnswerReturned uint32 `short:"m" long:"max-answer-returned" description:"Max answer returned" default:"0"`
TTL uint32 `long:"ttl" description:"TTL" default:"30"`
Tags []string `short:"T" long:"tag" description:"Tag (can be set multiple time)"`

HcTimeout string `short:"o" long:"hc-timeout" description:"Healthcheck timeout" default:"10s"`
HcInterval string `short:"i" long:"hc-interval" description:"Healthcheck interval" default:"30s"`
Expand Down Expand Up @@ -96,9 +94,13 @@ func (c *SetEntry) Execute([]string) error {
Entry: resp.GetEntry(),
Healthcheck: resp.GetHealthcheck(),
}
if c.isMerge() {
if c.isMerge() && !loaded {
proto.Merge(entryToSet, previousEntry)
}
if !loaded {
entryToSet.GetEntry().MembersIpv4 = previousEntry.GetEntry().GetMembersIpv4()
entryToSet.GetEntry().MembersIpv6 = previousEntry.GetEntry().GetMembersIpv6()
}
}
if loaded {
return c.apply(previousEntry, entryToSet)
Expand Down Expand Up @@ -128,14 +130,6 @@ func (c *SetEntry) apply(previousEntry, currentEntry *gslbsvc.SetEntryRequest) e
}

func (c *SetEntry) makeEntry() (*gslbsvc.SetEntryRequest, error) {
membersIpv4, err := ListMapToMembers(c.MembersIPv4)
if err != nil {
return nil, fmt.Errorf("invalid members ipv4: %s", err)
}
membersIpv6, err := ListMapToMembers(c.MembersIPv6)
if err != nil {
return nil, fmt.Errorf("invalid members ipv6: %s", err)
}
hc, err := c.makeHealthcheck()
if err != nil {
return nil, err
Expand All @@ -147,8 +141,6 @@ func (c *SetEntry) makeEntry() (*gslbsvc.SetEntryRequest, error) {
LbAlgoAlternate: entries.LBAlgo(entries.LBAlgo_value[c.LBAlgoAlternate]),
LbAlgoFallback: entries.LBAlgo(entries.LBAlgo_value[c.LBAlgoFallback]),
MaxAnswerReturned: c.MaxAnswerReturned,
MembersIpv4: membersIpv4,
MembersIpv6: membersIpv6,
Ttl: c.TTL,
Permissions: nil,
Tags: c.Tags,
Expand Down
2 changes: 2 additions & 0 deletions cli/set_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ func (c *SetMember) Execute([]string) error {
}
if c.Disabled != nil {
setMemberReq.Member.Disabled = *c.Disabled
} else {
setMemberReq.Member.Disabled = false
}

confirm, err := DiffAndConfirm(previousEntry, setMemberReq, c.Force)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/olekukonko/tablewriter v0.0.5
github.com/onsi/ginkgo/v2 v2.12.0
github.com/onsi/gomega v1.27.10
github.com/orange-cloudfoundry/gsloc-go-sdk v0.3.0
github.com/orange-cloudfoundry/gsloc-go-sdk v0.4.0
github.com/sourcegraph/conc v0.3.0
google.golang.org/grpc v1.58.0
google.golang.org/protobuf v1.31.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ github.com/onsi/ginkgo/v2 v2.12.0 h1:UIVDowFPwpg6yMUpPjGkYvf06K3RAiJXUhCxEwQVHRI
github.com/onsi/ginkgo/v2 v2.12.0/go.mod h1:ZNEzXISYlqpb8S36iN71ifqLi3vVD1rVJGvWRCJOUpQ=
github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI=
github.com/onsi/gomega v1.27.10/go.mod h1:RsS8tutOdbdgzbPtzzATp12yT7kM5I5aElG3evPbQ0M=
github.com/orange-cloudfoundry/gsloc-go-sdk v0.3.0 h1:NgYINWEci7ckQ4qyVEXTXjtqtpQ/5pPXdk4m/QraRpI=
github.com/orange-cloudfoundry/gsloc-go-sdk v0.3.0/go.mod h1:VdojO3XZ/TArQAXbbcCoOk2q6RTNbAo2X4Eit1vF6jg=
github.com/orange-cloudfoundry/gsloc-go-sdk v0.4.0 h1:Av1LVRMcd6Ko56nUYlF1V5kjZ2Yx8Lilf9Vb6PEnByM=
github.com/orange-cloudfoundry/gsloc-go-sdk v0.4.0/go.mod h1:VdojO3XZ/TArQAXbbcCoOk2q6RTNbAo2X4Eit1vF6jg=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down

0 comments on commit 99cc8c6

Please sign in to comment.