Skip to content

Commit

Permalink
Merge branch 'master' of github.com:exoscale/egoscale into pej/sc-827…
Browse files Browse the repository at this point in the history
…23/egoscale-v3-implement-a-credentials-provider

Signed-off-by: Pierre-Emmanuel Jacquier <15922119+pierre-emmanuelJ@users.noreply.github.com>
  • Loading branch information
pierre-emmanuelJ committed Feb 5, 2024
2 parents 1267e21 + 2f4651b commit 23f48da
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 49 deletions.
27 changes: 25 additions & 2 deletions v3/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,31 @@ func Ptr[T any](v T) *T {
}

// Validate any struct from schema or request
func Validate(r any) error {
return validator.New().Struct(r)
func (c Client) Validate(s any) error {
err := c.validate.Struct(s)
if err == nil {
return nil
}

// Print better error messages
validationErrors := err.(validator.ValidationErrors)

if len(validationErrors) > 0 {
e := validationErrors[0]
errorString := fmt.Sprintf(
"Request validation error: '%s' = '%v' does not validate ",
e.StructNamespace(),
e.Value(),
)
if e.Param() == "" {
errorString += fmt.Sprintf("'%s'", e.ActualTag())
} else {
errorString += fmt.Sprintf("'%s=%v'", e.ActualTag(), e.Param())
}
return fmt.Errorf(errorString)
}

return err
}

func prepareJSONBody(body any) (*bytes.Reader, error) {
Expand Down
15 changes: 15 additions & 0 deletions v3/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions v3/generator/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func Generate(doc libopenapi.Document, path, packageName string) error {
"github.com/exoscale/egoscale/v3/credentials"
"github.com/exoscale/egoscale/version"
"github.com/go-playground/validator/v10"
)
`, packageName))

Expand Down
76 changes: 45 additions & 31 deletions v3/generator/client/client.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Client struct {
serverURL string
httpClient *http.Client
pollingInterval time.Duration
validate *validator.Validate
trace bool

// A list of callbacks for modifying requests which are generated before sending over
Expand Down Expand Up @@ -58,6 +59,14 @@ func ClientOptWithTrace() ClientOpt {
}
}

// ClientOptWithValidator returns a ClientOpt with a given validator.
func ClientOptWithValidator(validate *validator.Validate) ClientOpt {
return func(c *Client) error {
c.validate = validate
return nil
}
}

// ClientOptWithURL returns a ClientOpt With a given zone URL.
func ClientOptWithURL(url URL) ClientOpt {
return func(c *Client) error {
Expand Down Expand Up @@ -95,13 +104,14 @@ func NewClient(credentials *credentials.Credentials, opts ...ClientOpt) (*Client
return nil, err
}

client := &Client{
apiKey: values.APIKey,
apiSecret: values.APISecret,
serverURL: string(CHGva2),
httpClient: http.DefaultClient,
pollingInterval: pollingInterval,
}
client := &Client{
apiKey: values.APIKey,
apiSecret: values.APISecret,
serverURL: string({{ .ServerURL }}),
httpClient: http.DefaultClient,
pollingInterval: pollingInterval,
validate: validator.New(),
}

for _, opt := range opts {
if err := opt(client); err != nil {
Expand All @@ -115,52 +125,56 @@ func NewClient(credentials *credentials.Credentials, opts ...ClientOpt) (*Client
// WithURL returns a copy of Client with new zone URL.
func (c *Client) WithURL(url URL) *Client {
return &Client{
apiKey: c.apiKey,
apiSecret: c.apiSecret,
serverURL: string(url),
httpClient: c.httpClient,
apiKey: c.apiKey,
apiSecret: c.apiSecret,
serverURL: string(url),
httpClient: c.httpClient,
requestInterceptors: c.requestInterceptors,
pollingInterval: c.pollingInterval,
trace: c.trace,
pollingInterval: c.pollingInterval,
trace: c.trace,
validate: c.validate,
}
}

// WithTrace returns a copy of Client with tracing enabled.
func (c *Client) WithTrace() *Client {
return &Client{
apiKey: c.apiKey,
apiSecret: c.apiSecret,
serverURL: c.serverURL,
httpClient: c.httpClient,
apiKey: c.apiKey,
apiSecret: c.apiSecret,
serverURL: c.serverURL,
httpClient: c.httpClient,
requestInterceptors: c.requestInterceptors,
pollingInterval: c.pollingInterval,
trace: true,
pollingInterval: c.pollingInterval,
trace: true,
validate: c.validate,
}
}

// WithHttpClient returns a copy of Client with new http.Client.
func (c *Client) WithHttpClient(client *http.Client) *Client {
return &Client{
apiKey: c.apiKey,
apiSecret: c.apiSecret,
serverURL: c.serverURL,
httpClient: client,
apiKey: c.apiKey,
apiSecret: c.apiSecret,
serverURL: c.serverURL,
httpClient: client,
requestInterceptors: c.requestInterceptors,
pollingInterval: c.pollingInterval,
trace: c.trace,
pollingInterval: c.pollingInterval,
trace: c.trace,
validate: c.validate,
}
}

// WithRequestInterceptor returns a copy of Client with new RequestInterceptors.
func (c *Client) WithRequestInterceptor(f ...RequestInterceptorFn) *Client {
return &Client{
apiKey: c.apiKey,
apiSecret: c.apiSecret,
serverURL: c.serverURL,
httpClient: c.httpClient,
apiKey: c.apiKey,
apiSecret: c.apiSecret,
serverURL: c.serverURL,
httpClient: c.httpClient,
requestInterceptors: append(c.requestInterceptors, f...),
pollingInterval: c.pollingInterval,
trace: c.trace,
pollingInterval: c.pollingInterval,
trace: c.trace,
validate: c.validate,
}
}

Expand Down
20 changes: 4 additions & 16 deletions v3/operations.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions v3/schemas.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 23f48da

Please # to comment.