From 99cc8c69928f7cecaefbb06f4077e3513c8d26da Mon Sep 17 00:00:00 2001 From: Arthur Halet Date: Sun, 10 Sep 2023 01:20:48 +0200 Subject: [PATCH] use call list entries status instead of make one --- cli/common.go | 36 ------------------------------------ cli/list_entries_status.go | 35 +++++------------------------------ cli/set_entry.go | 32 ++++++++++++-------------------- cli/set_member.go | 2 ++ go.mod | 2 +- go.sum | 4 ++-- 6 files changed, 22 insertions(+), 89 deletions(-) diff --git a/cli/common.go b/cli/common.go index 2ab35d5..696ae62 100644 --- a/cli/common.go +++ b/cli/common.go @@ -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" @@ -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) diff --git a/cli/list_entries_status.go b/cli/list_entries_status.go index b5485da..c6ffa60 100644 --- a/cli/list_entries_status.go +++ b/cli/list_entries_status.go @@ -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" ) @@ -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 } @@ -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) diff --git a/cli/set_entry.go b/cli/set_entry.go index b574044..3566b87 100644 --- a/cli/set_entry.go +++ b/cli/set_entry.go @@ -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"` @@ -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) @@ -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 @@ -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, diff --git a/cli/set_member.go b/cli/set_member.go index ae9ca59..23d8b3f 100644 --- a/cli/set_member.go +++ b/cli/set_member.go @@ -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) diff --git a/go.mod b/go.mod index df0d920..eec2825 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 64337ee..d5b9558 100644 --- a/go.sum +++ b/go.sum @@ -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=