Skip to content

Commit

Permalink
Merge pull request #153 from nabbar/fix_2023-03-01
Browse files Browse the repository at this point in the history
Fix 2023-03-01

Package Config/Component AWS :
- rework monitoring for status config to use Golib Request health system & config
- if Enable is true on the health config, use a request instance for healtcheck, otherwise use the aws check as healthcheck

Package AWS/Config :
- adjust code for config request health

Package Request :
- expose healthcheck into interface

Bump dependencies
  • Loading branch information
nabbar authored Mar 1, 2023
2 parents 7a5650a + c72199d commit a7b7657
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 34 deletions.
8 changes: 4 additions & 4 deletions aws/configAws/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"net"
"net/url"

moncfg "github.com/nabbar/golib/monitor/types"
libreq "github.com/nabbar/golib/request"

sdkaws "github.com/aws/aws-sdk-go-v2/aws"
libval "github.com/go-playground/validator/v10"
Expand All @@ -47,9 +47,9 @@ type Model struct {
}

type ModelStatus struct {
Config Model `json:"config" yaml:"config" toml:"config" mapstructure:"config" validate:"required,dive"`
HTTPClient libhtc.Options `json:"http-client" yaml:"http-client" toml:"http-client" mapstructure:"http-client" validate:"required,dive"`
Monitor moncfg.Config `json:"monitor" yaml:"monitor" toml:"monitor" mapstructure:"monitor" validate:"required,dive"`
Config Model `json:"config" yaml:"config" toml:"config" mapstructure:"config" validate:"required,dive"`
HTTPClient libhtc.Options `json:"http-client" yaml:"http-client" toml:"http-client" mapstructure:"http-client" validate:"required,dive"`
Monitor libreq.OptionsHealth `json:"health" yaml:"health" toml:"health" mapstructure:"health" validate:"required,dive"`
}

