Files
bridge/internal/metal/providers/emapi/config.go
2023-07-01 00:04:52 +00:00

82 lines
1.8 KiB
Go

package emapi
import (
"fmt"
"net/url"
)
// Config provides configuration for connecting to the Equinix Metal API provider.
type Config struct {
// BaseURL is the baseurl to use when connecting to the Equinix Metal API Provider.
BaseURL string
// AuthToken is the token to interact with the Equinix Metal API
AuthToken string
// ConsumerToken is the token to grant higher privileges in the Equinix Metal API
ConsumerToken string
}
func (c Config) Populated() bool {
return c.AuthToken != "" || c.ConsumerToken != "" || c.BaseURL != ""
}
// WithConfig applies all configurations defined in the config.
func WithConfig(config Config) Option {
return func(c *Client) error {
var options []Option
if config.BaseURL != "" {
options = append(options, WithBaseURL(config.BaseURL))
}
if config.AuthToken != "" {
options = append(options, WithAuthToken(config.AuthToken))
}
if config.ConsumerToken != "" {
options = append(options, WithConsumerToken(config.ConsumerToken))
}
for _, opt := range options {
if err := opt(c); err != nil {
return err
}
}
return nil
}
}
// WithBaseURL updates the baseurl used by the client.
func WithBaseURL(baseURL string) Option {
return func(c *Client) error {
u, err := url.Parse(baseURL)
if err != nil {
return fmt.Errorf("failed to parse emapi base url %s: %w", baseURL, err)
}
c.baseURL = u
return nil
}
}
// WithAuthToken sets the auth token to authenticate the request with.
func WithAuthToken(token string) Option {
return func(c *Client) error {
c.authToken = token
return nil
}
}
// WithConsumerToken sets the consumer token to elevate privileges for the request.
func WithConsumerToken(token string) Option {
return func(c *Client) error {
c.consumerToken = token
return nil
}
}