type awsModel struct {
Expand Down
8 changes: 4 additions & 4 deletions aws/configCustom/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"net/url"
"strings"

moncfg "github.com/nabbar/golib/monitor/types"
libreq "github.com/nabbar/golib/request"

sdkaws "github.com/aws/aws-sdk-go-v2/aws"
libval "github.com/go-playground/validator/v10"
Expand All @@ -50,9 +50,9 @@ type Model struct {
}

type ModelStatus struct {
Config Model `json:"config" yaml:"config" toml:"config" mapstructure:"config" validate:"required,dive"`
HTTPClient libhtc.Options `json:"http-client" yaml:"http-client" toml:"http-client" mapstructure:"http-client" validate:"required,dive"`
Monitor moncfg.Config `json:"monitor" yaml:"monitor" toml:"monitor" mapstructure:"monitor" validate:"required,dive"`
Config Model `json:"config" yaml:"config" toml:"config" mapstructure:"config" validate:"required,dive"`
HTTPClient libhtc.Options `json:"http-client" yaml:"http-client" toml:"http-client" mapstructure:"http-client" validate:"required,dive"`
Monitor libreq.OptionsHealth `json:"health" yaml:"health" toml:"health" mapstructure:"health" validate:"required,dive"`
}

type awsModel struct {
Expand Down
75 changes: 60 additions & 15 deletions config/components/aws/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ package aws
import (
"net/http"

libhtc "github.com/nabbar/golib/httpcli"
libreq "github.com/nabbar/golib/request"

libaws "github.com/nabbar/golib/aws"
libtls "github.com/nabbar/golib/certificates"
cfgtps "github.com/nabbar/golib/config/types"
Expand Down Expand Up @@ -172,36 +175,78 @@ func (o *componentAws) _runFct(fct func(cpt cfgtps.Component) liberr.Error) libe
}

func (o *componentAws) _runCli() liberr.Error {
var prt = ErrorComponentReload
var (
e error

err liberr.Error
cli libaws.AWS
cfg libaws.Config
mon *libreq.OptionsHealth
htc *libhtc.Options
opt *libreq.Options
req libreq.Request
prt = ErrorComponentReload
)

if !o.IsStarted() {
prt = ErrorComponentStart
}

if cfg, mon, htc, err := o._getConfig(); err != nil {
if cfg, mon, htc, err = o._getConfig(); err != nil {
return prt.Error(err)
} else if cli, er := libaws.New(o.x.GetContext(), cfg, o._getHttpClient()); er != nil {
return prt.Error(er)
} else {
} else if cli, err = libaws.New(o.x.GetContext(), cfg, o._getHttpClient()); err != nil {
return prt.Error(err)
}

if htc != nil {
o.RegisterHTTPClient(func() *http.Client {
if c, e := htc.GetClient(o._getTLS(), ""); e == nil {
return c
}

return &http.Client{}
})
}

if mon != nil && mon.Enable {
opt = &libreq.Options{
Endpoint: "",
HttpClient: *htc,
Auth: libreq.OptionsAuth{},
Health: *mon,
}

opt.SetDefaultTLS(o._getTLS)
opt.SetDefaultLog(o.getLogger)

o.m.Lock()
o.a = cli
req = o.r
o.m.Unlock()

if htc != nil {
o.RegisterHTTPClient(func() *http.Client {
if c, e := htc.GetClient(o._getTLS(), ""); e == nil {
return c
}

return &http.Client{}
})
if req != nil {
req.RegisterDefaultLogger(o.getLogger)
if req, e = opt.Update(o.x.GetContext, req); e != nil {
return prt.ErrorParent(e)
}
} else if req, e = libreq.New(o.x.GetContext, opt); e != nil {
return prt.ErrorParent(e)
}

if e := o._registerMonitor(mon, cfg); e != nil {
o.m.Lock()
o.r = req
o.m.Unlock()
}

if mon != nil {
if e = o._registerMonitor(mon, cfg); e != nil {
return prt.ErrorParent(e)
}
}

o.m.Lock()
o.a = cli
o.m.Unlock()

return nil
}

Expand Down
18 changes: 15 additions & 3 deletions config/components/aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
cfgcus "github.com/nabbar/golib/aws/configCustom"
liberr "github.com/nabbar/golib/errors"
libhtc "github.com/nabbar/golib/httpcli"
libmon "github.com/nabbar/golib/monitor/types"
libreq "github.com/nabbar/golib/request"
spfcbr "github.com/spf13/cobra"
spfvpr "github.com/spf13/viper"
)
Expand Down Expand Up @@ -111,12 +111,12 @@ func (o *componentAws) RegisterFlag(Command *spfcbr.Command) error {
return nil
}

func (o *componentAws) _getConfig() (libaws.Config, *libmon.Config, *libhtc.Options, liberr.Error) {
func (o *componentAws) _getConfig() (libaws.Config, *libreq.OptionsHealth, *libhtc.Options, liberr.Error) {
var (
key string
cfg libaws.Config
flg = o._getFlagUpdate()
mon *libmon.Config
mon *libreq.OptionsHealth
htc *libhtc.Options
vpr *spfvpr.Viper
err liberr.Error
Expand Down Expand Up @@ -194,6 +194,18 @@ func (o *componentAws) _getConfig() (libaws.Config, *libmon.Config, *libhtc.Opti
return nil, nil, nil, ErrorConfigInvalid.Error(err)
}

if mon != nil {
if err = mon.Validate(); err != nil {
return nil, nil, nil, ErrorConfigInvalid.Error(err)
}
}

if htc != nil {
if err = htc.Validate(); err != nil {
return nil, nil, nil, ErrorConfigInvalid.Error(err)
}
}

return cfg, mon, htc, nil
}

Expand Down
4 changes: 2 additions & 2 deletions config/components/aws/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var _defaultConfigStandard = []byte(`{
var _defaultConfigStandardWithStatus = []byte(`{
"config":` + string(DefaultConfigStandard(cfgcst.JSONIndent+cfgcst.JSONIndent)) + `,
"http-client":` + string(libhtc.DefaultConfig(cfgcst.JSONIndent+cfgcst.JSONIndent)) + `,
"monitor":` + string(montps.DefaultConfig(cfgcst.JSONIndent+cfgcst.JSONIndent)) + `
"health":` + string(montps.DefaultConfig(cfgcst.JSONIndent+cfgcst.JSONIndent)) + `
}`)

var _defaultConfigCustom = []byte(`{
Expand All @@ -60,7 +60,7 @@ var _defaultConfigCustom = []byte(`{
var _defaultConfigCustomWithStatus = []byte(`{
"config":` + string(DefaultConfigCustom(cfgcst.JSONIndent+cfgcst.JSONIndent)) + `,
"http-client":` + string(libhtc.DefaultConfig(cfgcst.JSONIndent+cfgcst.JSONIndent)) + `,
"monitor":` + string(montps.DefaultConfig(cfgcst.JSONIndent+cfgcst.JSONIndent)) + `
"health":` + string(montps.DefaultConfig(cfgcst.JSONIndent+cfgcst.JSONIndent)) + `
}`)

var _defaultConfig = _defaultConfigCustom
Expand Down
3 changes: 3 additions & 0 deletions config/components/aws/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
"net/http"
"sync"

libreq "github.com/nabbar/golib/request"

libaws "github.com/nabbar/golib/aws"
libtls "github.com/nabbar/golib/certificates"
libctx "github.com/nabbar/golib/context"
Expand All @@ -45,6 +47,7 @@ type componentAws struct {
c func() *http.Client
t libtls.FctTLSDefault
a libaws.AWS
r libreq.Request
}

func (o *componentAws) RegisterHTTPClient(fct func() *http.Client) {
Expand Down
24 changes: 20 additions & 4 deletions config/components/aws/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
"fmt"
"runtime"

libreq "github.com/nabbar/golib/request"

libaws "github.com/nabbar/golib/aws"
libmon "github.com/nabbar/golib/monitor"
moninf "github.com/nabbar/golib/monitor/info"
Expand Down Expand Up @@ -61,7 +63,7 @@ func (o *componentAws) _getMonitorPool() montps.Pool {
}
}

func (o *componentAws) _registerMonitor(cfg *montps.Config, aws libaws.Config) error {
func (o *componentAws) _registerMonitor(opt *libreq.OptionsHealth, aws libaws.Config) error {
var (
e error
key = o._getKey()
Expand All @@ -75,7 +77,7 @@ func (o *componentAws) _registerMonitor(cfg *montps.Config, aws libaws.Config) e
return nil
} else if len(key) < 1 {
return ErrorComponentNotInitialized.Error(nil)
} else if cfg == nil {
} else if opt == nil {
return nil
} else if aws == nil {
return ErrorConfigInvalid.Error(nil)
Expand All @@ -89,6 +91,7 @@ func (o *componentAws) _registerMonitor(cfg *montps.Config, aws libaws.Config) e
res["date"] = vrs.GetDate()
res["endpoint"] = aws.GetEndpoint().Host
res["region"] = aws.GetRegion()
res["health"] = o._getEndpoint(opt, aws)
}

if inf, e = moninf.New(defaultNameMonitor); e != nil {
Expand All @@ -110,7 +113,7 @@ func (o *componentAws) _registerMonitor(cfg *montps.Config, aws libaws.Config) e
}
}

if e = mon.SetConfig(o.x.GetContext, *cfg); e != nil {
if e = mon.SetConfig(o.x.GetContext, opt.Monitor); e != nil {
return e
}

Expand All @@ -125,6 +128,17 @@ func (o *componentAws) _registerMonitor(cfg *montps.Config, aws libaws.Config) e
return nil
}

func (o *componentAws) _getEndpoint(opt *libreq.OptionsHealth, aws libaws.Config) string {
o.m.RLock()
defer o.m.RUnlock()

if o.r != nil && len(opt.Endpoint) > 0 {
return opt.Endpoint
} else {
return aws.GetEndpoint().Host
}
}

func (o *componentAws) _newMonitor(inf montps.Info) (montps.Monitor, error) {
if c, e := libmon.New(o.x.GetContext, inf); e != nil {
return nil, e
Expand Down Expand Up @@ -172,7 +186,9 @@ func (o *componentAws) HealthCheck(ctx context.Context) error {

if !o.IsStarted() {
return fmt.Errorf("component not started")
} else {
} else if o.r == nil {
return o.a.Config().Check(ctx)
} else {
return o.r.HealthCheck(ctx)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ require (
github.com/nats-io/nuid v1.0.1 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/paulmach/orb v0.9.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pelletier/go-toml/v2 v2.0.7 // indirect
github.com/pierrec/lz4/v4 v4.1.17 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/term v1.2.0-beta.2 // indirect
Expand Down
1 change: 1 addition & 0 deletions request/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ type Request interface {
DoParse(model interface{}, validStatus ...int) liberr.Error

Monitor(ctx context.Context, vrs libver.Version) (montps.Monitor, error)
HealthCheck(ctx context.Context) error
}

func New(ctx libctx.FuncContext, opt *Options) (Request, error) {
Expand Down
23 changes: 22 additions & 1 deletion request/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type OptionsAuth struct {

type OptionsHealth struct {
Enable bool `json:"enable" yaml:"enable" toml:"enable" mapstructure:"enable"`
Endpoint string `json:"endpoint" yaml:"endpoint" toml:"endpoint" mapstructure:"endpoint" validate:"required,url"`
Endpoint string `json:"endpoint" yaml:"endpoint" toml:"endpoint" mapstructure:"endpoint" validate:"url"`
Auth OptionsAuth `json:"auth" yaml:"auth" toml:"auth" mapstructure:"auth" validate:"required,dive"`
Result OptionsHealthResult `json:"result" yaml:"result" toml:"result" mapstructure:"result" validate:"required,dive"`
Monitor moncfg.Config `json:"monitor" yaml:"monitor" toml:"monitor" mapstructure:"monitor" validate:"required,dive"`
Expand Down Expand Up @@ -104,6 +104,27 @@ func (o *Options) Validate() liberr.Error {
return e
}

func (o *OptionsHealth) Validate() liberr.Error {
var e = ErrorValidatorError.Error(nil)

if err := libval.New().Struct(o); err != nil {
if er, ok := err.(*libval.InvalidValidationError); ok {
e.AddParent(er)
}

for _, er := range err.(libval.ValidationErrors) {
//nolint #goerr113
e.AddParent(fmt.Errorf("config field '%s' is not validated by constraint '%s'", er.Namespace(), er.ActualTag()))
}
}

if !e.HasParent() {
e = nil
}

return e
}

func (o *Options) defaultTLS() libtls.TLSConfig {
if o.tls != nil {
return o.tls()
Expand Down

0 comments on commit a7b7657

Please sign in to comment.